forked from yandex/gixy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
107 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from __future__ import absolute_import | ||
from jinja2 import Environment, PackageLoader | ||
|
||
from gixy.utils.text import to_text | ||
|
||
|
||
def load_template(name): | ||
env = Environment(loader=PackageLoader('gixy', 'formatters/templates'), trim_blocks=True, lstrip_blocks=True) | ||
env.filters['to_text'] = to_text_filter | ||
return env.get_template(name) | ||
|
||
|
||
def to_text_filter(text): | ||
try: | ||
return text.encode('latin1').decode('utf-8') | ||
except UnicodeEncodeError: | ||
return to_text(text) |
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 |
---|---|---|
@@ -1,14 +1,13 @@ | ||
from __future__ import absolute_import | ||
from jinja2 import Environment, PackageLoader | ||
|
||
from gixy.formatters.base import BaseFormatter | ||
from gixy.formatters._jinja import load_template | ||
|
||
|
||
class ConsoleFormatter(BaseFormatter): | ||
def __init__(self): | ||
super(ConsoleFormatter, self).__init__() | ||
env = Environment(loader=PackageLoader('gixy', 'formatters/templates'), trim_blocks=True, lstrip_blocks=True) | ||
self.template = env.get_template('console.j2') | ||
self.template = load_template('console.j2') | ||
|
||
def format_reports(self, reports, stats): | ||
return self.template.render(reports=reports, stats=stats) |
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 |
---|---|---|
@@ -1,14 +1,13 @@ | ||
from __future__ import absolute_import | ||
from jinja2 import Environment, PackageLoader | ||
|
||
from gixy.formatters.base import BaseFormatter | ||
from gixy.formatters._jinja import load_template | ||
|
||
|
||
class TextFormatter(BaseFormatter): | ||
def __init__(self): | ||
super(TextFormatter, self).__init__() | ||
env = Environment(loader=PackageLoader('gixy', 'formatters/templates'), trim_blocks=True, lstrip_blocks=True) | ||
self.template = env.get_template('text.j2') | ||
self.template = load_template('text.j2') | ||
|
||
def format_reports(self, reports, stats): | ||
return self.template.render(reports=reports, stats=stats) |
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
Empty file.
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,72 @@ | ||
from __future__ import absolute_import | ||
from six import PY3, text_type, binary_type | ||
|
||
|
||
def to_bytes(obj, encoding='latin1', errors='strict', nonstring='replace'): | ||
if isinstance(obj, binary_type): | ||
return obj | ||
|
||
if isinstance(obj, text_type): | ||
try: | ||
# Try this first as it's the fastest | ||
return obj.encode(encoding, errors) | ||
except UnicodeEncodeError: | ||
return b'failed_to_encode' | ||
|
||
if nonstring == 'simplerepr': | ||
try: | ||
|
||
value = str(obj) | ||
except UnicodeError: | ||
try: | ||
value = repr(obj) | ||
except UnicodeError: | ||
# Giving up | ||
return b'failed_to_encode' | ||
elif nonstring == 'passthru': | ||
return obj | ||
elif nonstring == 'replace': | ||
return b'failed_to_encode' | ||
elif nonstring == 'strict': | ||
raise TypeError('obj must be a string type') | ||
else: | ||
raise TypeError('Invalid value %s for to_bytes\' nonstring parameter' % nonstring) | ||
|
||
return to_bytes(value, encoding, errors) | ||
|
||
|
||
def to_text(obj, encoding='latin1', errors='strict', nonstring='replace'): | ||
if isinstance(obj, text_type): | ||
return obj | ||
|
||
if isinstance(obj, binary_type): | ||
try: | ||
return obj.decode(encoding, errors) | ||
except UnicodeEncodeError: | ||
return u'failed_to_encode' | ||
|
||
if nonstring == 'simplerepr': | ||
try: | ||
value = str(obj) | ||
except UnicodeError: | ||
try: | ||
value = repr(obj) | ||
except UnicodeError: | ||
# Giving up | ||
return u'failed_to_encode' | ||
elif nonstring == 'passthru': | ||
return obj | ||
elif nonstring == 'replace': | ||
return u'failed_to_encode' | ||
elif nonstring == 'strict': | ||
raise TypeError('obj must be a string type') | ||
else: | ||
raise TypeError('Invalid value %s for to_text\'s nonstring parameter' % nonstring) | ||
|
||
return to_text(value, encoding, errors) | ||
|
||
|
||
if PY3: | ||
to_native = to_text | ||
else: | ||
to_native = to_bytes |
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