Package sourceinfo :: Module infolists

Source Code for Module sourceinfo.infolists

  1  # -*- coding: utf-8 -*- 
  2  """pysourceinfo.infolists - lists. 
  3   
  4  Details see [@local-manuals or @[https://pythonhosted.org/pysourceinfo/] 
  5  """ 
  6  from __future__ import absolute_import 
  7   
  8  import os 
  9  import sys 
 10  import re 
 11  from itertools import groupby 
 12   
 13  from sourceinfo import P_LONGEST, P_SHORTEST 
 14  from sourceinfo.helper import getpythonpath, getpythonpath_rel 
 15   
 16   
 17  __author__ = 'Arno-Can Uestuensoez' 
 18  __license__ = "Artistic-License-2.0 + Forced-Fairplay-Constraints" 
 19  __copyright__ = "Copyright (C) 2010-2018 Arno-Can Uestuensoez" \ 
 20      " @Ingenieurbuero Arno-Can Uestuensoez" 
 21  __version__ = '0.1.34' 
 22  __uuid__ = '9de52399-7752-4633-9fdc-66c87a9200b8' 
 23   
 24  __docformat__ = "restructuredtext en" 
 25   
 26   
 27  #--- 
 28  # 
 29  # redundant for autonomous load-independence by reduction of dependencies 
 30  # 
 31   
 32   
33 -def __sortu(sequence):
34 """sort unique""" 35 return (x[0] for x in groupby(sorted(sequence)))
36 37 38 #--- 39
40 -def getsysmodules_filepathname_list(spat=None, **kargs):
41 """Returns the list of file pathnames of all loaded modules 42 from *sys.modules*. 43 44 Args: 45 spat: 46 Search pattern, if provided it is used as pattern 47 to search on each module name, returns matches only. 48 Else returns the whole list. 49 50 kargs: 51 abs: 52 If *True* transforms each module name by 53 *helper.getpythonpath* before further 54 processing. 55 56 default := False 57 58 Returns: 59 Returns the list of file path names for the loaded modules, 60 or empty list. 61 62 Raises: 63 pass-through 64 65 """ 66 _abs = kargs.get('abs') 67 68 if not spat: 69 if _abs: 70 return sorted( 71 [ 72 getpythonpath(x.__file__, ispre=True) + x.__file__ for x in sys.modules.values() 73 if x and hasattr(x, '__file__') and x.__file__ 74 ] 75 ) 76 else: 77 return sorted( 78 [ 79 x.__file__ for x in sys.modules.values() 80 if x and hasattr(x, '__file__') and x.__file__ 81 ] 82 ) 83 84 if kargs.get('abs'): 85 _res = [] 86 for x in sys.modules.values(): 87 if x and hasattr(x, '__file__') and x.__file__: 88 if re.search(re.escape(spat), x.__file__): 89 _mp = getpythonpath(x.__file__, ispre=True, presolve=P_LONGEST) 90 _mprel = getpythonpath_rel(x.__file__, ispre=True, presolve=P_SHORTEST) 91 92 if os.path.isabs(x.__file__): 93 if re.search(re.escape(spat), x.__file__): 94 _res.append(x.__file__) 95 else: 96 _mp = getpythonpath(x.__file__, ispre=True, presolve=P_LONGEST) 97 _mprel = getpythonpath_rel(x.__file__, presolve=P_SHORTEST) 98 99 if _mp: 100 _sx = re.search(re.escape(spat), _mp + _mprel) 101 if _sx: 102 _res.append(_mp + _mprel) 103 104 return sorted(_res) 105 106 else: 107 return sorted( 108 [ 109 x.__file__ for x in sys.modules.values() 110 if x and hasattr(x, '__file__') and x.__file__ and re.search(re.escape(spat), x.__file__) 111 ] 112 )
113
114 -def getsysmodules_id_list(r=None):
115 """Returns the list of the IDs of the loaded modules. 116 117 Args: 118 119 r: 120 regexpr to be applied on the IDs. 121 122 Returns: 123 124 Returns the list of the loaded modules. 125 126 Raises: 127 128 pass-through 129 130 """ 131 if not r: 132 return sorted([id(x) for x in sys.modules.values() if x]) 133 return sorted([id(x) for x in sys.modules.values() 134 if x and re.search(str(r), str(id(x)))])
135 136
137 -def getsysmodules_list(r=None):
138 """Returns the list of the loaded modules. 139 140 Args: 141 r: 142 regexpr to be applied on the module names. 143 144 Returns: 145 Returns the list of the loaded modules. 146 147 Raises: 148 pass-through 149 150 """ 151 if not r: 152 ml = sorted([x for x in sys.modules.keys() 153 if sys.modules[x]]) 154 return map(lambda x: sys.modules[x], ml) 155 156 ml = sorted([x for x in sys.modules.keys() 157 if sys.modules[x] and re.search(r, x)]) 158 return map(lambda x: sys.modules[x], ml)
159 160
161 -def getsysmodules_name_list(r=None):
162 """Returns the list of the loaded modules. 163 164 Args: 165 r: 166 regexpr to be applied on the module names. 167 168 Returns: 169 Returns the list of the names of the loaded modules. 170 171 Raises: 172 passed through exceptions 173 174 """ 175 if not r: 176 return sorted([x for x in sys.modules.keys() 177 if sys.modules[x]]) 178 return sorted([x for x in sys.modules.keys() 179 if sys.modules[x] and re.search(re.escape(r), x)])
180 181
182 -def getsysmodules_pathname_list(r=None):
183 """Returns the list of path names of the loaded modules. 184 185 Args: 186 r: 187 regexpr to be applied on the module path names. 188 189 Returns: 190 Returns the list of pathnames of the loaded modules. 191 192 Raises: 193 pass-through 194 195 """ 196 if not r: 197 return list(__sortu([os.path.normpath(os.path.dirname(x.__file__)) 198 for x in sys.modules.values() if x and hasattr(x, '__file__') and x.__file__])) 199 return list(__sortu([os.path.normpath(os.path.dirname(x.__file__)) 200 for x in sys.modules.values() 201 if x and hasattr(x, '__file__') and x.__file__ and re.search(re.escape(r), x.__file__)]))
202 203
204 -def getsysmodules_pathname_rel_list(r=None, plist=None):
205 """Returns for the loaded modules the list of 206 path names relative to PYTHONPATH. 207 208 Args: 209 r: 210 regexpr to be applied on the module path names. 211 212 plist: 213 alternate search path list 214 215 Returns: 216 Returns the list of pathnames of the loaded modules. 217 218 Raises: 219 pass-through 220 221 """ 222 if not r: 223 return list(__sortu([getpythonpath_rel(x.__file__, plist) 224 for x in sys.modules.values() 225 if x and hasattr(x, '__file__') and x.__file__])) 226 return list(__sortu([getpythonpath_rel(x.__file__, plist) 227 for x in sys.modules.values() 228 if x and hasattr(x, '__file__') and x.__file__ and re.search(re.escape(r), x.__file__)]))
229 230
231 -def getsysmodules_python_pathname_list(pname=None, plist=None, **kargs):
232 """Returns the list of packages path names for the loaded 233 modules from PYTHONPATH. This is as mentioned related to the 234 containing package itself. 235 236 Args: 237 238 **pname**: 239 pathname 240 241 **plist**: 242 Search path list. 243 244 default := sys.path 245 246 kargs: 247 **presolve**: 248 The type of path resolution: :: 249 250 presolve := ( 251 P_FIRST 252 | P_LAST 253 | P_LONGEST 254 | P_SHORTEST 255 ) 256 257 default := pysourceinfo.presolve(P_FIRST) 258 259 Returns: 260 List of paths 261 262 Raises: 263 pass-through 264 265 """ 266 if not pname: 267 return list(__sortu([getpythonpath(x.__file__, plist, **kargs) 268 for x in sys.modules.values() 269 if x and hasattr(x, '__file__') and x.__file__])) 270 return list(__sortu([getpythonpath(x.__file__, plist, **kargs) 271 for x in sys.modules.values() 272 if x and hasattr(x, '__file__') and x.__file__ and re.search(re.escape(pname), x.__file__)]))
273