1# -*- coding: utf-8 -*-

2

3 u'''Single C{str}ing constants, C{intern}'ed across C{pygeodesy}

4 modules and function L{pygeodesy.machine}.

5'''

6 import sys as _sys

7

8_COMMASPACE_ = ', ' # overriden below

9_pf2List = [] # cached _platform2 list

10_Py3List = [] # cached _pythonarchine list

11

12

13 class _Dash(str):

14 '''(INTERNAL) Extended C{str} for prefix_DASH_.

15 '''

16 def __call__(self, *args):

17 '''Join C{self} plus all B{C{args}} like C{'-'.join((self,) + B{args})}.

18 '''

19 return _DASH_(self, *args) # re-callable

20

21

22 class _Int(int):

23 '''(INTERNAL) Unique C{int}.

24 '''

25 pass

26

27

28 class Str_(str):

29 '''Extended, I{callable} C{str} class, not nameable.

30

31 @see: Nameable and callable class L{pygeodesy.Str}.

32 '''

33 def join_(self, *args):

34 '''Join all positional B{C{args}} like C{self.join(B{args})}.

35

36 @return: All B{C{args}} joined by this instance (L{Str_}).

37

38 @note: An other L{Str_} instance is returned to make the

39 result re-callable.

40 '''

41 return Str_(str.join(self, map(str, args))) # re-callable

42

43 __call__ = join_

44

45 NN = Str_('') # PYCHOK Nomen Nescio <https://Wiktionary.org/wiki/N.N.>

46

47

48 class _Prefix(Str_):

49 '''(INTERNAL) Extended C{str} for prefix.

50 '''

51 def __call__(self, *args):

52 '''Join C{self} plus all B{C{args}} like C{" ".join((self,) + B{args})}.

53 '''

54 return _SPACE_.join_(self, *args) # re-callable

55

56

57 class _PyPy__(str): # overwritten by singleton below

58 '''(INTERNAL) Extended C{str} for C{"PyPy"} and version.

59 '''

60 def __call__(self, version=NN):

61 '''Return C{"PyPy <version>"} or C{NN}.

62 '''

63 v = version or _sys.version

64 if _PyPy__ in v:

65 v = v.split(_PyPy__)[1].split(None, 1)[0] # == _DOT_.join(_sys.pypy_version_info[:3])

66 return NN(_PyPy__, v)

67 else:

68 return NN

69

70

71 class _Python_(str): # overwritten by singleton below

72 '''(INTERNAL) Extended C{str} for C{"Python"} and version.

73 '''

74 def __call__(self, version=NN):

75 '''Return C{"Python <version>"}.

76 '''

77 v = version or _sys.version

78 return _SPACE_(self, v.split(None, 1)[0])

79

80

81 class _Range(str):

82 '''(INTERNAL) Extended C{str} for C{range} strings.

83 '''

84 def __call__(self, lo, hi, lopen=False, ropen=False,

85 prec=0, sep=_COMMASPACE_):

86 '''Return the range as C{"(lo, hi)"}, C{"(lo, hi]"},

87 C{"[lo, hi)"} or C{"[lo, hi]"}.

88 '''

89 from pygeodesy.streprs import Fmt

90 r = NN(Fmt.f(lo, prec=prec), sep,

91 Fmt.f(hi, prec=prec))

92 f = (Fmt.PAREN if ropen else Fmt.LOPEN) if lopen else \

93 (Fmt.ROPEN if ropen else Fmt.SQUARE)

94 return f(r)

95

96

97 class _Slicer(str):

98 '''(INTERNAL) String slicer C{.fromX} or C{.tillY}.

99 '''

100 def __getattr__(self, name): # .fromX, .tillY

101 n = len(name) - 4

102 if n > 0:

103 # assert len('till') == len(_from_) == 4

104 if name.startswith('till'):

105 i = self.find(name[4:])

106 return self if i < 0 else _Slicer(self[:i + n])

107 elif name.startswith(_from_):

108 i = self.find(name[4:])

109 return self if i < 0 else _Slicer(self[i:])

110 return str.__getattr__(self, name) # PYCHOK no cover

111

112

113 class MISSING(object):

114 '''(INTERNAL) Singleton C{str}.

115 '''

116 def toRepr(self, **unused):

117 return self.__class__.__name__

118

119 __repr__ = __str__ = toStr = toRepr

120

121 MISSING = MISSING() # PYCHOK singleton

122 MISSING.__name__ = str(MISSING)

123

124# __DUNDER__-style names would get mangled in classes

125_0_ = '0' # PYCHOK 'zero'

126_0to9_ = '0123456789' # PYCHOK OK

127_1_ = '1' # PYCHOK OK

128_2_ = '2' # PYCHOK OK

129_3_ = '3' # PYCHOK OK

130_4_ = '4' # PYCHOK OK

131_a_ = 'a' # PYCHOK OK

132_A_ = 'A' # PYCHOK OK

133_a12_ = 'a12' # PYCHOK OK

134_area_ = 'area' # PYCHOK OK

135_Airy1830_ = 'Airy1830' # PYCHOK OK

136_AiryModified_ = 'AiryModified' # PYCHOK OK

137_ambiguous_ = 'ambiguous' # PYCHOK OK

138_AMPERSAND_ = Str_('&') # PYCHOK OK

139_an_ = 'an' # PYCHOK OK

140_and_ = 'and' # PYCHOK OK

141# _AND_ = _AMPERSAND_ # PYCHOK OK

142_angle_ = 'angle' # PYCHOK OK

143_antipodal_ = 'antipodal' # PYCHOK OK

144_areaOf_ = 'areaOf' # PYCHOK OK

145_arg_ = 'arg' # PYCHOK OK

146_at_ = 'at' # PYCHOK OK

147_AT_ = Str_('@') # PYCHOK OK

148_AtoZnoIO_ = _Slicer('ABCDEFGHJKLMNPQRSTUVWXYZ') # PYCHOK in .gars, .mgrs and .wgrs

149_attribute_ = 'attribute' # PYCHOK OK

150_azi1_ = 'azi1' # PYCHOK OK

151_azi12_ = 'azi12' # PYCHOK OK

152_azi2_ = 'azi2' # PYCHOK OK

153_azimuth_ = 'azimuth' # PYCHOK OK

154_b_ = 'b' # PYCHOK OK

155_B_ = 'B' # PYCHOK OK

156_BACKSLASH_ = Str_('\\') # PYCHOK OK

157_band_ = 'band' # PYCHOK OK

158_BANG_ = Str_('!') # PYCHOK OK

159_BAR_ = Str_('|') # PYCHOK OK

160_bearing_ = 'bearing' # PYCHOK OK

161_Bessel1841_ = 'Bessel1841' # PYCHOK OK

162_beta_ = 'beta' # PYCHOK OK

163_by_ = 'by' # PYCHOK OK

164_c_ = 'c' # PYCHOK OK

165_C_ = 'C' # PYCHOK OK

166_cartesian_ = 'cartesian' # PYCHOK OK

167_center_ = 'center' # PYCHOK OK

168# _CIRCUMFLEX_ = Str_('^') # PYCHOK OK

169_Clarke1866_ = 'Clarke1866' # PYCHOK OK

170_Clarke1880IGN_ = 'Clarke1880IGN' # PYCHOK OK

171_clip_ = 'clip' # PYCHOK OK

172_clipid_ = 'clipid' # PYCHOK OK

173_coincident_ = 'coincident' # PYCHOK OK

174_colinear_ = 'colinear' # PYCHOK OK

175_COLON_ = Str_(':') # PYCHOK OK

176_COLONSPACE_ = Str_(': ') # PYCHOK OK

177_COMMA_ = Str_(',') # PYCHOK OK

178_COMMASPACE_ = Str_(_COMMASPACE_) # PYCHOK OK

179_composite_ = 'composite' # PYCHOK OK

180_concentric_ = 'concentric' # PYCHOK OK

181_convergence_ = _Prefix('convergence') # PYCHOK OK

182_conversion_ = 'conversion' # PYCHOK OK

183_convex_ = 'convex' # PYCHOK OK

184_cubic_ = 'cubic' # PYCHOK OK

185_d_ = 'd' # PYCHOK OK

186_D_ = 'D' # PYCHOK OK

187_DASH_ = Str_('-') # PYCHOK == _MINUS_

188_datum_ = 'datum' # PYCHOK OK

189_decode3_ = 'decode3' # PYCHOK OK

190_deg_ = 'deg' # PYCHOK OK

191_degrees_ = 'degrees' # PYCHOK OK

192_degrees2_ = 'degrees2' # PYCHOK SQUARED

193_delta_ = 'delta' # PYCHOK OK

194_DEPRECATED_ = 'DEPRECATED' # PYCHOK OK

195_DEQUALSPACED_ = Str_(' == ') # PYCHOK OK

196_distance_ = 'distance' # PYCHOK OK

197_distant_ = _Prefix('distant') # PYCHOK OK

198_doesn_t_exist_ = "doesn't exist" # PYCHOK OK

199_DOT_ = Str_('.') # PYCHOK OK

200_down_ = 'down' # PYCHOK OK

201_e_ = 'e' # PYCHOK OK

202_E_ = 'E' # PYCHOK OK

203_earth_ = 'earth' # PYCHOK OK

204_east_ = 'east' # PYCHOK OK

205_easting_ = 'easting' # PYCHOK OK

206_ecef_ = 'ecef' # PYCHOK OK

207_edge_ = 'edge' # PYCHOK OK

208_elevation_ = 'elevation' # PYCHOK OK

209_ELLIPSIS_ = Str_('...') # PYCHOK OK

210_ELLIPSIS4_ = Str_('....') # PYCHOK OK

211# _ELLIPSISPACED_ = Str_(' ... ') # PYCHOK <https://www.ThePunctuationGuide.com/ellipses.html>

212_ellipsoid_ = 'ellipsoid' # PYCHOK OK

213_ellipsoidal_ = 'ellipsoidal' # PYCHOK OK

214_enabled_ = 'enabled' # PYCHOK OK

215_encode_ = 'encode' # PYCHOK OK

216_end_ = 'end' # PYCHOK OK

217_epoch_ = 'epoch' # PYCHOK OK

218_eps_ = 'eps' # PYCHOK OK

219_EQUAL_ = Str_('=') # PYCHOK OK

220_EQUALSPACED_ = Str_(' = ') # PYCHOK OK

221_Error_ = 'Error' # PYCHOK OK

222_exceed_PI_radians_ = 'exceed PI radians' # PYCHOK OK

223_exceeds_ = _Prefix('exceeds') # PYCHOK OK

224# _EXCLAMATION_ = _BANG_ # PYCHOK OK

225_exists_ = 'exists' # PYCHOK OK

226_f_ = 'f' # PYCHOK OK

227_F_ = 'F' # PYCHOK OK

228_feet_ = 'feet' # PYCHOK OK

229_few_ = 'few' # PYCHOK OK

230_fi_ = 'fi' # PYCHOK OK

231_finite_ = 'finite' # PYCHOK OK

232_from_ = 'from' # PYCHOK OK

233_g_ = 'g' # PYCHOK OK

234_gamma_ = 'gamma' # PYCHOK OK

235_GRS80_ = 'GRS80' # PYCHOK OK

236_h_ = 'h' # PYCHOK OK

237_H_ = 'H' # PYCHOK OK

238_HASH_ = '#' # PYCHOK OK

239_height_ = 'height' # PYCHOK OK

240_hemipole_ = 'hemipole' # PYCHOK OK

241_i_ = 'i' # PYCHOK OK

242_iadd_op_ = '+=' # PYCHOK OK

243_immutable_ = 'immutable' # PYCHOK OK

244_in_ = 'in' # PYCHOK OK

245_incompatible_ = 'incompatible' # PYCHOK OK

246_INF_ = 'INF' # PYCHOK OK

247_infinite_ = 'infinite' # PYCHOK _not_finite_

248_initial_ = 'initial' # PYCHOK OK

249_inside_ = 'inside' # PYCHOK OK

250_insufficient_ = 'insufficient' # PYCHOK OK

251_intersection_ = 'intersection' # PYCHOK OK

252_Intl1924_ = 'Intl1924' # PYCHOK OK

253_invalid_ = 'invalid' # PYCHOK OK

254_isclockwise_ = 'isclockwise' # PYCHOK OK

255_ispolar_ = 'ispolar' # PYCHOK OK

256_j_ = 'j' # PYCHOK OK

257_k0_ = 'k0' # PYCHOK OK

258_kind_ = 'kind' # PYCHOK OK

259_knots_ = 'knots' # PYCHOK OK

260_Krassovski1940_ = 'Krassovski1940' # PYCHOK OK

261_Krassowsky1940_ = 'Krassowsky1940' # PYCHOK OK

262_LANGLE_ = '<' # PYCHOK OK

263_lam_ = 'lam' # PYCHOK OK

264_lat_ = 'lat' # PYCHOK OK

265_lat0_ = 'lat0' # PYCHOK OK

266_lat1_ = 'lat1' # PYCHOK OK

267_lat2_ = 'lat2' # PYCHOK OK

268_latlon_ = 'latlon' # PYCHOK OK

269_LatLon_ = 'LatLon' # PYCHOK OK

270_LCURLY_ = '{' # PYCHOK LBRACE

271_len_ = 'len' # PYCHOK OK

272_limit_ = 'limit' # PYCHOK OK

273_line_ = 'line' # PYCHOK OK

274_linear_ = 'linear' # PYCHOK OK

275_LPAREN_ = '(' # PYCHOK OK

276_lon_ = 'lon' # PYCHOK OK

277_lon0_ = 'lon0' # PYCHOK OK

278_lon1_ = 'lon1' # PYCHOK OK

279_lon2_ = 'lon2' # PYCHOK OK

280_low_ = 'low' # PYCHOK OK

281_LSQUARE_ = '[' # PYCHOK LBRACK

282_ltp_ = 'ltp' # PYCHOK OK

283_m_ = 'm' # PYCHOK OK

284_M_ = 'M' # PYCHOK OK

285_m12_ = 'm12' # PYCHOK OK

286_M12_ = 'M12' # PYCHOK OK

287_M21_ = 'M21' # PYCHOK OK

288_MANT_DIG_ = 'MANT_DIG' # PYCHOK OK

289_MAX_ = 'MAX' # PYCHOK OK

290_mean_ = 'mean' # PYCHOK OK

291_meanOf_ = 'meanOf' # PYCHOK OK

292_meridional_ = 'meridional' # PYCHOK OK

293_meter_ = 'meter' # PYCHOK OK

294_meter2_ = 'meter2' # PYCHOK SQUARED

295_MGRS_ = 'MGRS' # PYCHOK OK

296_MIN_ = 'MIN' # PYCHOK OK

297_MINUS_ = _DASH_ # PYCHOK OK

298_module_ = 'module' # PYCHOK OK

299_n_ = 'n' # PYCHOK OK

300_N_ = 'N' # PYCHOK OK

301_n_a_ = 'n/a' # PYCHOK OK

302_N_A_ = 'N/A' # PYCHOK OK

303_NAD27_ = 'NAD27' # PYCHOK OK

304_NAD83_ = 'NAD83' # PYCHOK OK

305_name_ = 'name' # PYCHOK OK

306_NAN_ = 'NAN' # PYCHOK OK

307_near_ = _Dash('near') # PYCHOK OK

308_nearestOn2_ = 'nearestOn2' # PYCHOK OK

309_negative_ = 'negative' # PYCHOK OK

310_NL_ = Str_('\n') # PYCHOK OK

311_NLATvar_ = Str_(_NL_ + '@var ') # PYCHOK OK

312_NLHASH_ = Str_(_NL_ + '# ') # PYCHOK OK

313# _NLNL_ = _DNL_ # PYCHOK OK

314_NN_ = 'NN' # PYCHOK OK

315_no_ = _Prefix('no') # PYCHOK OK

316_north_ = 'north' # PYCHOK OK

317_northing_ = 'northing' # PYCHOK OK

318_NorthPole_ = 'NorthPole' # PYCHOK OK

319_not_ = _Prefix('not') # PYCHOK OK

320_NOTEQUAL_ = _BANG_ + _EQUAL_ # PYCHOK OK

321_not_finite_ = 'not finite' # PYCHOK _not_(_finite_), _infinite_

322_not_scalar_ = 'not scalar' # PYCHOK _not_(_scalar_)

323_NTF_ = 'NTF' # PYCHOK OK

324_null_ = 'null' # PYCHOK OK

325_number_ = 'number' # PYCHOK OK

326_numpy_ = 'numpy' # PYCHOK OK

327_Nv00_ = 'Nv00' # PYCHOK OK

328_of_ = 'of' # PYCHOK OK

329_on_ = 'on' # PYCHOK OK

330_opposite_ = 'opposite' # PYCHOK OK

331_or_ = 'or' # PYCHOK OK

332_other_ = 'other' # PYCHOK OK

333_outside_ = 'outside' # PYCHOK OK

334_overlap_ = 'overlap' # PYCHOK OK

335_parallel_ = 'parallel' # PYCHOK OK

336_PERCENT_ = '%' # PYCHOK OK

337_PERCENTDOTSTAR_ = '%.*' # PYCHOK _DOT_(_PERCENT_, _STAR_)

338_phi_ = 'phi' # PYCHOK OK

339_PLUS_ = Str_('+') # PYCHOK OK

340_PLUSMINUS_ = _PLUS_ + _MINUS_ # PYCHOK OK

341_point_ = 'point' # PYCHOK OK

342_points_ = 'points' # PYCHOK OK

343_pole_ = 'pole' # PYCHOK OK

344_precision_ = 'precision' # PYCHOK OK

345_prime_vertical_ = 'prime_vertical' # PYCHOK OK

346_pygeodesy_ = 'pygeodesy' # PYCHOK OK

347_pygeodesy_abspath_ = 'pygeodesy_abspath' # PYCHOK OK

348_PyPy__ = _PyPy__('PyPy ') # PYCHOK + _SPACE_

349_Python_ = _Python_('Python') # PYCHOK singleton

350_python_ = 'python' # PYCHOK OK

351_QUOTE1_ = "'" # PYCHOK OK

352_QUOTE2_ = '"' # PYCHOK OK

353# _QUOTE3_ = "'''" # PYCHOK OK

354# _QUOTE6_ = '"""' # PYCHOK OK

355_R_ = 'R' # PYCHOK OK

356_radians_ = 'radians' # PYCHOK OK

357_radians2_ = 'radians2' # PYCHOK SQUARED

358_radius_ = 'radius' # PYCHOK OK

359_radius1_ = 'radius1' # PYCHOK OK

360_radius2_ = 'radius2' # PYCHOK OK

361_range_ = _Range('range') # PYCHOK OK

362_RANGLE_ = '>' # PYCHOK OK

363_RCURLY_ = '}' # PYCHOK RBRACE

364_reciprocal_ = 'reciprocal' # PYCHOK OK

365_reframe_ = 'reframe' # PYCHOK OK

366_resolution_ = 'resolution' # PYCHOK OK

367_rIn_ = 'rIn' # PYCHOK OK

368_RPAREN_ = ')' # PYCHOK OK

369_RSQUARE_ = ']' # PYCHOK RBRACK

370_s_ = 's' # PYCHOK OK

371_S_ = 'S' # PYCHOK OK

372_s12_ = 's12' # PYCHOK OK

373_S12_ = 'S12' # PYCHOK OK

374_scalar_ = 'scalar' # PYCHOK OK

375_scale_ = 'scale' # PYCHOK OK

376_scale0_ = 'scale0' # PYCHOK OK

377_scipy_ = 'scipy' # PYCHOK OK

378_semi_circular_ = 'semi-circular' # PYCHOK OK

379_sep_ = 'sep' # PYCHOK OK

380_singular_ = 'singular' # PYCHOK OK

381_SLASH_ = Str_('/') # PYCHOK OK

382_small_ = 'small' # PYCHOK OK

383_Sphere_ = 'Sphere' # PYCHOK OK

384_spherical_ = 'spherical' # PYCHOK OK

385_SouthPole_ = 'SouthPole' # PYCHOK OK

386_SPACE_ = Str_(' ') # PYCHOK OK

387_specified_ = 'specified' # PYCHOK OK

388_STAR_ = Str_('*') # PYCHOK OK

389_start_ = 'start' # PYCHOK OK

390_std_ = 'std' # PYCHOK OK

391_stdev_ = 'stdev' # PYCHOK OK

392_supported_ = 'supported' # PYCHOK OK

393_sx_ = 'sx' # PYCHOK OK

394_sy_ = 'sy' # PYCHOK OK

395_sz_ = 'sz' # PYCHOK OK

396_tbd_ = 'tbd' # PYCHOK OK

397_TILDE_ = '~' # PYCHOK OK

398_to_ = 'to' # PYCHOK OK

399_tolerance_ = _Prefix('tolerance') # PYCHOK OK

400_too_ = _Prefix('too') # PYCHOK OK

401_transform_ = 'transform' # PYCHOK OK

402_tx_ = 'tx' # PYCHOK OK

403_ty_ = 'ty' # PYCHOK OK

404_tz_ = 'tz' # PYCHOK OK

405_UNDER_ = Str_('_') # PYCHOK OK

406_units_ = 'units' # PYCHOK OK

407_UNUSED_ = 'UNUSED' # PYCHOK OK

408_up_ = 'up' # PYCHOK OK

409_UPS_ = 'UPS' # PYCHOK OK

410_utf_8_ = 'utf-8' # PYCHOK OK

411_UTM_ = 'UTM' # PYCHOK OK

412_V_ = 'V' # PYCHOK OK

413_valid_ = 'valid' # PYCHOK OK

414_value_ = 'value' # PYCHOK OK

415_version_ = 'version' # PYCHOK OK

416_vs_ = 'vs' # PYCHOK OK

417_W_ = 'W' # PYCHOK OK

418_WGS72_ = 'WGS72' # PYCHOK OK

419_WGS84_ = 'WGS84' # PYCHOK OK

420_width_ = 'width' # PYCHOK OK

421_with_ = 'with' # PYCHOK OK

422_x_ = 'x' # PYCHOK OK

423_X_ = 'X' # PYCHOK OK

424_xyz_ = 'xyz' # PYCHOK OK

425_y_ = 'y' # PYCHOK OK

426_Y_ = 'Y' # PYCHOK OK

427_z_ = 'z' # PYCHOK OK

428_Z_ = 'Z' # PYCHOK OK

429_zone_ = 'zone' # PYCHOK OK

430

431_EW_ = _E_ + _W_ # PYCHOK common cardinals

432_NE_ = _N_ + _E_ # PYCHOK positive ones

433_NS_ = _N_ + _S_ # PYCHOK OK

434_NSEW_ = _NS_ + _EW_ # PYCHOK OK

435_NW_ = _N_ + _W_ # PYCHOK OK

436_SE_ = _S_ + _E_ # PYCHOK OK

437_SW_ = _S_ + _W_ # PYCHOK negative ones

438# _NESW_ = _NE_ + _SW_ # PYCHOK clockwise

439

440_DDOT_ = Str_(_DOT_ * 2) # PYCHOK OK

441# _DEQUAL_ = Str_(_EQUAL_ * 2) # PYCHOK OK

442_DNL_ = Str_(_NL_ * 2) # PYCHOK OK

443# _DSLASH_ = Str_(_SLASH_ * 2) # PYCHOK OK

444# _DSTAR_ = Str_(_STAR_ * 2) # PYCHOK OK

445_DUNDER_ = Str_(_UNDER_ * 2) # PYCHOK OK

446

447_LR_PAIRS = {_LANGLE_: _RANGLE_,

448 _LCURLY_: _RCURLY_,

449 _LPAREN_: _RPAREN_,

450 _LSQUARE_: _RSQUARE_} # PYCHOK OK

451

452

453 def _dunder_nameof(inst, *dflt):

454 '''(INTERNAL) Get the double_underscore __name__ attr.

455 '''

456 try:

457 return inst.__name__

458 except AttributeError:

459 pass

460 return dflt[0] if dflt else inst.__class__.__name__

461

462

463 def _enquote(strs, quote=_QUOTE2_): # in .basics, .solveBase

464 '''(INTERNAL) Enquote a string containing whitespace.

465 '''

466 if len(strs.split()) > 1:

467 strs = NN(quote, strs, quote)

468 return strs

469

470

471 def _is(a, b): # PYCHOK no cover

472 '''(INTERNAL) C{a is b}? in C{PyPy}

473 '''

474 return (a == b) if _isPyPy() else (a is b)

475

476

477 def _isPyPy():

478 '''(INTERNAL) Is this C{PyPy}?

479 '''

480 # platform.python_implementation() == 'PyPy'

481 return _pythonarchine()[0].startswith(_PyPy__)

482

483

484 def _load_lib(name):

485 '''(INTERNAL) Load a C{dylib}, B{C{name}} must startwith('lib').

486 '''

487 # macOS 11+ (aka 10.16) no longer provides direct loading of

488 # system libraries. As a result, C{ctypes.util.find_library}

489 # will not find any library, unless previously installed by a

490 # low-level dlopen(name) call (with the library base C{name}).

491 from ctypes import CDLL

492 from ctypes.util import find_library

493

494 ns = find_library(name), name

495 if _sys.platform[:6] == 'darwin': # and os.name == 'posix'

496 from ctypes import _dlopen, DEFAULT_MODE

497 from os.path import join

498 ns += (_DOT_(name, 'dylib'),

499 _DOT_(name, 'framework'), join(

500 _DOT_(name, 'framework'), name))

501 else: # non-macOS

502 DEFAULT_MODE = 0

503

504 def _dlopen(*unused):

505 return True

506

507 for n in ns:

508 try:

509 if n and _dlopen(n, DEFAULT_MODE): # pre-load handle

510 lib = CDLL(n) # == ctypes.cdll.LoadLibrary(n)

511 if lib._name: # has a qualified name

512 return lib

513 except (AttributeError, OSError):

514 pass

515

516 return None # raise OSError

517

518

519 def machine():

520 '''Return standard C{platform.machine}, but distinguishing Intel I{native}

521 from Intel I{emulation} on Apple Silicon (on macOS only).

522

523 @return: Machine C{'arm64'} for Apple Silicon I{native}, C{'x86_64'}

524 for Intel I{native}, C{"arm64_x86_64"} for Intel I{emulation},

525 etc. (C{str} with C{comma}s replaced by C{underscore}s).

526 '''

527 return _platform2()[1]

528

529

530 def _platform2(sep=NN):

531 '''(INTERNAL) Get platform architecture and machine as C{2-list} or C{str}.

532 '''

533 L = _pf2List

534 if not L:

535 import platform

536 m = platform.machine() # ARM64, arm64, x86_64, iPhone13,2, etc.

537 m = m.replace(_COMMA_, _UNDER_)

538 if m.lower() == 'x86_64': # on Intel or Rosetta2 ...

539 v = platform.mac_ver() # ... and only on macOS ...

540 if v and _version2(v[0]) > (10, 15): # ... 11+ aka 10.16

541 # <https://Developer.Apple.com/forums/thread/659846>

542 if _sysctl_uint('sysctl.proc_translated') == 1: # and \

543# _sysctl_uint('hw.optional.arm64') == 1: # PYCHOK indent

544 m = _UNDER_('arm64', m) # Apple Si emulating Intel x86-64

545 L[:] = [platform.architecture()[0], # bits

546 m] # arm64, arm64_x86_64, x86_64, etc.

547 return sep.join(L) if sep else L # 2-list()

548

549

550 def _pythonarchine(sep=NN): # in test/bases.py versions

551 '''(INTERNAL) Get PyPy and Python versions and C{_platform2} as C{3- or 4-list} or C{str}.

552 '''

553 L = _Py3List

554 if not L:

555 v = _sys.version

556 L[:] = [_Python_(v)] + _platform2()

557 pypy = _PyPy__(v)

558 if pypy:

559 L.insert(0, pypy)

560 return sep.join(L) if sep else L # 3- or 4-list

561

562

563 def _sysctl_uint(name):

564 '''(INTERNAL) Get an unsigned int sysctl item by name, use on macOS ONLY!

565 '''

566 libc = _load_lib('libc')

567 if libc: # <https://StackOverflow.com/questions/759892/python-ctypes-and-sysctl>

568 from ctypes import byref, c_char_p, c_size_t, c_uint, sizeof # get_errno

569 n = name if str is bytes else bytes(name, _utf_8_) # PYCHOK isPython2 = str is bytes

570 u = c_uint(0)

571 z = c_size_t(sizeof(u))

572 r = libc.sysctlbyname(c_char_p(n), byref(u), byref(z), None, c_size_t(0))

573 else: # could find or load 'libc'

574 r = -2

575 return int(r if r else u.value) # -1 ENOENT error, -2 no libc

576

577

578 def _under(name): # PYCHOK in .datums, .auxilats, .ups, .utm, .utmupsBase, ...

579 '''(INTERNAL) Prefix C{name} with I{underscore}.

580 '''

581 return name if name.startswith(_UNDER_) else NN(_UNDER_, name)

582

583

584 def _usage(file_py, *args): # in .etm

585 '''(INTERNAL) Build "usage: python -m ..." cmd line for module B{C{file_py}}.

586 '''

587 import os

588 m = os.path.dirname(file_py).replace(os.getcwd(), _ELLIPSIS_) \

589 .replace(os.sep, _DOT_).strip()

590 b, x = os.path.splitext(os.path.basename(file_py))

591 if x == '.py' and b != '__main__':

592 m = _DOT_(m or _pygeodesy_, b)

593 p = NN(_python_, _sys.version_info[0])

594 return NN('usage', _SPACE_(_COLON_, p, '-m', _enquote(m), *args))

595

596

597 def _version2(version, n=2):

598 '''(INTERNAL) Split C{B{version} str} into a C{1-, 2- or 3-tuple} of C{int}s.

599 '''

600 def _ints(vs):

601 for v in vs:

602 if v:

603 try:

604 yield int(v.strip())

605 except (TypeError, ValueError):

606 pass

607

608 t = tuple(_ints(version.split(_DOT_, 2)))

609 if len(t) < n:

610 t += (0,) * n

611 return tuple(t[:n])

612

613

614__all__ = (_NN_, # not MISSING!

615 Str_.__name__, # classes

616 machine.__name__) # in .lazily

617__version__ = '23.08.28'

618

619 if __name__ == '__main__':

620

621 from pygeodesy import itemsorted, printf

622

623 t = b = 0

624 for n, v in itemsorted(locals(), asorted=False, reverse=True):

625 if n.endswith(_UNDER_) and n.startswith(_UNDER_) and \

626 not n.startswith(_DUNDER_):

627 t += 1

628 b += len(v)

629 m = n[1:-1]

630 if m != v and m.replace(_UNDER_, _SPACE_) != v:

631 printf('%4d: %s = %r', t, n, v)

632 n = len(locals())

633 printf('%4d (%d) names, %s chars total, %.2f chars avg', t, n, b, float(b) / t, nl=1)

634

635# **) MIT License

636#

637# Copyright (C) 2016-2023 -- mrJean1 at Gmail -- All Rights Reserved.

638#

639# Permission is hereby granted, free of charge, to any person obtaining a

640# copy of this software and associated documentation files (the "Software"),

641# to deal in the Software without restriction, including without limitation

642# the rights to use, copy, modify, merge, publish, distribute, sublicense,

643# and/or sell copies of the Software, and to permit persons to whom the

644# Software is furnished to do so, subject to the following conditions:

645#

646# The above copyright notice and this permission notice shall be included

647# in all copies or substantial portions of the Software.

648#

649# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS

650# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

651# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL

652# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR

653# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,

654# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR

655# OTHER DEALINGS IN THE SOFTWARE.