Completed
Push — master ( f47702...a2af6f )
by Charles
01:44
created

FileDumper.dump_xml()   B

Complexity

Conditions 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
dl 0
loc 19
ccs 10
cts 10
cp 1
crap 5
rs 8.5454
c 0
b 0
f 0
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
        else:
34 1
            return self.dump_json(data, target, namespace)
35
36 1
    def __check_target(self, target, cwd=None):
37 1
        if not os.path.isabs(target):
38 1
            cwd = cwd if cwd else os.getcwd()
39 1
            target = cwd + '/' + target
40
41 1
        self.__make_parent_dir(target)
42
43 1
        return target
44
45 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...
46 1
        parent_dir = os.path.dirname(target)
47 1
        if not os.path.exists(parent_dir):
48 1
            os.makedirs(parent_dir, 493)  # 493 in decimal as 755 in octal
49
50 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...
51 1
        to_dump = infos
52 1
        if namespace is not None and namespace != '':
53 1
            namespaces = namespace.split('.')
54 1
            namespaces.reverse()
55 1
            for name in namespaces:
56 1
                to_dump = {name: to_dump}
57
58 1
        return to_dump
59
60 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...
61 1
        if isinstance(item, unicode):
1 ignored issue
show
Comprehensibility Best Practice introduced by
Undefined variable 'unicode'
Loading history...
62 1
            return item.encode(encoding)
63
        else:
64 1
            return item
65
66 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...
67 1
        if isinstance(item, list):
68 1
            flattened_list = ''
69 1
            if len(item):
70 1
                flattened_list = "'{}'".format("', '".join(item))
71 1
            return "[{}]".format(flattened_list)
72
        else:
73 1
            return item
74
75 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...
76 1
        target = target + '.ini'
77
78 1
        if namespace is None or namespace == '':
79
            namespace = 'app_version'
80
81 1
        ini = configparser.RawConfigParser()
82 1
        ini.add_section(namespace)
83
84 1
        for key, val in data.items():
85 1
            if not PY3:
86 1
                val = self.__encode(self.__flatten(val))
87
88 1
            ini.set(namespace, key, val)
89
90 1
        with open(target, 'w') as fp:
0 ignored issues
show
Coding Style Naming introduced by
The name fp does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
91 1
            ini.write(fp)
92
93 1
        return target
94
95 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...
96 1
        target = target + '.xml'
97 1
        if namespace is None or namespace == '':
98 1
            namespace = 'app_version'
99
100 1
        with open(target, 'w') as fp:
0 ignored issues
show
Coding Style Naming introduced by
The name fp does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
101 1
            xml = xmltodict.unparse(
102
                self.__create_infos_to_dump(
103
                    data,
104
                    namespace),
105
                encoding='utf-8',
106
                pretty=True,
107
                indent='  ')
108 1
            if not PY3:
109 1
                xml = xml.encode('utf-8')
110
111 1
            fp.write(xml)
112
113 1
            return target
114
115 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...
116 1
        target = target + '.json'
117
118 1
        data1 = self.__create_infos_to_dump(data, namespace)
119
120 1
        with open(target, 'w') as fp:
0 ignored issues
show
Coding Style Naming introduced by
The name fp does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
121 1
            json.dump(data1, fp, indent=2)
122
123 1
        return target
124
125 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...
126 1
        target = target + '.yml'
127
128 1
        with open(target, 'w') as fp:
0 ignored issues
show
Coding Style Naming introduced by
The name fp does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
129 1
            if not data:
130 1
                fp.write("---\n")
131
            else:
132 1
                yaml.safe_dump(
133
                    self.__create_infos_to_dump(data, namespace),
134
                    fp,
135
                    default_flow_style=False,
136
                    explicit_start=True,
137
                    allow_unicode=True,
138
                    # force quoting
139
                    # to prevent abbrev_commit to be read as a float
140
                    default_style='\''
141
                )
142
143
        return target
144