Passed
Pull Request — master (#117)
by
unknown
02:05
created

gmp.clients.helper.pretty_print()   A

Complexity

Conditions 5

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
nop 1
dl 0
loc 18
rs 9.3333
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
# Copyright (C) 2018 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: GPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
import getpass
20
21
from lxml import etree
22
23
from gmp.error import GmpError
24
25
26
def authenticate(gmp, args):
27
    """Authentication helper
28
29
    Tries to get authentication username and password from args and if not
30
    present asks the username and/or password from the terminal.
31
32
    Attributes:
33
        gmp: A protocol instance
34
        args: The parsed arguments
35
36
    Raises:
37
        GmpError: Raises GmpError if authentication fails.
38
    """
39
    if gmp.is_authenticated():
40
        return
41
42
    username = args.gmp_username
43
    password = args.gmp_password
44
45
    # Ask for login credentials if none are given.
46
    if not username:
47
        while len(username) == 0:
0 ignored issues
show
Unused Code introduced by
Do not use len(SEQUENCE) as condition value
Loading history...
48
            username = input('Enter username: ')
49
50
    if not password:
51
        password = getpass.getpass(
52
            'Enter password for {0}: '.format(username))
53
54
    try:
55
        gmp.authenticate(username, password)
56
    except GmpError as e:
0 ignored issues
show
Coding Style Naming introduced by
The name e does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

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
        print('Could not authenticate. Please check your credentials.')
58
        raise e
59
60
61
def pretty_print(xml):
62
    """Prints beautiful XML-Code
63
64
    This function gets an object of list<lxml.etree._Element>
65
    or directly a lxml element.
66
    Print it with good readable format.
67
68
    Arguments:
69
        xml: List<lxml.etree.Element> or directly a lxml element
70
    """
71
    if isinstance(xml, list):
72
        for item in xml:
73
            if etree.iselement(item):
0 ignored issues
show
Bug introduced by
The Module lxml.etree does not seem to have a member named iselement.

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...
74
                print(etree.tostring(item, pretty_print=True).decode('utf-8'))
0 ignored issues
show
Bug introduced by
The Module lxml.etree does not seem to have a member named tostring.

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...
75
            else:
76
                print(item)
77
    elif etree.iselement(xml):
0 ignored issues
show
Bug introduced by
The Module lxml.etree does not seem to have a member named iselement.

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...
78
        print(etree.tostring(xml, pretty_print=True).decode('utf-8'))
0 ignored issues
show
Bug introduced by
The Module lxml.etree does not seem to have a member named tostring.

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...
79