ffmpeg-python/ffmpeg/_utils.py
Karl Kroening 7b2d8b63fc Merge remote-tracking branch 'origin/master' into feature/17
Conflicts:
	ffmpeg/_filters.py
	ffmpeg/_utils.py
	ffmpeg/nodes.py
	ffmpeg/tests/test_ffmpeg.py
2017-07-12 02:08:19 -06:00

44 lines
1.2 KiB
Python

from __future__ import unicode_literals
from builtins import str
from past.builtins import basestring
import hashlib
def _recursive_repr(item):
"""Hack around python `repr` to deterministically represent dictionaries.
This is able to represent more things than json.dumps, since it does not require things to be JSON serializable
(e.g. datetimes).
"""
if isinstance(item, basestring):
result = str(item)
elif isinstance(item, list):
result = '[{}]'.format(', '.join([_recursive_repr(x) for x in item]))
elif isinstance(item, dict):
kv_pairs = ['{}: {}'.format(_recursive_repr(k), _recursive_repr(item[k])) for k in sorted(item)]
result = '{' + ', '.join(kv_pairs) + '}'
else:
result = repr(item)
return result
def get_hash(item):
repr_ = _recursive_repr(item).encode('utf-8')
return hashlib.md5(repr_).hexdigest()
def get_hash_int(item):
return int(get_hash(item), base=16)
def escape_chars(text, chars):
"""Helper function to escape uncomfortable characters."""
text = str(text)
chars = list(set(chars))
if '\\' in chars:
chars.remove('\\')
chars.insert(0, '\\')
for ch in chars:
text = text.replace(ch, '\\' + ch)
return text