config.py 2.17 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().dirname()
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
    DEFAULTS = {
20
        'dummy_locales': [],
21 22 23 24 25
        'generate_merge': {},
        'ignore_dirs': [],
        'locales': ['en'],
        'segment': {},
        'source_locale': 'en',
26
        'third_party': [],
27
    }
28 29

    def __init__(self, filename):
30 31
        self._filename = filename
        self._config = self.read_config(filename)
32

33
    def read_config(self, filename):
34 35 36 37 38 39
        """
        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:
40
            return yaml.safe_load(stream)
41

42 43 44 45
    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))
46 47 48 49

    def get_messages_dir(self, locale):
        """
        Returns the name of the directory holding the po files for locale.
50
        Example: edx-platform/conf/locale/fr/LC_MESSAGES
51
        """
52
        return LOCALE_DIR.joinpath(locale, 'LC_MESSAGES')
53

54 55
    @property
    def source_messages_dir(self):
56 57
        """
        Returns the name of the directory holding the source-language po files (English).
58
        Example: edx-platform/conf/locale/en/LC_MESSAGES
59
        """
60
        return self.get_messages_dir(self.source_locale)
61

62 63 64 65 66 67
    @property
    def translated_locales(self):
        """
        Returns the set of locales to be translated (ignoring the source_locale).
        """
        return sorted(set(self.locales) - set([self.source_locale]))
68

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