mirror of
git://github.com/kovidgoyal/calibre.git
synced 2026-01-04 03:44:40 +01:00
DRYer
This commit is contained in:
parent
869c2bf89e
commit
138a0338f7
1 changed files with 15 additions and 11 deletions
|
|
@ -205,11 +205,14 @@ def __init__(self, name='dynamic'):
|
|||
dict.__init__(self, {})
|
||||
self.name = name
|
||||
self.defaults = {}
|
||||
self.file_path = os.path.join(config_dir, name+'.pickle')
|
||||
self.refresh()
|
||||
|
||||
@property
|
||||
def file_path(self):
|
||||
return os.path.join(config_dir, self.name+'.pickle')
|
||||
|
||||
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()
|
||||
|
||||
def refresh(self, clear_current=True):
|
||||
|
|
@ -221,7 +224,7 @@ def refresh(self, clear_current=True):
|
|||
d = cPickle.loads(raw) if raw.strip() else {}
|
||||
except SystemError:
|
||||
pass
|
||||
except:
|
||||
except Exception:
|
||||
print('WARNING: Failed to unpickle stored config object, ignoring')
|
||||
if DEBUG:
|
||||
import traceback
|
||||
|
|
@ -251,14 +254,15 @@ def set(self, key, val):
|
|||
self.__setitem__(key, val)
|
||||
|
||||
def commit(self):
|
||||
if hasattr(self, 'file_path') and self.file_path:
|
||||
if not os.path.exists(self.file_path):
|
||||
make_config_dir()
|
||||
with ExclusiveFile(self.file_path) as f:
|
||||
raw = cPickle.dumps(self, -1)
|
||||
f.seek(0)
|
||||
f.truncate()
|
||||
f.write(raw)
|
||||
if not getattr(self, 'name', None):
|
||||
return
|
||||
if not os.path.exists(self.file_path):
|
||||
make_config_dir()
|
||||
raw = cPickle.dumps(self, -1)
|
||||
with ExclusiveFile(self.file_path) as f:
|
||||
f.seek(0)
|
||||
f.truncate()
|
||||
f.write(raw)
|
||||
|
||||
|
||||
dynamic = DynamicConfig()
|
||||
|
|
|
|||
Loading…
Reference in a new issue