-
Notifications
You must be signed in to change notification settings - Fork 0
/
configurations_attributes_reader.py
70 lines (57 loc) · 2.42 KB
/
configurations_attributes_reader.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
import csv
from typing import Any
from flamapy.core.transformations import TextToModel
from flamapy.metamodels.configuration_metamodel.models import Configuration
class ConfigurationsAttributesReader(TextToModel):
"""Read a list of configurations in a csv file.
The file format is as follows:
Configuration, Attribute1, Attribute2,..., AttributeN
"['Element1', 'Element2',..., 'ElementX']", value1, value2,..., valueN
"['Element1', 'Element2',..., 'ElementY']", value1, value2,..., valueY
"['Element1', 'Element2',..., 'ElementZ']", value1, value2,..., valueZ
...
Each list represents the selected elements in a configuration.
The 'Configuration' column does not need to be the first column in the file,
but the configuration column must be called 'Configuration'.
"""
@staticmethod
def get_source_extension() -> str:
return 'csv'
def __init__(self, path: str) -> None:
self.path = path
def transform(self) -> list[tuple[Configuration, dict[str, Any]]]:
with open(self.path, newline='', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile, delimiter=',', quotechar='"', skipinitialspace=True)
content_list = []
for row in reader:
content_list.append(from_csv_to_configurations(row))
return content_list
def from_csv_to_configurations(content: dict[str, str]) -> tuple[Configuration, dict[str, Any]]:
"""
Returns a tuple that consists in:
- the configuration.
- a dictionary (key -> value), where:
- key is the attribute name.
- value is the attribute value.
"""
attributes_dict = {}
elements = {element: True for element in eval(content['Configuration'])}
config = Configuration(elements)
for key, value in content.items():
if key != 'Configuration':
attributes_dict[key] = parse_value(value)
return config, attributes_dict
def parse_value(value: str) -> Any:
"""Given a value represented in a string, returns the associated value instance."""
result = None
try:
result = int(value)
except ValueError:
try:
result = float(value)
except ValueError:
if value.lower() in ['true', 'false']:
result = True if value.lower() == 'true' else False
else:
result = value
return result