forked from jgomezdans/semidiscrete
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
108 lines (95 loc) · 3.55 KB
/
__init__.py
File metadata and controls
108 lines (95 loc) · 3.55 KB
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
import imp
import os
import os.path
import shutil
import sys
import tempfile
from rtmodel_ad_trans2 import *
if os.name == 'nt': import win32api
def _tmp_pkg(dir="/tmp/"):
"""
Create a temporary package.
Returns (name, path)
"""
while True:
path = tempfile.mkdtemp(dir=dir)
name = os.path.basename(path)
try:
modinfo = imp.find_module(name)
# if name is found, delete and try again
os.rmdir(path)
except:
break
init = file(os.path.join(path, '__init__.py'), 'w')
init.close()
return name, path
def mext(name):
"""
Load and return a unique copy of a (possibly) already loaded module.
This makes it possible to treat a module as a class and load multiple
instances of it.
"""
# first find the "real" module on the "real" syspath
srcfile, srcpath, srcdesc = imp.find_module(name)
# now create a temp directory for the bogus package
pkgname, pkgdir = _tmp_pkg('.')
# copy the original module to the new package
shutil.copy(srcpath, pkgdir)
# add the directory containing the new package to the search path
#sys.path.append(tmpdir)
# import the module
# __import__ returns the package, not the sub-module
pkg = __import__(pkgname, globals(), locals(), [name])
# remove the bogus directory from sys.path ???
#sys.path.remove(tmpdir)
# return the module object
print pkgdir
return getattr(pkg, name)
class MExt(object):
"""
Load a unique copy of a module that can be treated as a "class instance".
"""
def __init__(self, name):
self.name = name
# first find the "real" module on the "real" syspath
srcfile, srcpath, srcdesc = imp.find_module(name)
# now create a temp directory for the bogus package
self._pkgname, self._pkgdir = _tmp_pkg('.')
# copy the original module to the new package
shutil.copy(srcpath, self._pkgdir)
# add the directory containing the new package to the search path
#sys.path.append(tmpdir)
# import the module
# __import__ returns the package, not the sub-module
self._pkg = __import__(self._pkgname, globals(), locals(), [self.name])
# remove the bogus directory from sys.path ???
#sys.path.remove(tmpdir)
# return the module object
self._module = getattr(self._pkg, self.name)
# now add the module's stuff to this class
self.__dict__.update(self._module.__dict__)
def __del__(self):
# remove module
del sys.modules[self._module.__name__]
del sys.modules[self._pkg.__name__]
# on win32, the DLL must be unloaded forcefully in order to delete it.
# on Darwin (other unix???) this doesn't appear to be necessary
# try to unload the dll
if os.name == 'nt':
hModule = win32api.GetModuleHandle(self._module.__file__)
win32api.FreeLibrary(hModule)
# now try to delete the files and directory
shutil.rmtree(self._pkgdir)
# make sure the original module is loaded -
# otherwise python crashes on exit
# if MExt objects have not been explicitly 'del'd,
# and __del__ is occurring on python shutdown, the import will fail
# and the exception is caught here
try:
__import__(self.name)
except:
pass
class SemiDiscrete(object):
def __init__(self, *args, **kwargs):
semidiscrete_mod = MExt('rtmodel_ad_trans2')
self.semidiscrete = semidiscrete_mod._module