# -*- coding: utf-8 -*- ''' Genesis Add-on Copyright (C) 2015 lambda -Mofidied by The Crew -Copyright (C) 2019 The Crew This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . ''' import re import simplejson as json import six def json_load_as_str(file_handle): return byteify(json.load(file_handle, object_hook=byteify), ignore_dicts=True) def json_loads_as_str(json_text): return byteify(json.loads(json_text, object_hook=byteify), ignore_dicts=True) def byteify(data, ignore_dicts=False): if isinstance(data, six.string_types): if six.PY2: return data.encode('utf-8') else: return data if isinstance(data, list): return [byteify(item, ignore_dicts=True) for item in data] if isinstance(data, dict) and not ignore_dicts: return dict([(byteify(key, ignore_dicts=True), byteify(value, ignore_dicts=True)) for key, value in six.iteritems(data)]) return data def title_key(title): try: if title is None: title = '' articles_en = ['the', 'a', 'an'] articles_de = ['der', 'die', 'das'] articles = articles_en + articles_de match = re.match('^((\w+)\s+)', title.lower()) if match and match.group(2) in articles: offset = len(match.group(1)) else: offset = 0 return title[offset:] except: return title def chunks(l, n): """ Yield successive n-sized chunks from l. """ for i in list(range(0, len(l), n)): yield l[i:i + n] def _size(siz): if siz in ['0', 0, '', None]: return 0, '' div = 1 if siz.lower().endswith(('gb', 'gib')) else 1024 float_size = float(re.sub('[^0-9|/.|/,]', '', siz.replace(',', '.'))) / div str_size = str('%.2f GB' % float_size) return float_size, str_size