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_invokation_ = 'invokation' # PYCHOK OK

255_isclockwise_ = 'isclockwise' # PYCHOK OK

256_ispolar_ = 'ispolar' # PYCHOK OK

257_j_ = 'j' # PYCHOK OK

258_k0_ = 'k0' # PYCHOK OK

259_kind_ = 'kind' # PYCHOK OK

260_knots_ = 'knots' # PYCHOK OK

261_Krassovski1940_ = 'Krassovski1940' # PYCHOK OK

262_Krassowsky1940_ = 'Krassowsky1940' # PYCHOK OK

263_LANGLE_ = '<' # PYCHOK OK

264_lam_ = 'lam' # PYCHOK OK

265_lat_ = 'lat' # PYCHOK OK

266_lat0_ = 'lat0' # PYCHOK OK

267_lat1_ = 'lat1' # PYCHOK OK

268_lat2_ = 'lat2' # PYCHOK OK

269_latlon_ = 'latlon' # PYCHOK OK

270_LatLon_ = 'LatLon' # PYCHOK OK

271_LCURLY_ = '{' # PYCHOK LBRACE

272_len_ = 'len' # PYCHOK OK

273_limit_ = 'limit' # PYCHOK OK

274_line_ = 'line' # PYCHOK OK

275_linear_ = 'linear' # PYCHOK OK

276_LPAREN_ = '(' # PYCHOK OK

277_lon_ = 'lon' # PYCHOK OK

278_lon0_ = 'lon0' # PYCHOK OK

279_lon1_ = 'lon1' # PYCHOK OK

280_lon2_ = 'lon2' # PYCHOK OK

281_low_ = 'low' # PYCHOK OK

282_LSQUARE_ = '[' # PYCHOK LBRACK

283_ltp_ = 'ltp' # PYCHOK OK

284_m_ = 'm' # PYCHOK OK

285_M_ = 'M' # PYCHOK OK

286_m12_ = 'm12' # PYCHOK OK

287_M12_ = 'M12' # PYCHOK OK

288_M21_ = 'M21' # PYCHOK OK

289_MANT_DIG_ = 'MANT_DIG' # PYCHOK OK

290_MAX_ = 'MAX' # PYCHOK OK

291_mean_ = 'mean' # PYCHOK OK

292_meanOf_ = 'meanOf' # PYCHOK OK

293_meridional_ = 'meridional' # PYCHOK OK

294_meter_ = 'meter' # PYCHOK OK

295_meter2_ = 'meter2' # PYCHOK SQUARED

296_MGRS_ = 'MGRS' # PYCHOK OK

297_MIN_ = 'MIN' # PYCHOK OK

298_MINUS_ = _DASH_ # PYCHOK OK

299_module_ = 'module' # PYCHOK OK

300_n_ = 'n' # PYCHOK OK

301_N_ = 'N' # PYCHOK OK

302_n_a_ = 'n/a' # PYCHOK OK

303_N_A_ = 'N/A' # PYCHOK OK

304_NAD27_ = 'NAD27' # PYCHOK OK

305_NAD83_ = 'NAD83' # PYCHOK OK

306_name_ = 'name' # PYCHOK OK

307_NAN_ = 'NAN' # PYCHOK OK

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

309_nearestOn2_ = 'nearestOn2' # PYCHOK OK

310_negative_ = 'negative' # PYCHOK OK

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

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

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

314# _NLNL_ = _DNL_ # PYCHOK OK

315_NN_ = 'NN' # PYCHOK OK

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

317_north_ = 'north' # PYCHOK OK

318_northing_ = 'northing' # PYCHOK OK

319_NorthPole_ = 'NorthPole' # PYCHOK OK

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

321_NOTEQUAL_ = _BANG_ + _EQUAL_ # PYCHOK OK

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

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

324_NTF_ = 'NTF' # PYCHOK OK

325_null_ = 'null' # PYCHOK OK

326_number_ = 'number' # PYCHOK OK

327_numpy_ = 'numpy' # PYCHOK OK

328_Nv00_ = 'Nv00' # PYCHOK OK

329_of_ = 'of' # PYCHOK OK

330_on_ = 'on' # PYCHOK OK

331_opposite_ = 'opposite' # PYCHOK OK

332_or_ = 'or' # PYCHOK OK

333_other_ = 'other' # PYCHOK OK

334_outside_ = 'outside' # PYCHOK OK

335_overlap_ = 'overlap' # PYCHOK OK

336_parallel_ = 'parallel' # PYCHOK OK

337_PERCENT_ = '%' # PYCHOK OK

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

339_phi_ = 'phi' # PYCHOK OK

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

341_PLUSMINUS_ = _PLUS_ + _MINUS_ # PYCHOK OK

342_point_ = 'point' # PYCHOK OK

343_points_ = 'points' # PYCHOK OK

344_pole_ = 'pole' # PYCHOK OK

345_precision_ = 'precision' # PYCHOK OK

346_prime_vertical_ = 'prime_vertical' # PYCHOK OK

347_pygeodesy_ = 'pygeodesy' # PYCHOK OK

348_pygeodesy_abspath_ = 'pygeodesy_abspath' # PYCHOK OK

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

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

351_python_ = 'python' # PYCHOK OK

352_QUOTE1_ = "'" # PYCHOK OK

353_QUOTE2_ = '"' # PYCHOK OK

354# _QUOTE3_ = "'''" # PYCHOK OK

355# _QUOTE6_ = '"""' # PYCHOK OK

356_R_ = 'R' # PYCHOK OK

357_radians_ = 'radians' # PYCHOK OK

358_radians2_ = 'radians2' # PYCHOK SQUARED

359_radius_ = 'radius' # PYCHOK OK

360_radius1_ = 'radius1' # PYCHOK OK

361_radius2_ = 'radius2' # PYCHOK OK

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

363_RANGLE_ = '>' # PYCHOK OK

364_RCURLY_ = '}' # PYCHOK RBRACE

365_reciprocal_ = 'reciprocal' # PYCHOK OK

366_reframe_ = 'reframe' # PYCHOK OK

367_resolution_ = 'resolution' # PYCHOK OK

368_rIn_ = 'rIn' # PYCHOK OK

369_RPAREN_ = ')' # PYCHOK OK

370_RSQUARE_ = ']' # PYCHOK RBRACK

371_s_ = 's' # PYCHOK OK

372_S_ = 'S' # PYCHOK OK

373_s12_ = 's12' # PYCHOK OK

374_S12_ = 'S12' # PYCHOK OK

375_scalar_ = 'scalar' # PYCHOK OK

376_scale_ = 'scale' # PYCHOK OK

377_scale0_ = 'scale0' # PYCHOK OK

378_scipy_ = 'scipy' # PYCHOK OK

379_semi_circular_ = 'semi-circular' # PYCHOK OK

380_sep_ = 'sep' # PYCHOK OK

381_singular_ = 'singular' # PYCHOK OK

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

383_small_ = 'small' # PYCHOK OK

384_Sphere_ = 'Sphere' # PYCHOK OK

385_spherical_ = 'spherical' # PYCHOK OK

386_SouthPole_ = 'SouthPole' # PYCHOK OK

387_SPACE_ = Str_(' ') # PYCHOK OK

388_specified_ = 'specified' # PYCHOK OK

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

390_start_ = 'start' # PYCHOK OK

391_std_ = 'std' # PYCHOK OK

392_stdev_ = 'stdev' # PYCHOK OK

393_supported_ = 'supported' # PYCHOK OK

394_sx_ = 'sx' # PYCHOK OK

395_sy_ = 'sy' # PYCHOK OK

396_sz_ = 'sz' # PYCHOK OK

397_tbd_ = 'tbd' # PYCHOK OK

398_TILDE_ = '~' # PYCHOK OK

399_to_ = 'to' # PYCHOK OK

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

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

402_transform_ = 'transform' # PYCHOK OK

403_tx_ = 'tx' # PYCHOK OK

404_ty_ = 'ty' # PYCHOK OK

405_tz_ = 'tz' # PYCHOK OK

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

407_units_ = 'units' # PYCHOK OK

408_UNUSED_ = 'UNUSED' # PYCHOK OK

409_up_ = 'up' # PYCHOK OK

410_UPS_ = 'UPS' # PYCHOK OK

411_utf_8_ = 'utf-8' # PYCHOK OK

412_UTM_ = 'UTM' # PYCHOK OK

413_V_ = 'V' # PYCHOK OK

414_valid_ = 'valid' # PYCHOK OK

415_value_ = 'value' # PYCHOK OK

416_version_ = 'version' # PYCHOK OK

417_vs_ = 'vs' # PYCHOK OK

418_W_ = 'W' # PYCHOK OK

419_WGS72_ = 'WGS72' # PYCHOK OK

420_WGS84_ = 'WGS84' # PYCHOK OK

421_width_ = 'width' # PYCHOK OK

422_with_ = 'with' # PYCHOK OK

423_x_ = 'x' # PYCHOK OK

424_X_ = 'X' # PYCHOK OK

425_xyz_ = 'xyz' # PYCHOK OK

426_y_ = 'y' # PYCHOK OK

427_Y_ = 'Y' # PYCHOK OK

428_z_ = 'z' # PYCHOK OK

429_Z_ = 'Z' # PYCHOK OK

430_zone_ = 'zone' # PYCHOK OK

431

432_EW_ = _E_ + _W_ # PYCHOK common cardinals

433_NE_ = _N_ + _E_ # PYCHOK positive ones

434_NS_ = _N_ + _S_ # PYCHOK OK

435_NSEW_ = _NS_ + _EW_ # PYCHOK OK

436_NW_ = _N_ + _W_ # PYCHOK OK

437_SE_ = _S_ + _E_ # PYCHOK OK

438_SW_ = _S_ + _W_ # PYCHOK negative ones

439# _NESW_ = _NE_ + _SW_ # PYCHOK clockwise

440

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

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

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

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

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

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

447

448_LR_PAIRS = {_LANGLE_: _RANGLE_,

449 _LCURLY_: _RCURLY_,

450 _LPAREN_: _RPAREN_,

451 _LSQUARE_: _RSQUARE_} # PYCHOK OK

452

453

454 def _dunder_nameof(inst, *dflt):

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

456 '''

457 try:

458 return inst.__name__

459 except AttributeError:

460 pass

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

462

463

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

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

466 '''

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

468 strs = NN(quote, strs, quote)

469 return strs

470

471

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

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

474 '''

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

476

477

478 def _isPyPy():

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

480 '''

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

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

483

484

485 def _load_lib(name):

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

487 '''

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

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

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

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

492 from ctypes import CDLL

493 from ctypes.util import find_library

494

495 ns = find_library(name), name

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

497 from ctypes import _dlopen, DEFAULT_MODE

498 from os.path import join

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

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

501 _DOT_(name, 'framework'), name))

502 else: # non-macOS

503 DEFAULT_MODE = 0

504

505 def _dlopen(*unused):

506 return True

507

508 for n in ns:

509 try:

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

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

512 if lib._name: # has a qualified name

513 return lib

514 except (AttributeError, OSError):

515 pass

516

517 return None # raise OSError

518

519

520 def machine():

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

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

523

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

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

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

527 '''

528 return _platform2()[1]

529

530

531 def _platform2(sep=NN):

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

533 '''

534 L = _pf2List

535 if not L:

536 import platform

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

538 m = m.replace(_COMMA_, _UNDER_)

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

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

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

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

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

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

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

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

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

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

549

550

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

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

553 '''

554 L = _Py3List

555 if not L:

556 v = _sys.version

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

558 pypy = _PyPy__(v)

559 if pypy:

560 L.insert(0, pypy)

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

562

563

564 def _sysctl_uint(name):

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

566 '''

567 libc = _load_lib('libc')

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

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

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

571 u = c_uint(0)

572 z = c_size_t(sizeof(u))

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

574 else: # could find or load 'libc'

575 r = -2

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

577

578

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

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

581 '''

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

583

584

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

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

587 '''

588 import os

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

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

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

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

593 m = _DOT_(m or _pygeodesy_, b)

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

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

596

597

598 def _version2(version, n=2):

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

600 '''

601 def _ints(vs):

602 for v in vs:

603 if v:

604 try:

605 yield int(v.strip())

606 except (TypeError, ValueError):

607 pass

608

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

610 if len(t) < n:

611 t += (0,) * n

612 return tuple(t[:n])

613

614

615__all__ = (_NN_, # not MISSING!

616 Str_.__name__, # classes

617 machine.__name__) # in .lazily

618__version__ = '23.08.30'

619

620 if __name__ == '__main__':

621

622 from pygeodesy import itemsorted, printf

623

624 t = b = 0

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

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

627 not n.startswith(_DUNDER_):

628 t += 1

629 b += len(v)

630 m = n[1:-1]

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

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

633 n = len(locals())

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

635

636# **) MIT License

637#

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

639#

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

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

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

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

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

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

646#

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

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

649#

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

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

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

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

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

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

656# OTHER DEALINGS IN THE SOFTWARE.