summaryrefslogtreecommitdiffstats
path: root/deluge/common.py
diff options
context:
space:
mode:
authorCalum Lind <calumlind+deluge@gmail.com>2017-02-12 11:26:31 +0000
committerCalum Lind <calumlind+deluge@gmail.com>2017-02-22 12:36:33 +0000
commit1d9733014a0531fbf19da9262e2e2b2a7cc61284 (patch)
tree71fa90adb6d10fa69b3d36becfebc981a7657c26 /deluge/common.py
parent304ad1e72d9c11f4ce5995880fcd8056902c7f02 (diff)
downloaddeluge-1d9733014a0531fbf19da9262e2e2b2a7cc61284.tar.gz
deluge-1d9733014a0531fbf19da9262e2e2b2a7cc61284.tar.bz2
deluge-1d9733014a0531fbf19da9262e2e2b2a7cc61284.zip
[Core] Fixup maketorrent for unicode_literals
* Added a new convert_to_utf8 function to common that is handy for any nested dictionaries etc. * Small refactor to get rid of duplicate code and comment will be encoded by convert_to_utf8 function. * Passes test_maketorrent.
Diffstat (limited to 'deluge/common.py')
-rw-r--r--deluge/common.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/deluge/common.py b/deluge/common.py
index 19879aa64..7aea9b6af 100644
--- a/deluge/common.py
+++ b/deluge/common.py
@@ -839,6 +839,29 @@ def utf8_encoded(s, encoding='utf8'):
return s
+def convert_to_utf8(data):
+ """Recursively convert all unicode keys and values in a data structure to utf8.
+
+ e.g. converting keys and values for a dict with nested dicts and lists etc.
+
+ Args:
+ data (any): This can be any structure, dict, list or tuple.
+
+ Returns:
+ input type: The data with unicode keys and values converted to utf8.
+
+ """
+
+ if isinstance(data, unicode):
+ return data.encode('utf8')
+ elif isinstance(data, (list, tuple)):
+ return type(data)(map(convert_to_utf8, data))
+ elif isinstance(data, dict):
+ return dict(map(convert_to_utf8, data.items()))
+ else:
+ return data
+
+
@functools.total_ordering
class VersionSplit(object):
"""