Completed
Push — master ( 3882b4...45f2ac )
by Charles
02:14
created

Dumper.dumpJson()   A

Complexity

Conditions 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 9.6666
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 os
4 1
import json
5 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...
6 1
from collections import Iterable
0 ignored issues
show
Unused Code introduced by
Unused Iterable imported from collections
Loading history...
7
8 1
try:
9 1
    import configparser
10
except ImportError:
11
    import ConfigParser as configparser
0 ignored issues
show
Configuration introduced by
The import ConfigParser 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...
12
13 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...
14 1
try:
15 1
    from yaml import CDumper as Dumper
0 ignored issues
show
Unused Code introduced by
Unused CDumper imported from yaml as Dumper
Loading history...
16
except ImportError:
17
    from yaml import Dumper
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...
Unused Code introduced by
Unused Dumper imported from yaml
Loading history...
18
19 1
from git_app_version.helper.pyversion import PY3
20
21 1
class Dumper(object):
0 ignored issues
show
Bug introduced by
class already defined line 15
Loading history...
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...
22
23 1
    def dump(self, data = {}, format = 'json', target = None, cwd=None, namespace=''):
0 ignored issues
show
Coding Style introduced by
No space allowed around keyword argument assignment
def dump(self, data = {}, format = 'json', target = None, cwd=None, namespace=''):
^
Loading history...
Coding Style introduced by
No space allowed around keyword argument assignment
def dump(self, data = {}, format = 'json', target = None, cwd=None, namespace=''):
^
Loading history...
Coding Style introduced by
No space allowed around keyword argument assignment
def dump(self, data = {}, format = 'json', target = None, cwd=None, namespace=''):
^
Loading history...
Bug Best Practice introduced by
The default value {} might cause unintended side-effects.

Objects as default values are only created once in Python and not on each invocation of the function. If the default object is modified, this modification is carried over to the next invocation of the method.

# Bad:
# If array_param is modified inside the function, the next invocation will
# receive the modified object.
def some_function(array_param=[]):
    # ...

# Better: Create an array on each invocation
def some_function(array_param=None):
    array_param = array_param or []
    # ...
Loading history...
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...
Bug Best Practice introduced by
This seems to re-define the built-in format.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
best-practice introduced by
Too many arguments (6/5)
Loading history...
24 1
        target = self.__checkTarget(target, cwd)
25 1
        if format == 'yaml' or format == 'yml':
26 1
            return self.dumpYaml(data, target, namespace)
27 1
        elif format == 'xml':
28 1
            return self.dumpXml(data, target, namespace)
29 1
        elif format == 'ini':
30 1
            return self.dumpIni(data, target, namespace)
31
        else:
32 1
            return self.dumpJson(data, target, namespace)
33
34 1
    def __checkTarget(self, target, cwd=None):
0 ignored issues
show
Coding Style Naming introduced by
The name __checkTarget does not conform to the method 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...
35 1
        if not os.path.isabs(target):
36 1
            if cwd is None or not os.path.exists(cwd):
37
                cwd = os.getcwd()
38
39 1
            target = cwd+'/'+target
40
41 1
        self.__makeParentDir(target)
42
43 1
        return target
44
45 1
    def __makeParentDir(self, target):
0 ignored issues
show
Coding Style Naming introduced by
The name __makeParentDir does not conform to the method 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...
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
        parentDir = os.path.dirname(target)
0 ignored issues
show
Coding Style Naming introduced by
The name parentDir 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...
47 1
        if not os.path.exists(parentDir):
48
            os.makedirs(parentDir, 493) # 493 in decimal as 755 in octal
49
50 1
    def __createInfosToDump(self, infos, namespace=None):
0 ignored issues
show
Coding Style Naming introduced by
The name __createInfosToDump does not conform to the method 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...
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
        toDump = infos
0 ignored issues
show
Coding Style Naming introduced by
The name toDump 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...
52 1
        if namespace is not None and namespace != '' :
0 ignored issues
show
Coding Style introduced by
No space allowed before :
if namespace is not None and namespace != '' :
^
Loading history...
53 1
            namespaces = namespace.split('.')
54 1
            namespaces.reverse()
55 1
            for name in namespaces :
0 ignored issues
show
Coding Style introduced by
No space allowed before :
for name in namespaces :
^
Loading history...
56 1
                toDump = { name: toDump }
0 ignored issues
show
Coding Style introduced by
No space allowed after bracket
toDump = name: toDump
^
Loading history...
Coding Style introduced by
No space allowed before bracket
toDump = name: toDump
^
Loading history...
Coding Style Naming introduced by
The name toDump 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...
57
58 1
        return toDump
59
60 1
    def __encode(self, input, encoding='utf-8'):
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in input.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
61
        if isinstance(input, dict):
62
            return {self.__encode(key, encoding): self.__encode(value, encoding) for key, value in input.iteritems()}
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (117/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
63
        elif isinstance(input, list):
64
            return [self.__encode(element, encoding) for element in input]
65
        elif isinstance(input, unicode):
1 ignored issue
show
Comprehensibility Best Practice introduced by
Undefined variable 'unicode'
Loading history...
66
            return input.encode(encoding)
67
        else:
68
            return input
69
70 1
    def dumpIni(self, data, target, namespace=None):
0 ignored issues
show
Coding Style Naming introduced by
The name dumpIni does not conform to the method 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...
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...
71 1
        target = target+'.ini'
72 1
        namespace = 'app_version' if namespace is None or namespace == '' else namespace
73
74 1
        ini = configparser.RawConfigParser()
75 1
        ini.add_section(namespace)
76
77 1
        for key,val in data.items():
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comma
for key,val in data.items():
^
Loading history...
78 1
            if PY3:
79 1
                ini.set(namespace, key, val)
80
            else:
81
                ini.set(namespace, key, self.__encode(val))
82
83 1
        with open(target, 'w') as f:
0 ignored issues
show
Coding Style Naming introduced by
The name f 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...
84 1
            ini.write(f)
85
86 1
        return target
87
88 1
    def dumpXml(self, data, target, namespace=None):
0 ignored issues
show
Coding Style Naming introduced by
The name dumpXml does not conform to the method 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...
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+'.xml'
90 1
        namespace = 'app_version' if namespace is None or namespace == '' else namespace
91
92 1
        with open(target, 'w') as f:
0 ignored issues
show
Coding Style Naming introduced by
The name f 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...
93 1
            xml = xmltodict.unparse(self.__createInfosToDump(data, namespace), encoding='utf-8', pretty=True, indent='  ')
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (122/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
94 1
            if PY3:
95 1
                f.write(xml)
96
            else:
97
                f.write(xml.encode('utf-8'))
98
99 1
            return target
100
101 1
    def dumpJson(self, data, target, namespace=None):
0 ignored issues
show
Coding Style Naming introduced by
The name dumpJson does not conform to the method 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...
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...
102 1
        target = target+'.json'
103
104 1
        data1 = self.__createInfosToDump(data, namespace)
105
106 1
        with open(target, 'w') as f:
0 ignored issues
show
Coding Style Naming introduced by
The name f 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...
107 1
            json.dump(data1, f, indent=2)
108
109 1
        return target
110
111 1
    def dumpYaml(self, data, target, namespace=None):
0 ignored issues
show
Coding Style Naming introduced by
The name dumpYaml does not conform to the method 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...
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...
112 1
        target = target+'.yml'
113
114 1
        with open(target, 'w') as f:
0 ignored issues
show
Coding Style Naming introduced by
The name f 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...
115 1
            if not data :
0 ignored issues
show
Coding Style introduced by
No space allowed before :
if not data :
^
Loading history...
116 1
                f.write("---\n")
117
            else :
0 ignored issues
show
Coding Style introduced by
No space allowed before :
else :
^
Loading history...
118 1
                yaml.safe_dump(
119
                    self.__createInfosToDump(data, namespace),
120
                    f,
121
                    default_flow_style=False,
122
                    explicit_start=True,
123
                    allow_unicode=True,
124
                    default_style='\'' # force quoting to prevent abbrev_commit to be read as a float
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (101/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
125
                )
126
127
        return target
128