Esempio n. 1
0
 def store_python_code(self, path, code):
     self.assert_is_open()
     if not isstring(code):
         raise TypeError("Python code must be a string")
     ds = self.code_group.require_dataset(path,
                                          dtype=h5vstring, shape = ())
     ds[...] = code
     ds.attrs['ACTIVE_PAPER_LANGUAGE'] = "python"
     return ds
Esempio n. 2
0
 def store_python_code(self, path, code):
     self.assert_is_open()
     if not isstring(code):
         raise TypeError("Python code must be a string (is %s)" %
                         str(type(code)))
     ds = self.code_group.require_dataset(path, dtype=h5vstring, shape=())
     ds[...] = code.encode('utf-8')
     ds.attrs['ACTIVE_PAPER_LANGUAGE'] = "python"
     return ds
Esempio n. 3
0
 def __getitem__(self, path_or_ref):
     if isstring(path_or_ref):
         path = codepath(path_or_ref)
     else:
         path = self._node[path_or_ref].name
         assert path.startswith('/code')
     node = self._node[path]
     if isinstance(node, h5py.Group):
         return CodeGroup(self._paper, node)
     else:
         return CodeFile(self._paper, node)
Esempio n. 4
0
 def __getitem__(self, path_or_ref):
     if isstring(path_or_ref):
         path = datapath(path_or_ref)
     else:
         path = self._node[path_or_ref].name
         assert path.startswith('/data')
     path = path.split('/')
     if path[0] == '':
         # datapath() ensures that path must start with
         # ['', 'data'] in this case. Move up the parent
         # chain to the root of the /data hierarchy.
         path = path[2:]
         node = self
         while node is not node.parent:
             node = node.parent
     else:
         node = self
     for element in path:
         node = node._wrap_and_track_dependencies(node._node[element])
     return node
Esempio n. 5
0
    def __init__(self, filename, mode="r", dependencies=None):
        self.filename = filename
        self.file = h5py.File(filename, mode)
        self.open = True
        self.writable = False
        if mode[0] == 'r':
            assert dependencies is None
            if ascii(self.file.attrs['DATA_MODEL']) != 'active-papers-py':
                raise ValueError("File %s is not an ActivePaper" % filename)
            self.code_group = self.file["code"]
            self.data_group = self.file["data"]
            self.documentation_group = self.file["documentation"]
            self.writable = '+' in mode
            self.history = self.file['history']
            deps = self.file.get('external-dependencies/'
                                 'python-packages', None)
            if deps is None:
                self.dependencies = []
            else:
                self.dependencies = [ascii(n) for n in deps]
            for module_name in self.dependencies:
                importlib.import_module(module_name)
        elif mode[0] == 'w':
            self.file.attrs['DATA_MODEL'] = ascii('active-papers-py')
            self.file.attrs['DATA_MODEL_MAJOR_VERSION'] = 0
            self.file.attrs['DATA_MODEL_MINOR_VERSION'] = 1
            self.code_group = self.file.create_group("code")
            self.data_group = self.file.create_group("data")
            self.documentation_group = self.file.create_group("documentation")
            deps = self.file.create_group('external-dependencies')
            if dependencies is None:
                self.dependencies = []
            else:
                for module_name in dependencies:
                    assert isstring(module_name)
                    importlib.import_module(module_name)
                self.dependencies = dependencies
                deps.create_dataset('python-packages',
                                    data = dependencies)
            htype = np.dtype([('opened', np.int64),
                              ('closed', np.int64),
                              ('platform', h5vstring),
                              ('hostname', h5vstring),
                              ('username', h5vstring)]
                             + [(name+"_version", h5vstring)
                                for name in ['activepapers','python',
                                             'numpy', 'h5py', 'hdf5'] 
                                            + self.dependencies])
            self.history = self.file.create_dataset("history", shape=(0,),
                                                    dtype=htype,
                                                    chunks=(1,),
                                                    maxshape=(None,))
            readme = self.file.create_dataset("README",
                                              dtype=h5vstring, shape = ())
            readme[...] = readme_text
            self.writable = True

        if self.writable:
            self.update_history(close=False)

        import activepapers.utility
        self.data = DataGroup(self, None, self.data_group, ExternalCode(self))
        self.imported_modules = {}

        self._local_modules = {}

        paper_id = hex(id(self))[2:]
        paper_registry[paper_id] = self
Esempio n. 6
0
 def forbidden(cls, key):
     return isstring(key) and key.startswith('ACTIVE_PAPER')
Esempio n. 7
0
    def __init__(self, filename, mode="r", dependencies=None):
        self.filename = filename
        self.file = h5py.File(filename, mode)
        self.open = True
        self.writable = False
        if mode[0] == 'r':
            assert dependencies is None
            if ascii(self.file.attrs['DATA_MODEL']) != 'active-papers-py':
                raise ValueError("File %s is not an ActivePaper" % filename)
            self.code_group = self.file["code"]
            self.data_group = self.file["data"]
            self.documentation_group = self.file["documentation"]
            self.writable = '+' in mode
            self.history = self.file['history']
            deps = self.file.get('external-dependencies/'
                                 'python-packages', None)
            if deps is None:
                self.dependencies = []
            else:
                self.dependencies = [ascii(n) for n in deps]
            for module_name in self.dependencies:
                importlib.import_module(module_name)
        elif mode[0] == 'w':
            self.file.attrs['DATA_MODEL'] = ascii('active-papers-py')
            self.file.attrs['DATA_MODEL_MAJOR_VERSION'] = 0
            self.file.attrs['DATA_MODEL_MINOR_VERSION'] = 1
            self.code_group = self.file.create_group("code")
            self.data_group = self.file.create_group("data")
            self.documentation_group = self.file.create_group("documentation")
            deps = self.file.create_group('external-dependencies')
            if dependencies is None:
                self.dependencies = []
            else:
                for module_name in dependencies:
                    assert isstring(module_name)
                    importlib.import_module(module_name)
                self.dependencies = dependencies
                ds = deps.create_dataset('python-packages',
                                         dtype=h5vstring,
                                         shape=(len(dependencies), ))
                ds[:] = dependencies
            htype = np.dtype(
                [('opened', np.int64), ('closed',
                                        np.int64), ('platform', h5vstring),
                 ('hostname', h5vstring), ('username', h5vstring)] +
                [(name + "_version", h5vstring) for name in
                 ['activepapers', 'python', 'numpy', 'h5py', 'hdf5'] +
                 self.dependencies])
            self.history = self.file.create_dataset("history",
                                                    shape=(0, ),
                                                    dtype=htype,
                                                    chunks=(1, ),
                                                    maxshape=(None, ))
            readme = self.file.create_dataset("README",
                                              dtype=h5vstring,
                                              shape=())
            readme[...] = readme_text
            self.writable = True

        if self.writable:
            self.update_history(close=False)

        import activepapers.utility
        self.data = DataGroup(self, None, self.data_group, ExternalCode(self))
        self.imported_modules = {}

        self._local_modules = {}

        paper_registry[self._id()] = self