コード例 #1
0
def read_config():
    """Read Pharmaship configuration file.

    Read the configuration from yaml source file, validate it against
    a schema and then convert it to Munch instance.

    :return: validated config Munch instance or False
    :rtype: Munch or bool

    :Example:

    >>> read_config()
    """
    filename = settings.PHARMASHIP_CONF

    try:
        with open(filename, "r") as fdesc:
            content = fdesc.read()
    except IOError as error:
        log.exception("Config file not readable: %s. %s", filename, error)
        return False

    raw_config = load_config(content, "config.json")
    if not raw_config:
        return False

    # Transform dict in pseudo namedtuple recursively
    config = Munch.fromDict(raw_config)

    return config
コード例 #2
0
def write_config(data):
    """Write Pharmaship configuration file.

    :param data: Data to write into the configuration file.
    :type data: Munch instance

    :return: True is written with success.
    :rtype: bool

    :Example:

    >>> write_config(my_data)
    """
    filename = settings.PHARMASHIP_CONF

    try:
        dict_data = data.toDict()
    except AttributeError as error:
        log.exception("Data is not a Munch instance: %s", error)
        return False

    content = yaml.dump(dict_data, Dumper=Dumper, indent=2)

    try:
        with open(filename, "w") as fdesc:
            fdesc.write(content)
    except IOError as error:
        log.exception("Config file not writable: %s. %s", filename, error)
        return False

    return True
コード例 #3
0
def set_langage(lang_code: str):
    """Application wide language switcher.

    Set Django Translation (gettext) and C library `locale` for Glade/Gtk
    files.

    :param: lang_code: (string) language code
    """
    locale_str = None

    if platform.system() == "Windows":
        if lang_code == "fr":
            locale_str = "french"
        else:
            locale_str = "english"
    else:
        locale_str = (lang_code, "utf8")

    try:
        locale.setlocale(locale.LC_ALL, locale_str)
    except locale.Error:
        log.exception(
            "Locale (%s) non installed on the operating system. Switching to 'C' locale.",
            locale_str)
        locale.setlocale(locale.LC_ALL, 'C')
        translation.activate('en')
        return None

    # Activate Django translation language
    translation.activate(lang_code)

    return None
コード例 #4
0
    def test_load_config(self):
        """Check configuration file loading and parsing."""
        # Invalid YAML
        content = "inval:\nid{yaml},"
        schema = "dummy_schema"
        with self.assertLogs(log, level='ERROR') as cm:
            self.assertFalse(config.load_config(content, schema))
        for item in cm.output:
            self.assertIn("Unable to parse the config file.", item)

        # Invalid schema filename
        content = "dummy_content"
        schema = "dummy_schema"
        with self.assertLogs(log, level='ERROR') as cm:
            self.assertFalse(config.load_config(content, schema))
        for item in cm.output:
            self.assertIn("Unable to read the schema file.", item)

        # Invalid schema content
        content = "dummy_content"
        schema = Path("config_bad.json")
        with self.assertLogs(log, level='ERROR') as cm:
            self.assertFalse(config.load_config(content, schema))
        for item in cm.output:
            self.assertIn("Unable to parse the schema file.", item)

        # Invalid configuration object
        settings.VALIDATOR_PATH = self.validator_path

        schema = "config.json"

        filename = self.assets / "config_bad.yaml"
        try:
            with open(filename, "r") as fdesc:
                content = fdesc.read()
        except IOError as error:
            log.exception("Config file not readable: %s. %s", filename, error)
            return False

        with self.assertLogs(log, level='ERROR') as cm:
            self.assertFalse(config.load_config(content, schema))
        for item in cm.output:
            self.assertIn("Configuration file not validated.", item)

        # All good
        filename = settings.PHARMASHIP_DATA / "config_default.yaml"
        try:
            with open(filename, "r") as fdesc:
                content = fdesc.read()
        except IOError as error:
            log.exception("Config file not readable: %s. %s", filename, error)
            return False

        result = config.load_config(content, schema)
        self.assertIsInstance(result, dict)
コード例 #5
0
    def db_export(self, source, dialog):
        """Copy pharmaship database to user selected path."""
        path = dialog.get_filename()
        if not path:
            return False

        # Create a timestamped filename
        filename = "pharmaship_{0}.db".format(datetime.datetime.now().strftime("%Y-%m-%d_%H%M"))
        db_filename = settings.DATABASES['default']['NAME']

        try:
            shutil.copy(db_filename, filename)
        except OSError as error:
            log.exception("File impossible to save: %s. %s", filename, error)
            self.error_dialog(error)
            return False

        dialog.destroy()
        self.success_dialog()
        return True
コード例 #6
0
try:
    if hasattr(locale, 'bindtextdomain'):
        libintl = locale
    elif os.name == 'nt':
        libintl = ctypes.cdll.LoadLibrary('libintl-8.dll')
    elif sys.platform == 'darwin':
        libintl = ctypes.cdll.LoadLibrary('libintl.dylib')

    # setup the textdomain in gettext so Gtk3 can find it
    libintl.bindtextdomain("com.devmaretique.pharmaship",
                           str(settings.PHARMASHIP_LOCALE))
    libintl.textdomain("com.devmaretique.pharmaship")

except (OSError, AttributeError) as error:
    # disable translations altogether for consistency
    log.exception("Impossible to set translation for Pharmaship. %s", error)
    translation.activate('en_US.UTF-8')


def set_langage(lang_code: str):
    """Application wide language switcher.

    Set Django Translation (gettext) and C library `locale` for Glade/Gtk
    files.

    :param: lang_code: (string) language code
    """
    locale_str = None

    if platform.system() == "Windows":
        if lang_code == "fr":