Package pythonids

Source Code for Package pythonids

  1  # -*- coding: utf-8 -*- 
  2  """*pythonids* provides common information about the Python syntax. 
  3  """ 
  4  ############################################# 
  5  # 
  6  # See manuals for the detailed API. 
  7  # 
  8  ############################################# 
  9   
 10  import sys 
 11  import os 
 12  import re 
 13   
 14   
 15  __author__ = 'Arno-Can Uestuensoez' 
 16  __license__ = "Artistic-License-2.0 + Forced-Fairplay-Constraints" 
 17  __copyright__ = "Copyright (C) 2010-2018 Arno-Can Uestuensoez" \ 
 18                  " @Ingenieurbuero Arno-Can Uestuensoez" 
 19  __version__ = '0.1.31' 
 20  __uuid__ = "5624dc41-775a-4d17-ac42-14a0d5c41d1a" 
 21   
 22  __docformat__ = "restructuredtext en" 
 23   
 24   
 25  _debug = 0 
 26  _verbose = 0 
 27   
 28   
29 -class PythonIDsError(Exception):
30 """Subsystem *PythonIDs*. 31 """ 32 pass
33 34
35 -class PythonIDsImplementationError(Exception):
36 """Python implementation not supported. 37 """ 38 pass
39 40
41 -def decode_pysyntax_16bit_to_str(xyz, form="%d.%d.%d"):
42 """Decodes the compressed version 16bit integer bitmask 43 into the corresponding string. 44 45 The optional format string *form* provides the formatting 46 of a 3-value interger tuple. E.g. *form="%02d.%02d.%03d"*. 47 48 Due to the spared error checks the string has to be correct! 49 50 """ 51 return form % ( 52 (xyz >> 13) & 7, # bit 15 - 13 - see version_info[0] - PythonX 53 (xyz >> 8) & 31, # bit 12 - 8 - see version_info[1] - Pythonx.Y 54 xyz & 255, # bit 7 - 0 - see version_info[2] - Pythonx.y - x.y.Z 55 )
56 57
58 -def decode_pysyntax_16bit_to_tuple(xyz):
59 """ Decodes the compressed version 16bit integer bitmask 60 into the corresponding tuple of integer values. 61 62 """ 63 return ( 64 (xyz >> 13) & 7, # bit 15 - 13 - see version_info[0] - PythonX 65 (xyz >> 8) & 31, # bit 12 - 8 - see version_info[1] - Pythonx.Y 66 xyz & 255, # bit 7 - 0 - see version_info[2] - Pythonx.y - x.y.Y 67 )
68 69
70 -def decode_pysyntax_16bit_to_tuple_str(xyz):
71 """Decodes the compressed version 16bit integer bitmask 72 into the corresponding tuple of integer values. 73 74 """ 75 return ( 76 str((xyz >> 13) & 7), # bit 15 - 13 - see version_info[0] - PythonX 77 str((xyz >> 8) & 31), # bit 12 - 8 - see version_info[1] - Pythonx.Y 78 str(xyz & 255), # bit 7 - 0 - see version_info[2] - Pythonx.y - x.y.Y 79 )
80 81
82 -def decode_pysyntax_str_to_num(v):
83 """ Split a version string separated by '.' into an integer 84 tuple. :: 85 86 decode_pysyntax_str_to_num('1.22.17') => (1, 22, 17) 87 88 A tiny utility - frequently required. 89 90 Args: 91 92 Version string 'x.y.z'. 93 94 Returns: 95 96 Integer tuple (x, y, z) 97 98 Raises: 99 100 ValueError 101 102 """ 103 return tuple(int(x) for x in v.split('.'))
104 105
106 -def encode_pysyntax_to_16bit(x=0, y=0, z=0):
107 """ Encodes the version by calculating the 16bit integer 108 bitmask for the provided Python release values. 109 110 Args: 111 x: 112 The major version number. :: 113 114 0 <= x 115 116 0 <= x0 < 8 # internal low-level 16-bit optimization threshold 117 118 y: 119 The minor version number. :: 120 121 0 <= y < 32 122 123 z: 124 The numeric relase-build tag. :: 125 126 0 <= z < 256 127 128 Returns: 129 130 The bitmask. 131 132 Raises: 133 134 pass-through 135 136 """ 137 return ( 138 (x & 7) << 13 # bit 15 - 13 - see version_info[0] - PythonX 139 | (y & 31) << 8 # bit 12 - 8 - see version_info[1] - Pythonx.Y 140 | (z & 255) # bit 7 - 0 - see version_info[2] - Pythonx.y - x.y.Z 141 )
142 143 144 # 145 # official API 146 # 147 PYV2 = 16384 #: 16384 = encode_pysyntax_to_16bit(2,) 148 PYV27 = 18176 #: 18176 = encode_pysyntax_to_16bit(2, 7) 149 PYV3 = 24576 #: 24576 = encode_pysyntax_to_16bit(3,) 150 PYV33 = 25344 #: 25344 = encode_pysyntax_to_16bit(3, 3) 151 PYV35 = 25856 #: 25856 = encode_pysyntax_to_16bit(3, 5) 152 PYV36 = 26112 #: 26112 = encode_pysyntax_to_16bit(3, 6) 153 PYV37 = 26368 #: 26368 = encode_pysyntax_to_16bit(3, 7, 0) 154 PYV38 = 26624 #: 26624 = encode_pysyntax_to_16bit(3, 8, 0) 155 PYV39 = 26880 #: 26880 = encode_pysyntax_to_16bit(3, 9, 0) 156 157 158 # 159 # short term development support 160 # 161 PYV2715 = 18191 #: 18191 = encode_pysyntax_to_16bit(2, 7, 15) 162 PYV2716 = 18192 #: 18192 = encode_pysyntax_to_16bit(2, 7, 16) 163 PYV31 = 24832 #: 24832 = encode_pysyntax_to_16bit(3, 1) 164 PYV32 = 25088 #: 25088 = encode_pysyntax_to_16bit(3, 2) 165 PYV34 = 25600 #: 25600 = encode_pysyntax_to_16bit(3, 4) 166 PYV362 = 26114 #: 26114 = encode_pysyntax_to_16bit(3, 6, 2) 167 PYV365 = 26117 #: 26117 = encode_pysyntax_to_16bit(3, 6, 5) 168 PYV366 = 26118 #: 26118 = encode_pysyntax_to_16bit(3, 6, 6) 169 PYV367 = 26119 #: 26119 = encode_pysyntax_to_16bit(3, 6, 7) 170 PYV368 = 26120 #: 26120 = encode_pysyntax_to_16bit(3, 6, 8) 171 PYV369 = 26121 #: 26121 = encode_pysyntax_to_16bit(3, 6, 9) 172 PYV371 = 26369 #: 26369 = encode_pysyntax_to_16bit(3, 7, 1) 173 PYV372 = 26370 #: 26370 = encode_pysyntax_to_16bit(3, 7, 2) 174 PYV373 = 26371 #: 26371 = encode_pysyntax_to_16bit(3, 7, 3) 175 PYV374 = 26372 #: 26372 = encode_pysyntax_to_16bit(3, 7, 4) 176 PYV375 = 26373 #: 26373 = encode_pysyntax_to_16bit(3, 7, 5) 177 PYV376 = 26374 #: 26374 = encode_pysyntax_to_16bit(3, 7, 6) 178 PYV381 = 26625 #: 26625 = ncode_pysyntax_to_16bit(3, 8, 1) 179 180 #: The 3-value Python final release of the current process in accordance to PEP440. 181 #: The location of the implementation information varies, see *pythonids.pythondist*. 182 PYVxyz = encode_pysyntax_to_16bit(*sys.version_info[:3]) 183 184 #: 185 #: Adjust to current major Python version to Python3 vs. Python2. 186 #: 187 PYV27X = PYVxyz & PYV27 == PYV27 #: Python2.7 188 PYV3X = PYVxyz >= PYV3 #: Python3 189 PYV3X3 = PYVxyz >= PYV3 and PYVxyz < PYV34 #: Python3.0 - Python3.3 190 PYV35Less = PYVxyz < PYV35 #: Python3.0 - 3.4 - partially not supported, at least critical 191 PYV35Plus = PYVxyz >= PYV35 #: Python3.5+ - all following 192 193 if PYV35Plus: 194 ISSTR = (str, bytes) #: string and unicode 195 ISSTRBASE = (str,) #: str 196 197 #: Superpose for generic Python3 compatibility. 198 unicode = str # @ReservedAssignment 199 200 elif PYV3X: 201 ISSTR = (str, bytes) #: string and unicode 202 ISSTRBASE = (str,) #: str 203 204 #: Superpose for generic Python3 compatibility. 205 unicode = str # @ReservedAssignment 206 207 elif PYV27X: 208 ISSTR = (str, unicode) #: string and unicode 209 ISSTRBASE = (str, unicode,) #: basestring 210 unicode = unicode # @ReservedAssignment 211 212 else: 213 raise PythonIDsError( 214 "Requires Python 2.7+, or 3.5+, current: " 215 + str(sys.version_info[:2])) 216