-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectory_Operations.py
111 lines (87 loc) · 3.49 KB
/
Directory_Operations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 1. read in directory listing
# 2. create lists of *.conf and *.comp files from directory
# 3. for each file, parse XML for required tag and add value to list
# 4. remove duplicates from list
import xml.etree.ElementTree as ET
from fnmatch import fnmatch
from os import listdir
class ReadConfigFiles:
"""
Reads SECI configuration files and extracts VI names
"""
def __init__(self, search_path):
"""
create lists for filenames and initialise to empty lists
call methods for reading directory contents and searching for config filenames
:param search_path: the search path for files
"""
self.full_path = search_path
self.directory_contents = listdir(self.full_path)
self.config_filenames = []
self.comp_filenames = []
self.vi_list = []
self._extract_config_filenames()
def _extract_config_filenames(self):
"""
extract SECI config (*.conf) & component (*.comp) filenames
add to separate lists
"""
for filename in self.directory_contents:
if fnmatch(filename, "*.conf"):
self.config_filenames.append(filename)
elif fnmatch(filename, "*.comp"):
self.comp_filenames.append(filename)
def _parse_vis_from_files(self, filenames):
"""
extracts VI names and paths from config files
:param filenames: list of filenames to process
:return: vis_in_files: list of VIs with fullpaths
"""
vis_in_files = []
for filename in filenames:
filename_and_path = self.full_path + filename
vis_in_file = self._parse_file(filename_and_path)
vis_in_files.extend(vis_in_file)
return vis_in_files
@staticmethod
def _parse_file(filename_and_path):
"""
reads XML file and creates list of values for "FilePath" tag
:param filename_and_path: absolute path of file to be parsed
:return: vis_in_file: list of VIs in file
"""
vis_in_file = []
tree = ET.parse(filename_and_path)
root = tree.getroot()
for child in root.iter("FilePath"):
vi_path = ET.tostring(child, encoding=None, method="text")
vis_in_file.append(vi_path.strip())
return vis_in_file
@staticmethod
def _remove_duplicates(input_list):
"""
remove duplicates from list, then sort alphabetically
:param input_list: input list
:return: output_list: original list with duplicates removed
"""
# maintain list of "encountered" items in dummy list
# if item not in this list, add item to it and to output list
output_list = list()
encountered = set()
for item in input_list:
item = str(item, "UTF-8")
if item not in encountered:
output_list.append(item)
encountered.add(item)
# sort alphabetically
output_list.sort()
return output_list
def analyse_config_files(self):
"""
call methods to process files
:return: config_vis: list containing absolute paths of VIs within SECI config file
:return: comp_vis: list containing absolute paths of VIs within SECI sub-config file
"""
config_vis = self._remove_duplicates(self._parse_vis_from_files(self.config_filenames))
comp_vis = self._remove_duplicates(self._parse_vis_from_files(self.comp_filenames))
return config_vis, comp_vis