|
6 | 6 | import dataset |
7 | 7 |
|
8 | 8 |
|
9 | | -'''___Authorship___''' |
| 9 | +"""___Authorship___""" |
10 | 10 | __author__ = "Sam Hunt" |
11 | 11 | __created__ = "3/8/2020" |
12 | 12 | __version__ = __version__ |
13 | 13 | __maintainer__ = "Sam Hunt" |
14 | 14 | __email__ = "sam.hunt@npl.co.uk" |
15 | 15 | __status__ = "Development" |
16 | 16 |
|
| 17 | +PROCESSOR_CONFIG_PROTECTED_VALUES = [] |
| 18 | + |
17 | 19 |
|
18 | 20 | class Context: |
19 | 21 | """ |
20 | 22 | Class to determine and store processor state |
| 23 | +
|
| 24 | + :type processor_config: configparser.RawConfigParser |
| 25 | + :param processor_config: processor config data object |
| 26 | +
|
| 27 | + :type job_config: configparser.RawConfigParser |
| 28 | + :param job_config: job config data object |
21 | 29 | """ |
22 | 30 |
|
23 | | - def __init__(self, job_config=None, processor_config=None, logger=None): |
| 31 | + def __init__(self, processor_config=None, job_config=None, logger=None): |
24 | 32 |
|
25 | 33 | # Initialise attributes |
26 | | - self.logger = None |
| 34 | + self.config_values = {} |
| 35 | + self.logger = logger |
27 | 36 | self.metadata_db = None |
28 | 37 | self.anomoly_db = None |
29 | 38 |
|
30 | | - # File attributes |
31 | | - self.time = None |
32 | | - |
33 | | - # Processor Attributes |
34 | | - # Processor |
35 | | - self.version = None |
| 39 | + # Unpack processor_config to set relevant attributes |
| 40 | + if processor_config is not None: |
| 41 | + self.unpack_config(processor_config) |
36 | 42 |
|
37 | | - # Input |
38 | | - self.metadata_db_url = None |
| 43 | + # Unpack processor_config to set relevant attributes |
| 44 | + if job_config is not None: |
| 45 | + self.unpack_config( |
| 46 | + job_config, protected_values=PROCESSOR_CONFIG_PROTECTED_VALUES |
| 47 | + ) |
39 | 48 |
|
40 | | - # Output |
41 | | - self.raw_data_directory = None |
| 49 | + # Connect to metadata database |
| 50 | + if "metadata_db_url" in self.get_config_names(): |
| 51 | + self.metadata_db = dataset.connect(self.get_config_value("metadata_db_url")) |
42 | 52 |
|
43 | | - # Job attributes |
44 | | - # Info |
45 | | - self.network = None |
46 | | - self.site = None |
| 53 | + # Connect to anomoly database |
| 54 | + if "anomoly_db_url" in self.get_config_names(): |
| 55 | + self.anomoly_db = dataset.connect(self.get_config_value("anomoly_db_url")) |
47 | 56 |
|
48 | | - # Input |
49 | | - self.raw_data_directory = None |
50 | | - self.anomoly_db_url = None |
| 57 | + def unpack_config(self, config, protected_values=None): |
| 58 | + """ |
| 59 | + Unpacks config data, sets relevant entries to values instance attribute |
51 | 60 |
|
52 | | - # Processing Options |
53 | | - self.l1_mf_name = None |
54 | | - self.l2_mf_name = None |
55 | | - self.write_l1a = None |
| 61 | + :type config: configparser.RawConfigParser |
| 62 | + :param config: config data |
| 63 | + """ |
56 | 64 |
|
57 | | - # Set logger attribute |
58 | | - if logger is not None: |
59 | | - self.logger = logger |
| 65 | + protected_values = [] if protected_values is None else protected_values |
60 | 66 |
|
61 | | - # Unpack job_config to set relevant attributes |
62 | | - if job_config is not None: |
63 | | - self._unpack_job_config(job_config) |
| 67 | + for section in config.sections(): |
| 68 | + for name in config[section].keys(): |
64 | 69 |
|
65 | | - # Connect to anomoly database |
66 | | - self.anomoly_db = dataset.connect(self.anomoly_db_url) |
| 70 | + if name not in protected_values: |
| 71 | + value = get_config_value(config, section, name) |
| 72 | + self.set_config_value(name, value) |
67 | 73 |
|
68 | | - # Unpack processor_config to set relevant attributes |
69 | | - if processor_config is not None: |
70 | | - self._unpack_processor_config(processor_config) |
| 74 | + def set_config_value(self, name, value): |
| 75 | + """ |
| 76 | + Sets config data to values instance attribute |
71 | 77 |
|
72 | | - # Connect to metadata database |
73 | | - self.metadata_db = dataset.connect(self.metadata_db_url) |
| 78 | + :type name: str |
| 79 | + :param name: config data name |
74 | 80 |
|
75 | | - def _unpack_job_config(self, job_config): |
| 81 | + :param value: config data value |
76 | 82 | """ |
77 | | - Unpacks processor_config, sets relevant object attributes |
78 | 83 |
|
79 | | - :type job_config: configparser.RawConfigParser |
80 | | - :param job_config: job configuration information |
| 84 | + self.config_values[name] = value |
81 | 85 |
|
| 86 | + def get_config_value(self, name): |
82 | 87 | """ |
| 88 | + Get config value |
83 | 89 |
|
84 | | - # Unpack Info |
85 | | - self.network = get_config_value(job_config, "Info", "network") |
86 | | - self.site = get_config_value(job_config, "Info", "site") |
| 90 | + :type name: str |
| 91 | + :param name: config data name |
87 | 92 |
|
88 | | - # Unpack Input |
89 | | - self.raw_data_directory = get_config_value(job_config, "Input", "raw_data_directory") |
90 | | - |
91 | | - # Unpack Output |
92 | | - self.anomoly_db_url = get_config_value(job_config, "Output", "anomoly_db_url") |
| 93 | + :return: config value |
| 94 | + """ |
93 | 95 |
|
94 | | - # Unpack processing options |
95 | | - self.l1_mf_name = get_config_value(job_config, "Processing Options", "measurement_function_name") |
96 | | - self.l2_mf_name = get_config_value(job_config, "Processing Options", "reflectance_protocol_name") |
97 | | - self.write_l1a = get_config_value(job_config, "Processing Options", "write_l1a", dtype=bool) |
| 96 | + return self.config_values[name] if name in self.get_config_names() else None |
98 | 97 |
|
99 | | - def _unpack_processor_config(self, processor_config): |
| 98 | + def get_config_names(self): |
100 | 99 | """ |
101 | | - Unpacks processor_config, sets relevant object attributes |
| 100 | + Get available config value names |
102 | 101 |
|
103 | | - :type processor_config: configparser.RawConfigParser |
104 | | - :param processor_config: processor configuration information |
| 102 | + :return: config value names |
| 103 | + :rtype: list |
105 | 104 | """ |
106 | 105 |
|
107 | | - self.version = get_config_value(processor_config, "Processor", "version") |
108 | | - self.metadata_db_url = get_config_value(processor_config, "Input", "metadata_db_url") |
109 | | - self.archive_directory = get_config_value(processor_config, "Output", "archive_directory") |
| 106 | + return list(self.config_values.keys()) |
110 | 107 |
|
111 | 108 |
|
112 | | -if __name__ == '__main__': |
| 109 | +if __name__ == "__main__": |
113 | 110 | pass |
0 commit comments