コード例 #1
0
 def test_dict(self):
     spec = ModuleSpec('slug', 'Name', 'Other', [], loads_data=True)
     self.assertEquals(
         dict(spec), {
             'id_name': 'slug',
             'name': 'Name',
             'category': 'Other',
             'parameters': [],
             'loads_data': True,
         })
コード例 #2
0
 def test_dict(self):
     spec = ModuleSpec("slug", "Name", "Other", [], loads_data=True)
     self.assertEquals(
         dict(spec),
         {
             "id_name": "slug",
             "name": "Name",
             "category": "Other",
             "parameters": [],
             "loads_data": True,
         },
     )
コード例 #3
0
def import_module_from_directory(version: str,
                                 importdir: Path,
                                 force_reload=False):
    module_files = ModuleFiles.load_from_dirpath(importdir)  # raise ValueError
    spec = ModuleSpec.load_from_path(module_files.spec)  # raise ValueError
    validate_python_functions(module_files.code)  # raise ValueError

    if not force_reload:
        # Don't allow importing the same version twice
        try:
            ModuleVersion.objects.get(id_name=spec.id_name,
                                      source_version_hash=version)
            raise ValueError(f'Version {version} of module {spec.id_name}'
                             ' has already been imported')
        except ModuleVersion.DoesNotExist:
            # this is what we want
            pass

    if module_files.javascript:
        js_module = module_files.javascript.read_text(encoding='utf-8')
    else:
        js_module = ''

    # Copy whole directory to S3
    prefix = '%s/%s/' % (spec.id_name, version)

    try:
        # If files already exist, delete them so we can overwrite them.
        #
        # This can race: a worker may be loading the code to execute it. But
        # races are unlikely to affect anybody because:
        #
        # * If force_reload=True we're in dev or test, where we control
        #   everything.
        # * Otherwise, we've already checked there's no ModuleVersion, so
        #   probably nothing is trying and load what we're deleting here.
        minio.remove_recursive(minio.ExternalModulesBucket, prefix)
    except FileNotFoundError:
        pass  # common case: we aren't overwriting code

    minio.fput_directory_contents(minio.ExternalModulesBucket, prefix,
                                  Path(importdir))

    # If that succeeds, initialise module in our database
    module_version = ModuleVersion.create_or_replace_from_spec(
        spec, source_version_hash=version, js_module=js_module)

    logger.info('Imported module %s' % spec.id_name)

    return module_version
コード例 #4
0
    ) -> ProcessResult:

Looking up modules
==================

This ``__init__.py`` imports all the modules automatically, finding them by
their ``.json`` spec files.

>>> from server import modules
>>> modules.pythoncode  # direct access
>>> modules.Lookup['pythoncode']  # dynamic lookup by id_name
"""
import importlib
import pathlib
from server.models.module_loader import ModuleFiles, ModuleSpec


Lookup = {}
Specs = {}

SpecPaths = (
    list(pathlib.Path(__file__).parent.glob('*.json'))
    + list(pathlib.Path(__file__).parent.glob('*.yaml'))
)
for spec_path in SpecPaths:
    spec = ModuleSpec.load_from_path(spec_path)
    id_name = spec_path.stem
    module = importlib.import_module('.' + id_name, __package__)
    Lookup[id_name] = module
    Specs[id_name] = spec
コード例 #5
0
 def _load(self, filename, data):
     path = MockPath(["root", filename], data)
     return ModuleSpec.load_from_path(path)