示例#1
0
    def __init__(self, path=None):
        import punx.cache_manager
        if path is None:
            cm = punx.cache_manager.CacheManager()
            if cm is None or cm.default_file_set is None:
                raise ValueError('Could not get NXDL file set from the cache')
            path = cm.default_file_set.path
        schema_file = os.path.join(path, 'nxdl.xsd')
        if not os.path.exists(schema_file):
            raise punx.FileNotFound(schema_file)

        self.schema_file = schema_file
        if not os.path.exists(self.schema_file):
            raise punx.FileNotFound('XML Schema file: ' + self.schema_file)

        self.lxml_tree = lxml.etree.parse(self.schema_file)
        self.lxml_schema = lxml.etree.XMLSchema(self.lxml_tree)
        self.lxml_root = self.lxml_tree.getroot()

        nodes = self.lxml_root.xpath('xs:element', namespaces=self.ns)
        if len(nodes) != 1:
            raise punx.InvalidNxdlFile(self.schema_file)
        self.nxdl = Schema_Root(nodes[0],
                                ns_dict=self.ns,
                                schema_root=self.lxml_root,
                                schema_manager=self)

        # cleanup these internal structures
        del self.lxml_root
        #del self.lxml_schema    # needed for XML file validation
        del self.lxml_tree
示例#2
0
 def __init__(self, filename):
     '''store filename and test if file is NeXus HDF5'''
     self.requested_filename = filename
     self.filename = None
     self.show_attributes = True
     if os.path.exists(filename):
         self.filename = filename
         self.isNeXus = isNeXusFile(filename)
     else:
         raise punx.FileNotFound(filename)
示例#3
0
def abs_NXDL_filename(file_name):
    '''return absolute path to file_name, within NXDL directory'''
    import punx.cache_manager
    cm = punx.cache_manager.CacheManager()
    punx.LOG_MESSAGE('file_name: ' + str(file_name), punx.DEBUG)
    punx.LOG_MESSAGE('cm.default_file_set: ' + str(cm.default_file_set), punx.DEBUG)
    if cm.default_file_set is None:
        raise ValueError('no default file set')
    absolute_name = os.path.abspath(os.path.join(cm.default_file_set.path, file_name))
    if not os.path.exists(absolute_name):
        msg = 'file does not exist: ' + absolute_name
        raise punx.FileNotFound(msg)
    punx.LOG_MESSAGE('user cache: ' + absolute_name, punx.DEBUG)
    return absolute_name
示例#4
0
def get_NXDL_file_list(nxdl_dir):
    '''
    return a list of all NXDL files in the ``nxdl_dir``
    '''
    if not os.path.exists(nxdl_dir):
        raise punx.FileNotFound('NXDL directory: ' + nxdl_dir)
    NXDL_categories = 'base_classes applications contributed_definitions'.split(
    )
    nxdl_file_list = []
    for category in NXDL_categories:
        path = os.path.join(nxdl_dir, category)
        if not os.path.exists(path):
            raise IOError('no definition available, cannot find ' + path)
        for fname in sorted(os.listdir(path)):
            if fname.endswith('.nxdl.xml'):
                nxdl_file_list.append(os.path.join(path, fname))
    return nxdl_file_list
示例#5
0
    def __init__(self, file_set):
        import punx.cache_manager
        assert (isinstance(file_set, punx.cache_manager.NXDL_File_Set))
        if file_set.path is None or not os.path.exists(file_set.path):
            raise punx.FileNotFound('NXDL directory: ' + str(file_set.path))

        self.nxdl_file_set = file_set
        self.classes = collections.OrderedDict()
        #         get_element = file_set.nxdl_element_factory.get_element

        for nxdl_file_name in get_NXDL_file_list(file_set.path):
            definition = NXDL_element__definition(file_set.path)  # the default
            obj = copy.deepcopy(definition)  # ALWAYS make a copy of that
            # TODO: adjust minOccurs defaults for application definition or contributed definition
            obj.set_file(nxdl_file_name)
            obj.parse()
            self.classes[obj.title] = obj
示例#6
0
    def read_info_file(self, file_name=None):
        if file_name is None and self.ref is None:
            raise ValueError('NXDL_File_Set() does not refer to any files')

        file_name = file_name or self.info
        if not os.path.exists(file_name):
            raise punx.FileNotFound('info file not found: ' + file_name)

        self.info = file_name
        self.path = os.path.abspath(os.path.dirname(file_name))
        if self.path.find(os.path.join('punx', 'cache')) > 0:
            self.cache = u'source'
        else:
            self.cache = u'user'

        # read the NXDL file set's info file for GitHub information
        obj = read_json_file(file_name)
        for k in self.json_file_keys:
            self.__setattr__(k, obj.get(k))
示例#7
0
 def report(self, show_attributes=True):
     '''
     return the structure of the HDF5 file in a list of strings
     
     The work of parsing the datafile is done in this method.
     '''
     if self.filename is None: return None
     if not os.path.exists(self.filename):
         raise punx.FileNotFound(self.filename)
     self.show_attributes = show_attributes
     try:
         f = h5py.File(self.filename, 'r')
     except IOError:
         raise punx.HDF5_Open_Error(self.filename)
     txt = self.filename
     if self.isNeXus:
         txt += " : NeXus data file"
     structure = self._renderGroup(f, txt, indentation="")
     f.close()
     return structure
示例#8
0
    def parse_nxdlTypes(self):
        '''
        get the allowed data types and unit types from nxdlTypes.xsd
        '''
        if os.path.exists(self.schema_file):
            path = os.path.dirname(self.schema_file)
        else:
            import punx.cache_manager
            cm = punx.cache_manager.CacheManager()
            if cm is None or cm.default_file_set is None:
                raise ValueError('Could not get NXDL file set from the cache')
            path = cm.default_file_set.path

        self.types_file = os.path.join(path, 'nxdlTypes.xsd')
        if not os.path.exists(self.types_file):
            raise punx.FileNotFound(self.types_file)
        lxml_types_tree = lxml.etree.parse(self.types_file)

        db = {}
        root = lxml_types_tree.getroot()
        for node in root:
            if isinstance(node, lxml.etree._Comment):
                pass
            elif node.tag.endswith('}annotation'):
                pass
            else:
                obj = Schema_nxdlType(node, ns_dict=self.ns, schema_root=root)
                if obj.name is not None:
                    db[obj.name] = obj

        # re-arrange
        units = list(db['anyUnitsAttr'].values)
        del db['anyUnitsAttr']
        del db['primitiveType']

        return db, units