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 #40 from mpenkov/dev
Thanks.
- Loading branch information
Showing
3 changed files
with
152 additions
and
11 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 |
---|---|---|
@@ -1,8 +1,65 @@ | ||
# | ||
# Names of encodings that we publicly support. | ||
# | ||
ASCII = 'ascii' | ||
JIS = 'jis' | ||
UNICODE = 'unicode' | ||
ENCODINGS = (ASCII, JIS, UNICODE) | ||
|
||
# return "str" | ||
def dump_user_comment(string): | ||
return None | ||
# | ||
# The actual encodings accepted by the standard library differ slightly from | ||
# the above. | ||
# | ||
_JIS = 'shift_jis' | ||
_UNICODE = 'utf_16_be' | ||
|
||
# return "bytes" | ||
def load_user_comment(binary): | ||
return None | ||
_PREFIX_SIZE = 8 | ||
# | ||
# From Table 9: Character Codes and their Designation | ||
# | ||
_ASCII_PREFIX = b'\x41\x53\x43\x49\x49\x00\x00\x00' | ||
_JIS_PREFIX = b'\x4a\x49\x53\x00\x00\x00\x00\x00' | ||
_UNICODE_PREFIX = b'\x55\x4e\x49\x43\x4f\x44\x45\x00' | ||
_UNDEFINED_PREFIX = b'\x00\x00\x00\x00\x00\x00\x00\x00' | ||
|
||
|
||
def load_user_comment(data): | ||
""" | ||
Convert "UserComment" value in exif format to str. | ||
:param bytes data: "UserComment" value from exif | ||
:return: u"foobar" | ||
:rtype: str(Unicode) | ||
:raises: ValueError if the data does not conform to the EXIF specification, | ||
or the encoding is unsupported. | ||
""" | ||
if len(data) < _PREFIX_SIZE: | ||
raise ValueError('not enough data to decode UserComment') | ||
prefix = data[:_PREFIX_SIZE] | ||
body = data[_PREFIX_SIZE:] | ||
if prefix == _UNDEFINED_PREFIX: | ||
raise ValueError('prefix is UNDEFINED, unable to decode UserComment') | ||
try: | ||
encoding = { | ||
_ASCII_PREFIX: ASCII, _JIS_PREFIX: _JIS, _UNICODE_PREFIX: _UNICODE, | ||
}[prefix] | ||
except KeyError: | ||
raise ValueError('unable to determine appropriate encoding') | ||
return body.decode(encoding, errors='replace') | ||
|
||
|
||
def dump_user_comment(data, encoding="ascii"): | ||
""" | ||
Convert str to appropriate format for "UserComment". | ||
:param data: Like u"foobar" | ||
:param str encoding: "ascii", "jis", or "unicode" | ||
:return: b"ASCII\x00\x00\x00foobar" | ||
:rtype: bytes | ||
:raises: ValueError if the encoding is unsupported. | ||
""" | ||
if encoding not in ENCODINGS: | ||
raise ValueError('encoding %r must be one of %r' % (encoding, ENCODINGS)) | ||
prefix = {ASCII: _ASCII_PREFIX, JIS: _JIS_PREFIX, UNICODE: _UNICODE_PREFIX}[encoding] | ||
internal_encoding = {UNICODE: _UNICODE, JIS: _JIS}.get(encoding, encoding) | ||
return prefix + data.encode(internal_encoding, errors='replace') |
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,20 @@ | ||
alabaster==0.7.10 | ||
Babel==2.5.0 | ||
certifi==2017.7.27.1 | ||
chardet==3.0.4 | ||
docutils==0.14 | ||
idna==2.6 | ||
imagesize==0.7.1 | ||
Jinja2==2.9.6 | ||
MarkupSafe==1.0 | ||
olefile==0.44 | ||
Pillow==4.2.1 | ||
Pygments==2.2.0 | ||
pytz==2017.2 | ||
requests==2.18.4 | ||
six==1.10.0 | ||
snowballstemmer==1.2.1 | ||
Sphinx==1.6.3 | ||
sphinxcontrib-websupport==1.0.1 | ||
typing==3.6.2 | ||
urllib3==1.22 |
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