Pyjo.Loader - Loader¶
from Pyjo.Loader import embedded_file, load_module
# Find modules in a namespace
for module in find_modules('Some.Namespace'):
# Load them safely
try:
load_module(module)
except ImportError as e:
print('Loading "{0}" failed: {1}'.format(module, e))
# And extract files from the DATA section
print(embedded_file(module, 'some_file.txt'))
Pyjo.Loader
is a module loader and plugin framework. Aside from finding
modules and loading classes, it allows multiple files to be stored in the
DATA
variable of a module, which can then be accessed individually.
# Some/Namespace/Module.py
DATA = r'''
@@ test.txt
This is the first file.
@@ test2.html (base64)
VGhpcyBpcyB0aGUgc2Vjb25kIGZpbGUu
@@ test
This is the
third file.
'''
Each file has a header starting with @@
, followed by the file name and
optional instructions for decoding its content. Currently only the Base64
encoding is supported, which can be quite convenient for the storage of binary
data.
Functions¶
-
Pyjo.Loader.
embedded_file
(module, filename=None)¶ all_files = embedded_file('Foo.Bar') index = embedded_file('Foo.Bar', 'index.html') main_module_files = embedded_file(sys.modules[__name__])
Extract embedded file from the
DATA
variable of a module, all files will be cached once they have been accessed for the first time.for content in data_section('Foo.Bar'): print(content)