Completed
Pull Request — master (#2295)
by Mischa
01:46
created

LanguageDefinition.__init__()   B

Complexity

Conditions 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 24
rs 8.9713
1
import os
2
3
from coalib.bearlib.abstractions.SectionCreatable import SectionCreatable
4
from coalib.misc import Constants
5
from coalib.parsing.ConfParser import ConfParser
6
7
8
class LanguageDefinition(SectionCreatable):
9
10
    def __init__(self, language: str, coalang_dir=None):
11
        """
12
        Creates a new LanguageDefinition object from file.
13
14
        A Language Definition holds constants which may help parsing the
15
        language. If you want to write a bear you'll probably want to use those
16
        definitions to keep your bear independent of the semantics of each
17
        language.
18
19
        :param language:           The actual language (e.g. C++).
20
        :param coalang_dir:        Path to directory with coalang language
21
                                   definition files. This replaces the default
22
                                   path if given.
23
        :raises FileNotFoundError: Raised when no definition is available for
24
                                   the given language.
25
        """
26
        SectionCreatable.__init__(self)
27
        self.language = language.lower()
28
29
        coalang_file = os.path.join(
30
            coalang_dir or Constants.language_definitions,
31
            self.language + ".coalang")
32
33
        self.lang_dict = ConfParser().parse(coalang_file)["default"]
34
35
    def __getitem__(self, item):
36
        return self.lang_dict[item]
37
38
    def __contains__(self, item):
39
        return item in self.lang_dict
40