def parse(self): src = '' migrate = False path = self.config_file_path if os.path.exists(path): with ExclusiveFile(path) as f: try: src = f.read().decode('utf-8') except ValueError: print("Failed to parse", path) traceback.print_exc() if not src: path = path.rpartition('.')[0] from calibre.utils.shared_file import share_open try: with share_open(path, 'rb') as f: src = f.read().decode('utf-8') except Exception: pass else: migrate = bool(src) ans = self.option_set.parse_string(src) if migrate: new_src = self.option_set.serialize(ans, ignore_unserializable=True) with ExclusiveFile(self.config_file_path) as f: f.seek(0), f.truncate() f.write(new_src) return ans
def refresh(self, clear_current=True): d = {} migrate = False if clear_current: self.clear() if os.path.exists(self.file_path): with ExclusiveFile(self.file_path) as f: raw = f.read() if raw: try: d = json_loads(raw) except Exception as err: print( 'Failed to de-serialize JSON representation of stored dynamic data for {} with error: {}' .format(self.name, err)) else: d = self.read_old_serialized_representation() migrate = bool(d) else: d = self.read_old_serialized_representation() migrate = bool(d) if migrate and d: raw = json_dumps(d, ignore_unserializable=True) with ExclusiveFile(self.file_path) as f: f.seek(0), f.truncate() f.write(raw) self.update(d)
def parse_config_file(path=DEFAULT_CONFIG): try: with ExclusiveFile(path) as f: raw = f.read().decode('utf-8') except EnvironmentError as err: if err.errno != errno.ENOENT: raise raw = '' ans = {} for line in raw.splitlines(): line = line.strip() if line.startswith('#'): continue key, rest = line.partition(' ')[::2] opt = options.get(key) if opt is None: continue val = rest if isinstance(opt.default, bool): val = val.lower() in ('true', 'yes', 'y') elif isinstance(opt.default, numbers.Number): try: val = type(opt.default)(rest) except Exception: raise ValueError('The value for %s: %s is not a valid number' % (key, rest)) elif opt.choices: if rest not in opt.choices: raise ValueError('The value for %s: %s is not valid' % (key, rest)) ans[key] = val return Options(**ans)
def save_defaults(name, recs): path = name_to_path(name) raw = str(recs) with open(path, 'wb'): pass with ExclusiveFile(path) as f: f.write(raw)
def save_defaults(name, recs): path = name_to_path(name) raw = recs.serialize() with lopen(path, 'wb'): pass with ExclusiveFile(path) as f: f.write(raw)
def run(self, path_to_ebook): ''' Se llama si on_import tiene el valor TRUE ''' print("GetFileType::run - ", path_to_ebook) import calibre_plugins.getfilename.prefs as prefs self.prefs = prefs.GetFileName_Prefs() fich = os.path.basename(self.original_path_to_file) fich_name = self.prefs.getTemporaryFile() print("GetFileType::run - Fich name: ", fich_name) with ExclusiveFile(fich_name) as file: try: print("GetFileType::run - Have file") dictio_aux = json.load(file) except Exception as e: print("GetFileType::run - error opening file:", e) dictio_aux = {} dictio_aux[fich] = self.original_path_to_file data = json.dumps(dictio_aux) if not isinstance(data, bytes): data = data.encode('utf-8') file.seek(0) file.write(data) file.truncate() print("GetFileType::run-fich") return path_to_ebook
def as_string(self): if not os.path.exists(self.config_file_path): return '' try: with ExclusiveFile(self.config_file_path) as f: return f.read().decode('utf-8') except LockError: raise IOError('Could not lock config file: %s'%self.config_file_path)
def path_to_dictionary(dictionary_name, cache_callback=None): cd = getattr(path_to_dictionary, 'cache_dir', None) or cache_dir() cache_path = get_cache_path(cd) with ExclusiveFile(os.path.join(cache_path, 'lock')): if not is_cache_up_to_date(cache_path): extract_dicts(cache_path) if cache_callback is not None: cache_callback() return os.path.join(cache_path, 'f', dictionary_name)
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)
def parse(self): src = '' if os.path.exists(self.config_file_path): with ExclusiveFile(self.config_file_path) as f: try: src = f.read().decode('utf-8') except ValueError: print "Failed to parse", self.config_file_path traceback.print_exc() return self.option_set.parse_string(src)
def commit(self): if not getattr(self, 'name', None): return if not os.path.exists(self.file_path): make_config_dir() raw = json_dumps(self) with ExclusiveFile(self.file_path) as f: f.seek(0) f.truncate() f.write(raw)
def load_defaults(name): path = name_to_path(name) if not os.path.exists(path): open(path, 'wb').close() with ExclusiveFile(path) as f: raw = f.read() r = GuiRecommendations() if raw: r.deserialize(raw) return r
def commit(self): if hasattr(self, 'file_path') and self.file_path: dpath = os.path.dirname(self.file_path) if not os.path.exists(dpath): os.makedirs(dpath, mode=CONFIG_DIR_MODE) with ExclusiveFile(self.file_path) as f: raw = self.to_raw() f.seek(0) f.truncate() f.write(raw)
def save_defaults(name, recs): path = name_to_path(name) raw = recs.serialize() os.makedirs(config_dir, exist_ok=True) with lopen(path, 'wb'): pass with ExclusiveFile(path) as f: f.write(raw)
def load_all_defaults(): ans = {} for x in os.listdir(config_dir): if x.endswith('.py'): path = os.path.join(config_dir, x) with ExclusiveFile(path) as f: raw = f.read() r = GuiRecommendations() if raw: r.deserialize(raw) ans[os.path.splitext(x)[0]] = r.copy() return ans
def write_scheduler_file(self): from calibre.utils.lock import ExclusiveFile self.root.text = '\n\n\t' for x in self.root: x.tail = '\n\n\t' if len(self.root) > 0: self.root[-1].tail = '\n\n' with ExclusiveFile(self.conf_path) as f: f.seek(0) f.truncate() f.write(etree.tostring(self.root, encoding='utf-8', xml_declaration=True, pretty_print=False))
def write_config_file(opts, path=DEFAULT_CONFIG): changed = {name:getattr(opts, name) for name in options if getattr(opts, name) != options[name].default} lines = [] for name in sorted(changed): o = options[name] lines.append('# ' + o.shortdoc) if o.longdoc: lines.append('# ' + o.longdoc) lines.append('%s %s' % (name, changed[name])) raw = '\n'.join(lines).encode('utf-8') with ExclusiveFile(path) as f: f.truncate() f.write(raw)
def test_exclusive_file_same_process(self): fname = 'testsp' with ExclusiveFile(fname): ef = FastFailEF(fname) self.assertRaises(EnvironmentError, ef.__enter__) t = Other() t.start(), t.join() self.assertIs(t.locked, False) if not iswindows: with unix_open(fname) as f: self.assertEqual( 1, fcntl.fcntl(f.fileno(), fcntl.F_GETFD) & fcntl.FD_CLOEXEC)
def set(self, name, val): if not self.option_set.has_option(name): raise ValueError('The option %s is not defined.' % name) if not os.path.exists(config_dir): make_config_dir() with ExclusiveFile(self.config_file_path) as f: src = f.read() opts = self.option_set.parse_string(src) setattr(opts, name, val) src = self.option_set.serialize(opts) f.seek(0) f.truncate() if isinstance(src, str): src = src.encode('utf-8') f.write(src)
def refresh(self): d = {} if os.path.exists(self.file_path): with ExclusiveFile(self.file_path) as f: raw = f.read() try: d = self.raw_to_object(raw) if raw.strip() else {} except SystemError: pass except: import traceback traceback.print_exc() d = {} self.clear() self.update(d)
def refresh(self): d = {} if os.path.exists(self.file_path): with ExclusiveFile(self.file_path) as f: raw = f.read() try: d = cPickle.loads(raw) if raw.strip() else {} except SystemError: pass except: import traceback print 'Failed to unpickle stored object:' traceback.print_exc() d = {} self.clear() self.update(d)
def __init__(self): from calibre.utils.config import config_dir from calibre.utils.lock import ExclusiveFile self.conf_path = os.path.join(config_dir, 'scheduler.xml') old_conf_path = os.path.join(config_dir, 'scheduler.pickle') self.root = E.recipe_collection() self.lock = RLock() if os.access(self.conf_path, os.R_OK): with ExclusiveFile(self.conf_path) as f: try: self.root = etree.fromstring(f.read()) except: print 'Failed to read recipe scheduler config' import traceback traceback.print_exc() elif os.path.exists(old_conf_path): self.migrate_old_conf(old_conf_path)
def refresh(self, clear_current=True): d = {} if os.path.exists(self.file_path): with ExclusiveFile(self.file_path) as f: raw = f.read() try: d = cPickle.loads(raw) if raw.strip() else {} except SystemError: pass except: print 'WARNING: Failed to unpickle stored config object, ignoring' if DEBUG: import traceback traceback.print_exc() d = {} if clear_current: self.clear() self.update(d)
def set(self, name, val): if not self.option_set.has_option(name): raise ValueError('The option %s is not defined.'%name) try: if not os.path.exists(config_dir): make_config_dir() with ExclusiveFile(self.config_file_path) as f: src = f.read() opts = self.option_set.parse_string(src) setattr(opts, name, val) footer = self.option_set.get_override_section(src) src = self.option_set.serialize(opts)+ '\n\n' + footer + '\n' f.seek(0) f.truncate() if isinstance(src, unicode): src = src.encode('utf-8') f.write(src) except LockError: raise IOError('Could not lock config file: %s'%self.config_file_path)
def run_other_ef_op(self, clean_exit): child = run_worker('calibre.utils.test_lock', 'other1') try: while child.poll() is None: if os.path.exists('ready'): break time.sleep(0.01) self.assertIsNone(child.poll(), 'child died without creating ready dir') ef = FastFailEF('test') self.assertRaises(EnvironmentError, ef.__enter__) if clean_exit: os.mkdir('quit') else: child.kill() self.assertIsNotNone(child.wait()) with ExclusiveFile('test', timeout=3): pass finally: if child.poll() is None: child.kill()
def main(path=None, title=None): # Ensure we can continue to function if GUI is closed os.environ.pop('CALIBRE_WORKER_TEMP_DIR', None) reset_base_dir() if iswindows: # Ensure that all instances are grouped together in the task bar. This # prevents them from being grouped with viewer/editor process when # launched from within calibre, as both use calibre-parallel.exe set_app_uid(TOC_DIALOG_APP_UID) with ExclusiveFile(path + '.lock') as wf: override = 'calibre-gui' if islinux else None app = Application([], override_program_name=override) d = TOCEditor(path, title=title) d.start() ret = 1 if d.exec_() == QDialog.DialogCode.Accepted: ret = 0 wf.write('{}'.format(ret).encode('ascii')) del d del app raise SystemExit(ret)
def other1(): e = ExclusiveFile('test') with e: os.mkdir('ready') while not os.path.exists('quit'): time.sleep(0.02)
def FastFailEF(name): return ExclusiveFile(name, sleep_time=0.01, timeout=0.05)
def cache_lock(): return ExclusiveFile(os.path.join(book_cache_dir(), 'metadata.json'))
def as_string(self): if not os.path.exists(self.config_file_path): return '' with ExclusiveFile(self.config_file_path) as f: return f.read().decode('utf-8')