Пример #1
0
 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
Пример #2
0
    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)
Пример #3
0
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)
Пример #4
0
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)
Пример #5
0
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)
Пример #6
0
    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
Пример #7
0
 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)
Пример #8
0
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)
Пример #9
0
 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)
Пример #10
0
 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)
Пример #11
0
 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)
Пример #12
0
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
Пример #13
0
 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)
Пример #14
0
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)
Пример #15
0
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
Пример #16
0
 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))
Пример #17
0
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)
Пример #18
0
 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)
Пример #19
0
 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)
Пример #20
0
 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)
Пример #21
0
 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)
Пример #22
0
 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)
Пример #23
0
 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)
Пример #24
0
 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)
Пример #25
0
 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()
Пример #26
0
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)
Пример #27
0
def other1():
    e = ExclusiveFile('test')
    with e:
        os.mkdir('ready')
        while not os.path.exists('quit'):
            time.sleep(0.02)
Пример #28
0
def FastFailEF(name):
    return ExclusiveFile(name, sleep_time=0.01, timeout=0.05)
Пример #29
0
def cache_lock():
    return ExclusiveFile(os.path.join(book_cache_dir(), 'metadata.json'))
Пример #30
0
 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')