-
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.
Merge pull request #3 from tmichela/refactor1
Refactor1
- Loading branch information
Showing
2 changed files
with
90 additions
and
97 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from functools import partial, wraps | ||
import re | ||
from time import sleep | ||
|
||
import pypandoc | ||
|
||
|
||
def textile_to_md(text): | ||
text = pypandoc.convert_text(text, to='markdown_github', format='textile') | ||
return re.sub(r'\\(.)', r'\1', text) | ||
|
||
|
||
def indent(text, offset=3): | ||
"""Indent text with offset * 4 blank spaces | ||
""" | ||
def indented_lines(): | ||
for ix, line in enumerate(text.splitlines(True)): | ||
if ix == 0: | ||
yield line | ||
else: | ||
yield ' ' * offset + line if line.strip() else line | ||
return ''.join(indented_lines()) | ||
|
||
|
||
def retry(func=None, *, attempts=1, delay=0, exc=(Exception,)): | ||
"""Re-execute decorated function. | ||
:attemps int: number of tries, default 1 | ||
:delay float: timeout between each tries in seconds, default 0 | ||
:exc tuple: collection of exceptions to be caugth | ||
""" | ||
if func is None: | ||
return partial(retry, attempts=attempts, delay=delay, exc=exc) | ||
|
||
@wraps(func) | ||
def retried(*args, **kwargs): | ||
retry._tries[func.__name__] = 0 | ||
for i in reversed(range(attempts)): | ||
retry._tries[func.__name__] += 1 | ||
try: | ||
ret = func(*args, *kwargs) | ||
except exc: | ||
if i <= 0: | ||
raise | ||
sleep(delay) | ||
continue | ||
else: | ||
break | ||
return ret | ||
|
||
retry._tries = {} | ||
return retried |