@@ -14,30 +14,51 @@ class RuleManager:
1414 def __init__ (self ):
1515 self ._config : Dict [str , List [str ]] = {}
1616 self ._header : List [str ] = []
17+ self ._loadedRules = False
1718
1819 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" )
2325
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 ()
2729
28- full_file_path = automapper_path .joinpath (Path (filename ))
30+ full_file_path = automapper_path .joinpath (Path (filename ))
2931
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 )
3335
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 )
3846
3947 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 )
4054
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 ):
4162 # remove mime type and check it if exists
4263 splits = filename .split ("." )
4364 if len (splits ) > 2 :
@@ -46,11 +67,7 @@ def saveRule(self, filename, rule_name):
4667 if splits [1 ] != "rules" :
4768 raise ValueError (f"Unknown rule mime type '{ splits [1 ]} '" )
4869 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
5471
5572 @staticmethod
5673 def _createRulesFromTileHandler ():
0 commit comments