357857bb7882f0ede56d1dc58f36a159d4aa8e47
3 from typing
import Set
, Dict
, Any
6 class ConfigParseError(Exception):
11 def __init__(self
, config
: "Config", identifier
: str):
13 self
.identifier
= identifier
15 def __eq__(self
, other
):
16 return self
.identifier
== other
.identifier
19 return hash(self
.identifier
)
22 return f
"Person(config=..., identifier={self.identifier!r})"
26 def __init__(self
, bugzilla_url
: str, people
: Dict
[str, Person
], aliases
: Dict
[str, Person
] = None):
27 self
.bugzilla_url
= bugzilla_url
32 aliases
[i
.identifier
] = i
33 self
.aliases
= aliases
36 return f
"Config(bugzilla_url={self.bugzilla_url!r}, " \
37 f
"people={self.people!r}, aliases={self.aliases!r})"
40 def _parse_people(people
: Any
) -> Dict
[str, Person
]:
41 raise NotImplementedError()
44 def _parse_aliases(people
: Dict
[str, Person
], aliases
: Any
) -> Dict
[str, Person
]:
45 if not isinstance(aliases
, dict):
46 raise ConfigParseError("`aliases` entry must be a table")
48 raise NotImplementedError()
52 def _from_toml(parsed_toml
: Dict
[str, Any
]) -> "Config":
56 for k
, v
in parsed_toml
.items():
58 people
= Config
._parse
_people
(v
)
61 elif k
== "bugzilla_url":
62 if not isinstance(v
, str):
63 raise ConfigParseError("`bugzilla_url` must be a string")
66 raise ConfigParseError(f
"unknown config entry: `{k}`")
69 raise ConfigParseError("`people` key is missing")
71 if bugzilla_url
is None:
72 raise ConfigParseError("`bugzilla_url` key is missing")
74 if aliases
is not None:
75 aliases
= Config
._parse
_aliases
(people
, aliases
)
77 return Config(bugzilla_url
=bugzilla_url
,
82 def from_str(text
: str) -> "Config":
84 parsed_toml
= toml
.loads(text
)
85 except toml
.TomlDecodeError
as e
:
86 new_err
= ConfigParseError(f
"TOML parse error: {e}")
87 raise new_err
.with_traceback(sys
.exc_info()[2])
88 return Config
._from
_toml
(parsed_toml
)
91 def from_file(file: Any
) -> "Config":
92 if isinstance(file, list):
93 raise TypeError("list is not a valid file or path")
95 parsed_toml
= toml
.load(file)
96 except toml
.TomlDecodeError
as e
:
97 new_err
= ConfigParseError(f
"TOML parse error: {e}")
98 raise new_err
.with_traceback(sys
.exc_info()[2])
99 return Config
._from
_toml
(parsed_toml
)