mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
DRYer
This commit is contained in:
parent
869c2bf89e
commit
138a0338f7
@ -205,11 +205,14 @@ class DynamicConfig(dict):
|
|||||||
dict.__init__(self, {})
|
dict.__init__(self, {})
|
||||||
self.name = name
|
self.name = name
|
||||||
self.defaults = {}
|
self.defaults = {}
|
||||||
self.file_path = os.path.join(config_dir, name+'.pickle')
|
|
||||||
self.refresh()
|
self.refresh()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def file_path(self):
|
||||||
|
return os.path.join(config_dir, self.name+'.pickle')
|
||||||
|
|
||||||
def decouple(self, prefix):
|
def decouple(self, prefix):
|
||||||
self.file_path = os.path.join(os.path.dirname(self.file_path), prefix + os.path.basename(self.file_path))
|
self.name = prefix + self.name
|
||||||
self.refresh()
|
self.refresh()
|
||||||
|
|
||||||
def refresh(self, clear_current=True):
|
def refresh(self, clear_current=True):
|
||||||
@ -221,7 +224,7 @@ class DynamicConfig(dict):
|
|||||||
d = cPickle.loads(raw) if raw.strip() else {}
|
d = cPickle.loads(raw) if raw.strip() else {}
|
||||||
except SystemError:
|
except SystemError:
|
||||||
pass
|
pass
|
||||||
except:
|
except Exception:
|
||||||
print('WARNING: Failed to unpickle stored config object, ignoring')
|
print('WARNING: Failed to unpickle stored config object, ignoring')
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
import traceback
|
import traceback
|
||||||
@ -251,14 +254,15 @@ class DynamicConfig(dict):
|
|||||||
self.__setitem__(key, val)
|
self.__setitem__(key, val)
|
||||||
|
|
||||||
def commit(self):
|
def commit(self):
|
||||||
if hasattr(self, 'file_path') and self.file_path:
|
if not getattr(self, 'name', None):
|
||||||
if not os.path.exists(self.file_path):
|
return
|
||||||
make_config_dir()
|
if not os.path.exists(self.file_path):
|
||||||
with ExclusiveFile(self.file_path) as f:
|
make_config_dir()
|
||||||
raw = cPickle.dumps(self, -1)
|
raw = cPickle.dumps(self, -1)
|
||||||
f.seek(0)
|
with ExclusiveFile(self.file_path) as f:
|
||||||
f.truncate()
|
f.seek(0)
|
||||||
f.write(raw)
|
f.truncate()
|
||||||
|
f.write(raw)
|
||||||
|
|
||||||
|
|
||||||
dynamic = DynamicConfig()
|
dynamic = DynamicConfig()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user