Skip to content

Commit 96cc29c

Browse files
committed
Feature: new configuration readers from JSON files
1 parent 2cda1e9 commit 96cc29c

File tree

3 files changed

+121
-1
lines changed

3 files changed

+121
-1
lines changed
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
from .configuration_basic_reader import ConfigurationBasicReader # pylint: disable=cyclic-import
2+
from .configuration_json_reader import ConfigurationJSONReader # pylint: disable=cyclic-import
3+
from .uvls_json_reader import UVLSJSONReader # pylint: disable=cyclic
24

35

4-
__all__ = ["ConfigurationBasicReader"]
6+
__all__ = ['ConfigurationBasicReader',
7+
'ConfigurationJSONReader',
8+
'UVLSJSONReader']
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import json
2+
import pathlib
3+
4+
from flamapy.core.transformations.text_to_model import TextToModel
5+
6+
from flamapy.metamodels.configuration_metamodel.models.configuration import Configuration
7+
from flamapy.core.exceptions import ConfigurationNotFound
8+
9+
10+
class ConfigurationJSONReader(TextToModel):
11+
"""Reads a configuration from a JSON file or directory and transforms it into
12+
a Configuration model.
13+
14+
The JSON file should contain a dictionary where keys are element names (e.g., features)
15+
and values are the corresponding values for those elements.
16+
17+
If the path is a directory, it will read all JSON files in that directory and concatenate
18+
their configurations into a single Configuration object.
19+
This allows for managing multiple configurations associated with variability models split in
20+
several files (e.g., imports in UVL models).
21+
22+
If the JSON file or directory does not exist, it raises a ConfigurationNotFound exception.
23+
"""
24+
25+
@staticmethod
26+
def get_source_extension() -> str:
27+
return 'json'
28+
29+
def __init__(self, path: str) -> None:
30+
self._path: str = path
31+
32+
def transform(self) -> Configuration:
33+
path = pathlib.Path(self._path)
34+
if path.is_file():
35+
return self.get_configuration_from_json(path)
36+
elif path.is_dir():
37+
return self.get_configuration_from_directory(path)
38+
else:
39+
raise ConfigurationNotFound
40+
41+
def get_configuration_from_json(self, path: pathlib.Path) -> Configuration:
42+
"""Reads a JSON file and returns a Configuration object."""
43+
with open(path, 'r', encoding='utf-8') as file:
44+
json_dict = json.load(file)
45+
elements = json_dict
46+
return Configuration(elements)
47+
48+
def get_configuration_from_directory(self, directory: pathlib.Path) -> Configuration:
49+
"""Reads all JSON files in a directory and returns a Configuration object as
50+
result of concatenating all configurations."""
51+
elements = {}
52+
for filepath in directory.rglob('*.json'):
53+
if filepath.is_file():
54+
config = self.get_configuration_from_json(filepath)
55+
elements.update(config.elements)
56+
return Configuration(elements)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import json
2+
import pathlib
3+
4+
from flamapy.core.transformations.text_to_model import TextToModel
5+
6+
from flamapy.metamodels.configuration_metamodel.models.configuration import Configuration
7+
from flamapy.core.exceptions import ConfigurationNotFound
8+
9+
10+
class UVLSJSONReader(TextToModel):
11+
"""Reads a configuration generated with the UVLS tool from a JSON file or directory
12+
and transforms it into a Configuration model.
13+
14+
The JSON file should contain a dictionary where keys are element names (e.g., features)
15+
and values are the corresponding values for those elements.
16+
17+
The JSON file contains a 'file' and a 'config' key.
18+
The 'config' key will be used to extract the configuration elements.
19+
The JSON file can also contain metadata, which will be ignored in the transformation.
20+
21+
If the JSON file is a directory, it will read all JSON files in that directory and concatenate
22+
their configurations into a single Configuration object.
23+
This allows for managing multiple configurations associated with variability models split in
24+
several files (e.g., imports in UVL models).
25+
26+
If the JSON file or directory does not exist, it raises a ConfigurationNotFound exception.
27+
"""
28+
29+
@staticmethod
30+
def get_source_extension() -> str:
31+
return 'uvl.json'
32+
33+
def __init__(self, path: str) -> None:
34+
self._path: str = path
35+
36+
def transform(self) -> Configuration:
37+
path = pathlib.Path(self._path)
38+
if path.is_file():
39+
return self.get_configuration_from_json(path)
40+
elif path.is_dir():
41+
return self.get_configuration_from_directory(path)
42+
else:
43+
raise ConfigurationNotFound
44+
45+
def get_configuration_from_json(self, path: pathlib.Path) -> Configuration:
46+
"""Reads a JSON file and returns a Configuration object."""
47+
with open(path, 'r', encoding='utf-8') as file:
48+
json_dict = json.load(file)
49+
elements = json_dict['config']
50+
return Configuration(elements)
51+
52+
def get_configuration_from_directory(self, directory: pathlib.Path) -> Configuration:
53+
"""Reads all JSON files in a directory and returns a Configuration object as
54+
result of concatenating all configurations."""
55+
elements = {}
56+
for filepath in directory.rglob('*.json'):
57+
if filepath.is_file():
58+
config = self.get_configuration_from_json(filepath)
59+
elements.update(config.elements)
60+
return Configuration(elements)

0 commit comments

Comments
 (0)