Example #1
0
def appendPropertyPvs(cf, p, owner, v, pvs, sep=";"):
    """
    """
    if not hasProperty(cf, p):
        # in this set, value of property is not needed (confusing).  since set
        # is creating a new (p,v) pair. Before attach it to a PV, its value is
        # meaningless.
        cf.set(property=Property(p, owner, v))
        logging.info("create new property (%s,%s,%s)" % (p, owner, v))

    for pv in pvs:
        ch = cf.find(name=pv, property=[(p, '*')])
        if not ch:
            cf.update(property=Property(p, owner, v), channelName=pv)
        elif len(ch) > 1:
            raise RuntimeError("more than one result for pv={0}".format(pv))
        else:
            vals = set()
            val0 = ch[0].getProperties().get(p, "")
            for val in val0.split(sep):
                vals.add(val)
            print(v, vals)
            vals = set(v.split(sep) + list(vals))
            val = sep.join(sorted(list(vals)))
            cf.update(property=Property(p, owner, val), channelName=pv)

    logging.info("batch append property (%s,%s,%s) for %d pvs" %
                 (p, owner, v, len(pvs)))
Example #2
0
def simple_test():
    cf = ChannelFinderClient(**cfinput)
    #cf.set(property=Property('test-prop1', 'lyyang', 'v1'))
    #cf.set(property=Property('test-prop2', 'cf-asd', 'v2'))
    #cf.set(property=Property('test-prop3', 'lyyang'))
    #cf.set(property=Property('test-prop4', 'lyyang'))
    #cf.delete(propertyName='test-prop1')
    #cf.delete(propertyName='test-prop3')

    pvname1 = 'SR:C00-BI:G00{DCCT:00}CUR-RB'
    pvname2 = 'SR:C00-Glb:G00{CHROM:00}RB-X'
    pvname3 = 'SR:C00-Glb:G00{CHROM:00}RB-Y'
    cf.update(property=Property('test-prop2', 'cf-asd', 'v1'),
              channelNames=[pvname1, pvname2, pvname3])
    #chs = cf.find(property=[('test-prop2', '*')])
    #if chs is not None:
    #    for ch in chs:
    #        print ch.Name
    #        for p in ch.Properties:
    #            print "  ", p.Name,"=",p.Value, p.Owner, ", "
    #        print "/"

    cf.delete(property=Property('test-prop2', 'cf-asd', 'v1'),
              channelNames=[pvname2, pvname3])
    chs = cf.find(property=[('test-prop2', '*')])
    if chs is not None:
        for ch in chs:
            print ch.Name,
            for p in ch.Properties:
                print p.Name, "=", p.Value, ", ",
            print " /"
Example #3
0
    def testChannelUtil(self):
        channel1 = Channel('chName1',
                           'chOwner',
                           properties=[
                               Property('location', 'propOwner', 'propVal'),
                               Property('prop1', 'propOwner', 'propVal')
                           ],
                           tags=[Tag('myTag', 'tagOwner')])
        channel2 = Channel(
            'chName2',
            'chOwner',
            properties=[
                Property('location', 'propOwner', 'propVal'),
                Property('prop2', 'propOwner', 'propVal')
            ],
            tags=[Tag('myTag', 'tagOwner'),
                  Tag('goldenOrbit', 'tagOwner')])
        channels = [channel1, channel2]
        allTags = ChannelUtil.getAllTags(channels)
        self.assertTrue(len(allTags) == 2, \
                        'expected 2 tags found ' + str(len(allTags)))
        allPropertyNames = ChannelUtil.getAllProperties(channels)
        self.assertTrue(len(allPropertyNames) == 3, \
                        'expected 3 unique properties but found ' + str(len(allPropertyNames)))

        pass
Example #4
0
 def testValidateWithProperty(self):
     ch1 = Channel('ch1',
                   'chOwner',
                   properties=[
                       Property('location', 'propOwner', '234'),
                       Property('prop1', 'propOwner', 'propVal')
                   ])
     ch2 = Channel('ch2',
                   'chOwner',
                   properties=[
                       Property('location', 'propOwner', 'SR'),
                       Property('prop2', 'propOwner', 'propVal')
                   ])
     ch3 = Channel('ch3',
                   'chOwner',
                   properties=[
                       Property('location', 'propOwner', 'SR:234'),
                       Property('prop2', 'propOwner', 'propVal')
                   ])
     self.assertTrue(
         ChannelUtil.validateChannelWithProperty([ch2, ch3],
                                                 Property(
                                                     'prop2', 'anyOwner',
                                                     'propVal')))
     self.assertFalse(ChannelUtil.validateChannelWithProperty([ch1, ch2, ch3], Property('prop2', 'anyOwner', 'propVal')), \
                      'Failed to correctly validate channels based on a PropertyValidator')
     pass
Example #5
0
 def testValidateWithTag(self):   
     ch1 = Channel('chName1', 'chOwner',
                       properties=[Property('location', 'propOwner', 'propVal'),
                                   Property('prop1', 'propOwner', 'propVal')],
                       tags=[Tag('myTag', 'tagOwner')])
     ch2 = Channel('chName2', 'chOwner',
                       properties=[Property('location', 'propOwner', 'propVal'),
                                   Property('prop2', 'propOwner', 'propVal')],
                       tags=[Tag('myTag', 'tagOwner'),
                             Tag('goldenOrbit', 'tagOwner')])
     self.assertTrue(ChannelUtil.validateChannelsWithTag([ch1, ch2], Tag('myTag', 'someOwner')), \
                     'Failed to validate channels based on TagValidator')
     self.assertFalse(ChannelUtil.validateChannelsWithTag([ch1, ch2], Tag('goldenOrbit', 'someOwner')), \
                      'Failed to correctly validate channels based on a TagValidator')  
     pass
Example #6
0
def updatePropertyPvs(cf, p, owner, v, pvs):
    """
    """
    if not hasProperty(cf, p):
        # in this set, value of property is not needed (confusing).  since set
        # is creating a new (p,v) pair. Before attach it to a PV, its value is
        # meaningless.
        try:
            cf.set(property=Property(p, owner, v))
            logging.info("create new property (%s,%s,%s)" % (p, owner, v))
        except:
            logging.error("can not create new property (%s,%s,%s)" %
                          (p, owner, v))
    logging.info("adding property (%s,%s,%s) for %d pvs: %s" %
                 (p, owner, v, len(pvs), str(pvs)))
    ret = cf.update(property=Property(p, owner, v), channelNames=pvs)
Example #7
0
 def testAddUpdateChannelsWithProperties(self):
     '''
     This is to check that existing properties of channels are not affected.
     '''
     unaffectedProperty = Property('unaffectedProperty', self.owner, 'unchanged')
     # create default client
     client = ChannelFinderClient(BaseURL=self.baseURL, username=self.username, password=self.password)
     client.set(property=unaffectedProperty)
     
     # add new pv's
     t1 = str(time())
     hostName1 = 'update-test-hostname' + t1
     iocName1 = 'update-test-iocName' + t1
     # New Channels added
     client = ChannelFinderClient(BaseURL=self.baseURL, username=self.username, password=self.password);
     client.set(channel=Channel('cf-update-pv1', 'cf-update', properties=[unaffectedProperty]))
     updateChannelFinder(['cf-update-pv1', 'cf-update-pv2'], \
                         hostName1, \
                         iocName1, \
                         owner=self.owner, \
                         time=t1, \
                         service=self.baseURL , \
                         username=self.username, \
                         password=self.password)
     channels = client.find(property=[('hostName', hostName1), ('iocName', iocName1), ('time', t1)])
     self.assertTrue(len(channels) == 2, 'failed to create the channels with appropriate properties')
     channels = client.find(name='cf-update-pv1')
     self.assertTrue(len(channels) == 1)
     self.assertTrue(len(channels[0].Properties) == 5)
     # Cleanup
     client.delete(channelName='cf-update-pv1')
     client.delete(channelName='cf-update-pv2')
     client.delete(propertyName=unaffectedProperty.Name)
Example #8
0
 def testGetAllPropValues(self):
     ch1 = Channel('ch1',
                   'chOwner',
                   properties=[
                       Property('location', 'propOwner', '234'),
                       Property('prop1', 'propOwner', 'propVal')
                   ])
     ch2 = Channel('ch2',
                   'chOwner',
                   properties=[
                       Property('location', 'propOwner', 'SR'),
                       Property('prop2', 'propOwner', 'propVal')
                   ])
     ch3 = Channel('ch3',
                   'chOwner',
                   properties=[
                       Property('location', 'propOwner', 'SR:234'),
                       Property('prop2', 'propOwner', 'propVal')
                   ])
     chs = [ch1, ch2, ch3]
     values = ChannelUtil.getAllPropValues(chs, propertyName='location')
     self.assertTrue('234' in values, \
                     'Failed to find property(location), value 234 for ch1')
     self.assertTrue('SR' in values, \
                     'Failed to find property(location), value SR for ch2')
     self.assertTrue('SR:234' in values, \
                     'Failed to find property(location), value SR:234 for ch3')
     pass
Example #9
0
def initCfs(fpv, sep=","):
    dat = {}
    for line in open(fpv, 'r').readlines():
        #pv, hdl, name, idx, fam, fld
        rec = [v.strip() for v in line.split(sep)]
        pv, prpt = rec[0], rec[1:]
        dat.setdefault(pv, [])
        dat[pv].append(tuple(rec[1:]))

    import conf
    cfinput = {
        'BaseURL': cfsurl,
        'username': conf.username,
        'password': conf.password
    }
    cf = ChannelFinderClient(**cfinput)
    prpts = [p.Name for p in cf.getAllProperties()]
    for p in ["elemName", "elemIndex", "elemType", "elemField"]:
        if p in prpts: continue
        # property owner is cf-asd
        logging.info("add new property '%s'" % p)
        cf.set(property=Property(p, "cf-asd"))

    for pv, prptsets in dat.items():
        if len(prptsets) == 1:
            hdl, name, idx, fam, fld = prptsets[0]
            #cf.update(channel=Channel(pv, "cf-update", properties=[
            #    Property("elemHandle", "cf-asd", hdl),
            #    Property("elemName",   "cf-asd", name),
            #    Property("elemIndex",  "cf-asd", idx),
            #    Property("elemType",   "cf-asd", fam),
            #    Property("elemField",  "cf-asd", fld)]))
            cf.update(property=Property("elemHandle", "cf-asd", hdl),
                      channelName=pv)
            cf.update(property=Property("elemName", "cf-asd", name),
                      channelName=pv)
            cf.update(property=Property("elemIndex", "cf-asd", idx),
                      channelName=pv)
            cf.update(property=Property("elemType", "cf-asd", fam),
                      channelName=pv)
            cf.update(property=Property("elemField", "cf-asd", fld),
                      channelName=pv)
            logging.info("Done with %s" % pv)
Example #10
0
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")
Example #11
0
def addPvProperty(cf, pv, p, v, owner):
    if not hasProperty(cf, p):
        cf.set(property=Property(p, owner, v))
    cf.update(channelName=pv, property=Property(p, owner, v))
Example #12
0
def removePropertyPvs(cf, p, owner, pvs):
    logging.info("remove property '%s' from %d pvs '%s'" %
                 (p, len(pvs), str(pvs)))
    cf.delete(property=Property(p, owner), channelNames=pvs)
Example #13
0
 def testPreservingOfAttributes(self):
     '''
     This test is to ensure that existing properties and tags are left untouched.
     Case1:
     first time the cf-update comes across these channels and adds hostName and iocName
     Case2:
     the hostName is changed
     Case3:
     the iocName is changed
     Case4:
     both hostName and iocName are changed
     Case5:
     the channel is removed
     in all cases the existing unaffected* property and tag should remain with the channel               
     '''
     unaffectedProperty = Property('unaffectedProperty', self.owner, 'unchanged')
     unaffectedTag = Tag('unaffectedTag', self.owner)
     # create default client
     client = ChannelFinderClient(BaseURL=self.baseURL, username=self.username, password=self.password)
     client.set(property=unaffectedProperty)
     client.set(tag=unaffectedTag)
     
     client.set(channel=Channel('cf-update-pv1', 'cf-update', properties=[unaffectedProperty], tags=[unaffectedTag]))     
     client.set(channel=Channel('cf-update-pv2', 'cf-update', properties=[unaffectedProperty], tags=[unaffectedTag]))
     
     # Case1:
     hostName = 'initialHost'
     iocName = 'initialIoc'
     updateChannelFinder(['cf-update-pv1', 'cf-update-pv2'], \
                         hostName, \
                         iocName, \
                         owner=self.owner, \
                         time=time(), \
                         service=self.baseURL , \
                         username=self.username, \
                         password=self.password)
     channels = client.find(name='cf-update-pv*')
     for channel in channels:
         self.assertTrue(unaffectedProperty in channel.Properties and unaffectedTag in channel.Tags)
         self.assertTrue(channel.getProperties()['hostName'] == hostName and \
                         channel.getProperties()['iocName'] == iocName and \
                         channel.getProperties()['pvStatus'] == 'Active', \
                         'Failed to update channels with the correct hostName and/or iocName')
     # Case2:
     hostName = 'newHost'
     updateChannelFinder(['cf-update-pv1', 'cf-update-pv2'], \
                         hostName, \
                         iocName, \
                         owner=self.owner, \
                         time=time(), \
                         service=self.baseURL , \
                         username=self.username, \
                         password=self.password)
     channels = client.find(name='cf-update-pv*')
     for channel in channels:
         self.assertTrue(unaffectedProperty in channel.Properties and unaffectedTag in channel.Tags)
         self.assertTrue(channel.getProperties()['hostName'] == hostName and \
                         channel.getProperties()['iocName'] == iocName and \
                         channel.getProperties()['pvStatus'] == 'Active', \
                         'Failed to update channels with the correct hostName and/or iocName')
     self.assertTrue(client.find(property=[('hostName', 'initialHost')]) == None, 'Failed to cleanup old property')
     # Case 3:
     iocName = 'newIoc'
     updateChannelFinder(['cf-update-pv1', 'cf-update-pv2'], \
                         hostName, \
                         iocName, \
                         owner=self.owner, \
                         time=time(), \
                         service=self.baseURL , \
                         username=self.username, \
                         password=self.password)
     channels = client.find(name='cf-update-pv*')
     for channel in channels:
         self.assertTrue(unaffectedProperty in channel.Properties and unaffectedTag in channel.Tags)
         self.assertTrue(channel.getProperties()['hostName'] == hostName and \
                         channel.getProperties()['iocName'] == iocName and \
                         channel.getProperties()['pvStatus'] == 'Active', 'Failed to update channels with the correct hostName and/or iocName')
     self.assertTrue(client.find(property=[('hostName', 'initialHost')]) == None, 'Failed to cleanup old property')
     self.assertTrue(client.find(property=[('iocName', 'initialIoc')]) == None, 'Failed to cleanup old property')
     # Case 4:
     updateChannelFinder([], \
                         hostName, \
                         iocName, \
                         owner=self.owner, \
                         time=time(), \
                         service=self.baseURL , \
                         username=self.username, \
                         password=self.password)
     channels = client.find(name='cf-update-pv*')
     for channel in channels:
         self.assertTrue(unaffectedProperty in channel.Properties and unaffectedTag in channel.Tags)
         self.assertTrue(channel.getProperties()['hostName'] == hostName and \
                         channel.getProperties()['iocName'] == iocName and \
                         channel.getProperties()['pvStatus'] == 'InActive', \
                         'Failed to update channels with the correct hostName and/or iocName')
     self.assertTrue(client.find(property=[('hostName', 'initialHost')]) == None, 'Failed to cleanup old property')
     self.assertTrue(client.find(property=[('iocName', 'initialIoc')]) == None, 'Failed to cleanup old property')
     
     # Cleanup
     client.delete(channelName='cf-update-pv1')
     client.delete(channelName='cf-update-pv2')
     client.delete(propertyName=unaffectedProperty.Name)
     client.delete(tagName=unaffectedTag.Name)