Test Failed
Pull Request — master (#73)
by macartur
01:46
created

UsersAPI.create()   A

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
1
"""Translate cli commands to non-cli code."""
2
import logging
3
import os
4
import re
5
6
from kytos.utils.users import UsersManager
7
8
log = logging.getLogger(__name__)
9
10
11
class UsersAPI:
12
    """An API for the command-line interface.
13
14
    Use the config file only for required options. Static methods are called
15
    by the parser and they instantiate an object of this class to fulfill the
16
    request.
17
    """
18
19
    user_manager = UsersManager()
20
21
    @classmethod
22
    def create(cls, args):
23
        """Create a new User in Napps server."""
24
        result = cls.user_manager.register()
25
        print(result)
26
27
    @classmethod
28
    def print_users(cls, users):
29
        """Print all required fields from a user list."""
30
        if not users:
31
            print('No Users found.')
32
            return
33
34
        enabled_w = 8
35
        username_w = max(len('Username'),
36
                         max(len(n['username']) for n in users))
37
        firstname_w = max(len('First Name'),
38
                          max(len(n['first_name']) for n in users))
39
        email_w = max(len('Email'),
40
                      max(len(n['email']) for n in users))
41
        term_w = os.popen('stty size', 'r').read().split()[1]
42
        remaining = int(term_w) - enabled_w - username_w - enabled_w
43
        email_w = min(email_w, remaining)
44
        widths = (enabled_w, username_w, firstname_w, email_w)
45
46
        header = '\n{:^%d} | {:^%d} | {:^%d} | {:^%d}' % widths
47
        row = '{:^%d} | {:<%d} | {:<%d} | {:^%d}' % widths
48
        print(header.format('Enabled', 'Username', 'First Name', 'Email'))
49
        print('=+='.join('=' * w for w in widths))
50
        for user in users:
51
            enabled = 'True' if user['enabled'] else 'False'
52
            print(row.format(enabled, user['username'],
53
                             user['first_name'], user['email']))
54