-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathcommon.py
More file actions
148 lines (120 loc) · 4.35 KB
/
Copy pathcommon.py
File metadata and controls
148 lines (120 loc) · 4.35 KB
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
38
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
81
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
110
111
112
113
114
115
116
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
146
147
148
# Copyright (C) 2021-2022 Intel Corporation
# Copyright (C) CVAT.ai Corporation
#
# SPDX-License-Identifier: MIT
import argparse
import importlib
import importlib.util
import logging
import sys
from http.client import HTTPConnection
from pathlib import Path
from typing import Any
import attrs
import cvat_sdk.auto_annotation as cvataa
from cvat_sdk.core.auth import (
ClientAuthParameters,
configure_client_auth_arguments,
make_client_from_cli,
)
from cvat_sdk.core.client import Client
from cvat_sdk.core.exceptions import AuthStoreError
from ..version import VERSION
from .parsers import BuildDictAction, parse_function_parameter
from .utils import popattr
class CriticalError(Exception):
pass
def configure_common_arguments(parser: argparse.ArgumentParser) -> None:
parser.add_argument("--version", action="version", version=VERSION)
configure_client_auth_arguments(parser)
parser.add_argument(
"--debug",
action="store_const",
dest="loglevel",
const=logging.DEBUG,
default=logging.INFO,
help="show debug output",
)
def configure_logger(logger: logging.Logger, parsed_args: argparse.Namespace) -> None:
level = popattr(parsed_args, "loglevel")
formatter = logging.Formatter(
"[%(asctime)s] %(levelname)s: %(message)s", datefmt="%Y-%m-%d %H:%M:%S", style="%"
)
handler = logging.StreamHandler(sys.stderr)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(level)
if level <= logging.DEBUG:
HTTPConnection.debuglevel = 1
def build_client(parsed_args: argparse.Namespace, logger: logging.Logger) -> Client:
auth_args = ClientAuthParameters.from_namespace(parsed_args)
for field in attrs.fields(ClientAuthParameters):
popattr(parsed_args, field.name)
try:
client = make_client_from_cli(auth_args, logger=logger)
except AuthStoreError as e:
raise CriticalError(str(e)) from e
client.check_server_version(fail_if_unsupported=False)
return client
def configure_function_implementation_arguments(parser: argparse.ArgumentParser) -> None:
function_group = parser.add_mutually_exclusive_group(required=True)
function_group.add_argument(
"--function-module",
metavar="MODULE",
help="qualified name of a module to use as the function",
)
function_group.add_argument(
"--function-file",
metavar="PATH",
type=Path,
help="path to a Python source file to use as the function",
)
parser.add_argument(
"--function-parameter",
"-p",
metavar="NAME=TYPE:VALUE",
type=parse_function_parameter,
action=BuildDictAction,
dest="function_parameters",
help="parameter for the function",
)
original_executor = parser.get_default("_executor")
def execute_with_function_loader(
client,
*,
function_module: str | None,
function_file: Path | None,
function_parameters: dict[str, Any],
**kwargs,
):
original_executor(
client,
function_loader=FunctionLoader(function_module, function_file, function_parameters),
**kwargs,
)
parser.set_defaults(_executor=execute_with_function_loader)
@attrs.frozen
class FunctionLoader:
function_module: str | None
function_file: Path | None
function_parameters: dict[str, Any]
def __attrs_post_init__(self):
assert self.function_module is not None or self.function_file is not None
def load(self) -> cvataa.AutoAnnotationFunction:
if self.function_module is not None:
function = importlib.import_module(self.function_module)
else:
module_spec = importlib.util.spec_from_file_location(
"__cvat_function__", self.function_file
)
function = importlib.util.module_from_spec(module_spec)
module_spec.loader.exec_module(function)
if hasattr(function, "create"):
# this is actually a function factory
function = function.create(**self.function_parameters)
else:
if self.function_parameters:
raise TypeError("function takes no parameters")
if not hasattr(function, "spec"):
raise cvataa.BadFunctionError("function has no 'spec' attribute")
return function