tracim.command   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
eloc 64
dl 0
loc 105
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A TracimCLI.prepare_to_run_command() 0 2 1
A TracimCLI.initialize_app() 0 2 1
A Extender.__call__() 0 15 4
A TracimCLI.__init__() 0 6 1
A TracimCLI.clean_up() 0 4 2
A AppContextCommand.take_action() 0 6 4
A TestTracimCommand.take_app_action() 0 6 1
A AppContextCommand.get_parser() 0 11 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A main() 0 3 1
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...
Bug introduced by
There seems to be a cyclic import (tracim.models.auth -> tracim.models.data).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
Bug introduced by
There seems to be a cyclic import (tracim.models.data -> tracim.models.revision_protection).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
2
import sys
3
import argparse
4
import transaction
5
6
from cliff.app import App
7
from cliff.command import Command
8
from cliff.commandmanager import CommandManager
9
10
from pyramid.paster import bootstrap
11
from pyramid.scripting import AppEnvironment
12
from tracim.exceptions import CommandAbortedError
13
14
15
class TracimCLI(App):
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...
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
16
    def __init__(self) -> None:
17
        super(TracimCLI, self).__init__(
18
            description='TracimCli',
19
            version='0.1',
20
            command_manager=CommandManager('tracimcli'),
21
            deferred_help=True,
22
            )
23
24
    def initialize_app(self, argv) -> None:
25
        self.LOG.debug('initialize_app')
26
27
    def prepare_to_run_command(self, cmd) -> None:
28
        self.LOG.debug('prepare_to_run_command %s', cmd.__class__.__name__)
29
30
    def clean_up(self, cmd, result, err) -> None:
31
        self.LOG.debug('clean_up %s', cmd.__class__.__name__)
32
        if err:
33
            self.LOG.debug('got an error: %s', err)
34
35
36
def main(argv=sys.argv[1:]):
0 ignored issues
show
Bug Best Practice introduced by
The default value sys.argv[1:] (builtins.list) 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 function 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...
37
    myapp = TracimCLI()
38
    return myapp.run(argv)
39
40
41
if __name__ == "__main__":
42
    main()
43
44
45
class AppContextCommand(Command):
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
46
    """
47
    Command who initialize app context at beginning of take_action method.
48
    """
49
    auto_setup_context = True
50
51
    def take_action(self, parsed_args: argparse.Namespace) -> None:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable argparse does not seem to be defined.
Loading history...
52
        super(AppContextCommand, self).take_action(parsed_args)
53
        if self.auto_setup_context:
54
            with bootstrap(parsed_args.config_file) as app_context:
55
                with app_context['request'].tm:
56
                    self.take_app_action(parsed_args, app_context)
0 ignored issues
show
Bug introduced by
The Instance of AppContextCommand does not seem to have a member named take_app_action.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
57
58
    def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
59
        parser = super(AppContextCommand, self).get_parser(prog_name)
60
61
        parser.add_argument(
62
            "-c",
63
            "--config",
64
            help='application config file to read (default: development.ini)',
65
            dest='config_file',
66
            default="development.ini"
67
        )
68
        return parser
69
70
    # def run(self, parsed_args):
71
    #     super().run(parsed_args)
72
    #     transaction.commit()
73
74
75
# TODO - G.M - 10-04-2018 - [Cleanup][tempExample] - Drop this
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
76
class TestTracimCommand(AppContextCommand):
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...
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
77
78
    def take_app_action(
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...
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...
79
            self,
80
            parser: argparse.ArgumentParser,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable argparse does not seem to be defined.
Loading history...
Unused Code introduced by
The argument parser seems to be unused.
Loading history...
81
            app_context: AppEnvironment
0 ignored issues
show
Unused Code introduced by
The argument app_context seems to be unused.
Loading history...
82
    ) -> None:
83
        print('test')
84
85
86
class Extender(argparse.Action):
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
87
    """
88
    Copied class from http://stackoverflow.com/a/12461237/801924
89
    """
90
    def __call__(self, parser, namespace, values, option_strings=None):
91
        # Need None here incase `argparse.SUPPRESS` was supplied for `dest`
92
        dest = getattr(namespace, self.dest, None)
93
        # print dest,self.default,values,option_strings
94
        if not hasattr(dest, 'extend') or dest == self.default:
95
            dest = []
96
            setattr(namespace, self.dest, dest)
97
            # if default isn't set to None, this method might be called
98
            # with the default as `values` for other arguements which
99
            # share this destination.
100
            parser.set_defaults(**{self.dest: None})
101
        try:
102
            dest.extend(values)
103
        except ValueError:
104
            dest.append(values)
105