示例#1
0
文件: MDB_CLI.py 项目: monarin/lcls2
 def client(self):
     kwargs = self.kwargs
     host = kwargs.get('host', None)
     port = kwargs.get('port', None)
     msg = 'MongoDB client host:%s port:%d' % (host, port)
     logger.info(msg)
     return dbu.connect_to_server(host, port)
示例#2
0
def connect_client(host=None,
                   port=None,
                   user=cp.user,
                   upwd=cp.upwd):  # user=dbu.cc.USERNAME
    _host = cp.cdb_host.value() if host is None else host
    _port = cp.cdb_port.value() if port is None else port
    #logger.debug('CMDBBUtils: Connect client to host: %s port: %d user: %s upwd: %s' % (_host, _port, user, upwd))
    return dbu.connect_to_server(_host, _port, user, upwd)
示例#3
0
def scan_calib_for_experiment(exp='cxix25615', **kwargs):

    host = kwargs.get('host', None)
    port = kwargs.get('port', None)
    user = kwargs.get('user', None)
    upwd = kwargs.get('upwd', None)
    verbose = kwargs.get('verbose', False)

    client = dbu.connect_to_server(host, port, user, upwd)
    dbname = dbu.db_prefixed_name(exp)
    if dbu.database_exists(client, dbname):
        msg = 'Experiment %s already has a database. Consider to delete it from the list:\n%s'%\
              (exp, str(dbu.database_names(client)))+\
              '\nBefore adding consider to delete existing DB using command: cdb deldb --dbname %s -C -u <username> -p <password>' % dbname
        logger.warning(msg)
        return

    dircalib = nm.dir_calib(exp)
    #if verbose :
    logger.info('Scan: %s' % dircalib)

    for dir0 in gu.get_list_of_files_in_dir_for_part_fname(dircalib,
                                                           pattern='::'):
        if not os.path.isdir(dir0): continue
        calibvers = os.path.basename(dir0)
        logger.debug('  %s ' % calibvers)

        for dir1 in gu.get_list_of_files_in_dir_for_part_fname(dir0,
                                                               pattern=':'):
            if not os.path.isdir(dir1): continue
            detname = os.path.basename(dir1)
            detname_m = detname_conversion(detname)
            logger.debug('    %s' % detname_m)

            for cftype in gu.get_list_of_files_in_dir(dir1):
                if not (cftype in cc.list_calib_names): continue
                dir2 = '%s/%s' % (dir1, cftype)
                if not os.path.isdir(dir2): continue
                logger.debug('      %s' % cftype)

                cfdir = '%s/%s/%s/%s' % (dircalib, calibvers, detname, cftype)
                listdicts = history_list_of_dicts('%s/HISTORY' % cfdir,
                                                  verbose)
                #logger.debug('XXX listdicts %s' % listdicts)
                count = 0
                for fname in gu.get_list_of_files_in_dir(dir2):
                    logger.debug('        %s' % fname)
                    if fname == 'HISTORY': continue
                    if os.path.splitext(fname)[1] != '.data': continue
                    logger.debug('  XXX begin adding: %s %s %s %s' %
                                 (dircalib, detname_m, cftype, fname))
                    add_calib_file_to_cdb(exp, dircalib, calibvers, detname_m,
                                          cftype, fname, cfdir, listdicts,
                                          **kwargs)
                    count += 1

                logger.info('  converted %3d files from: %s' % (count, cfdir))
示例#4
0
 def client(self):
     kwargs = self.kwargs
     host = kwargs.get('host', None)
     port = kwargs.get('port', None)
     user = kwargs.get('user', None)
     upwd = kwargs.get('upwd', None)
     ctout = kwargs.get('ctout', 5000)
     stout = kwargs.get('stout', 30000)
     return dbu.connect_to_server(host, port, user, upwd, ctout, stout)
示例#5
0
def test_xtcav_calib_constants(fname=
                               '/reg/d/psdm/XPP/xpptut15/calib/Xtcav::CalibV1/XrayTransportDiagnostic.0:Opal1000.0/pedestals/101-102.data',\
                               add_constants_to_db=False) :

    _, exp, _, cvers, detname, ctype, cfname = fname.rsplit('/',6) 
    resp = parse_calib_file_name(cfname)
    begin, end, ext = resp if resp is not None else (None, None, None)
    det = detname_conversion(detname)
    run = begin
    dbname_exp = dbu.db_prefixed_name(exp)
    dbname_det = dbu.db_prefixed_name(det)

    print('LCLS1 Xtcav calibration file: %s' % fname)
    print('Parameters form path: exp:%s det:%s ctype:%s run:%s dbname_exp:%s dbname_det:%s'%\
          (exp, det, ctype, run, dbname_exp, dbname_det))

    #Save(ct,fname)
    o1 = Load(fname)
    d1 = dict_from_xtcav_calib_object(o1)
    print('Xtcav calibration constants as dict:\n', d1)

    if not add_constants_to_db : return
    #==================================

    #---- Delete databases for experiment and detector

    client = dbu.connect_to_server(HOST, PORT)
    print('Open client on host:%s port:%s' % (HOST, PORT))

    print('Delete database %s'% dbname_exp)
    dbu.delete_database(client, dbname_exp)

    print('Delete database %s'% dbname_det)
    dbu.delete_database(client, dbname_det)

    #---- Add data to experiment and detector dbs
    print('Add Xtcav constants') 

    kwargs = {'host' : HOST,\
              'port' : PORT,\
              'version' : 'V01',\
              'comment' : 'test of add-retrieve xtcav constants'
             }
    #insert_calib_data(data, *kwargs)
    dbu.insert_constants(o1, exp, det, ctype, run, time_sec='1000000000', **kwargs)

    #msg = dbu.database_info(client, dbname_exp, level=10)
    #print(msg)

    print('Xtcav constants inserted, now retrieve them from db:%s collection:%s' % (dbname_exp, det))

    db, fs = dbu.db_and_fs(client, dbname_exp)
    col = dbu.collection(db, det)

    #for doc in col.find() :
    #    print(doc)

    doc = dbu.find_doc(col, query={'ctype':ctype, 'run':run})
    print('Found doc:\n', doc)

    o2 = dbu.get_data_for_doc(fs, doc)
    d2 = dict_from_xtcav_calib_object(o2)
    print('Xtcav calib object converted to dict:\n', d2)

    #print('cmp(d1,d2) :', str(d1==d2))

    print('\nCompare dictionaries for Xtcav calib objects loaded directly from calib file and passed through the CDB')
    compare_dicts(d1,d2)

    client.close()
    return
示例#6
0
def connect_client(host=None, port=None):
    _host = cp.cdb_host.value() if host is None else host
    _port = cp.cdb_port.value() if port is None else port
    #logger.debug('Connect client to host: %s port: %d' % (_host, _port))
    return dbu.connect_to_server(_host, _port)