args.py 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
# Copyright (c) 2017 VisualDL Authors. All Rights Reserve.
#
# 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.
# =======================================================================

import sys
from argparse import ArgumentParser

from visualdl.server.log import logger

default_host = '127.0.0.1'
default_port = 8040
default_cache_timeout = 20

default_public_path = '/app'


class DefaultArgs(object):
    def __init__(self, args):
        self.logdir = args.get('logdir')
        self.host = args.get('host', default_host)
        self.port = args.get('port', default_port)
        self.cache_timeout = args.get('cache_timeout', default_cache_timeout)
        self.language = args.get('language')
        self.public_path = args.get('public_path')
        self.api_only = args.get('api_only', False)
        self.open_browser = args.get('open_browser', False)
走神的阿圆's avatar
走神的阿圆 已提交
38
        self.model = args.get('model', '')
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80


def validate_args(args):
    # if not in API mode, public path cannot be set to root path
    if not args.api_only and args.public_path == '/':
        logger.error('Public path cannot be set to root path.')
        sys.exit(-1)

    # public path must start with `/`
    if args.public_path is not None and not args.public_path.startswith('/'):
        logger.error('Public path should always start with a `/`.')
        sys.exit(-1)


def format_args(args):
    validate_args(args)

    # set default public path according to API mode option
    if args.public_path is None:
        args.public_path = '' if args.api_only else default_public_path
    else:
        args.public_path = args.public_path.rstrip('/')

    # don't open browser in API mode
    if args.api_only:
        args.open_browser = False

    return args


class ParseArgs(object):
    def __init__(self, **kwargs):
        args = format_args(DefaultArgs(kwargs))

        self.logdir = args.logdir
        self.host = args.host
        self.port = args.port
        self.cache_timeout = args.cache_timeout
        self.language = args.language
        self.public_path = args.public_path
        self.api_only = args.api_only
        self.open_browser = args.open_browser
81
        self.model = args.model
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109


def parse_args():
    """
    :return:
    """
    parser = ArgumentParser(description="VisualDL, a tool to visualize deep learning.")
    parser.add_argument(
        "--logdir",
        action="store",
        dest="logdir",
        nargs="+",
        help="log file directory")
    parser.add_argument(
        "-p",
        "--port",
        type=int,
        default=default_port,
        action="store",
        dest="port",
        help="api service port")
    parser.add_argument(
        "-t",
        "--host",
        type=str,
        default=default_host,
        action="store",
        help="api service ip")
110 111 112 113 114 115 116
    parser.add_argument(
        "--model",
        type=str,
        action="store",
        dest="model",
        default="",
        help="model file path")
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    parser.add_argument(
        "--cache_timeout",
        action="store",
        dest="cache_timeout",
        type=float,
        default=default_cache_timeout,
        help="memory cache timeout duration in seconds, default 20", )
    parser.add_argument(
        "-L",
        "--language",
        type=str,
        action="store",
        default=None,
        help="set the default language")
    parser.add_argument(
        "-P",
        "--public-path",
        type=str,
        action="store",
        default=None,
        help="set public path"
    )
    parser.add_argument(
        "-A",
        "--api-only",
        action="store_true",
        default=False,
        help="serve api only"
    )
走神的阿圆's avatar
走神的阿圆 已提交
146 147 148 149 150 151 152
    parser.add_argument(
        "-B",
        "--open_browser",
        action="store_true",
        default=False,
        help="open browser automatically"
    )
153 154 155 156

    args = parser.parse_args()

    return format_args(args)