import re
from collections import OrderedDict, namedtuple
from email.errors import HeaderParseError
-from typing import Tuple, Optional, List, Callable, Union, TypeVar, Dict
+from typing import Tuple, Optional, List, Callable, Union, TypeVar, Dict, NamedTuple
import mwparserfromhell # https://github.com/earwig/mwparserfromhell
'[[Birgitzer Alm]]' => '[[Birgitzer Alm]]'
"""
if re.match(r'\[\[[^\[\]]+]]$', value) is None:
- raise ValueError('No valid wiki page name "{}"'.format(value))
+ raise ValueError(f'No valid wiki page name "{value}"')
return value
try:
email.headerregistry.Address(addr_spec=value)
except HeaderParseError as e:
- raise ValueError('Invalid email address: {}'.format(value), e)
+ raise ValueError(f'Invalid email address: {value}', e)
return value
# longitude/latitude converter
# ----------------------------
-LonLat = namedtuple('LonLat', ['lon', 'lat'])
-
-
-lonlat_none = LonLat(None, None)
+class LonLat(NamedTuple):
+ lon: float
+ lat: float
def lonlat_from_str(value: str) -> LonLat:
def lonlat_to_str(value: LonLat) -> str:
- return '{:.6f} N {:.6f} E'.format(value.lat, value.lon)
+ return f'{value.lat:.6f} N {value.lon:.6f} E'
-def opt_lonlat_from_str(value: str) -> LonLat:
- return opt_from_str(value, lonlat_from_str, lonlat_none)
+def opt_lonlat_from_str(value: str) -> Optional[LonLat]:
+ return opt_from_str(value, lonlat_from_str, None)
-def opt_lonlat_to_str(value: LonLat) -> str:
- return opt_to_str(value, lonlat_to_str, lonlat_none)
+def opt_lonlat_to_str(value: Optional[LonLat]) -> str:
+ return opt_to_str(value, lonlat_to_str, None)
opt_lonlat_converter = FromToConverter(opt_lonlat_from_str, opt_lonlat_to_str)
for key, converter in converter_dict.items():
try:
if not template.has(key):
- raise ValueError('Missing parameter "{}"'.format(key))
+ raise ValueError(f'Missing parameter "{key}"')
result[key] = converter.from_str(str(template.get(key).value.strip()))
except ValueError as e:
exceptions_dict[key] = e
wikicode = mwparserfromhell.parse(value)
template_list = wikicode.filter_templates(recursive=False, matches=lambda t: t.name.strip() == name)
if len(template_list) == 0:
- raise ValueError('No "{}" template was found'.format(name))
+ raise ValueError(f'No "{name}" template was found')
if len(template_list) > 1:
- raise ValueError('{} "{}" templates were found'.format(len(template_list), name))
+ raise ValueError(f'{len(template_list)} "{name}" templates were found')
return template_list[0]