Esempio n. 1
0
 def load(self):
     timestamp = None
     try:
         timestamp = resource_timestamp(self.filename)
         self.result = load_dictionary(self.filename)
     except Exception as e:
         log.debug('loading dictionary %s failed', self.filename, exc_info=True)
         self.result = DictionaryLoaderException(self.filename, e)
         self.result.timestamp = timestamp
Esempio n. 2
0
def _get_dictionary_module(filename):
    extension = splitext(filename)[1].lower()
    try:
        dictionary_module = dictionaries[extension]
    except KeyError:
        raise DictionaryLoaderException(
            'Unsupported extension: %s. Supported extensions: %s' %
            (extension, ', '.join(sorted(dictionaries.keys()))))
    return dictionary_module
Esempio n. 3
0
def _get_dictionary_module(filename):
    extension = splitext(filename)[1].lower()[1:]
    try:
        entrypoint = registry.get_plugin('dictionary', extension)
    except KeyError:
        raise DictionaryLoaderException(
            'Unsupported extension: %s. Supported extensions: %s' %
            (extension, ', '.join(sorted(
                registry.list_plugins('dictionary')))))
    return entrypoint.resolve()
Esempio n. 4
0
def load_dictionary(data):
    """Load a json dictionary from a string."""
    def h(pairs):
        return StenoDictionary((normalize_steno(x[0]), x[1]) for x in pairs)

    try:
        try:
            return json.loads(data, object_pairs_hook=h)
        except UnicodeDecodeError:
            return json.loads(data, 'latin-1', object_pairs_hook=h)
    except ValueError:
        raise DictionaryLoaderException('Dictionary is not valid json.')
Esempio n. 5
0
def load_dictionary(filename):
    """Load a dictionary from a file."""
    extension = splitext(filename)[1].lower()

    try:
        dict_type = dictionaries[extension]
    except KeyError:
        raise DictionaryLoaderException(
            'Cette extension n\'est pas supportée pour le dictionnaire: %s. Extensions supportées: %s'
            % (extension, ', '.join(dictionaries.keys())))

    loader = dict_type.load_dictionary

    try:
        with open(filename, 'rb') as f:
            d = loader(f.read())
    except IOError as e:
        raise DictionaryLoaderException(unicode(e))

    d.save = ThreadedSaver(d, filename, dict_type.save_dictionary)
    return d
Esempio n. 6
0
def load_dictionary(filename):
    """Load a dictionary from a file."""
    extension = splitext(filename)[1].lower()

    try:
        dict_type = dictionaries[extension]
    except KeyError:
        raise DictionaryLoaderException(
            'Unsupported extension %s. Supported extensions: %s',
            (extension, ', '.join(loaders.keys())))

    loader = dict_type.load_dictionary

    try:
        with open(filename, 'rb') as f:
            d = loader(f.read())
    except IOError as e:
        raise DictionaryLoaderException(unicode(e))

    d.save = ThreadedSaver(d, filename, dict_type.save_dictionary)
    return d
Esempio n. 7
0
def create_dictionary(filename):
    '''Create a new dictionary.

    The format is inferred from the extension.

    Note: the file is not created! The resulting dictionary save
    method must be called to finalize the creation on disk.
    '''
    dictionary_module = _get_dictionary_module(filename)
    if dictionary_module.create_dictionary is None:
        raise DictionaryLoaderException('%s does not support creation' %
                                        dictionary_module.__name__)
    try:
        d = dictionary_module.create_dictionary()
    except Exception as e:
        ne = DictionaryLoaderException('creating %s failed: %s' %
                                       (filename, str(e)))
        reraise(type(ne), ne, sys.exc_info()[2])
    d.set_path(filename)
    d.save = ThreadedSaver(d, filename, dictionary_module.save_dictionary)
    return d
Esempio n. 8
0
def load_dictionary(fp):
    """Load a json dictionary from a string."""
    def h(pairs):
        return StenoDictionary((normalize_steno(x[0]), x[1]) for x in pairs)

    data = fp.read()
    try:
        try:
            return json.loads(data, object_pairs_hook=h)
        except UnicodeDecodeError:
            return json.loads(data, 'latin-1', object_pairs_hook=h)
    except ValueError as e:
        raise DictionaryLoaderException('\'%s\' is not valid json: %s' %
                                        (fp.name, str(e)))
Esempio n. 9
0
def load_dictionary(filename):
    '''Load a dictionary from a file.

    The format is inferred from the extension.
    '''
    dictionary_module = _get_dictionary_module(filename)
    try:
        d = dictionary_module.load_dictionary(filename)
    except Exception as e:
        ne = DictionaryLoaderException('loading \'%s\' failed: %s' %
                                       (filename, str(e)))
        reraise(type(ne), ne, sys.exc_info()[2])
    d.set_path(filename)
    d.save = ThreadedSaver(d, filename, dictionary_module.save_dictionary)
    return d