-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
parse charset and decode text #11
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,21 @@ class DataURIError(ValueError): | |
pass | ||
|
||
|
||
# https://github.com/bottlepy/bottle/commit/fa7733e075da0d790d809aa3d2f53071897e6f76 | ||
class CachedProperty(object): | ||
def __init__(self, func): | ||
self.func = func | ||
|
||
def __get__(self, obj, cls): | ||
if obj is None: | ||
return self | ||
value = obj.__dict__[self.func.__name__] = self.func(obj) | ||
return value | ||
|
||
|
||
cached_property = CachedProperty | ||
|
||
|
||
class ParsedDataURI: | ||
""" | ||
Container for parsed data URIs. | ||
|
@@ -33,6 +48,21 @@ def __init__(self, media_type, data, uri): | |
self.data = data | ||
self.uri = uri | ||
|
||
@cached_property | ||
def charset(self): | ||
prefix = 'charset=' | ||
chunks = self.media_type.split(';') | ||
for chunk in chunks: | ||
if chunk.startswith(prefix): | ||
return chunk[len(prefix):] | ||
return None | ||
|
||
@cached_property | ||
def text(self): | ||
if not self.media_type.startswith('text/'): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. intuitively i'd say utf-8 is a saner and more useful default. or does the spec explicitly forbid that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it is:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok cool ascii it is then |
||
return None | ||
return self.data.decode(self.charset or 'ascii') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will raise There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think, something should be raised to allow lib users to make the decision about such cases themself. |
||
|
||
def __repr__(self): | ||
raw = self.data | ||
if len(raw) > 20: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,6 @@ deps=-rrequirements-test.txt | |
commands= | ||
pytest --cov {envsitepackagesdir}/datauri {posargs} tests/ | ||
flake8 datauri/ | ||
|
||
[flake8] | ||
max-line-length = 90 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this seems unrelated but ok There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, not related to the issue, but with 79 chars I can't even put the link on the source of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
cached_property
(or an aliascached_property = CachedProperty
) makes usage look more like@property
for which this is a drop-in replacement anywayThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed 👍