Completed
Pull Request — master (#1155)
by Mischa
01:43
created

__init__()   A

Complexity

Conditions 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 22
rs 9.2
1
import os
2
3
from coalib.bearlib.abstractions.SectionCreatable import SectionCreatable
4
from coalib.misc.Constants import Constants
5
from coalib.parsing.ConfParser import ConfParser
6
7
8
class LanguageDefinition(SectionCreatable):
9
    def __init__(self, language_family: str, language: str):
10
        """
11
        Creates a new LanguageDefinition object from file.
12
13
        A Language Definition holds constants which may help parsing the
14
        language. If you want to write a bear you'll probably want to use those
15
        definitions to keep your bear independent of the semantics of each
16
        language.
17
18
        :param language_family:    The language family. E.g. C for C++ and C
19
                                   and C# and so on.
20
        :param language:           The actual language (e.g. C++).
21
        :raises FileNotFoundError: Raised when no definition is available for
22
                                   the given family.
23
        :raises KeyError:          Raised when no definition is available for
24
                                   the given language.
25
        """
26
        SectionCreatable.__init__(self)
27
        self.language = language.lower()
28
        filename = os.path.join(Constants.language_definitions,
29
                                language_family.lower() + ".coalang")
30
        self.lang_dict = ConfParser().parse(filename)[language.lower()]
31
32
    def __getitem__(self, item):
33
        return self.lang_dict[item]
34