@@ -14,30 +14,51 @@ class RuleManager:
14
14
def __init__ (self ):
15
15
self ._config : Dict [str , List [str ]] = {}
16
16
self ._header : List [str ] = []
17
+ self ._loadedRules = False
17
18
18
19
def _loadRuleFile (self , filename_base ):
19
- filename = f"{ filename_base } .rules"
20
- data_path = Path (ConfigManager .config ()["data_path" ])
21
- if not data_path :
22
- raise ValueError ("No editor directory path known" )
20
+ if not self ._loadedRules :
21
+ filename = f"{ filename_base } .rules"
22
+ data_path = Path (ConfigManager .config ()["data_path" ])
23
+ if not data_path :
24
+ raise ValueError ("No editor directory path known" )
23
25
24
- automapper_path = data_path .joinpath (Path ("editor/automap" ))
25
- if not automapper_path .exists ():
26
- automapper_path .mkdir ()
26
+ automapper_path = data_path .joinpath (Path ("editor/automap" ))
27
+ if not automapper_path .exists ():
28
+ automapper_path .mkdir ()
27
29
28
- full_file_path = automapper_path .joinpath (Path (filename ))
30
+ full_file_path = automapper_path .joinpath (Path (filename ))
29
31
30
- # load file if it exists
31
- if full_file_path .is_file ():
32
- self ._readRuleFile (full_file_path )
32
+ # load file if it exists
33
+ if full_file_path .is_file ():
34
+ self ._readRuleFile (full_file_path )
33
35
34
- # file doesn't exist, just to be explicit
35
- else :
36
- self ._config = {}
37
- self ._header = []
36
+ # file doesn't exist, just to be explicit
37
+ else :
38
+ self ._config = {}
39
+ self ._header = []
40
+ self ._loadedRules = True
41
+
42
+ def loadRules (self , filename ):
43
+ if not self ._loadedRules :
44
+ filename = RuleManager ._getFileBase (filename )
45
+ self ._loadRuleFile (filename )
38
46
39
47
def saveRule (self , filename , rule_name ):
48
+ filename = RuleManager ._getFileBase (filename )
49
+ if not self ._loadedRules :
50
+ self ._loadRuleFile (filename )
51
+ self ._config [rule_name ] = [] # overwrite rules
52
+ self ._config [rule_name ] = RuleManager ._createRulesFromTileHandler ()
53
+ self ._writeRuleFile (filename )
40
54
55
+ def getRules (self ) -> List [str ]:
56
+ if not self ._loadedRules :
57
+ raise ValueError ("Rules are not loaded yet" )
58
+ return list (self ._config .keys ())
59
+
60
+ @staticmethod
61
+ def _getFileBase (filename ):
41
62
# remove mime type and check it if exists
42
63
splits = filename .split ("." )
43
64
if len (splits ) > 2 :
@@ -46,11 +67,7 @@ def saveRule(self, filename, rule_name):
46
67
if splits [1 ] != "rules" :
47
68
raise ValueError (f"Unknown rule mime type '{ splits [1 ]} '" )
48
69
filename = splits [0 ]
49
-
50
- self ._loadRuleFile (filename )
51
- self ._config [rule_name ] = [] # overwrite rules
52
- self ._config [rule_name ] = RuleManager ._createRulesFromTileHandler ()
53
- self ._writeRuleFile (filename )
70
+ return filename
54
71
55
72
@staticmethod
56
73
def _createRulesFromTileHandler ():
0 commit comments