Skip to content

Commit

Permalink
cleanup for full PEP-8 compliance (see #36) (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeSmithEU authored and danielthepope committed Mar 14, 2019
1 parent b168c9c commit 626b6da
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 59 deletions.
6 changes: 3 additions & 3 deletions src/conferatur/api/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ def argparser(parser):
parser.add_argument('--entrypoint', default='/api',
help='the jsonrpc api address')
parser.add_argument('--with-explorer', action='store_true',
help='also create the explorer to test api calls with, this is a rudimentary feature currently '
help='also create the explorer to test api calls with, '
'this is a rudimentary feature currently '
'only meant for testing and debugging')
parser.add_argument('--list-methods', action='store_true',
help='list the available jsonrpc methods')
return parser


def create_app(entrypoint: str=None, with_explorer: bool=None):
def create_app(entrypoint: str = None, with_explorer: bool = None):
"""
Create the Flask app
Expand Down Expand Up @@ -90,4 +91,3 @@ def main(parser, args):
else:
app = create_app(args.entrypoint, args.with_explorer)
app.run(host=args.host, port=args.port, debug=args.debug)

1 change: 0 additions & 1 deletion src/conferatur/api/jsonrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,3 @@ def _help():
for name, func in methods.items.items()}

return methods

5 changes: 1 addition & 4 deletions src/conferatur/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def next_field():
yield yield_line()


def reader(file: typing.io.TextIO, dialect: typing.Union[None, str, Dialect]=None) -> Reader:
def reader(file: typing.io.TextIO, dialect: typing.Union[None, str, Dialect] = None) -> Reader:
if dialect is None:
dialect = DefaultDialect
elif type(dialect) is str:
Expand All @@ -285,6 +285,3 @@ def reader(file: typing.io.TextIO, dialect: typing.Union[None, str, Dialect]=Non
dialect = known_dialects[dialect]

return Reader(file, dialect)



23 changes: 15 additions & 8 deletions src/conferatur/docblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@
logger = logging.getLogger(__name__)

Docblock = namedtuple('Docblock', ['docs', 'params', 'result', 'result_type'])
Param = namedtuple('Param', ['name', 'type', 'type_doc', 'is_required', 'description', 'examples'])
Param = namedtuple('Param', ['name', 'type', 'type_doc', 'is_required',
'description', 'examples'])
DocblockParam = namedtuple('DocblockParam', ['name', 'type', 'value'])


def format_docs(docs):
return textwrap.dedent(docs).strip()


def doc_param_parser(docstring, key, no_name=None, allow_multiple=None, replace_strat=None):
def doc_param_parser(docstring, key, no_name=None, allow_multiple=None,
replace_strat=None):
results = [] if no_name or allow_multiple else {}

if replace_strat is None:
Expand All @@ -44,7 +46,8 @@ def _(match):
param = DocblockParam(**param)
if allow_multiple:
# check if it already exists, if not create a new object
idx = [idx for idx, val in enumerate(results) if match[2] not in val]
idx = [idx for idx, val in enumerate(results)
if match[2] not in val]
if not len(idx):
idx = len(results)
results.append({})
Expand All @@ -61,7 +64,9 @@ def _(match):
else:
regex = r'^[ \t]*:%s[ \t]+(?:([^:]+)[ \t]+)?([a-z_]+):(?:[ \t]+(.*))?$'

docs = re.sub(regex % (re.escape(key),), _, docstring, flags=re.MULTILINE).strip()
docs = re.sub(
regex % (re.escape(key),), _, docstring, flags=re.MULTILINE
).strip()

return docs, results

Expand Down Expand Up @@ -95,7 +100,8 @@ def decode_examples(match, param):
param['value'] = decode_literal(param['value'])
return ''

docs, examples = doc_param_parser(docs, 'example', allow_multiple=True, replace_strat=decode_examples)
docs, examples = doc_param_parser(docs, 'example', allow_multiple=True,
replace_strat=decode_examples)

params = []
for idx, name in enumerate(args):
Expand All @@ -106,7 +112,8 @@ def decode_examples(match, param):
description = doc_params[name].value

param = Param(name,
argspec.annotations[name] if name in argspec.annotations else None,
argspec.annotations[name] if name in argspec.annotations
else None,
type_,
idx < defaults_idx,
description,
Expand All @@ -132,5 +139,5 @@ def apply_template(self):
def rst_to_html(text):
writer = HTML5Writer()
settings = {'output_encoding': 'unicode', 'table_style': 'table'}
return publish_string(text, writer=writer, writer_name='html5', settings_overrides=settings)

return publish_string(text, writer=writer, writer_name='html5',
settings_overrides=settings)
1 change: 0 additions & 1 deletion src/conferatur/normalization/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,3 @@ def main(parser, args):
output_file = output_files[0]
output_file.write(text)
output_file.close()

60 changes: 41 additions & 19 deletions src/conferatur/normalization/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ def normalize(text: str) -> str:

class LocalizedFile:
"""
Reads and applies normalization rules from a locale-based file, it will automatically determine the "best fit" for a given locale, if one is available.
Reads and applies normalization rules from a locale-based file, it will
automatically determine the "best fit" for a given locale, if one is
available.
:param str|class normalizer: Normalizer name (or class)
:param str locale: Which locale to search for
Expand All @@ -44,7 +46,8 @@ class LocalizedFile:
def __init__(self, normalizer, locale: str, path: str, encoding=None):
path = os.path.realpath(path)
if not os.path.isdir(path):
raise NotADirectoryError("Expected '%s' to be a directory" % (str(path),))
raise NotADirectoryError("Expected '%s' to be a directory" %
(str(path),))

files = {standardize_tag(file): file
for file in os.listdir(path)
Expand All @@ -53,7 +56,9 @@ def __init__(self, normalizer, locale: str, path: str, encoding=None):
locale = standardize_tag(locale)
match = best_match(locale, files.keys())[0]
if match == 'und':
raise FileNotFoundError("Could not find a locale file for locale '%s' in '%s'" % (locale, str(path)))
raise FileNotFoundError(
"Could not find a locale file for locale '%s' in '%s'" %
(locale, str(path)))

file = os.path.join(path, files[match])

Expand All @@ -80,7 +85,7 @@ class Replace:
:example return: "Nudge wink!"
"""

def __init__(self, search: str, replace: str=''):
def __init__(self, search: str, replace: str = ''):
self._search = search
self._replace = replace

Expand All @@ -106,7 +111,7 @@ class ReplaceWords:
def __init__(self, search: str, replace: str):
search = search.strip()
replace = replace.strip()

args = tuple(map(re.escape, [
search[0].upper(),
search[0].lower(),
Expand Down Expand Up @@ -144,7 +149,8 @@ class File:

def __init__(self, normalizer, file, encoding=None):
try:
cls = normalizer if inspect.isclass(normalizer) else normalization.name_to_normalizer(normalizer)
cls = normalizer if inspect.isclass(normalizer) else \
normalization.name_to_normalizer(normalizer)
except ValueError:
raise ValueError("Unknown normalizer %s" %
(repr(normalizer)))
Expand Down Expand Up @@ -173,7 +179,8 @@ class RegexReplace:
Case-insensitivity is supported by adding inline modifiers.
You might want to use capturing groups to preserve the case. When replacing a character not captured, the information about its case is lost...
You might want to use capturing groups to preserve the case. When replacing
a character not captured, the information about its case is lost...
Eg. would replace "HAHA! Hahaha!" to "HeHe! Hehehe!":
Expand All @@ -184,7 +191,8 @@ class RegexReplace:
+------------------+-------------+
No regex flags are set by default, you can set them yourself though in the regex, and combine them at will, eg. multiline, dotall and ignorecase.
No regex flags are set by default, you can set them yourself though in the
regex, and combine them at will, eg. multiline, dotall and ignorecase.
Eg. would replace "New<CRLF>line" to "newline":
Expand All @@ -200,7 +208,7 @@ class RegexReplace:
:example return: "HeHe! Hehehe!"
"""

def __init__(self, search: str, replace: str=None):
def __init__(self, search: str, replace: str = None):
self._pattern = re.compile(search)
self._substitution = replace if replace is not None else ''

Expand All @@ -223,7 +231,8 @@ def __init__(self):

class AlphaNumericUnicode(RegexReplace):
"""
Simple alphanumeric filter, takes into account all unicode alphanumeric characters
Simple alphanumeric filter, takes into account all unicode alphanumeric
characters.
:example text: "Das, öder die Flipper-Wåld Gespütt!"
:example return: "DasöderdieFlipperWåldGespütt"
Expand All @@ -249,7 +258,8 @@ def normalize(self, text: str) -> str:

class Unidecode:
"""
Unidecode characters to ASCII form, see `Python's Unidecode package <https://pypi.org/project/Unidecode>`_ for more info.
Unidecode characters to ASCII form, see `Python's Unidecode package
<https://pypi.org/project/Unidecode>`_ for more info.
:example text: "𝖂𝖊𝖓𝖓 𝖎𝖘𝖙 𝖉𝖆𝖘 𝕹𝖚𝖓𝖘𝖙ü𝖈𝖐 𝖌𝖎𝖙 𝖚𝖓𝖉 𝕾𝖑𝖔𝖙𝖊𝖗𝖒𝖊𝖞𝖊𝖗?"
:example return: "Wenn ist das Nunstuck git und Slotermeyer?"
Expand All @@ -262,23 +272,29 @@ def normalize(self, text: str) -> str:

class Config:
r"""
Use config notation to define normalization rules. This notation is a list of normalizers, one per line, with optional arguments (separated by a space).
Use config notation to define normalization rules. This notation is a
list of normalizers, one per line, with optional arguments (separated by a
space).
The normalizers can be any of the core normalizers, or you can refer to your own normalizer class (like you would use in a python import, eg. `my.own.package.MyNormalizerClass`).
The normalizers can be any of the core normalizers, or you can refer to your
own normalizer class (like you would use in a python import, eg.
`my.own.package.MyNormalizerClass`).
Additional rules:
- Normalizer names are case-insensitive.
- Arguments MAY be wrapped in double quotes.
- If an argument contains a space, newline or double quote, it MUST be wrapped in double quotes.
- A double quote itself is represented in this quoted argument as two double quotes: `""`.
- If an argument contains a space, newline or double quote, it MUST be
wrapped in double quotes.
- A double quote itself is represented in this quoted argument as two
double quotes: `""`.
The normalization rules are applied top-to-bottom and follow this format:
.. code-block:: text
Normalizer1 arg1 "arg 2"
# This is a comment
Normalizer2
# (Normalizer2 has no arguments)
Normalizer3 "This is argument 1
Expand All @@ -289,7 +305,13 @@ class Config:
:param str config: configuration text
:example text: "He bravely turned his tail and fled"
:example config: '# using a simple config file\nLowercase \n\n # it even supports comments\n# If there is a space in the argument, make sure you quote it though!\n regexreplace "y t" "Y T"\n \n # extraneous whitespaces are ignored \n replace e a\n'
:example config: '''# using a simple config file\nLowercase \n
# it even supports comments
# If there is a space in the argument, make sure you quote it though!
regexreplace "y t" "Y T"
\n\n
# extraneous whitespaces are ignored
replace e a\n'''
:example return: "ha bravalY Turnad his tail and flad"
"""

Expand All @@ -313,7 +335,8 @@ def normalize(self, text: str) -> str:

class ConfigFile(Config):
"""
Load config from a file, see :py:class:`Config` for information about config notation
Load config from a file, see :py:class:`Config` for information about config
notation
:param typing.io.TextIO file: The file
:param str encoding: The file encoding
Expand All @@ -330,4 +353,3 @@ def __init__(self, file, encoding=None):

with open(file, encoding=encoding) as f:
self._parse_config(f)

1 change: 0 additions & 1 deletion src/conferatur/normalization/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,3 @@ def _(cls, text):
normalize_stack.pop()
return result
return _

Loading

0 comments on commit 626b6da

Please sign in to comment.