示例#1
0
    def load(self, path=None):

        with sp.File(self._fpath, 'r') as grp:

            #msg = 'Load data from file %s and fill %s object for group "%s"' % (self._fpath, self._name, grp.name)
            #log.info(msg, self._name)
            log.info('Load data from file %s' % self._fpath, self._name)

            for k, v in dict(grp).iteritems():
                #subgrp = v
                #print '    ', k # , "   ", subg.name #, val, subg.len(), type(subg),

                if isinstance(v, sp.dataset_t):
                    log.debug('load dataset "%s"' % k, self._name)
                    if k == 'dettype': self.set_dettype(v[0])
                    elif k == 'detid': self.set_detid(v[0])
                    elif k == 'detname': self.set_detname(v[0])
                    elif k == 'tscfile': self.set_tscfile(v[0])
                    elif k == 'predecessor': self.set_predecessor(v[0])
                    elif k == 'successor': self.set_successor(v[0])
                    else:
                        log.warning(
                            'hdf file has unrecognized dataset "%s"' % k,
                            self._name)

                elif isinstance(v, sp.group_t):
                    if self.is_base_group(k, v): continue
                    log.debug('load group "%s"' % k, self._name)
                    o = self.add_ctype(k, cmt=False)
                    o.load(v)
示例#2
0
def create_directory(dir, mode=0775):
    #print 'create_directory: %s' % dir
    if os.path.exists(dir):
        log.debug('Directory exists: %s' % dir, __name__)
    else:
        os.makedirs(dir, mode)
        #os.chmod(dir, mode)
        #os.system(cmd)
        log.info('Directory created: %s' % dir, __name__)
示例#3
0
    def add_version(self, vnum=None, tsec_prod=None, nda=None, cmt=False):
        vn = self.vnum_last() + 1 if vnum is None else vnum
        if vn in self._dicvers.keys():
            return self._dicvers[vn]
        o = self._dicvers[vn] = DCVersion(vn, tsec_prod, nda)

        rec = self.make_record('add version', str(vn), cmt)
        if cmt is not False: self.add_history_record(rec)
        log.info(rec, self.__class__.__name__)
        return o
示例#4
0
    def add_range(self, begin, end=None, cmt=False):
        keyrng = key(begin, end)
        if keyrng in self._dicranges.keys():
            return self._dicranges[keyrng]
        o = self._dicranges[keyrng] = DCRange(begin, end)

        rec = self.make_record('add range', keyrng, cmt)
        if cmt is not False: self.add_history_record(rec)
        log.info(rec, self.__class__.__name__)
        return o
示例#5
0
    def __init__(self, fname=None):
        """Constructor.
           - fname the file name with configuration parameters, if not specified then default value.
        """
        ConfigParameters.__init__(self)
        self.name = self.__class__.__name__
        self.fname_cp = 'confpars-dcs.txt'  # Re-define default config file name
        log.info('In %s c-tor', self.name)

        self.declareParameters()
        self.readParametersFromFile(fname)
示例#6
0
    def mark_ctype(self, ctype, cmt=False):
        """Marks child object for deletion in save()"""
        if ctype in self._dicctypes.keys():
            self._lst_del_keys.append(ctype)

            rec = self.make_record('del ctype', ctype, cmt)
            if cmt is not False: self.add_history_record(rec)
            log.info(rec, self.__class__.__name__)
            return ctype
        else:
            msg = 'Marking of non-existent ctype "%s"' % str(ctype)
            log.warning(msg, self._name)
            return None
示例#7
0
    def mark_version(self, vnum=None, cmt=False):
        """Marks child object for deletion in save()"""
        vers = self.vnum_last() if vnum is None else vnum

        if vers in self._dicvers.keys():
            self._lst_del_keys.append(vers)

            rec = self.make_record('del version', str(vers), cmt)
            if cmt is not False: self.add_history_record(rec)
            log.info(rec, self.__class__.__name__)
            return vers
        else:
            msg = 'Marking of non-existent version %s' % str(vers)
            log.warning(msg, self._name)
            return None
示例#8
0
    def add_ctype(self, ctype, cmt=False):
        if not (ctype in gu.calib_names):
            msg = 'ctype "%s" is not in the list of known types:\n  %s' % (
                ctype, gu.calib_names)
            log.error(msg, self.__class__.__name__)
            return None

        if ctype in self._dicctypes.keys():
            return self._dicctypes[ctype]
        o = self._dicctypes[ctype] = DCType(ctype)

        rec = self.make_record('add ctype', ctype, cmt)
        if cmt is not False: self.add_history_record(rec)
        log.info(rec, self.__class__.__name__)
        return o
示例#9
0
    def mark_range_for_key(self, keyrng, cmt=False):
        """Marks child object for deletion in save()"""
        if keyrng in self._dicranges.keys():
            #o = self._dicranges[keyrng]
            #o.mark_versions()
            self._lst_del_keys.append(keyrng)

            rec = self.make_record('del range', keyrng, cmt)
            if cmt is not False: self.add_history_record(rec)
            log.info(rec, self.__class__.__name__)
            return keyrng
        else:
            msg = 'Marking of non-existent range %s' % str(keyrng)
            log.warning(msg, self._name)
            return None
示例#10
0
    def save(self, path=None, mode='r+'):
        if path is not None: self._fpath = path
        if not isinstance(self._fpath, str):
            msg = 'Invalid file name: %s' % str(self._fpath)
            log.error(msg, self.__class__.__name__)
            raise ValueError(msg)

        mode_rw = mode if os.path.exists(self._fpath) else 'w'

        with sp.File(self._fpath, mode_rw) as grp:

            msg = '= save(), group %s object for %s' % (grp.name,
                                                        self.detname())
            log.debug(msg, self._name)

            ds1 = save_object_as_dset(grp, 'dettype',
                                      data=self.dettype())  # 'str'
            ds2 = save_object_as_dset(grp, 'detname',
                                      data=self.detname())  # 'str'
            ds3 = save_object_as_dset(grp, 'detid', data=self.detid())  # 'str'
            ds4 = save_object_as_dset(grp, 'tscfile',
                                      data=self.tscfile())  # 'double'
            ds5 = save_object_as_dset(grp,
                                      'predecessor',
                                      data=self.predecessor())  # 'str'
            ds6 = save_object_as_dset(grp, 'successor',
                                      data=self.successor())  # 'str'

            # save/delete objects in/from hdf5 file
            for k, v in self._dicctypes.iteritems():
                if k in self._lst_del_keys: delete_object(grp, k)
                else: v.save(grp)
                #self._dicctypes[k].save(grp)

            # deletes items from dictionary
            for k in self._lst_del_keys:
                del self._dicctypes[k]

            self._lst_del_keys = []

            self.save_base(grp)

            grp.close()
            log.info('File %s is updated/saved' % self._fpath, self._name)