コード例 #1
0
ファイル: MDB_CLI.py プロジェクト: irischang020/lcls2
    def deldoc(self):
        """Deletes specified document in the database.
        """
        mode, kwargs = self.mode, self.kwargs
        dbname = mu.get_dbname(**kwargs)
        client = self.client()
        if not self.check_database(client, dbname): return

        detname = kwargs.get('detector', None)
        if detname is None:
            logger.warning(
                '%s needs in the collection name. Please specify the detector name.'
                % (mode))
        colname = detname
        db, fs = mu.db_and_fs(client, dbname)
        colnames = mu.collection_names(db)

        if not (colname in colnames):  # mu.collection_exists(db, colname)
            logger.warning('db "%s" does not have collection "%s"' %
                           (db.name, str(colname)))
            return

        col = mu.collection(db, colname)

        logger.info('command mode: "%s" db: "%s" collection: "%s"' %
                    (mode, db.name, str(colname)))

        defs = self.defs
        ctype = kwargs.get('ctype', None)
        run = kwargs.get('run', None)
        tsec = kwargs.get('time_sec', None)
        tstamp = kwargs.get('time_stamp', None)
        vers = kwargs.get('version', None)
        confirm = kwargs.get('confirm', False)

        query = {'detector': detname}
        if ctype != defs['ctype']: query['ctype'] = ctype
        if run != defs['run']: query['run'] = run
        if vers != defs['version']: query['version'] = vers
        #if tsec  != defs['time_sec'] : query['time_sec'] = tsec
        if gu.is_in_command_line('-s', '--time_sec'): query['time_sec'] = tsec
        if gu.is_in_command_line('-t', '--time_stamp'):
            query['time_stamp'] = tstamp

        logger.info('query: %s' % str(query))

        docs = mu.find_docs(col, query)
        if docs is None or docs.count() == 0:
            logger.warning('Can not find document for query: %s' % str(query))
            return

        for i, doc in enumerate(docs):
            msg = '  deldoc %2d:'%i + doc['time_stamp'] + ' ' + str(doc['time_sec'])\
                + ' %s'%doc['ctype'].ljust(16) + ' %4d'%doc['run'] + ' ' + str(doc['id_data'])
            logger.info(msg)
            if confirm:
                mu.delete_document_from_collection(col, doc['_id'])
                mu.del_document_data(doc, fs)

        if not confirm: mu.request_confirmation()
コード例 #2
0
ファイル: MDB_CLI.py プロジェクト: irischang020/lcls2
    def get(self):
        """Gets constans from DB and saves them in file.
        """
        mode, kwargs = self.mode, self.kwargs
        defs = self.defs
        host = kwargs.get('host', None)
        port = kwargs.get('port', None)
        exp = kwargs.get('experiment', None)
        det = kwargs.get('detector', None)
        ctype = kwargs.get('ctype', None)
        dtype = kwargs.get('dtype', None)
        run = kwargs.get('run', None)
        run_end = kwargs.get('run_end', None)
        tsec = kwargs.get('time_sec', None) if gu.is_in_command_line(
            '-s', '--time_sec') else None
        tstamp = kwargs.get('time_stamp', None) if gu.is_in_command_line(
            '-t', '--time_stamp') else None
        vers = kwargs.get('version', None)
        prefix = kwargs.get('iofname', None)
        verb = self.loglevel == 'DEBUG'

        db_det, db_exp, colname, query = mu.dbnames_collection_query(
            det, exp, ctype, run, tsec, vers, dtype)
        logger.debug('get: %s %s %s %s' %
                     (db_det, db_exp, colname, str(query)))
        dbname = db_det if exp is None else db_exp

        client = self.client()
        if not self.check_database(client, dbname): return

        db, fs = mu.db_and_fs(client, dbname)
        colnames = mu.collection_names(db)

        if not (colname in colnames):  # mu.collection_exists(db, colname)
            logger.warning('db "%s" does not have collection "%s"' %
                           (db.name, str(colname)))
            return

        col = mu.collection(db, colname)

        logger.debug('Search document in db "%s" collection "%s"' %
                     (dbname, colname))

        doc = mu.find_doc(col, query)
        if doc is None:
            logger.warning('Can not find document for query: %s' % str(query))
            return

        logger.debug('get doc:', doc)

        data = mu.get_data_for_doc(fs, doc)
        if data is None:
            logger.warning('Can not load data for doc: %s' % str(doc))
            return

        if prefix is None: prefix = mu.out_fname_prefix(**doc)

        mu.save_doc_and_data_in_file(doc,
                                     data,
                                     prefix,
                                     control={
                                         'data': True,
                                         'meta': True
                                     })