MCPcopy Create free account
hub / github.com/pyfa-org/Pyfa / SettingsProvider

Class SettingsProvider

service/settings.py:39–86  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

37
38
39class SettingsProvider:
40 if config.savePath:
41 BASE_PATH = os.path.join(config.savePath, 'settings')
42 settings = {}
43 _instance = None
44
45 @classmethod
46 def getInstance(cls):
47 if cls._instance is None:
48 cls._instance = SettingsProvider()
49
50 return cls._instance
51
52 def __init__(self):
53 if hasattr(self, 'BASE_PATH'):
54 if not os.path.exists(self.BASE_PATH):
55 os.mkdir(self.BASE_PATH)
56
57 def getSettings(self, area, defaults=None):
58 # type: (basestring, dict) -> service.Settings
59 # NOTE: needed to change for tests
60 # TODO: Write to memory with mmap -> https://docs.python.org/2/library/mmap.html
61 settings_obj = self.settings.get(area)
62 if settings_obj is None: # and hasattr(self, 'BASE_PATH'):
63 canonical_path = os.path.join(self.BASE_PATH, area) if hasattr(self, 'BASE_PATH') else ""
64 if not os.path.exists(canonical_path): # path string or empty string.
65 info = {}
66 if defaults:
67 info.update(defaults)
68 else:
69 try:
70 with open(canonical_path, "rb") as f:
71 info = pickle.load(f)
72 for item in defaults:
73 if item not in info:
74 info[item] = defaults[item]
75 except (KeyboardInterrupt, SystemExit):
76 raise
77 except:
78 info = {}
79 info.update(defaults)
80
81 self.settings[area] = settings_obj = Settings(canonical_path, info)
82 return settings_obj
83
84 def saveAll(self):
85 for settings in self.settings.values():
86 settings.save()
87
88
89class Settings:

Callers 1

getInstanceMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected