ConfigurationInspector.__init__()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 3
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 3
loc 3
rs 10
1
import logging
2
3
from ..yaml import yaml_load
4
5
logger = logging.getLogger('binstar.auth')
6
7
8
class ProjectFilesInspector(object):
9
    def __init__(self, pfiles):
10
        self.pfiles = pfiles
11
12
    def update(self, metadata):
13
        metadata['files'] = [pfile.to_dict() for pfile in self.pfiles]
14
        return metadata
15
16
17 View Code Duplication
class DocumentationInspector(object):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
18
    valid_names = [
19
        'README.md',
20
        'README.rst',
21
        'README.txt',
22
        'README'
23
    ]
24
25
    def __init__(self, pfiles):
26
        self.pfiles = pfiles
27
        self.doc_pfile = None
28
29
    def update(self, metadata):
30
        if self.has_doc():
31
            with open(self.doc_pfile.fullpath) as docfile:
32
                metadata['readme'] = docfile.read()
33
        return metadata
34
35
    def has_doc(self):
36
        def is_readme(basename, relativepath, fullpath):
37
            return basename == relativepath and basename in self.valid_names
38
39
        for pfile in self.pfiles:
40
            if pfile.validate(is_readme):
41
                self.doc_pfile = pfile
42
                break
43
44
        return self.doc_pfile is not None
45
46
47 View Code Duplication
class ConfigurationInspector(object):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
48
    valid_names = [
49
        'project.yml',
50
        'project.yaml'
51
    ]
52
53
    def __init__(self, pfiles):
54
        self.pfiles = pfiles
55
        self.config_pfile = None
56
57
    def update(self, metadata):
58
        try:
59
            if self.has_config():
60
                with open(self.config_pfile.fullpath) as configfile:
61
                    metadata['configuration'] = yaml_load(configfile)
62
        except:
63
            logger.warning("Could not parse configuration file.")
64
        return metadata
65
66
    def has_config(self):
67
        def is_config(basename, relativepath, fullpath):
68
            return basename == relativepath and basename in self.valid_names
69
70
        for pfile in self.pfiles:
71
            if pfile.validate(is_config):
72
                self.config_pfile = pfile
73
                break
74
75
        return self.config_pfile is not None
76
77
78
inspectors = [
79
    DocumentationInspector,
80
    ProjectFilesInspector,
81
    ConfigurationInspector
82
]
83