Exemplo n.º 1
0
def test_parse_config_file() -> None:
    result = parse_config_files(["tests/configs/config_file.json"])
    expected_result = {
        "options": {"http": {"port": 4711, "access_log": True}},
        "uuid": "21a24416-1427-4603-c7c9-0ff8ab1f1c20",
    }
    assert result == expected_result
Exemplo n.º 2
0
    def run_command(self, args: List[str]) -> None:
        if len(args) == 0:
            print(self.run_command_usage())
        else:
            configuration = None
            log_level = logging.INFO

            if '-c' in args or '--config' in args:
                index = args.index('-c') if '-c' in args else args.index('--config')
                args.pop(index)

                config_files = []  # type: List[str]
                while len(args) > index and args[index][0] != '-':
                    value = args.pop(index)
                    if value not in config_files:
                        config_files.append(value)

                if not len(config_files):
                    print('Missing config file on command line')
                    sys.exit(2)

                try:
                    configuration = parse_config_files(config_files)
                except FileNotFoundError as e:
                    print('Invalid config file: {}'.format(str(e)))
                    sys.exit(2)
                except ValueError as e:
                    print('Invalid config file, invalid JSON format: {}'.format(str(e)))
                    sys.exit(2)

            if '--production' in args:
                index = args.index('--production')
                args.pop(index)
                watcher = None
            else:
                cwd = os.getcwd()
                root_directories = [os.getcwd()]
                for arg in set(args):
                    root_directories.append(os.path.dirname('{}/{}'.format(os.path.realpath(cwd), arg)))
                watcher = Watcher(root=root_directories, configuration=configuration)

            if '-l' in args or '--log' in args:
                index = args.index('-l') if '-l' in args else args.index('--log')
                args.pop(index)
                if len(args) > index:
                    try:
                        log_level = getattr(logging, args.pop(index).upper())
                    except AttributeError:
                        pass

            logging.basicConfig(format='%(asctime)s (%(name)s): %(message)s', level=log_level)
            logging.Formatter(fmt='%(asctime)s.%(msecs).03d', datefmt='%Y-%m-%d %H:%M:%S')
            ServiceLauncher.run_until_complete(set(args), configuration, watcher)
        sys.exit(0)
Exemplo n.º 3
0
def test_parse_config_file() -> None:
    result = parse_config_files(['tests/configs/config_file.json'])
    expected_result = {
        'options': {
            'http': {
                'port': 4711,
                'access_log': True
            }
        },
        'uuid': '21a24416-1427-4603-c7c9-0ff8ab1f1c20'
    }
    assert result == expected_result
Exemplo n.º 4
0
def test_parse_no_config_file() -> None:
    result = parse_config_files([])
    assert result is None
Exemplo n.º 5
0
    def run_command(self, args: List[str]) -> None:
        if len(args) == 0:
            print(self.run_command_usage())
        else:
            configuration = None
            log_level = logging.INFO

            env_loop = str(os.getenv("TOMODACHI_LOOP", "")).lower() or None

            if env_loop or "--loop" in args:
                if "--loop" in args:
                    index = args.index("--loop")
                    args.pop(index)
                    value = args.pop(index).lower()

                    if env_loop and env_loop != value:
                        print(
                            "Invalid argument to --loop, '{}' differs from env TOMODACHI_LOOP"
                            .format(value))
                        sys.exit(2)
                elif env_loop:
                    value = env_loop
                else:
                    value = "auto"

                if value in ("auto", "default"):
                    pass
                elif value in ("asyncio", "aio", "async"):
                    asyncio.set_event_loop_policy(
                        asyncio.DefaultEventLoopPolicy())
                    pass
                elif value in ("uvloop", "libuv", "uv"):
                    try:
                        import uvloop  # noqa  # isort:skip
                    except Exception:  # pragma: no cover
                        print(
                            "The 'uvloop' package needs to be installed to use uvloop event loop"
                        )
                        sys.exit(2)
                    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
                else:
                    print(
                        "Invalid argument to --loop, event loop '{}' not recognized"
                        .format(value))
                    sys.exit(2)

            if "-c" in args or "--config" in args:
                index = args.index("-c") if "-c" in args else args.index(
                    "--config")
                args.pop(index)

                config_files: List[str] = []
                while len(args) > index and args[index][0] != "-":
                    value = args.pop(index)
                    if value not in config_files:
                        config_files.append(value)

                if not len(config_files):
                    print("Missing config file on command line")
                    sys.exit(2)

                try:
                    configuration = parse_config_files(config_files)
                except FileNotFoundError as e:
                    print("Invalid config file: {}".format(str(e)))
                    sys.exit(2)
                except ValueError as e:
                    print(
                        "Invalid config file, invalid JSON format: {}".format(
                            str(e)))
                    sys.exit(2)

            env_production = str(os.getenv("TOMODACHI_PRODUCTION",
                                           "")).lower() or None
            if env_production and env_production in ("0", "no", "none",
                                                     "false"):
                env_production = None

            if env_production or "--production" in args:
                if "--production" in args:
                    index = args.index("--production")
                    args.pop(index)
                watcher = None
            else:
                cwd = os.path.realpath(os.getcwd())
                root_directories = [cwd]
                for arg in set(args):
                    if not arg.startswith("/") and not arg.startswith("~"):
                        root_directories.append(
                            os.path.realpath(
                                os.path.dirname(os.path.join(cwd, arg))))
                    else:
                        root_directories.append(
                            os.path.realpath(os.path.dirname(arg)))
                for p in str(os.getenv("PYTHONPATH", "")).split(os.pathsep):
                    if not p:
                        continue
                    if not p.startswith("/") and not p.startswith("~"):
                        root_directories.append(
                            os.path.realpath(os.path.join(cwd, p)))
                    else:
                        root_directories.append(os.path.realpath(p))

                from tomodachi.watcher import Watcher  # noqa  #  isort:skip

                watcher = Watcher(root=root_directories,
                                  configuration=configuration)

            if "-l" in args or "--log" in args or "--log-level" in args:
                index = (args.index("-l")
                         if "-l" in args else args.index("--log")
                         if "--log" in args else args.index("--log-level"))
                args.pop(index)
                if len(args) > index:
                    log_level = getattr(logging,
                                        args.pop(index).upper(),
                                        None) or log_level

            logging.basicConfig(format="%(asctime)s (%(name)s): %(message)s",
                                level=log_level)
            logging.Formatter(fmt="%(asctime)s.%(msecs).03d",
                              datefmt="%Y-%m-%d %H:%M:%S")

            ServiceLauncher.run_until_complete(set(args), configuration,
                                               watcher)
        sys.exit(tomodachi.SERVICE_EXIT_CODE)