def convCfsToSqlite(url, prefix = '', ignore = []): """ url - channel finder server URL prefix - output DB file name prefix ignore - list of ignored lattice name """ cf = ChannelFinderClient(BaseURL=url) tagprefx = "aphla.sys." tags = [tag.Name for tag in cf.getAllTags() if tag.Name.startswith(tagprefx)] for tag in tags: latname = tag[len(tagprefx):] if latname in ignore: continue cfa = ChannelFinderAgent() cfa.downloadCfs(url, property=[('hostName', '*')], tagName=tag) #cfa.splitPropertyValue('elemGroups') cfa.splitChainedElement('elemName') cfa.saveSqlite("%s%s.sqlite" % (prefix, latname))
def convCfsToSqlite(url, prefix = '', ignore = []): """ url - channel finder server URL prefix - output DB file name prefix ignore - list of ignored lattice name """ cf = ChannelFinderClient(BaseURL=url) tagprefx = "aphla.sys." tags = [tag.Name for tag in cf.getAllTags() if tag.Name.startswith(tagprefx)] for tag in tags: latname = tag[len(tagprefx):] if latname in ignore: continue cfa = ChannelFinderAgent() cfa.downloadCfs(url, property=[('hostName', '*')], tagName=tag) #cfa.splitPropertyValue('elemGroups') cfa.splitChainedElement('elemName') cfa.saveSqlite("%s%s.sqlite" % (prefix, latname))
def cfs_append_from_sqlite(fname, update_only): sq = ap.chanfinder.ChannelFinderAgent() sq.loadSqlite(fname) cf = ChannelFinderClient(**cfinput) all_prpts = [p.Name for p in cf.getAllProperties()] all_tags = [t.Name for t in cf.getAllTags()] ignore_prpts = ['hostName', 'iocName'] allpvs = [] tag_owner = OWNER prpt_owner = PRPTOWNER prpt_data, tag_data = {}, {} # the data body for pv, prpts, tags in sq.rows: if not pv: continue if pv in allpvs: continue if pv.find("SR:") != 0: continue logging.info("updating '{0}'".format(pv)) allpvs.append(pv) prpt_list, tag_list = [], [] for k, v in prpts.items(): if k not in [ "elemIndex", "system", "elemType", "elemHandle", "elemName", "elemField" ]: continue prpt_data.setdefault((k, v), []) prpt_data[(k, v)].append(pv) for tag in tags: if not tag.startswith("aphla."): continue tag_data.setdefault(tag, []) tag_data[tag].append(pv) #tag_list.append(Tag(r.strip()), tag_owner) logging.info("{0}: {1} ({2})".format(pv, tag, tag_owner)) #addPvTag(cf, pv, tag, tag_owner) errpvs = [] for pv in allpvs: chs = cf.find(name=pv) if not chs: errpvs.append(pv) print("PV '%s' does not exist" % pv) continue elif len(chs) != 1: print("Find two results for pv=%s" % pv) continue prpts = chs[0].getProperties() if not prpts: continue for prpt, val in prpts.items(): pvlst = prpt_data.get((prpt, val), []) if not pvlst: continue try: j = pvlst.index(pv) prpt_data[(prpt, val)].pop(j) except: # the existing data is not in the update list, skip pass #if errpvs: # #raise RuntimeError("PVs '{0}' are missing".format(errpvs)) # print logging.warn("{0} does not exist in DB".format(errpvs)) for k, v in prpt_data.items(): vf = [pv for pv in v if pv not in errpvs] if not vf: logging.info("no valid PVs for {0}".format(k)) continue updatePropertyPvs(cf, k[0], prpt_owner, k[1], vf) logging.info("add property {0} for pvs {1}".format(k, vf)) for k, v in tag_data.items(): vf = [pv for pv in v if pv not in errpvs] addTagPvs(cf, k, vf, tag_owner) logging.info("add tag {0} for pvs {1}".format(k, vf))
def cfs_append_from_csv2(rec_list, update_only): cf = ChannelFinderClient(**cfinput) all_prpts = [p.Name for p in cf.getAllProperties()] all_tags = [t.Name for t in cf.getAllTags()] ignore_prpts = ['hostName', 'iocName'] import csv rd = csv.reader(rec_list) allpvs = [] tag_owner = OWNER prpt_owner = PRPTOWNER prpt_data, tag_data = {}, {} # the data body for s in rd: if not s: continue if not s[0].strip(): continue if s[0].strip().startswith('#'): continue pv = s[0].strip() logging.info("updating '{0}'".format(pv)) #cf.update(property=Property('elemType', PRPTOWNER, 'QUAD'), # channelNames = [pv]) #chs = cf.find(name=pv) #sys.exit(0) #logging.info("{0} and {1}".format(chs[0].Name, type(chs[0].Name))) #logging.info("{0} and {1}".format(pv, type(pv))) allpvs.append(pv) prpt_list, tag_list = [], [] for r in s[1:]: if r.find('=') > 0: prpt, val = [v.strip() for v in r.split('=')] prpt_data.setdefault((prpt, val), []) prpt_data[(prpt, val)].append(pv) else: # it is a tag tag = r.strip() if not tag: continue tag_data.setdefault(tag, []) tag_data[tag].append(pv) #tag_list.append(Tag(r.strip()), tag_owner) logging.info("{0}: {1} ({2})".format(pv, tag, tag_owner)) #addPvTag(cf, pv, tag, tag_owner) errpvs = [] for pv in allpvs: chs = cf.find(name=pv) if not chs: errpvs.append(pv) print("PV '%s' does not exist" % pv) continue elif len(chs) != 1: print("Find two results for pv=%s" % pv) continue for prpt, val in chs[0].getProperties().items(): pvlst = prpt_data.get((prpt, val), []) if not pvlst: continue try: j = pvlst.index(pv) prpt_data[(prpt, val)].pop(j) except: # the existing data is not in the update list, skip pass #if errpvs: # #raise RuntimeError("PVs '{0}' are missing".format(errpvs)) # print logging.warn("{0} does not exist in DB".format(errpvs)) for k, v in prpt_data.items(): vf = [pv for pv in v if pv not in errpvs] if not vf: logging.info("no valid PVs for {0}".format(k)) continue updatePropertyPvs(cf, k[0], prpt_owner, k[1], vf) logging.info("add property {0} for pvs {1}".format(k, vf)) for k, v in tag_data.items(): vf = [pv for pv in v if pv not in errpvs] addTagPvs(cf, k, vf, tag_owner) logging.info("add tag {0} for pvs {1}".format(k, vf))
def cfs_append_from_csv1(rec_list, update_only): cf = ChannelFinderClient(**cfinput) all_prpts = [p.Name for p in cf.getAllProperties()] all_tags = [t.Name for t in cf.getAllTags()] ignore_prpts = ['hostName', 'iocName'] import csv rd = csv.reader(rec_list) # header line header = rd.next() # lower case of header hlow = [s.lower() for s in header] # number of headers, pv + properties nheader = len(header) # the index of PV, properties and tags ipv = hlow.index('pv') # the code did not rely on it, but it is a good practice if ipv != 0: raise RuntimeError("the first column should be pv") iprpt, itags = [], [] for i, h in enumerate(header): if i == ipv: continue # if the header is empty, it is a tag if len(h.strip()) == 0: itags.append(i) else: iprpt.append(i) tag_owner = OWNER prpt_owner = PRPTOWNER tags = {} # the data body for s in rd: prpts = [Property(header[i], prpt_owner, s[i]) for i in iprpt if s[i]] # itags could be empty if we put all tags in the end columns for i in itags + range(nheader, len(s)): rec = tags.setdefault(s[i].strip(), []) rec.append(s[ipv].strip()) #print s[ipv], prpts, tags ch = cf.find(name=s[ipv]) if ch is None: logging.warning("pv {0} does not exist".format(s[ipv])) elif len(ch) > 1: logging.warning("pv {0} is not unique ({1})".format( s[ipv], len(ch))) else: for p in prpts: #continue if p.Name in ignore_prpts: continue #if p.Name != 'symmetry': continue logging.info("updating '{0}' with property, {1}={2}".format( s[ipv], p.Name, p.Value)) cf.update(channelName=s[ipv], property=p) logging.info("finished updating properties") for t, pvs in tags.items(): if not hasTag(cf, t): cf.set(tag=Tag(t, tag_owner)) if 'V:1-SR-BI{BETA}X-I' in pvs: continue if 'V:1-SR-BI{BETA}Y-I' in pvs: continue try: cf.update(tag=Tag(t, tag_owner), channelNames=pvs) except: print(t, pvs) raise logging.info("update '{0}' for {1} pvs".format(t, len(pvs))) logging.info("finished updating tags")
def cfs_append_from_sqlite(fname, update_only): sq = ap.chanfinder.ChannelFinderAgent() sq.loadSqlite(fname) cf = ChannelFinderClient(**cfinput) all_prpts = [p.Name for p in cf.getAllProperties()] all_tags = [t.Name for t in cf.getAllTags()] ignore_prpts = ['hostName', 'iocName'] allpvs = [] tag_owner = OWNER prpt_owner = PRPTOWNER prpt_data, tag_data = {}, {} # the data body for pv, prpts, tags in sq.rows: if not pv: continue if pv in allpvs: continue if pv.find("SR:") != 0: continue logging.info("updating '{0}'".format(pv)) allpvs.append(pv) prpt_list, tag_list = [], [] for k,v in prpts.items(): if k not in ["elemIndex", "system", "elemType", "elemHandle", "elemName", "elemField"]: continue prpt_data.setdefault((k, v), []) prpt_data[(k, v)].append(pv) for tag in tags: if not tag.startswith("aphla."): continue tag_data.setdefault(tag, []) tag_data[tag].append(pv) #tag_list.append(Tag(r.strip()), tag_owner) logging.info("{0}: {1} ({2})".format(pv, tag, tag_owner)) #addPvTag(cf, pv, tag, tag_owner) errpvs = [] for pv in allpvs: chs = cf.find(name=pv) if not chs: errpvs.append(pv) print "PV '%s' does not exist" % pv continue elif len(chs) != 1: print "Find two results for pv=%s" % pv continue prpts = chs[0].getProperties() if not prpts: continue for prpt,val in prpts.items(): pvlst = prpt_data.get((prpt, val), []) if not pvlst: continue try: j = pvlst.index(pv) prpt_data[(prpt,val)].pop(j) except: # the existing data is not in the update list, skip pass #if errpvs: # #raise RuntimeError("PVs '{0}' are missing".format(errpvs)) # print logging.warn("{0} does not exist in DB".format(errpvs)) for k,v in prpt_data.iteritems(): vf = [pv for pv in v if pv not in errpvs] if not vf: logging.info("no valid PVs for {0}".format(k)) continue updatePropertyPvs(cf, k[0], prpt_owner, k[1], vf) logging.info("add property {0} for pvs {1}".format(k, vf)) for k,v in tag_data.iteritems(): vf = [pv for pv in v if pv not in errpvs] addTagPvs(cf, k, vf, tag_owner) logging.info("add tag {0} for pvs {1}".format(k, vf))
def cfs_append_from_csv2(rec_list, update_only): cf = ChannelFinderClient(**cfinput) all_prpts = [p.Name for p in cf.getAllProperties()] all_tags = [t.Name for t in cf.getAllTags()] ignore_prpts = ['hostName', 'iocName'] import csv rd = csv.reader(rec_list) allpvs = [] tag_owner = OWNER prpt_owner = PRPTOWNER prpt_data, tag_data = {}, {} # the data body for s in rd: if not s: continue if not s[0].strip(): continue if s[0].strip().startswith('#'): continue pv = s[0].strip() logging.info("updating '{0}'".format(pv)) #cf.update(property=Property('elemType', PRPTOWNER, 'QUAD'), # channelNames = [pv]) #chs = cf.find(name=pv) #sys.exit(0) #logging.info("{0} and {1}".format(chs[0].Name, type(chs[0].Name))) #logging.info("{0} and {1}".format(pv, type(pv))) allpvs.append(pv) prpt_list, tag_list = [], [] for r in s[1:]: if r.find('=') > 0: prpt, val = [v.strip() for v in r.split('=')] prpt_data.setdefault((prpt, val), []) prpt_data[(prpt, val)].append(pv) else: # it is a tag tag = r.strip() if not tag: continue tag_data.setdefault(tag, []) tag_data[tag].append(pv) #tag_list.append(Tag(r.strip()), tag_owner) logging.info("{0}: {1} ({2})".format(pv, tag, tag_owner)) #addPvTag(cf, pv, tag, tag_owner) errpvs = [] for pv in allpvs: chs = cf.find(name=pv) if not chs: errpvs.append(pv) print "PV '%s' does not exist" % pv continue elif len(chs) != 1: print "Find two results for pv=%s" % pv continue for prpt,val in chs[0].getProperties().items(): pvlst = prpt_data.get((prpt, val), []) if not pvlst: continue try: j = pvlst.index(pv) prpt_data[(prpt,val)].pop(j) except: # the existing data is not in the update list, skip pass #if errpvs: # #raise RuntimeError("PVs '{0}' are missing".format(errpvs)) # print logging.warn("{0} does not exist in DB".format(errpvs)) for k,v in prpt_data.iteritems(): vf = [pv for pv in v if pv not in errpvs] if not vf: logging.info("no valid PVs for {0}".format(k)) continue updatePropertyPvs(cf, k[0], prpt_owner, k[1], vf) logging.info("add property {0} for pvs {1}".format(k, vf)) for k,v in tag_data.iteritems(): vf = [pv for pv in v if pv not in errpvs] addTagPvs(cf, k, vf, tag_owner) logging.info("add tag {0} for pvs {1}".format(k, vf))
def cfs_append_from_csv1(rec_list, update_only): cf = ChannelFinderClient(**cfinput) all_prpts = [p.Name for p in cf.getAllProperties()] all_tags = [t.Name for t in cf.getAllTags()] ignore_prpts = ['hostName', 'iocName'] import csv rd = csv.reader(rec_list) # header line header = rd.next() # lower case of header hlow = [s.lower() for s in header] # number of headers, pv + properties nheader = len(header) # the index of PV, properties and tags ipv = hlow.index('pv') # the code did not rely on it, but it is a good practice if ipv != 0: raise RuntimeError("the first column should be pv") iprpt, itags = [], [] for i, h in enumerate(header): if i == ipv: continue # if the header is empty, it is a tag if len(h.strip()) == 0: itags.append(i) else: iprpt.append(i) tag_owner = OWNER prpt_owner = PRPTOWNER tags = {} # the data body for s in rd: prpts = [Property(header[i], prpt_owner, s[i]) for i in iprpt if s[i]] # itags could be empty if we put all tags in the end columns for i in itags + range(nheader, len(s)): rec = tags.setdefault(s[i].strip(), []) rec.append(s[ipv].strip()) #print s[ipv], prpts, tags ch = cf.find(name=s[ipv]) if ch is None: logging.warning("pv {0} does not exist".format(s[ipv])) elif len(ch) > 1: logging.warning("pv {0} is not unique ({1})".format(s[ipv], len(ch))) else: for p in prpts: #continue if p.Name in ignore_prpts: continue #if p.Name != 'symmetry': continue logging.info("updating '{0}' with property, {1}={2}".format( s[ipv], p.Name, p.Value)) cf.update(channelName=s[ipv], property=p) logging.info("finished updating properties") for t,pvs in tags.iteritems(): if not hasTag(cf, t): cf.set(tag=Tag(t, tag_owner)) if 'V:1-SR-BI{BETA}X-I' in pvs: continue if 'V:1-SR-BI{BETA}Y-I' in pvs: continue try: cf.update(tag=Tag(t, tag_owner), channelNames=pvs) except: print t, pvs raise logging.info("update '{0}' for {1} pvs".format(t, len(pvs))) logging.info("finished updating tags")