DocumentationInspector   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 28
Duplicated Lines 100 %

Importance

Changes 0
Metric Value
dl 28
loc 28
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A is_readme() 2 2 1
A has_doc() 10 10 4
A __init__() 3 3 1
A update() 5 5 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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