Skip to content

Commit

Permalink
Add some types
Browse files Browse the repository at this point in the history
  • Loading branch information
Josef-Friedrich committed Jun 23, 2022
1 parent 9f1c009 commit f4f22ed
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 33 deletions.
53 changes: 35 additions & 18 deletions tmep/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,45 @@
from tmep import functions
import textwrap
import typing
from typing import List, Dict
Functions = functions.Functions


FunctionDoc = Dict[str, str]


class Doc(object):

synopsises: FunctionDoc
examples: FunctionDoc
descriptions: FunctionDoc
functions: List[str]

def __init__(self):
functions_ = Functions()
functions = functions_.functions()
self.synopsises = {}
self.examples = {}
self.descriptions = {}
self.functions = []
self.synopsises: FunctionDoc = {}
self.examples: FunctionDoc = {}
self.descriptions: FunctionDoc = {}
self.functions: List[str] = []
for name, function in functions.items():
self.functions.append(name)
doc = function.__doc__
if doc:
doc = self.prepare_docstrings(doc)
self.synopsises[name] = self.extract_value(doc, 'synopsis')
self.examples[name] = self.extract_value(doc, 'example')
self.descriptions[name] = self.extract_value(
synopse = self.extract_value(doc, 'synopsis')
if synopse:
self.synopsises[name] = synopse

example = self.extract_value(doc, 'example')
if example:
self.examples[name] = example

description = self.extract_value(
doc, 'description', False
)
if description:
self.descriptions[name] = description
self.functions.sort()

def prepare_docstrings(self, string: str) -> str:
Expand Down Expand Up @@ -66,18 +83,18 @@ def format(self, text: str, width: int = 78, indent: int = 4) -> str:
subsequent_indent=' ' * indent,
)

def get(self):
def get(self) -> str:
"""Retrieve a formated text string"""
output = ''
for f in self.functions:
output += self.underline(f) + '\n\n'
if f in self.synopsises and isinstance(self.synopsises[f], str):
output += self.format(self.synopsises[f]) + '\n'
if f in self.descriptions and isinstance(
self.descriptions[f], str
):
output += self.format(self.descriptions[f], indent=8) + '\n'
if f in self.examples and isinstance(self.examples[f], str):
output += self.format(self.examples[f], indent=8) + '\n'
for function_name in self.functions:
output += self.underline(function_name) + '\n\n'
if function_name in self.synopsises:
output += self.format(self.synopsises[function_name]) + '\n'
if function_name in self.descriptions:
output += self.format(
self.descriptions[function_name], indent=8) + '\n'
if function_name in self.examples:
output += self.format(
self.examples[function_name], indent=8) + '\n'
output += '\n'
return output
11 changes: 5 additions & 6 deletions tmep/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
import re
import textwrap
import typing
from typing import Optional
from unidecode import unidecode

from . types import Values, FunctionCollection


def _int_arg(s: str) -> int:
"""Convert a string argument to an integer for use in a template
Expand All @@ -29,10 +32,6 @@ def _int_arg(s: str) -> int:
return int(s.strip())


FunctionCollection = typing.Dict[str, typing.Callable[..., str]]
Values = typing.Optional[typing.Dict[str, str]]


class Functions:
"""A container class for the default functions provided to path
templates. These functions are contained in an object to provide
Expand All @@ -41,11 +40,11 @@ class Functions:
"""
prefix = 'tmpl_'

values: Values
values: Optional[Values]

func_names: typing.List[str]

def __init__(self, values: Values = None):
def __init__(self, values: Optional[Values] = None):
"""Parametrize the functions.
"""
self.values = values
Expand Down
11 changes: 8 additions & 3 deletions tmep/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import sys
import functools

from .types import Values, FunctionCollection

SYMBOL_DELIM = '$'
FUNC_DELIM = '%'
GROUP_OPEN = '{'
Expand Down Expand Up @@ -561,15 +563,17 @@ def __init__(self, template: str):
def __eq__(self, other):
return self.original == other.original

def interpret(self, values={}, functions={}):
def interpret(self, values: Values = {},
functions: FunctionCollection = {}):
"""Like `substitute`, but forces the interpreter (rather than
the compiled version) to be used. The interpreter includes
exception-handling code for missing variables and buggy template
functions but is much slower.
"""
return self.expr.evaluate(Environment(values, functions))

def substitute(self, values={}, functions={}):
def substitute(self, values: Values = {},
functions: FunctionCollection = {}):
"""Evaluate the template given the values and functions.
"""
try:
Expand All @@ -594,7 +598,8 @@ def translate(self):
[ast.Return(ast.List(expressions, ast.Load()))],
)

def wrapper_func(values={}, functions={}):
def wrapper_func(values: Values = {},
functions: FunctionCollection = {}):
args = {}
for varname in varnames:
args[VARIABLE_PREFIX + varname] = values[varname]
Expand Down
5 changes: 5 additions & 0 deletions tmep/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import typing

Values = typing.Dict[str, typing.Any]

FunctionCollection = typing.Dict[str, typing.Callable[..., str]]
13 changes: 7 additions & 6 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
[tox]
envlist = py36, py37, py38, docs, flake8
envlist = py38, py39, py310, docs, flake8

[testenv]
basepython = python3.8
deps = nose
commands = nosetests

[testenv:py36]
basepython = python3.6

[testenv:py37]
basepython = python3.7

[testenv:py38]
basepython = python3.8

[testenv:py39]
basepython = python3.9

[testenv:py310]
basepython = python3.10

[testenv:docs]
deps =
sphinx
Expand Down

0 comments on commit f4f22ed

Please sign in to comment.