From 5499be89203a18ca61a21cfc7266cf0f4ebe6547 Mon Sep 17 00:00:00 2001 From: RaNaN Date: Thu, 15 Dec 2011 23:18:21 +0100 Subject: refractoring --- module/Utils.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'module/Utils.py') diff --git a/module/Utils.py b/module/Utils.py index c965e33c4..9ad7c2737 100644 --- a/module/Utils.py +++ b/module/Utils.py @@ -8,6 +8,7 @@ import time import re from os.path import join from string import maketrans +from itertools import islice from htmlentitydefs import name2codepoint def chmod(*args): @@ -168,6 +169,13 @@ def lock(func): return new +def chunks(iterable, size): + it = iter(iterable) + item = list(islice(it, size)) + while item: + yield item + item = list(islice(it, size)) + def fixup(m): text = m.group(0) -- cgit v1.2.3 From 6eae782f13953dd0ba2bbe1b582cf33fd4d7d90a Mon Sep 17 00:00:00 2001 From: RaNaN Date: Mon, 19 Dec 2011 23:10:49 +0100 Subject: configparser v2, warning CONFIG will be DELETED. --- module/Utils.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'module/Utils.py') diff --git a/module/Utils.py b/module/Utils.py index 9ad7c2737..b26a9960c 100644 --- a/module/Utils.py +++ b/module/Utils.py @@ -21,7 +21,10 @@ def chmod(*args): def decode(string): """ decode string with utf if possible """ try: - return string.decode("utf8", "replace") + if type(string) == str: + return string.decode("utf8", "replace") + else: + return string except: return string -- cgit v1.2.3 From 958bf611f5d9d117f19f824990ec6fd6b537e967 Mon Sep 17 00:00:00 2001 From: RaNaN Date: Thu, 22 Dec 2011 23:45:38 +0100 Subject: accountmanager v2, delete your accounts.conf and re-enter them in pyload, new nice debug functions, try core.shell() and core.breakpoint() --- module/Utils.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'module/Utils.py') diff --git a/module/Utils.py b/module/Utils.py index b26a9960c..b80cfe1c2 100644 --- a/module/Utils.py +++ b/module/Utils.py @@ -104,6 +104,8 @@ def formatSpeed(speed): def freeSpace(folder): + folder = fs_encode(folder) + if os.name == "nt": import ctypes -- cgit v1.2.3 From d35c003cc53d4723d1dfe0d81eeb9bea78cee594 Mon Sep 17 00:00:00 2001 From: RaNaN Date: Sat, 31 Dec 2011 16:01:24 +0100 Subject: new crypter plugin API, now decrypting possible for now. --- module/Utils.py | 214 -------------------------------------------------------- 1 file changed, 214 deletions(-) delete mode 100644 module/Utils.py (limited to 'module/Utils.py') diff --git a/module/Utils.py b/module/Utils.py deleted file mode 100644 index 86fd67558..000000000 --- a/module/Utils.py +++ /dev/null @@ -1,214 +0,0 @@ -# -*- coding: utf-8 -*- - -""" Store all usefull functions here """ - -import os -import sys -import time -import re -from os.path import join -from string import maketrans -from itertools import islice -from htmlentitydefs import name2codepoint - -def chmod(*args): - try: - os.chmod(*args) - except: - pass - - -def decode(string): - """ decode string with utf if possible """ - try: - if type(string) == str: - return string.decode("utf8", "replace") - else: - return string - except: - return string - - -def remove_chars(string, repl): - """ removes all chars in repl from string""" - if type(string) == str: - return string.translate(maketrans("", ""), repl) - elif type(string) == unicode: - return string.translate(dict([(ord(s), None) for s in repl])) - - -def save_path(name): - #remove some chars - if os.name == 'nt': - return remove_chars(name, '/\\?%*:|"<>') - else: - return remove_chars(name, '/\\"') - - -def save_join(*args): - """ joins a path, encoding aware """ - return fs_encode(join(*[x if type(x) == unicode else decode(x) for x in args])) - - -# File System Encoding functions: -# Use fs_encode before accesing files on disk, it will encode the string properly - -if sys.getfilesystemencoding().startswith('ANSI'): - def fs_encode(string): - try: - string = string.encode('utf-8') - finally: - return string - - fs_decode = decode #decode utf8 - -else: - fs_encode = fs_decode = lambda x: x # do nothing - -def get_console_encoding(enc): - if os.name == "nt": - if enc == "cp65001": # aka UTF-8 - print "WARNING: Windows codepage 65001 is not supported." - enc = "cp850" - else: - enc = "utf8" - - return enc - -def compare_time(start, end): - start = map(int, start) - end = map(int, end) - - if start == end: return True - - now = list(time.localtime()[3:5]) - if start < now < end: return True - elif start > end and (now > start or now < end): return True - elif start < now > end < start: return True - else: return False - - -def formatSize(size): - """formats size of bytes""" - size = int(size) - steps = 0 - sizes = ["B", "KiB", "MiB", "GiB", "TiB"] - while size > 1000: - size /= 1024.0 - steps += 1 - return "%.2f %s" % (size, sizes[steps]) - - -def formatSpeed(speed): - return formatSize(speed) + "/s" - - -def freeSpace(folder): - folder = fs_encode(folder) - - if os.name == "nt": - import ctypes - - free_bytes = ctypes.c_ulonglong(0) - ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(folder), None, None, ctypes.pointer(free_bytes)) - return free_bytes.value - else: - from os import statvfs - - s = statvfs(folder) - return s.f_bsize * s.f_bavail - - -def uniqify(seq, idfun=None): -# order preserving - if idfun is None: - def idfun(x): return x - seen = {} - result = [] - for item in seq: - marker = idfun(item) - # in old Python versions: - # if seen.has_key(marker) - # but in new ones: - if marker in seen: continue - seen[marker] = 1 - result.append(item) - return result - - -def parseFileSize(string, unit=None): #returns bytes - if not unit: - m = re.match(r"(\d*[\.,]?\d+)(.*)", string.strip().lower()) - if m: - traffic = float(m.group(1).replace(",", ".")) - unit = m.group(2) - else: - return 0 - else: - if isinstance(string, basestring): - traffic = float(string.replace(",", ".")) - else: - traffic = string - - #ignore case - unit = unit.lower().strip() - - if unit in ("gb", "gig", "gbyte", "gigabyte", "gib", "g"): - traffic *= 1 << 30 - elif unit in ("mb", "mbyte", "megabyte", "mib", "m"): - traffic *= 1 << 20 - elif unit in ("kb", "kib", "kilobyte", "kbyte", "k"): - traffic *= 1 << 10 - - return traffic - - -def lock(func): - def new(*args): - #print "Handler: %s args: %s" % (func,args[1:]) - args[0].lock.acquire() - try: - return func(*args) - finally: - args[0].lock.release() - - return new - -def chunks(iterable, size): - it = iter(iterable) - item = list(islice(it, size)) - while item: - yield item - item = list(islice(it, size)) - - -def fixup(m): - text = m.group(0) - if text[:2] == "&#": - # character reference - try: - if text[:3] == "&#x": - return unichr(int(text[3:-1], 16)) - else: - return unichr(int(text[2:-1])) - except ValueError: - pass - else: - # named entity - try: - name = text[1:-1] - text = unichr(name2codepoint[name]) - except KeyError: - pass - - return text # leave as is - - -def html_unescape(text): - """Removes HTML or XML character references and entities from a text string""" - return re.sub("&#?\w+;", fixup, text) - -if __name__ == "__main__": - print freeSpace(".") - - print remove_chars("ab'cdgdsf''ds'", "'ghd") -- cgit v1.2.3