Coverage for pygeodesy/internals.py: 94%
211 statements
« prev ^ index » next coverage.py v7.2.2, created at 2024-06-10 14:08 -0400
« prev ^ index » next coverage.py v7.2.2, created at 2024-06-10 14:08 -0400
1# -*- coding: utf-8 -*-
3u'''Mostly INTERNAL functions, except L{machine}, L{print_} and L{printf}.
4'''
5# from pygeodesy.basics import isiterablen # _MODS
6# from pygeodesy.errors import _AttributeError, _error_init, _UnexpectedError, _xError2 # _MODS
7from pygeodesy.interns import NN, _COLON_, _DOT_, _ELLIPSIS_, _EQUALSPACED_, \
8 _immutable_, _NL_, _pygeodesy_, _PyPy__, _python_, \
9 _QUOTE1_, _QUOTE2_, _s_, _SPACE_, _sys, _UNDER_, _utf_8_
10from pygeodesy.interns import _COMMA_, _Python_ # PYCHOK used!
11# from pygeodesy.streprs import anstr, pairs, unstr # _MODS
13import os as _os # in .lazily, ...
14import os.path as _os_path
15# import sys as _sys # from .interns
17_0_0 = 0.0 # PYCHOK in .basics, .constants
18_arm64_ = 'arm64'
19_iOS_ = 'iOS'
20_macOS_ = 'macOS'
21_Windows_ = 'Windows'
24def _dunder_nameof(inst, *dflt):
25 '''(INTERNAL) Get the double_underscore __name__ attr.
26 '''
27 try:
28 return inst.__name__
29 except AttributeError:
30 pass
31 return dflt[0] if dflt else inst.__class__.__name__
34def _Property_RO(method):
35 '''(INTERNAL) Can't I{recursively} import L{props.property_RO}.
36 '''
37 name = _dunder_nameof(method)
39 def _del(inst, attr): # PYCHOK no cover
40 delattr(inst, attr) # force error
42 def _get(inst, **unused): # PYCHOK 2 vs 3 args
43 try: # to get the cached value immediately
44 v = inst.__dict__[name]
45 except (AttributeError, KeyError):
46 # cache the value in the instance' __dict__
47 inst.__dict__[name] = v = method(inst)
48 return v
50 def _set(inst, val): # PYCHOK no cover
51 setattr(inst, name, val) # force error
53 return property(_get, _set, _del)
56class _MODS_Base(object):
57 '''(INTERNAL) Base-class for C{lazily._ALL_MODS}.
58 '''
59 def __delattr__(self, attr): # PYCHOK no cover
60 self.__dict__.pop(attr, None)
62 def __setattr__(self, attr, value): # PYCHOK no cover
63 m = _MODS.errors
64 t = _EQUALSPACED_(self._DOT_(attr), repr(value))
65 raise m._AttributeError(_immutable_, txt=t)
67 @_Property_RO
68 def bits_machine2(self):
69 '''Get platform 2-list C{[bits, machine]}, I{once}.
70 '''
71 import platform as p
73 m = p.machine() # ARM64, arm64, x86_64, iPhone13,2, etc.
74 m = m.replace(_COMMA_, _UNDER_)
75 if m.lower() == 'x86_64': # PYCHOK on Intel or Rosetta2 ...
76 v = p.mac_ver()[0] # ... and only on macOS ...
77 if v and _version2(v) > (10, 15): # ... 11+ aka 10.16
78 # <https://Developer.Apple.com/forums/thread/659846>
79 # _sysctl_uint('hw.optional.arm64') and \
80 if _sysctl_uint('sysctl.proc_translated'):
81 m = _UNDER_(_arm64_, m) # Apple Si emulating Intel x86-64
82 return [p.architecture()[0], # bits
83 m] # arm64, arm64_x86_64, x86_64, etc.
85 @_Property_RO
86 def ctypes3(self):
87 '''Get 3-tuple C{(ctypes.CDLL, ._dlopen, .util.findlibrary)}, I{once}.
88 '''
89 if _ismacOS():
90 from ctypes import CDLL, DEFAULT_MODE, _dlopen
92 def dlopen(name):
93 return _dlopen(name, DEFAULT_MODE)
94 else: # PYCHOK no cover
95 from ctypes import CDLL
96 dlopen = _passarg
98 from ctypes.util import find_library
99 return CDLL, dlopen, find_library
101 @_Property_RO
102 def ctypes5(self):
103 '''Get 5-tuple C{(ctypes.byref, .c_char_p, .c_size_t, .c_uint, .sizeof)}, I{once}.
104 '''
105 from ctypes import byref, c_char_p, c_size_t, c_uint, sizeof # get_errno
106 return byref, c_char_p, c_size_t, c_uint, sizeof
108 def _DOT_(self, name): # PYCHOK no cover
109 return _DOT_(self.name, name)
111 @_Property_RO
112 def errors(self):
113 '''Get module C{pygeodesy.errors}, I{once}.
114 '''
115 from pygeodesy import errors # DON'T _lazy_import2
116 return errors
118 def ios_ver(self):
119 '''Mimick C{platform.xxx_ver} for C{iOS}.
120 '''
121 try: # Pythonista only
122 from platform import iOS_ver
123 return iOS_ver()
124 except (AttributeError, ImportError):
125 return NN, (NN, NN, NN), NN
127 @_Property_RO
128 def libc(self):
129 '''Load C{libc.dll|dylib}, I{once}.
130 '''
131 return _load_lib('libc')
133 @_Property_RO
134 def name(self):
135 '''Get this name (C{str}).
136 '''
137 return _dunder_nameof(self.__class__)
139 @_Property_RO
140 def nix2(self): # PYCHOK no cover
141 '''Get Linux 2-list C{[distro, version]}, I{once}.
142 '''
143 import platform as p
145 n, v = p.uname()[0], NN
146 if n.lower() == 'linux':
147 try: # use distro only for Linux, not macOS, etc.
148 import distro # <https://PyPI.org/project/distro>
149 _a = _MODS.streprs.anstr
150 v = _a(distro.version()) # first
151 n = _a(distro.id()) # .name()?
152 except (AttributeError, ImportError):
153 pass # v = str(_0_0)
154 n = n.capitalize()
155 return n, v
157 def nix_ver(self): # PYCHOK no cover
158 '''Mimick C{platform.xxx_ver} for C{*nix}.
159 '''
160 _, v = _MODS.nix2
161 t = _version2(v, n=3) if v else (NN, NN, NN)
162 return v, t, machine()
164 @_Property_RO
165 def osversion2(self):
166 '''Get 2-list C{[OS, release]}, I{once}.
167 '''
168 import platform as p
170 _Nix, _ = _MODS.nix2
171 # - mac_ver() returns ('10.12.5', ..., 'x86_64') on
172 # macOS and ('10.3.3', ..., 'iPad4,2') on iOS
173 # - win32_ver is ('XP', ..., 'SP3', ...) on Windows XP SP3
174 # - platform() returns 'Darwin-16.6.0-x86_64-i386-64bit'
175 # on macOS and 'Darwin-16.6.0-iPad4,2-64bit' on iOS
176 # - sys.platform is 'darwin' on macOS, 'ios' on iOS,
177 # 'win32' on Windows and 'cygwin' on Windows/Gygwin
178 # - distro.id() and .name() return 'Darwin' on macOS
179 for n, v in ((_iOS_, _MODS.ios_ver),
180 (_macOS_, p.mac_ver),
181 (_Windows_, p.win32_ver),
182 (_Nix, _MODS.nix_ver),
183 ('Java', p.java_ver),
184 ('uname', p.uname)):
185 v = v()[0]
186 if v and n:
187 break
188 else:
189 n = v = NN # XXX AssertioError?
190 return [n, v]
192 @_Property_RO
193 def Pythonarchine(self):
194 '''Get 3- or 4-list C{[PyPy, Python, bits, machine]}, I{once}.
195 '''
196 v = _sys.version
197 l3 = [_Python_(v)] + self.bits_machine2
198 pypy = _PyPy__(v)
199 if pypy: # PYCHOK no cover
200 l3.insert(0, pypy)
201 return l3
203 @_Property_RO
204 def streprs(self):
205 '''Get module C{pygeodesy.streprs}, I{once}.
206 '''
207 from pygeodesy import streprs # DON'T _lazy_import2
208 return streprs
210_MODS = _MODS_Base() # PYCHOK overwritten by .lazily
213def _caller3(up): # in .lazily, .named
214 '''(INTERNAL) Get 3-tuple C{(caller name, file name, line number)}
215 for the caller B{C{up}} stack frames in the Python call stack.
216 '''
217 # sys._getframe(1) ... 'importlib._bootstrap' line 1032,
218 # may throw a ValueError('call stack not deep enough')
219 f = _sys._getframe(up + 1)
220 c = f.f_code
221 return (c.co_name, # caller name
222 _os_path.basename(c.co_filename), # file name .py
223 f.f_lineno) # line number
226def _dunder_ismain(name):
227 '''(INTERNAL) Return C{name == '__main__'}.
228 '''
229 return name == '__main__'
232def _enquote(strs, quote=_QUOTE2_, white=NN): # in .basics, .solveBase
233 '''(INTERNAL) Enquote a string containing whitespace or replace
234 whitespace by C{white} if specified.
235 '''
236 if strs:
237 t = strs.split()
238 if len(t) > 1:
239 strs = white.join(t if white else (quote, strs, quote))
240 return strs
243def _headof(name):
244 '''(INTERNAL) Get the head name of qualified C{name} or the C{name}.
245 '''
246 i = name.find(_DOT_)
247 return name if i < 0 else name[:i]
250# def _is(a, b): # PYCHOK no cover
251# '''(INTERNAL) C{a is b}? in C{PyPy}
252# '''
253# return (a == b) if _isPyPy() else (a is b)
256def _isAppleM():
257 '''(INTERNAL) Is this C{Apple Silicon}? (C{bool})
258 '''
259 return _ismacOS() and machine().startswith(_arm64_)
262def _isiOS(): # in test/bases.py
263 '''(INTERNAL) Is this C{iOS}? (C{bool})
264 '''
265 return _MODS.osversion2[0] is _iOS_
268def _ismacOS(): # in test/bases.py
269 '''(INTERNAL) Is this C{macOS}? (C{bool})
270 '''
271 return _sys.platform[:6] == 'darwin' and \
272 _MODS.osversion2[0] is _macOS_ # and os.name == 'posix'
275def _isNix(): # in test/bases.py
276 '''(INTERNAL) Is this a C{Linux} distro? (C{str} or L{NN})
277 '''
278 return _MODS.nix2[0]
281def _isPyPy(): # in test/bases.py
282 '''(INTERNAL) Is this C{PyPy}? (C{bool})
283 '''
284 # platform.python_implementation() == 'PyPy'
285 return _MODS.Pythonarchine[0].startswith(_PyPy__)
288def _isWindows(): # in test/bases.py
289 '''(INTERNAL) Is this C{Windows}? (C{bool})
290 '''
291 return _sys.platform[:3] == 'win' and \
292 _MODS.osversion2[0] is _Windows_
295def _load_lib(name):
296 '''(INTERNAL) Load a C{dylib}, B{C{name}} must startwith('lib').
297 '''
298 # macOS 11+ (aka 10.16) no longer provides direct loading of
299 # system libraries. As a result, C{ctypes.util.find_library}
300 # will not find any library, unless previously installed by a
301 # low-level dlopen(name) call (with the library base C{name}).
302 CDLL, dlopen, find_lib = _MODS.ctypes3
304 ns = find_lib(name), name
305 if dlopen is not _passarg: # _ismacOS()
306 ns += (_DOT_(name, 'dylib'),
307 _DOT_(name, 'framework'), _os_path.join(
308 _DOT_(name, 'framework'), name))
309 for n in ns:
310 try:
311 if n and dlopen(n): # pre-load handle
312 lib = CDLL(n) # == ctypes.cdll.LoadLibrary(n)
313 if lib._name: # has a qualified name
314 return lib
315 except (AttributeError, OSError):
316 pass
318 return None # raise OSError
321def machine():
322 '''Return standard C{platform.machine}, but distinguishing Intel I{native}
323 from Intel I{emulation} on Apple Silicon (on macOS only).
325 @return: Machine C{'arm64'} for Apple Silicon I{native}, C{'x86_64'}
326 for Intel I{native}, C{"arm64_x86_64"} for Intel I{emulation},
327 etc. (C{str} with C{comma}s replaced by C{underscore}s).
328 '''
329 return _MODS.bits_machine2[1]
332def _name_version(pkg):
333 '''(INTERNAL) Return C{pskg.__name__ + ' ' + .__version__}.
334 '''
335 return _SPACE_(pkg.__name__, pkg.__version__)
338def _osversion2(sep=NN): # in .lazily, test/bases.versions
339 '''(INTERNAL) Get the O/S name and release as C{2-list} or C{str}.
340 '''
341 l2 = _MODS.osversion2
342 return sep.join(l2) if sep else l2 # 2-list()
345def _passarg(arg):
346 '''(INTERNAL) Helper, no-op.
347 '''
348 return arg
351def _passargs(*args):
352 '''(INTERNAL) Helper, no-op.
353 '''
354 return args
357def _plural(noun, n):
358 '''(INTERNAL) Return C{noun}['s'] or C{NN}.
359 '''
360 return NN(noun, _s_) if n > 1 else (noun if n else NN)
363def print_(*args, **nl_nt_prec_prefix__end_file_flush_sep__kwds): # PYCHOK no cover
364 '''Python 3+ C{print}-like formatting and printing.
366 @arg args: Values to be converted to C{str} and joined by B{C{sep}},
367 all positional.
369 @see: Function L{printf} for further details.
370 '''
371 return printf(NN, *args, **nl_nt_prec_prefix__end_file_flush_sep__kwds)
374def printf(fmt, *args, **nl_nt_prec_prefix__end_file_flush_sep__kwds):
375 '''C{Printf-style} and Python 3+ C{print}-like formatting and printing.
377 @arg fmt: U{Printf-style<https://Docs.Python.org/3/library/stdtypes.html#
378 printf-style-string-formatting>} format specification (C{str}).
379 @arg args: Arguments to be formatted (any C{type}, all positional).
380 @kwarg nl_nt_prec_prefix__end_file_flush_sep__kwds: Optional keyword arguments
381 C{B{nl}=0} for the number of leading blank lines (C{int}), C{B{nt}=0}
382 the number of trailing blank lines (C{int}), C{B{prefix}=NN} to be
383 inserted before the formatted text (C{str}) and Python 3+ C{print}
384 keyword arguments C{B{end}}, C{B{sep}}, C{B{file}} and C{B{flush}}.
385 Any remaining C{B{kwds}} are C{printf-style} name-value pairs to be
386 formatted, I{iff no B{C{args}} are present} using C{B{prec}=6} for
387 the number of decimal digits (C{int}).
389 @return: Number of bytes written.
390 '''
391 b, e, f, fl, p, s, kwds = _print7(**nl_nt_prec_prefix__end_file_flush_sep__kwds)
392 try:
393 if args:
394 t = (fmt % args) if fmt else s.join(map(str, args))
395 elif kwds:
396 t = (fmt % kwds) if fmt else s.join(
397 _MODS.streprs.pairs(kwds, prec=p))
398 else:
399 t = fmt
400 except Exception as x:
401 _E, s = _MODS.errors._xError2(x)
402 unstr = _MODS.streprs.unstr
403 t = unstr(printf, fmt, *args, **nl_nt_prec_prefix__end_file_flush_sep__kwds)
404 raise _E(s, txt=t, cause=x)
405 try:
406 n = f.write(NN(b, t, e))
407 except UnicodeEncodeError: # XXX only Windows
408 t = t.replace('\u2032', _QUOTE1_).replace('\u2033', _QUOTE2_)
409 n = f.write(NN(b, t, e))
410 if fl: # PYCHOK no cover
411 f.flush()
412 return n
415def _print7(nl=0, nt=0, prec=6, prefix=NN, sep=_SPACE_, file=_sys.stdout,
416 end=_NL_, flush=False, **kwds):
417 '''(INTERNAL) Unravel the C{printf} and remaining keyword arguments.
418 '''
419 if nl > 0:
420 prefix = NN(_NL_ * nl, prefix)
421 if nt > 0:
422 end = NN(end, _NL_ * nt)
423 return prefix, end, file, flush, prec, sep, kwds
426def _Pythonarchine(sep=NN): # in .lazily, test/bases.py versions
427 '''(INTERNAL) Get PyPy and Python versions, bit and machine as C{3- or 4-list} or C{str}.
428 '''
429 l3 = _MODS.Pythonarchine
430 return sep.join(l3) if sep else l3 # 3- or 4-list
433def _sizeof(obj):
434 '''(INTERNAL) Recursively size an C{obj}ect.
436 @return: The C{obj} size in bytes (C{int}),
437 ignoring class attributes and
438 counting duplicates only once or
439 C{None}.
441 @note: With C{PyPy}, the size is always C{None}.
442 '''
443 try:
444 _zB = _sys.getsizeof
445 _zD = _zB(None) # some default
446 except TypeError: # PyPy3.10
447 return None
449 _isiterablen = _MODS.basics.isiterablen
451 def _zR(s, iterable):
452 z, _s = 0, s.add
453 for o in iterable:
454 i = id(o)
455 if i not in s:
456 _s(i)
457 z += _zB(o, _zD)
458 if isinstance(o, dict):
459 z += _zR(s, o.keys())
460 z += _zR(s, o.values())
461 elif _isiterablen(o): # not map, ...
462 z += _zR(s, o)
463 else:
464 try: # size instance' attr values only
465 z += _zR(s, o.__dict__.values())
466 except AttributeError: # None, int, etc.
467 pass
468 return z
470 return _zR(set(), (obj,))
473def _sysctl_uint(name):
474 '''(INTERNAL) Get an unsigned int sysctl item by name, use on macOS ONLY!
475 '''
476 libc = _MODS.libc
477 if libc: # <https://StackOverflow.com/questions/759892/python-ctypes-and-sysctl>
478 byref, char_p, size_t, uint, sizeof = _MODS.ctypes5
479 n = name if str is bytes else bytes(name, _utf_8_) # PYCHOK isPython2 = str is bytes
480 u = uint(0)
481 z = size_t(sizeof(u))
482 r = libc.sysctlbyname(char_p(n), byref(u), byref(z), None, size_t(0))
483 else: # could find or load 'libc'
484 r = -2
485 return int(r if r else u.value) # -1 ENOENT error, -2 no libc
488def _tailof(name):
489 '''(INTERNAL) Get the base name of qualified C{name} or the C{name}.
490 '''
491 i = name.rfind(_DOT_) + 1
492 return name[i:] if i > 0 else name
495def _under(name): # PYCHOK in .datums, .auxilats, .ups, .utm, .utmupsBase, ...
496 '''(INTERNAL) Prefix C{name} with an I{underscore}.
497 '''
498 return name if name.startswith(_UNDER_) else NN(_UNDER_, name)
501def _usage(file_py, *args): # in .etm
502 '''(INTERNAL) Build "usage: python -m ..." cmd line for module B{C{file_py}}.
503 '''
504 m = _os_path.dirname(file_py).replace(_os.getcwd(), _ELLIPSIS_) \
505 .replace(_os.sep, _DOT_).strip()
506 b, x = _os_path.splitext(_os_path.basename(file_py))
507 if x == '.py' and not _dunder_ismain(b):
508 m = _DOT_(m or _pygeodesy_, b)
509 p = NN(_python_, _sys.version_info[0])
510 u = _COLON_(_dunder_nameof(_usage)[1:], NN)
511 return _SPACE_(u, p, '-m', _enquote(m), *args)
514def _version2(version, n=2):
515 '''(INTERNAL) Split C{B{version} str} into a C{1-, 2- or 3-tuple} of C{int}s.
516 '''
517 t = _version_ints(version.split(_DOT_, 2))
518 if len(t) < n:
519 t += (0,) * n
520 return t[:n]
523def _version_info(package): # in .Base.karney, .basics
524 '''(INTERNAL) Get the C{package.__version_info__} as a 2- or
525 3-tuple C{(major, minor, revision)} if C{int}s.
526 '''
527 try:
528 return _version_ints(package.__version_info__)
529 except AttributeError:
530 return _version2(package.__version__.strip(), n=3)
533def _version_ints(vs):
534 # helper for _version2 and _version_info above
536 def _ints(vs):
537 for v in vs:
538 try:
539 yield int(v.strip())
540 except (TypeError, ValueError):
541 pass
543 return tuple(_ints(vs))
546__all__ = tuple(map(_dunder_nameof, (machine, print_, printf)))
547__version__ = '24.06.05'
549if _dunder_ismain(__name__): # PYCHOK no cover
551 from pygeodesy import _isfrozen, isLazy, version as vs
553 print_(_pygeodesy_, vs, *(_Pythonarchine() + _osversion2()
554 + ['_isfrozen', _isfrozen,
555 'isLazy', isLazy]))
557# **) MIT License
558#
559# Copyright (C) 2016-2024 -- mrJean1 at Gmail -- All Rights Reserved.
560#
561# Permission is hereby granted, free of charge, to any person obtaining a
562# copy of this software and associated documentation files (the "Software"),
563# to deal in the Software without restriction, including without limitation
564# the rights to use, copy, modify, merge, publish, distribute, sublicense,
565# and/or sell copies of the Software, and to permit persons to whom the
566# Software is furnished to do so, subject to the following conditions:
567#
568# The above copyright notice and this permission notice shall be included
569# in all copies or substantial portions of the Software.
570#
571# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
572# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
573# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
574# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
575# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
576# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
577# OTHER DEALINGS IN THE SOFTWARE.