cli.py 2.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# 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
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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.

Y
Yang Zhang 已提交
15
from argparse import ArgumentParser, RawDescriptionHelpFormatter
16 17 18

import yaml

Y
Yang Zhang 已提交
19 20
__all__ = ['ColorTTY', 'ArgsParser']

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

class ColorTTY(object):
    def __init__(self):
        super(ColorTTY, self).__init__()
        self.colors = ['red', 'green', 'yellow', 'blue', 'magenta', 'cyan']

    def __getattr__(self, attr):
        if attr in self.colors:
            color = self.colors.index(attr) + 31

            def color_message(message):
                return "[{}m{}".format(color, message)

            setattr(self, attr, color_message)
            return color_message

    def bold(self, message):
        return self.with_code('01', message)

    def with_code(self, code, message):
        return "[{}m{}".format(code, message)


Y
Yang Zhang 已提交
44 45 46 47 48 49 50 51
class ArgsParser(ArgumentParser):

    def __init__(self):
        super(ArgsParser, self).__init__(
            formatter_class=RawDescriptionHelpFormatter)
        self.add_argument("-c", "--config", help="configuration file to use")
        self.add_argument("-o", "--opt", nargs='*',
                          help="set configuration options")
52

Y
Yang Zhang 已提交
53 54 55 56 57 58
    def parse_args(self, argv=None):
        args = super(ArgsParser, self).parse_args(argv)
        assert args.config is not None, \
            "Please specify --config=configure_file_path."
        args.opt = self._parse_opt(args.opt)
        return args
59

Y
Yang Zhang 已提交
60 61 62 63 64
    def _parse_opt(self, opts):
        config = {}
        if not opts:
            return config
        for s in opts:
65
            s = s.strip()
66
            k, v = s.split('=', maxsplit=1)
67
            if '.' not in k:
68
                config[k] = yaml.load(v, Loader=yaml.Loader)
69 70
            else:
                keys = k.split('.')
71 72
                if keys[0] not in config:
                    config[keys[0]] = {}
Y
Yang Zhang 已提交
73
                cur = config[keys[0]]
74 75 76 77 78 79
                for idx, key in enumerate(keys[1:]):
                    if idx == len(keys) - 2:
                        cur[key] = yaml.load(v, Loader=yaml.Loader)
                    else:
                        cur[key] = {}
                        cur = cur[key]
Y
Yang Zhang 已提交
80
        return config