import imp import os os path import zipimport class Importer object de

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import imp
import os, os.path
import zipimport
class Importer(object):
def __init__(self, path):
## print '__init__', path
self._path = path
self._importers = {}
self._last_name = (None, None)
return
def _detect_zip_name(self,path,imp):
def subArray(arr,sub_arr,start):
arr_index=len(arr)-1
while start>=0:
if arr[arr_index]!=sub_arr[start]: return False
arr_index-=1
start-=1
return True
imp_index=len(imp)-1
while imp_index>=0:
if subArray(path,imp,imp_index):
return (path[:(len(path)-imp_index)+1],imp[imp_index+1:],imp[:imp_index+2])
imp_index-=1
return None
def find_module(self, fullname, path=None):
## print 'find_module', fullname, path
parts = fullname.split('.')
if path is None:
path = self._path
#zz = fullname.replace('.', '\\')
#pos = path.find()
pathparts = path.split('\\')
result=self._detect_zip_name(pathparts,parts)
if result is None:
## print 'matuk 2:',pathparts, ' ' ,parts
return None
zip,tail,zpack=result
## print 'Zip:',zip,' tail:',tail,' zpack:',zpack
'''
zfile = '\\'.join(zip)
zfile += '\\'+tail[0]+'.zip'
if os.path.isfile(zfile):
print 'find_module: found', zfile
#zippackage = '.'.join(parts[:index+1])
zippackage='.'.join(zpack)
_importer=self._importers.get(zippackage,None)
if _importer is None:
_importer=zipimport.zipimporter(zfile)
self._importers[zippackage] = _importer
_module_name='\\'.join(tail)
if _importer.find_module(_module_name) is None:
return None
self._last_name = (fullname, _importer)
print 'IMPORTER: FULL_NAME< %s > ZIP_NAME< %s > IN_ZIP_NAME< %s >'%(path,zfile,_module_name)
return self
return None
'''
path = '\\'.join(zip)#os.path.join(*zip)
parts = tail
index=0
zippackage = '.'.join(zpack[:-1])
## print 'PATH:', path, parts
for p in parts:
path = os.path.join(path, p)
zfile = path+'.zip'
## print 'ZFILE:', zfile
zippackage += '.'+p
if os.path.isfile(zfile):
## print 'find_module: found', zfile
#zippackage = '.'.join(parts[:index+1])
## print 'ZP:', zippackage
_importer=self._importers.get(zippackage,None)
if _importer is None:
_importer=zipimport.zipimporter(zfile)
self._importers[zippackage] = _importer
_module_name='\\'.join(parts[index:])
## print 'module name:', _module_name
if _importer.find_module(_module_name) is None:
return None
self._last_name = (fullname, _importer)
## print 'IMPORTER: FULL_NAME< %s > ZIP_NAME< %s > IN_ZIP_NAME< %s >'%(path,zfile,_module_name)
return self
index+=1
## print 'find_module: not found'
return None
def load_module(self, fullname):
## print 'load_module', fullname
path = fullname.split('.')
impdata = self._find_importer(path)
#print fullname, impdata
#importer, left, right = impdata
importer, left, right, text_right = impdata
ispkg = self.is_package(fullname, impdata)
code = self.get_code(fullname, impdata)
mod = sys.modules.setdefault(fullname, imp.new_module(fullname))
#mod.__file__ = "<%s>" % self.__class__.__name__
mod.__file__ = os.path.join(self._path, *path[1:])
mod.__loader__ = self
if ispkg:
mod.__path__ = [os.path.join(self._path, *left[1:-2])]
#mod.__path__ = [self._path]
#print self._path
## print '!'*10, impdata, mod.__path__
exec code in mod.__dict__
return mod
def get_data(self, path):
#print 'get_data', path
left = path.split('\\')
right = []
while left:
zfile = '\\'.join(left)+'.zip'
if os.path.exists(zfile):
importer = zipimport.zipimporter(zfile)
right.insert(0, left.pop())
fname = '\\'.join(right)
#print 'found', zfile, fname
return importer.get_data(fname)
right.insert(0, left.pop())
raise IOError, 'Cannot find file %s'%path
def is_package(self, fullname, impdata=None):
#print 'is_package', fullname, impdata
if impdata is None:
impdata = self._find_importer(fullname.split('.'))
importer, left, right, text_right = impdata
return importer.is_package(text_right)
def get_code(self, fullname, impdata=None):
#print 'get_code', fullname, impdata
if impdata is None:
impdata = self._find_importer(fullname.split('.'))
importer, left, right, text_right = impdata
return importer.get_code(text_right)
def get_source(self, fullname, impdata=None):
#print 'get_source', fullname, impdata
if impdata is None:
impdata = self._find_importer(fullname.split('.'))
importer, left, right, text_right = impdata
return importer.get_source(text_right)
def _find_importer(self, parts):
#print '_find_importer', parts
left = []
right = []
importer = None
index = 0
zippackage = None
for p in parts:
left.append(p)
if zippackage is not None:
zippackage += '.'+p
else:
zippackage = p
importer = self._importers.get(zippackage, None)
if importer is not None:
right = parts[index:]
break
index +=1
#if importer is not None:
# break
text_right = '\\'.join(right)
return importer, left, right, text_right
import sys
sys.path_hooks.append(Importer)
#Выкладываю без изменений - как он у меня сохранился. Установка хука - в последней строке