Completed
Pull Request — master (#2295)
by Lasse
01:47
created

LanguageDefinition   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __contains__() 0 2 1
B __init__() 0 26 2
A __getitem__() 0 2 1
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_path=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
        :coalang_path:             Path to coalang definition for language.
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
29
        if not coalang_path:
30
            filename = os.path.join(Constants.language_definitions,
31
                                    language.lower() + ".coalang")
32
        else:
33
            filename = coalang_path
34
35
        self.lang_dict = ConfParser().parse(filename)["default"]
36
37
    def __getitem__(self, item):
38
        return self.lang_dict[item]
39
40
    def __contains__(self, item):
41
        return item in self.lang_dict
42