Skip to content
Extraits de code Groupes Projets
Valider 689e97a8 rédigé par Patrick Watrin's avatar Patrick Watrin
Parcourir les fichiers

config file loading and checking (work in progress)

parent 776b28f2
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -2,6 +2,8 @@ global:
debug: 1
verbose: 1
tempdir: "/tmp"
persistence: 1
virtualization: 1
......@@ -19,7 +21,8 @@ resources:
- /home/resources/media/fr/unitex/dictionary/toponyms.bin
# The 'options' section can contain any of the argument used by the unitex tools
# functions.
# functions. Note that some argument will be overriden to fit the 'tag' and 'extract'
# behaviour. For intance, there is not point to define a font or a context for
# 'concord'.
options:
match-mode: longest
output-mode: merge
match_mode: longest
......@@ -2,13 +2,14 @@
# -*- coding: utf-8 -*-
import os
import tempfile
import yaml
from unitex import UnitexException, LOGGER, DEFAULT_ENCODING
from unitex import UnitexException, LOGGER, DEFAULT_ENCODING, VERBOSE, DEBUG
class UnitexSettings(object):
class UnitexConfig(object):
def __init__(self):
self.__settings = None
......@@ -21,36 +22,63 @@ class UnitexSettings(object):
raise UnitexException("Key '%s' not found!" % key)
return self.__settings[key]
def set(self, key, value):
def __setitem__(self, key, value):
self.__settings[key] = value
def load(self, f):
with open(f, 'r') as config:
self.__settings = yaml.load(config)
self.check()
def check(self):
resources = self.__settings.get("resources", None)
if resources is None:
raise UnitexException("You must provide the 'resources' config element.")
language = resources.get("language", None)
def __load_global(self, options):
verbose = options.get("verbose", VERBOSE)
if verbose not in (0, 1, 2):
raise UnitexException("Wrong value for the 'verbose' global option.")
self.__settings["verbose"] = verbose
debug = options.get("debug", DEBUG)
if debug not in (0, 1):
raise UnitexException("Wrong value for the 'debug' global option.")
self.__settings["debug"] = debug
for handler in LOGGER.handlers:
if debug == 1:
fh.setLevel(logging.DEBUG)
elif verbose == 1:
fh.setLevel(logging.WARNING)
elif verbose == 2:
fh.setLevel(logging.INFO)
else:
fh.setLevel(logging.ERROR)
persistence = options.get("persistence", 0)
if persistence not in (0, 1)
raise UnitexException("Wrong value for the 'persistence' global option.")
self.__settings["persistence"] = bool(persistence)
virtualization = options.get("virtualization", 0)
if virtualization not in (0, 1)
raise UnitexException("Wrong value for the 'virtualization' global option.")
self.__settings["virtualization"] = bool(virtualization)
def __load_resources(self, options):
language = options.get("language", None)
if language is None:
raise UnitexException("The 'resources' section must contain the 'language' element.")
self.__settings["language"] = language
alphabet = resources.get("alphabet", None)
alphabet = options.get("alphabet", None)
if alphabet is None:
LOGGER.warning("No alphabet file provided.")
elif not os.path.exists(alphabet):
raise UnitexException("Alphabet file '%s' doesn't exist." % alphabet)
self.__settings["alphabet"] = alphabet
alphabet_sort = resources.get("alphabet-sort", None)
alphabet_sort = options.get("alphabet-sort", None)
if alphabet_sort is None:
LOGGER.warning("No sorted alphabet file provided.")
elif not os.path.exists(alphabet_sort):
raise UnitexException("Sorted alphabet file '%s' doesn't exist." % alphabet_sort)
self.__settings["alphabet-sort"] = alphabet_sort
sentence = resources.get("sentence", None)
sentence = options.get("sentence", None)
if sentence is None:
LOGGER.warning("No sentence grammar provided.")
else:
......@@ -60,7 +88,9 @@ class UnitexSettings(object):
if not os.path.exists(sentence):
raise UnitexException("Sentence grammar file '%s' doesn't exist." % sentence)
replace = resources.get("replace", None)
self.__settings["sentence"] = sentence
replace = options.get("replace", None)
if replace is None:
LOGGER.warning("No replace grammar provided.")
else:
......@@ -70,7 +100,9 @@ class UnitexSettings(object):
if not os.path.exists(replace):
raise UnitexException("Replace grammar file '%s' doesn't exist." % replace)
dictionaries = resources.get("dictionaries", None)
self.__settings["replace"] = replace
dictionaries = options.get("dictionaries", None)
if dictionaries is None:
LOGGER.warning("No dictionaries provided.")
else:
......@@ -85,6 +117,31 @@ class UnitexSettings(object):
if not os.path.exists("%s.bin" % prefix):
raise UnitexException("Dictionary .inf file missing for '%s'." % dictionary)
self.__settings["dictionaries"] = dictionaries
def __load_normalize_options(self, options):
pass
def load(self, path):
self.__settings = {}
settings = None
with open(path, 'r') as config:
settings = yaml.load(config)
if not settings:
return
if "global" in settings:
self.__load_global(settings["global"])
if "resources" not in settings:
raise UnitexException("You must provide the 'resources' config element.")
self.__load_resources(settings["resources"])
if "options" in settings:
pass
class UnitexProcessor(object):
......@@ -96,7 +153,7 @@ class UnitexProcessor(object):
self.reset(config)
def reset(self, config):
self.__settings = UnitexSettings()
self.__settings = UnitexConfig()
self.__settings.load(config)
def open(self, path, mode="srtlf", encoding=None, tagged=False, virtualize=False):
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter