Skip to content
Snippets Groups Projects
Commit 34e18224 authored by Mattia Mancini's avatar Mattia Mancini
Browse files

SSB-47: Refactor read method

parent 03852c5d
No related branches found
No related tags found
1 merge request!44Merge back holography to master
......@@ -113,24 +113,22 @@ def list_bsf_files_in_path(path):
matched_path_list = glob(bsf_files_path_pattern)
matched_file_path_list = list(
filter(lambda path_i: os.path.isfile(path_i), matched_path_list))
matched_file_name_list = list(map(lambda path_i: os.path.basename(path_i),
matched_file_path_list))
holography_beam_specification = create_hs_list_from_name_list_and_path(
matched_file_name_list, path)
holography_beam_specification = create_hs_list_from_name_list_and_path(matched_file_path_list)
logger.info("Loading holography beam specifications were successfully loaded from \"%s\".",
path)
return holography_beam_specification
def create_hs_list_from_name_list_and_path(name_list, path):
return [HolographySpecification(name, path) for name in name_list]
def create_hs_list_from_name_list_and_path(path_list):
return [HolographySpecification.read_file(path) for path in path_list]
def is_holography_specification_file_name(name):
return re.match(HolographySpecification.hs_name_pattern, name) is not None
def extract_id_date_comment_from_name(name):
def extract_id_date_comment_from_filename(name):
match = re.match(HolographySpecification.hs_name_pattern, name)
date = match.group('date')
hs_id = int(match.group('id'))
......@@ -229,18 +227,16 @@ class HolographySpecification(object):
"""
hs_name_pattern = r'Holog-(?P<date>\d{8})-(?P<comment>.*)-(?P<id>\d{3}).txt'
def __init__(self, filename, path):
def __init__(self, path):
"""
Construct the holography specification object and extracts id date and comment from
the file name (see HolographySpecification.extract_id_date_comment_from_name)
:param filename: file name of the holography specification
:type filename: str
:param path: path of the holography specification file
:type path: str
"""
self.path = os.path.join(path, filename)
self.name = filename
self.id, self.date, self.comment = extract_id_date_comment_from_name(filename)
self.path = path
self.name = os.path.basename(path)
self.id, self.date, self.comment = extract_id_date_comment_from_filename(self.name)
self.reference_station_names = None
self.target_station_names = None
self.station_specification_map = defaultdict(list)
......@@ -268,13 +264,17 @@ class HolographySpecification(object):
"""
return self.station_specification_map[station_name]
def read_file(self):
logger.debug("Reading holography file \"%s\"...", self.path)
lines = self._read_lines()
self._parse_header(lines[0])
self._parse_lines(lines[1:])
self._update_class_attributes()
logger.debug("Reading holography file \"%s\" done.", self.path)
@staticmethod
def read_file(path):
spec = HolographySpecification(path)
logger.debug("Reading holography file \"%s\"...", spec.path)
lines = spec._read_lines()
spec._parse_header(lines[0])
spec._parse_lines(lines[1:])
spec._update_class_attributes()
logger.debug("Reading holography file \"%s\" done.", spec.path)
return spec
def _read_lines(self):
with open(self.path, 'r') as fstream_in:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment