diff --git a/tangostationcontrol/tangostationcontrol/toolkit/archiver.py b/tangostationcontrol/tangostationcontrol/toolkit/archiver.py index 3dc170b52e180a5ffc1d5354dc34169cad920f51..d56ec7d3c17ceab810ea6133f93e759f77dce957 100644 --- a/tangostationcontrol/tangostationcontrol/toolkit/archiver.py +++ b/tangostationcontrol/tangostationcontrol/toolkit/archiver.py @@ -41,7 +41,26 @@ def parse_device_name(device_name:str, tango_host:str = 'databaseds:10000'): return device_name else: raise ValueError(f'{device_name} is a wrong device name') - + +def split_tango_name(tango_fqname:str, tango_type:str): + """ + Helper function to split device or attribute Tango full qualified names + into its components + """ + if tango_type.lower()=='device': + try: + domain, family, member = tango_fqname.split('/') + return domain, family, member + except: + raise AttributeFormatException(f"Could not parse device name {tango_fqname}. Please provide FQDN, e.g. STAT/Device/1") + elif tango_type.lower()=='attribute': + try: + domain, family, member, name = tango_fqname.split('/') + return domain, family, member, name + except: + raise AttributeFormatException(f"Could not parse attribute name {tango_fqname}. Please provide FQDN, e.g. STAT/Device/1/Attribute") + else: + raise ValueError(f"Invalid value: {tango_type}. Please set 'device' or 'attribute'") class Archiver(): """ @@ -528,10 +547,7 @@ class Retriever(): """ Takes as input the fully-qualified name of a device and returns a list of its archived attributes """ - try: - domain, family, member = device_fqname.split('/') - except: - raise AttributeFormatException(f"Could not parse device name {device_fqname}. Please provide FQDN, e.g. STAT/Device/1") + domain, family, member = split_tango_name(device_fqname,"device") attrs = self.session.query(self.ab.Attribute).filter(and_(self.ab.Attribute.domain == domain, self.ab.Attribute.family == family, \ self.ab.Attribute.member == member)).all() # Returns the representation as set in __repr__ method of the mapper class @@ -541,10 +557,7 @@ class Retriever(): """ Takes as input the fully-qualified name of an attribute and returns its id. """ - try: - domain, family, member, name = attribute_fqname.split('/') - except: - raise AttributeFormatException(f"Could not parse attribute name {attribute_fqname}. Please provide FQDN, e.g. STAT/Device/1/Attribute") + domain, family, member, name = split_tango_name(attribute_fqname,"attribute") try: result = self.session.query(self.ab.Attribute.att_conf_id).filter(and_(self.ab.Attribute.domain == domain, self.ab.Attribute.family == family, \ self.ab.Attribute.member == member, self.ab.Attribute.name == name)).one() @@ -560,10 +573,7 @@ class Retriever(): Data Type name indicates the type (e.g. string, int, ...) and the read/write property. The name is used as DB table name suffix in which values are stored. """ - try: - domain, family, member, name = attribute_fqname.split('/') - except: - raise AttributeFormatException(f"Could not parse attribute name {attribute_fqname}. Please provide FQDN, e.g. STAT/Device/1/Attribute") + domain, family, member, name = split_tango_name(attribute_fqname,"attribute") try: if self.dbms=='mysql': result = self.session.query(self.ab.DataType.data_type).join(self.ab.Attribute,self.ab.Attribute.att_conf_data_type_id==self.ab.DataType.att_conf_data_type_id).\ @@ -583,10 +593,7 @@ class Retriever(): Formats are basically three: Scalar, Spectrum and Image. * Works only for POSTGRESQL * """ - try: - domain, family, member, name = attribute_fqname.split('/') - except: - raise AttributeFormatException(f"Could not parse attribute name {attribute_fqname}. Please provide FQDN, e.g. STAT/Device/1/Attribute") + domain, family, member, name = split_tango_name(attribute_fqname,"attribute") try: result = self.session.query(self.ab.Format.format).join(self.ab.Attribute,self.ab.Attribute.att_conf_format_id==self.ab.Format.att_conf_format_id).\ filter(and_(self.ab.Attribute.domain == domain, self.ab.Attribute.family == family, self.ab.Attribute.member == member, self.ab.Attribute.name == name)).one() @@ -597,10 +604,7 @@ class Retriever(): raise Exception(f"No records of attribute {attribute_fqname} found in DB") from e def get_attribute_tablename(self,attribute_fqname: str): - try: - [domain, family, member, name] = attribute_fqname.split('/') - except: - raise AttributeFormatException(f"Could not parse attribute name {attribute_fqname}. Please provide FQDN, e.g. STAT/Device/1/Attribute") + domain, family, member, name = split_tango_name(attribute_fqname,"attribute") try: result = self.session.query(self.ab.Attribute.table_name).filter(and_(self.ab.Attribute.domain == domain, self.ab.Attribute.family == family, \ self.ab.Attribute.member == member, self.ab.Attribute.name == name)).one()