本文整理汇总了Python中types.ModuleType.__file__方法的典型用法代码示例。如果您正苦于以下问题:Python ModuleType.__file__方法的具体用法?Python ModuleType.__file__怎么用?Python ModuleType.__file__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类types.ModuleType
的用法示例。
在下文中一共展示了ModuleType.__file__方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __file__ [as 别名]
def setUp(self):
# `dummyns.submod` emulates a package that also provide the
# `dummyns` namespace package that got installed after the
# other package `dummyns`.
ds_egg_root = join(mkdtemp(self), 'dummyns.submod')
dummyns_path = join(ds_egg_root, 'dummyns')
dummyns = ModuleType('dummyns')
dummyns.__file__ = join(dummyns_path, '__init__.py')
dummyns.__path__ = [dummyns_path]
self.addCleanup(sys.modules.pop, 'dummyns')
sys.modules['dummyns'] = dummyns
dummyns_submod_path = join(ds_egg_root, 'dummyns', 'submod')
dummyns_submod = ModuleType('dummyns.submod')
dummyns_submod.__file__ = join(dummyns_submod_path, '__init__.py')
dummyns_submod.__path__ = [dummyns_submod_path]
self.addCleanup(sys.modules.pop, 'dummyns.submod')
sys.modules['dummyns.submod'] = dummyns_submod
os.makedirs(dummyns_submod_path)
with open(join(dummyns_path, '__init__.py'), 'w') as fd:
fd.write('')
with open(join(dummyns_submod_path, '__init__.py'), 'w') as fd:
fd.write('')
self.nested_res = join(dummyns_submod_path, 'data.txt')
self.nested_data = 'data'
with open(self.nested_res, 'w') as fd:
fd.write(self.nested_data)
# create the package proper
self.dummyns_submod_dist = make_dummy_dist(self, ((
'namespace_packages.txt',
'dummyns\n'
'dummyns.submod\n',
), (
'entry_points.txt',
'[dummyns.submod]\n'
'dummyns.submod = dummyns.submod:attr\n',
),), 'dummyns.submod', '1.0', working_dir=ds_egg_root)
self.ds_egg_root = ds_egg_root
self.dummyns_path = dummyns_path
self.mod_dummyns = dummyns
self.mod_dummyns_submod = dummyns_submod
示例2: from_config_status
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __file__ [as 别名]
def from_config_status(cls, path):
"""Create an instance from a config.status file."""
code_cache = cls._CODE_CACHE
mtime = os.path.getmtime(path)
# cache the compiled code as it can be reused
# we cache it the first time, or if the file changed
if not path in code_cache or code_cache[path][0] != mtime:
# Add config.status manually to sys.modules so it gets picked up by
# iter_modules_in_path() for automatic dependencies.
mod = ModuleType('config.status')
mod.__file__ = path
sys.modules['config.status'] = mod
with open(path, 'rt') as fh:
source = fh.read()
code_cache[path] = (
mtime,
compile(source, path, 'exec', dont_inherit=1)
)
g = {
'__builtins__': __builtins__,
'__file__': path,
}
l = {}
exec(code_cache[path][1], g, l)
config = BuildConfig()
for name in l['__all__']:
setattr(config, name, l[name])
return config
示例3: refresh_model
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __file__ [as 别名]
def refresh_model(self):
""" Refresh the compiled model object.
This method will (re)compile the model for the given model text
and update the 'compiled_model' attribute. If a compiled view is
available and has a member named 'model', the model will be
applied to the view.
"""
text = self.model_text
filename = self.model_filename
_fake_linecache(text, filename)
try:
if not text:
self.compiled_model = None
self._model_module = None
else:
code = compile(text, filename, 'exec')
module = ModuleType(filename.rsplit('.', 1)[0])
module.__file__ = filename
namespace = module.__dict__
exec_(code, namespace)
model = namespace.get(self.model_item, lambda: None)()
self.compiled_model = model
self._model_module = module
self.relink_view()
except Exception:
self.traceback = traceback.format_exc()
else:
self.traceback = ''
示例4: update_from_file
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __file__ [as 别名]
def update_from_file(self, filename, overwrite=False):
"""Updates configuration from Python file.
For example, if there's :file:`dev.cfg`::
debug = False
database_uri = 'sqlite://'
so you can load it using :meth:`update_from_file()` method::
config.update_from_file('dev.cfg')
Like :meth:`update_from_object()` method, it also ignores
variables that start with underscore.
:param filename: the path of Python file to load
:type filename: :class:`basestring`
:param overwrite: keys that already exist are ignored by default.
if ``True`` has given to this parameter,
these are not overwritten
:type overwrite: :class:`bool`
"""
module = ModuleType(filename)
module.__file__ = abspath(filename)
execfile(filename, module.__dict__)
self.update_from_object(module, overwrite)
示例5: new_module
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __file__ [as 别名]
def new_module(name, doc=None):
import sys
from types import ModuleType
m = ModuleType(name, doc)
m.__file__ = name + '.py'
sys.modules[name] = m
return m
示例6: main
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __file__ [as 别名]
def main(module=None):
# Pygame won't run from a normal virtualenv copy of Python on a Mac
if not _check_python_ok_for_pygame():
_substitute_full_framework_python()
if not module:
parser = OptionParser()
options, args = parser.parse_args()
if len(args) != 1:
parser.error("You must specify which module to run.")
if __debug__:
warnings.simplefilter('default', DeprecationWarning)
path = args[0]
else:
path = module
with open(path) as f:
src = f.read()
code = compile(src, os.path.basename(path), 'exec', dont_inherit=True)
loaders.set_root(path)
pygame.display.set_mode((100, 100), DISPLAY_FLAGS)
name, _ = os.path.splitext(os.path.basename(path))
mod = ModuleType(name)
mod.__file__ = path
mod.__name__ = name
mod.__dict__.update(builtins.__dict__)
sys.modules[name] = mod
exec(code, mod.__dict__)
Juego(mod).run()
示例7: __call__
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __file__ [as 别名]
def __call__(self, cls):
"""
Decorate `cls`
"""
expose_internal = self.expose_internal
if self.submodule:
self.name += "." + cls.__name__
if self.name not in sys.modules:
orig = ModuleType(self.name)
orig.__name__ = self.name
orig.__file__ = getfile(cls)
else:
orig = sys.modules[self.name]
if isinstance(orig, ModuleFacade):
raise TypeError("Facade() used inside module which is already "
"wrapped - only once Facade() allowed per module."
" inside {0}".format(orig))
class _wrapper_cls(cls, ModuleFacade, ModuleType, object):
_facade_wrapped = orig
_facade_cls = cls
def __dir__(self):
items = set()
items.update(self.__dict__)
items.update(self._facade_cls.__dict__)
if hasattr(self._facade_cls, "__dir__"):
items.update(self._facade_cls.__dir__(self))
if expose_internal:
items.update(orig.__dict__)
return sorted(items)
def __getattr__(self, key):
if expose_internal and hasattr(orig, key):
return getattr(orig, key)
sup = super(_wrapper_cls, self)
if hasattr(sup, "__getattr__"):
result = sup.__getattr__(key)
if result is not None:
return result
raise AttributeError("'{0}' object has no attribute '{1}'"
.format(self, key))
_wrapper_cls.__name__ = "ModuleFacade({0})".format(cls.__name__)
inst = _wrapper_cls(self.name)
sys.modules[self.name] = inst
for key in "__name__ __doc__ __file__ __path__".split():
if hasattr(orig, key):
setattr(inst, key, getattr(orig, key))
return inst
示例8: _loadSingle
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __file__ [as 别名]
def _loadSingle(mk, mf, m=None):
trace("execfile", mk.name, m)
if m is None:
m = ModuleType(mk.name)
contents = {}
code = execfile(mk.filePath.path, contents)
contents['__exocet_context__'] = mf
m.__dict__.update(contents)
m.__file__ = mk.filePath.path
return m
示例9: new_module
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __file__ [as 别名]
def new_module(self, module_name, file_path):
self.module_name = module_name
module = ModuleType(module_name)
module.__package__ = module_name.rpartition('.')[0]
module.__file__ = file_path
module.__loader__ = FileWithMacrosLoader(module_name, module)
return module
示例10: _pl_local_init
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __file__ [as 别名]
def _pl_local_init(initfile = "init.py", eval = __builtins__.eval):
from types import ModuleType
import os.path
if os.path.exists(initfile):
# XXX: Do permission check on init.py
with open(initfile) as init_file:
bc = compile(init_file.read(), initfile, 'exec')
module = ModuleType('__pg_init__')
module.__file__ = initfile
module.__builtins__ = __builtins__
eval(bc, module.__dict__, module.__dict__)
sys.modules['__pg_init__'] = module
LOG('loaded Python module "__pg_init__" (init.py)')
示例11: get_module
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __file__ [as 别名]
def get_module(mod_path, filepath):
'''Compile a plugin module
:param filepath: full filepath to python plugin module
'''
with open(filepath, 'r') as f:
c = compile(f.read(), '', 'exec')
module = ModuleType(mod_path)
module.__file__ = filepath
exec(c, module.__dict__)
return module
示例12: _loadSingle
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __file__ [as 别名]
def _loadSingle(mk, mf, m=None):
trace("execfile", mk.name, m)
if m is None:
m = ModuleType(mk.name)
# Run the actual exec.
contents = {}
with mk.filePath.open("rb") as handle:
exec handle in contents
contents['__exocet_context__'] = mf
m.__dict__.update(contents)
m.__file__ = mk.filePath.path
return m
示例13: _exec
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __file__ [as 别名]
def _exec(engine, data, width, height):
_main = sys.modules["__main__"]
try:
mod = ModuleType("__main__")
mod.__file__ = __file__
mod._engine_ = weakref.ref(engine)
mod._data_ = data
mod._width_ = width
mod._height_ = height
mod._mmap_size_ = None
mod._mmap_ = None
sys.modules["__main__"] = mod
exec(code, mod.__dict__)
finally:
sys.modules["__main__"] = _main
return mod
示例14: refresh_view
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __file__ [as 别名]
def refresh_view(self):
""" Refresh the compiled view object.
This method will (re)compile the view for the given view text
and update the 'compiled_view' attribute. If a compiled model
is available and the view has a member named 'model', the model
will be applied to the view.
"""
text = self.view_text
filename = self.view_filename
_fake_linecache(text, filename)
try:
if not text:
self.compiled_view = None
self._view_module = None
else:
ast = parse(text, filename=filename)
code = EnamlCompiler.compile(ast, filename)
module = ModuleType('__main__')
module.__file__ = filename
namespace = module.__dict__
with enaml.imports():
exec_(code, namespace)
view = namespace.get(self.view_item, lambda: None)()
if isinstance(view, Object) and 'model' in view.members():
view.model = self.compiled_model
# trap any initialization errors and roll back the view
old = self.compiled_view
try:
self.compiled_view = view
except Exception:
self.compiled_view = old
if isinstance(old, Widget):
old.show()
raise
self._view_module = module
if old is not None and not old.is_destroyed:
old.destroy()
except Exception:
self.traceback = traceback.format_exc()
else:
self.traceback = ''
示例15: import_file
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __file__ [as 别名]
def import_file(filename, name = None, package = None, add_to_sys_modules = False):
if os.path.isdir(filename):
filename = os.path.join(filename, "__init__.py")
if name is None:
name = os.path.basename(filename).split(".")[0]
dirname = os.path.basename(os.path.dirname(filename)).strip()
if name == "__init__" and dirname:
name = dirname
elif package is not None:
name = package + "." + name
mod = ModuleType(name)
mod.__file__ = os.path.abspath(filename)
if package is not None:
mod.__package__ = package
mod.__path__ = [os.path.dirname(mod.__file__)]
if add_to_sys_modules:
sys.modules[name] = mod
execfile(filename, mod.__dict__)
return mod