cli.py 2.56 KB
Newer Older
Markus Scheidgen's avatar
Markus Scheidgen committed
1
2
3
4
#
# Copyright The NOMAD Authors.
#
# This file is part of NOMAD. See https://nomad-lab.eu for further info.
5
6
7
8
9
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
Markus Scheidgen's avatar
Markus Scheidgen committed
10
#     http://www.apache.org/licenses/LICENSE-2.0
11
12
#
# Unless required by applicable law or agreed to in writing, software
Markus Scheidgen's avatar
Markus Scheidgen committed
13
# distributed under the License is distributed on an "AS IS" BASIS,
14
15
16
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Markus Scheidgen's avatar
Markus Scheidgen committed
17
#
18
19
20
21
22

import click
import logging
import os

23
from nomad import config, utils
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48


class POPO(dict):
    '''
    A dict subclass that uses attributes as key/value pairs.
    '''
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def __getattr__(self, name):
        if name in self:
            return self[name]
        else:
            raise AttributeError("No such attribute: " + name)

    def __setattr__(self, name, value):
        self[name] = value

    def __delattr__(self, name):
        if name in self:
            del self[name]
        else:
            raise AttributeError("No such attribute: " + name)


49
50
51
@click.group(help=(
    'This is the entry point to nomad\'s command line interface CLI. '
    'It uses a sub-command structure similar to the git command.'))
52
53
@click.option('-v', '--verbose', help='sets log level to info', is_flag=True)
@click.option('--debug', help='sets log level to debug', is_flag=True)
54
@click.option('--log-label', type=str, help='Label applied to logg entries.')
55
@click.pass_context
56
def cli(ctx, verbose: bool, debug: bool, log_label: str):
57
    config.meta.service = os.environ.get('NOMAD_SERVICE', 'cli')
58
    config.meta.label = log_label
59

60
    if debug:
61
        config.console_log_level = logging.DEBUG
62
    elif verbose:
63
        config.console_log_level = logging.INFO
64
    else:
65
        config.console_log_level = logging.WARNING
66
    utils.set_console_log_level(config.console_log_level)
67
68
69
70


def run_cli():
    try:
71
        return cli(obj=POPO())  # pylint: disable=E1120,E1123
72
73
    except ImportError:
        import sys
74
75
76
77
78

        if next(arg for arg in sys.argv if arg == '-v') is not None:
            import traceback
            traceback.print_exc()

79
80
81
82
        print(
            'You are accessing functionality that requires extra dependencies.\n'
            'Check the NOMAD documentation or install all extra dependencies:\n'
            '  pip install nomad[all]', file=sys.stderr)
83

84
        sys.exit(1)