-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* BREAKING CHANGE: Functions now have parameters * Added gender parameter to random_first_name * Added random_int function * Added tests for functions
- Loading branch information
Umut Seven
committed
Feb 10, 2021
1 parent
7c9d144
commit 326a6b2
Showing
12 changed files
with
270 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import re | ||
from typing import List, Tuple, Dict | ||
|
||
from apyr.exceptions import FunctionException | ||
from apyr.models import ContentFunction | ||
|
||
|
||
class FunctionHandler: | ||
@staticmethod | ||
def parse_content(content: str) -> List[ContentFunction]: | ||
regex = re.compile( | ||
r"(?P<full>%(?P<name>[^%]+?)\((?P<params>.*?)\)%)", flags=re.M | ||
) | ||
match: List[Tuple[str, str, str]] = regex.findall(content) | ||
|
||
functions: List[ContentFunction] = [] | ||
|
||
for capture in match: | ||
full, name, params = capture | ||
param_mapped = list( | ||
map(lambda x: x.strip(), params.split(",")) | ||
) # Remove whitespace from params | ||
param_list = list( | ||
filter(lambda x: x != "", param_mapped) | ||
) # Filter out empty params | ||
functions.append(ContentFunction(full=full, name=name, params=param_list)) | ||
|
||
return functions | ||
|
||
@staticmethod | ||
def execute_functions( | ||
content_functions: List[ContentFunction], functions: Dict, content: str | ||
) -> str: | ||
for content_function in content_functions: | ||
fun = functions.get(content_function.name) | ||
|
||
if fun is None: | ||
print(f"Function {content_function.name} not found. Skipping..") | ||
continue | ||
|
||
try: | ||
result = fun(*content_function.params) | ||
except Exception as ex: | ||
raise FunctionException(content_function.full, str(ex)) from ex | ||
|
||
content = content.replace(content_function.full, str(result)) | ||
|
||
return content | ||
|
||
@staticmethod | ||
def run(content: str, functions: Dict) -> str: | ||
content_functions = FunctionHandler.parse_content(content) | ||
return FunctionHandler.execute_functions(content_functions, functions, content) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import random | ||
from typing import Optional | ||
|
||
import names | ||
|
||
|
||
def random_first_name(gender: Optional[str]) -> str: | ||
return names.get_first_name(gender) | ||
|
||
|
||
def random_last_name() -> str: | ||
return names.get_last_name() | ||
|
||
|
||
def random_int(start: str, end: str) -> int: | ||
return random.randint(int(start), int(end)) | ||
|
||
|
||
FUNCTIONS = { | ||
"random_first_name": random_first_name, | ||
"random_last_name": random_last_name, | ||
"random_int": random_int, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.