Skip to content
Snippets Groups Projects
Commit 83091a58 authored by Stefano Di Frischia's avatar Stefano Di Frischia
Browse files

L2SS-528: remove duplicate code

parent f0b982cb
No related branches found
No related tags found
1 merge request!193Resolve L2SS-528 "Timescaledb defaults"
...@@ -42,6 +42,25 @@ def parse_device_name(device_name:str, tango_host:str = 'databaseds:10000'): ...@@ -42,6 +42,25 @@ def parse_device_name(device_name:str, tango_host:str = 'databaseds:10000'):
else: else:
raise ValueError(f'{device_name} is a wrong device name') 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(): class Archiver():
""" """
...@@ -528,10 +547,7 @@ class Retriever(): ...@@ -528,10 +547,7 @@ class Retriever():
""" """
Takes as input the fully-qualified name of a device and returns a list of its archived attributes Takes as input the fully-qualified name of a device and returns a list of its archived attributes
""" """
try: domain, family, member = split_tango_name(device_fqname,"device")
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")
attrs = self.session.query(self.ab.Attribute).filter(and_(self.ab.Attribute.domain == domain, self.ab.Attribute.family == family, \ 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() self.ab.Attribute.member == member)).all()
# Returns the representation as set in __repr__ method of the mapper class # Returns the representation as set in __repr__ method of the mapper class
...@@ -541,10 +557,7 @@ class Retriever(): ...@@ -541,10 +557,7 @@ class Retriever():
""" """
Takes as input the fully-qualified name of an attribute and returns its id. Takes as input the fully-qualified name of an attribute and returns its id.
""" """
try: domain, family, member, name = split_tango_name(attribute_fqname,"attribute")
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")
try: try:
result = self.session.query(self.ab.Attribute.att_conf_id).filter(and_(self.ab.Attribute.domain == domain, self.ab.Attribute.family == family, \ 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() self.ab.Attribute.member == member, self.ab.Attribute.name == name)).one()
...@@ -560,10 +573,7 @@ class Retriever(): ...@@ -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 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. as DB table name suffix in which values are stored.
""" """
try: domain, family, member, name = split_tango_name(attribute_fqname,"attribute")
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")
try: try:
if self.dbms=='mysql': 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).\ 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(): ...@@ -583,10 +593,7 @@ class Retriever():
Formats are basically three: Scalar, Spectrum and Image. Formats are basically three: Scalar, Spectrum and Image.
* Works only for POSTGRESQL * * Works only for POSTGRESQL *
""" """
try: domain, family, member, name = split_tango_name(attribute_fqname,"attribute")
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")
try: 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).\ 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() 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(): ...@@ -597,10 +604,7 @@ class Retriever():
raise Exception(f"No records of attribute {attribute_fqname} found in DB") from e raise Exception(f"No records of attribute {attribute_fqname} found in DB") from e
def get_attribute_tablename(self,attribute_fqname: str): def get_attribute_tablename(self,attribute_fqname: str):
try: domain, family, member, name = split_tango_name(attribute_fqname,"attribute")
[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")
try: try:
result = self.session.query(self.ab.Attribute.table_name).filter(and_(self.ab.Attribute.domain == domain, self.ab.Attribute.family == family, \ 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() self.ab.Attribute.member == member, self.ab.Attribute.name == name)).one()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment