2
2
3
3
import logging
4
4
import os
5
- from dataclasses import dataclass
5
+ from dataclasses import dataclass , field
6
6
from enum import Enum
7
7
from pathlib import Path
8
8
from typing import TYPE_CHECKING , cast
14
14
from arho_feature_template .qgis_plugin_tools .tools .resources import resources_path
15
15
16
16
if TYPE_CHECKING :
17
+ from numbers import Number
17
18
from typing import Literal
18
19
19
20
from qgis .core import QgsMapLayer
@@ -34,10 +35,11 @@ def __init__(self, message: str):
34
35
35
36
class UninitializedError (Exception ):
36
37
def __init__ (self ):
37
- super ().__init__ ("PlanRegulationsSet is not initialized. Call 'load_config ' first" )
38
+ super ().__init__ ("PlanRegulationsSet is not initialized. Call 'initialize ' first" )
38
39
39
40
40
41
class ValueType (Enum ):
42
+ DECIMAL = "desimaali"
41
43
POSITIVE_DECIMAL = "positiivinen desimaali"
42
44
POSITIVE_INTEGER = "positiivinen kokonaisluku"
43
45
POSITIVE_INTEGER_RANGE = "positiivinen kokonaisluku arvoväli"
@@ -50,6 +52,8 @@ class Unit(Enum):
50
52
EFFICIENCY_RATIO = "k-m2/m2"
51
53
PERCENTAGE = "prosentti"
52
54
AREA_RATIO = "m2/k-m2"
55
+ DEGREES = "°"
56
+ DECIBEL = "dB"
53
57
54
58
55
59
# TODO: Same as in PlanManager, should refactor
@@ -75,6 +79,7 @@ class PlanRegulationsSet:
75
79
76
80
version : str
77
81
regulations : list [PlanRegulationConfig ]
82
+ regulations_dict : dict [str , PlanRegulationConfig ] = field (default_factory = dict )
78
83
79
84
_instance : PlanRegulationsSet | None = None
80
85
@@ -87,9 +92,18 @@ def get_instance(cls) -> PlanRegulationsSet:
87
92
88
93
@classmethod
89
94
def get_regulations (cls ) -> list [PlanRegulationConfig ]:
90
- """Get the list of regulation configs, if instance is initialized."""
91
- instance = cls .get_instance ()
92
- return instance .regulations
95
+ """Get the list of top-level regulation configs, if instance is initialized."""
96
+ return cls .get_instance ().regulations
97
+
98
+ @classmethod
99
+ def get_regulations_dict (cls ) -> dict [str , PlanRegulationConfig ]:
100
+ """Get all regulations in a dictionary where keys are regulations codes and values PlanRegulationConfigs."""
101
+ return cls .get_instance ().regulations_dict
102
+
103
+ @classmethod
104
+ def get_regulation_by_code (cls , regulation_code : str ) -> PlanRegulationConfig | None :
105
+ """Get a regulation by it's regulation code (if exists)."""
106
+ return cls .get_instance ().regulations_dict .get (regulation_code )
93
107
94
108
@classmethod
95
109
def initialize (
@@ -103,12 +117,17 @@ def initialize(
103
117
data = yaml .safe_load (f )
104
118
cls ._instance = cls .from_dict (data )
105
119
106
- # Add names from plan regulation layer
120
+ regulations = cls .get_regulations ()
121
+ regulations_dict : dict [str , PlanRegulationConfig ] = {}
107
122
mapping = get_name_mapping_for_plan_regulations (type_of_plan_regulations_layer_name )
108
- if mapping :
109
- for regulation in cls .get_regulations ():
123
+
124
+ # Add names to dictionary, add names to regulations
125
+ for regulation in regulations :
126
+ regulation .add_to_dictionary (regulations_dict )
127
+ if mapping :
110
128
regulation .add_name (mapping , language )
111
129
130
+ cls ._instance .regulations_dict = regulations_dict
112
131
logger .info ("PlanRegulationsSet initialized successfully." )
113
132
return cls ._instance
114
133
@@ -156,3 +175,29 @@ def add_name(self, code_to_name_mapping: dict[str, dict[str, str]], language: Li
156
175
self .name = language_to_name_dict [language ] if language_to_name_dict else self .regulation_code
157
176
for regulation in self .child_regulations :
158
177
regulation .add_name (code_to_name_mapping , language )
178
+
179
+ def add_to_dictionary (self , dictionary : dict [str , PlanRegulationConfig ]):
180
+ dictionary [self .regulation_code ] = self
181
+ for regulation in self .child_regulations :
182
+ regulation .add_to_dictionary (dictionary )
183
+
184
+
185
+ @dataclass
186
+ class PlanRegulationDefinition :
187
+ """Associates a PlanRegulationConfig with an optional default value and additional data."""
188
+
189
+ regulation_config : PlanRegulationConfig
190
+ default_value : str | Number | None
191
+ additional_info : dict [str , str | Number | None ] # NOTE: Correct typing for additional information values?
192
+ regulation_number : int | None
193
+ attached_files : list [Path ]
194
+
195
+ @classmethod
196
+ def from_dict (cls , data : dict ) -> PlanRegulationDefinition :
197
+ return cls (
198
+ regulation_config = data ["config" ],
199
+ default_value = data .get ("default_value" ),
200
+ additional_info = data .get ("additional_info" , {}),
201
+ regulation_number = data .get ("regulation_number" ),
202
+ attached_files = data .get ("attached_files" , []),
203
+ )
0 commit comments