Completed
Push — master ( 419779...7515a9 )
by Charles
02:09
created

FileDumper.dump_yaml()   A

Complexity

Conditions 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
dl 0
loc 17
ccs 7
cts 7
cp 1
crap 3
rs 9.4285
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 os
4 1
import json
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
from git_app_version.helper.pyversion import PY3
8
9 1
try:
10 1
    import ConfigParser as configparser
11
except ImportError:
12
    import configparser
13
14 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...
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
            if cwd is None or not os.path.exists(cwd):
39
                cwd = os.getcwd()
40
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
            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):
1 ignored issue
show
Comprehensibility Best Practice introduced by
Undefined variable 'unicode'
Loading history...
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
            if len(item):
71 1
                return "['{}']".format("', '".join(item))
72
            return None
73
        else:
74 1
            return item
75
76 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...
77 1
        target = target + '.ini'
78 1
        namespace = 'app_version' if namespace is None or namespace == '' else namespace
79
80 1
        ini = configparser.RawConfigParser()
81 1
        ini.add_section(namespace)
82
83 1
        for key, val in data.items():
84 1
            if PY3:
85
                ini.set(namespace, key, val)
86
            else:
87 1
                ini.set(namespace, key, self.__encode(self.__flatten(val)))
88
89 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...
90 1
            ini.write(fp)
91
92 1
        return target
93
94 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...
95 1
        target = target + '.xml'
96 1
        namespace = 'app_version' if namespace is None or namespace == '' else namespace
97
98 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...
99 1
            xml = xmltodict.unparse(
100
                self.__create_infos_to_dump(
101
                    data,
102
                    namespace),
103
                encoding='utf-8',
104
                pretty=True,
105
                indent='  ')
106 1
            if PY3:
107
                fp.write(xml)
108
            else:
109 1
                fp.write(xml.encode('utf-8'))
110
111 1
            return target
112
113 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...
114 1
        target = target + '.json'
115
116 1
        data1 = self.__create_infos_to_dump(data, namespace)
117
118 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...
119 1
            json.dump(data1, fp, indent=2)
120
121 1
        return target
122
123 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...
124 1
        target = target + '.yml'
125
126 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...
127 1
            if not data:
128 1
                fp.write("---\n")
129
            else:
130 1
                yaml.safe_dump(
131
                    self.__create_infos_to_dump(data, namespace),
132
                    fp,
133
                    default_flow_style=False,
134
                    explicit_start=True,
135
                    allow_unicode=True,
136
                    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 (102/100).

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

Loading history...
137
                )
138
139
        return target
140