Completed
Push — master ( d5feb1...7c9762 )
by Charles
02:37
created

FileDumper   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 140
ccs 85
cts 85
cp 1
rs 8.2608
c 1
b 0
f 0
wmc 40

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __create_infos_to_dump() 0 9 4
A __encode() 0 5 2
A dump_json() 0 9 2
A dump_sh() 0 10 4
A dump_yaml() 0 19 3
A __check_target() 0 8 3
B dump_ini() 0 19 6
A __flatten() 0 8 3
B dump() 0 18 6
A __make_parent_dir() 0 4 2
B dump_xml() 0 19 5

How to fix   Complexity   

Complex Class

Complex classes like FileDumper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# -*- coding: utf-8 -*-
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
3 1
import json
4 1
import os
5
6 1
import xmltodict
0 ignored issues
show
Configuration introduced by
The import xmltodict could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
7 1
import yaml
0 ignored issues
show
Configuration introduced by
The import yaml could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
8
9 1
from git_app_version.helper.pyversion import PY3
10
11 1
try:
12 1
    import ConfigParser as configparser
13
except ImportError:
14
    import configparser
15
16
17 1
class FileDumper(object):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
18
19 1
    def dump(
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
best-practice introduced by
Too many arguments (6/5)
Loading history...
20
            self,
21
            data=None,
22
            fileformat='json',
23
            target=None,
24
            cwd=None,
25
            namespace=''):
26 1
        target = self.__check_target(target, cwd)
27 1
        if fileformat == 'yaml' or fileformat == 'yml':
28 1
            return self.dump_yaml(data, target, namespace)
29 1
        elif fileformat == 'xml':
30 1
            return self.dump_xml(data, target, namespace)
31 1
        elif fileformat == 'ini':
32 1
            return self.dump_ini(data, target, namespace)
33 1
        elif fileformat == 'sh':
34 1
            return self.dump_sh(data, target)
35
        else:
36 1
            return self.dump_json(data, target, namespace)
37
38 1
    def __check_target(self, target, cwd=None):
39 1
        if not os.path.isabs(target):
40 1
            cwd = cwd if cwd else os.getcwd()
41 1
            target = cwd + '/' + target
42
43 1
        self.__make_parent_dir(target)
44
45 1
        return target
46
47 1
    def __make_parent_dir(self, target):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
48 1
        parent_dir = os.path.dirname(target)
49 1
        if not os.path.exists(parent_dir):
50 1
            os.makedirs(parent_dir, 493)  # 493 in decimal as 755 in octal
51
52 1
    def __create_infos_to_dump(self, infos, namespace=None):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
53 1
        to_dump = infos
54 1
        if namespace is not None and namespace != '':
55 1
            namespaces = namespace.split('.')
56 1
            namespaces.reverse()
57 1
            for name in namespaces:
58 1
                to_dump = {name: to_dump}
59
60 1
        return to_dump
61
62 1
    def __encode(self, item, encoding='utf-8'):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
63 1
        if isinstance(item, unicode):
64 1
            return item.encode(encoding)
65
        else:
66 1
            return item
67
68 1
    def __flatten(self, item):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
69 1
        if isinstance(item, list):
70 1
            flattened_list = ''
71 1
            if len(item):
72 1
                flattened_list = "'{}'".format("', '".join(item))
73 1
            return "[{}]".format(flattened_list)
74
        else:
75 1
            return item
76
77 1
    def dump_sh(self, data, target):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
78 1
        target = target + '.sh'
79
80 1
        with open(target, 'w') as fpt:
81 1
            for key, val in data.items():
82 1
                if not PY3:
83 1
                    val = self.__encode(self.__flatten(val))
84 1
                fpt.write("{}=\"{}\"\n".format(key, val))
85
86 1
        return target
87
88 1
    def dump_ini(self, data, target, namespace=None):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
89 1
        target = target + '.ini'
90
91 1
        if namespace is None or namespace == '':
92 1
            namespace = 'app_version'
93
94 1
        ini = configparser.RawConfigParser()
95 1
        ini.add_section(namespace)
96
97 1
        for key, val in data.items():
98 1
            if not PY3:
99 1
                val = self.__encode(self.__flatten(val))
100
101 1
            ini.set(namespace, key, val)
102
103 1
        with open(target, 'w') as fpt:
104 1
            ini.write(fpt)
105
106 1
        return target
107
108 1
    def dump_xml(self, data, target, namespace=None):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
109 1
        target = target + '.xml'
110 1
        if namespace is None or namespace == '':
111 1
            namespace = 'app_version'
112
113 1
        with open(target, 'w') as fpt:
114 1
            xml = xmltodict.unparse(
115
                self.__create_infos_to_dump(
116
                    data,
117
                    namespace),
118
                encoding='utf-8',
119
                pretty=True,
120
                indent='  ')
121 1
            if not PY3:
122 1
                xml = xml.encode('utf-8')
123
124 1
            fpt.write(xml)
125
126 1
            return target
127
128 1
    def dump_json(self, data, target, namespace=None):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
129 1
        target = target + '.json'
130
131 1
        data1 = self.__create_infos_to_dump(data, namespace)
132
133 1
        with open(target, 'w') as fpt:
134 1
            json.dump(data1, fpt, indent=2)
135
136 1
        return target
137
138 1
    def dump_yaml(self, data, target, namespace=None):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
139 1
        target = target + '.yml'
140
141 1
        with open(target, 'w') as fpt:
142 1
            if not data:
143 1
                fpt.write("---\n")
144
            else:
145 1
                yaml.safe_dump(
146
                    self.__create_infos_to_dump(data, namespace),
147
                    fpt,
148
                    default_flow_style=False,
149
                    explicit_start=True,
150
                    allow_unicode=True,
151
                    # force quoting
152
                    # to prevent abbrev_commit to be read as a float
153
                    default_style='\''
154
                )
155
156
        return target
157