2018-11-30 23:39:13 +03:00
|
|
|
import csv
|
2018-12-03 17:56:38 +03:00
|
|
|
import enum
|
2019-05-26 22:11:24 +03:00
|
|
|
import warnings
|
2018-12-03 17:56:38 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Usability(enum.Enum):
|
|
|
|
UNKNOWN = 0
|
|
|
|
USER = 1
|
|
|
|
BOT = 2
|
|
|
|
BOTH = 4
|
2018-11-30 23:39:13 +03:00
|
|
|
|
2023-01-11 21:53:20 +03:00
|
|
|
@property
|
|
|
|
def key(self):
|
|
|
|
return {
|
|
|
|
Usability.UNKNOWN: 'unknown',
|
|
|
|
Usability.USER: 'user',
|
|
|
|
Usability.BOT: 'bot',
|
|
|
|
Usability.BOTH: 'both',
|
|
|
|
}[self]
|
|
|
|
|
2018-11-30 23:39:13 +03:00
|
|
|
|
|
|
|
class MethodInfo:
|
2019-05-26 22:11:24 +03:00
|
|
|
def __init__(self, name, usability, errors, friendly):
|
2018-11-30 23:39:13 +03:00
|
|
|
self.name = name
|
|
|
|
self.errors = errors
|
2019-05-26 22:11:24 +03:00
|
|
|
self.friendly = friendly
|
2018-12-03 17:56:38 +03:00
|
|
|
try:
|
|
|
|
self.usability = {
|
|
|
|
'unknown': Usability.UNKNOWN,
|
|
|
|
'user': Usability.USER,
|
|
|
|
'bot': Usability.BOT,
|
|
|
|
'both': Usability.BOTH,
|
|
|
|
}[usability.lower()]
|
|
|
|
except KeyError:
|
|
|
|
raise ValueError('Usability must be either user, bot, both or '
|
|
|
|
'unknown, not {}'.format(usability)) from None
|
2018-11-30 23:39:13 +03:00
|
|
|
|
|
|
|
|
2019-05-26 22:11:24 +03:00
|
|
|
def parse_methods(csv_file, friendly_csv_file, errors_dict):
|
2018-11-30 23:39:13 +03:00
|
|
|
"""
|
|
|
|
Parses the input CSV file with columns (method, usability, errors)
|
|
|
|
and yields `MethodInfo` instances as a result.
|
|
|
|
"""
|
2019-05-26 22:11:24 +03:00
|
|
|
raw_to_friendly = {}
|
|
|
|
with friendly_csv_file.open(newline='') as f:
|
|
|
|
f = csv.reader(f)
|
|
|
|
next(f, None) # header
|
|
|
|
for ns, friendly, raw_list in f:
|
|
|
|
for raw in raw_list.split():
|
|
|
|
raw_to_friendly[raw] = (ns, friendly)
|
|
|
|
|
2018-12-21 15:24:16 +03:00
|
|
|
with csv_file.open(newline='') as f:
|
2018-11-30 23:39:13 +03:00
|
|
|
f = csv.reader(f)
|
|
|
|
next(f, None) # header
|
|
|
|
for line, (method, usability, errors) in enumerate(f, start=2):
|
|
|
|
try:
|
|
|
|
errors = [errors_dict[x] for x in errors.split()]
|
|
|
|
except KeyError:
|
|
|
|
raise ValueError('Method {} references unknown errors {}'
|
|
|
|
.format(method, errors)) from None
|
|
|
|
|
2019-05-26 22:11:24 +03:00
|
|
|
friendly = raw_to_friendly.pop(method, None)
|
|
|
|
yield MethodInfo(method, usability, errors, friendly)
|
|
|
|
|
|
|
|
if raw_to_friendly:
|
|
|
|
warnings.warn('note: unknown raw methods in friendly mapping: {}'
|
|
|
|
.format(', '.join(raw_to_friendly)))
|