config.py 2.3 KB
Newer Older
1
import os
2 3

import yaml
4
from path import path
5 6

# BASE_DIR is the working directory to execute django-admin commands from.
7
# Typically this should be the 'edx-platform' directory.
8
BASE_DIR = path(__file__).abspath().dirname().joinpath('..').normpath()
9 10

# LOCALE_DIR contains the locale files.
11 12 13
# Typically this should be 'edx-platform/conf/locale'
LOCALE_DIR = BASE_DIR.joinpath('conf', 'locale')

14

15
class Configuration(object):
16
    """
17
    Reads localization configuration in json format.
18
    """
19 20 21 22 23 24 25
    DEFAULTS = {
        'generate_merge': {},
        'ignore_dirs': [],
        'locales': ['en'],
        'segment': {},
        'source_locale': 'en',
    }
26 27

    def __init__(self, filename):
28 29
        self._filename = filename
        self._config = self.read_config(filename)
30

31
    def read_config(self, filename):
32 33 34 35 36 37
        """
        Returns data found in config file (as dict), or raises exception if file not found
        """
        if not os.path.exists(filename):
            raise Exception("Configuration file cannot be found: %s" % filename)
        with open(filename) as stream:
38
            return yaml.safe_load(stream)
39

40 41 42 43
    def __getattr__(self, name):
        if name in self.DEFAULTS:
            return self._config.get(name, self.DEFAULTS[name])
        raise AttributeError("Configuration has no such setting: {!r}".format(name))
44

45 46
    @property
    def dummy_locale(self):
47
        """
48
        Returns a locale to use for the dummy text, e.g. 'eo'.
David Baumgold committed
49
        Throws exception if no dummy-locale is declared.
50 51
        The locale is a string.
        """
52
        dummy = self._config.get('dummy-locale', None)
53 54 55 56 57 58 59
        if not dummy:
            raise Exception('Could not read dummy-locale from configuration file.')
        return dummy

    def get_messages_dir(self, locale):
        """
        Returns the name of the directory holding the po files for locale.
60
        Example: edx-platform/conf/locale/fr/LC_MESSAGES
61
        """
62
        return LOCALE_DIR.joinpath(locale, 'LC_MESSAGES')
63

64 65
    @property
    def source_messages_dir(self):
66 67
        """
        Returns the name of the directory holding the source-language po files (English).
68
        Example: edx-platform/conf/locale/en/LC_MESSAGES
69
        """
70
        return self.get_messages_dir(self.source_locale)
71 72


73
CONFIGURATION = Configuration(LOCALE_DIR.joinpath('config.yaml').normpath())