summaryrefslogtreecommitdiffstats
path: root/module/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'module/utils.py')
-rw-r--r--module/utils.py100
1 files changed, 63 insertions, 37 deletions
diff --git a/module/utils.py b/module/utils.py
index 8748b7693..5e8c2b12b 100644
--- a/module/utils.py
+++ b/module/utils.py
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
-""" Store all usefull functions here """
+""" Store all useful functions here """
import os
import sys
@@ -10,6 +10,7 @@ from os.path import join
from string import maketrans
from htmlentitydefs import name2codepoint
+
def chmod(*args):
try:
os.chmod(*args)
@@ -27,24 +28,39 @@ def decode(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]))
+ if type(repl) == unicode:
+ for badc in list(repl):
+ string = string.replace(badc, "")
+ return string
+ else:
+ 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
+def safe_path(name):
+ """ remove bad chars """
+ name = name.encode('ascii', 'replace') # Non-ASCII chars usually breaks file saving. Replacing.
if os.name == 'nt':
- return remove_chars(name, '/\\?%*:|"<>')
+ return remove_chars(name, u'\00\01\02\03\04\05\06\07\10\11\12\13\14\15\16\17\20\21\22\23\24\25\26\27\30\31\32'
+ u'\33\34\35\36\37/\\?%*:|"<>')
else:
- return remove_chars(name, '/\\"')
+ return remove_chars(name, u'\0/\\"')
+
+#: Deprecated method
+def save_path(name):
+ return safe_path(name)
-def save_join(*args):
+def safe_join(*args):
""" joins a path, encoding aware """
return fs_encode(join(*[x if type(x) == unicode else decode(x) for x in args]))
+#: Deprecated method
+def save_join(*args):
+ return safe_join(*args)
+
# File System Encoding functions:
# Use fs_encode before accesing files on disk, it will encode the string properly
@@ -61,6 +77,7 @@ if sys.getfilesystemencoding().startswith('ANSI'):
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
@@ -68,9 +85,10 @@ def get_console_encoding(enc):
enc = "cp850"
else:
enc = "utf8"
-
+
return enc
+
def compare_time(start, end):
start = map(int, start)
end = map(int, end)
@@ -88,7 +106,7 @@ def formatSize(size):
"""formats size of bytes"""
size = int(size)
steps = 0
- sizes = ["B", "KiB", "MiB", "GiB", "TiB"]
+ sizes = ("B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB")
while size > 1000:
size /= 1024.0
steps += 1
@@ -107,32 +125,35 @@ def freeSpace(folder):
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)
+ s = os.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 fs_bsize(path):
+ """ get optimal file system buffer size (in bytes) for I/O calls """
+ path = fs_encode(path)
+
+ if os.name == "nt":
+ import ctypes
+
+ drive = "%s\\" % os.path.splitdrive(path)[0]
+ cluster_sectors, sector_size = ctypes.c_longlong(0)
+ ctypes.windll.kernel32.GetDiskFreeSpaceW(ctypes.c_wchar_p(drive), ctypes.pointer(cluster_sectors), ctypes.pointer(sector_size), None, None)
+ return cluster_sectors * sector_size
+ else:
+ return os.statvfs(path).f_bsize
+
+
+def uniqify(seq): #: Originally by Dave Kirby
+ """ removes duplicates from list, preserve order """
+ seen = set()
+ seen_add = seen.add
+ return [x for x in seq if x not in seen and not seen_add(x)]
def parseFileSize(string, unit=None): #returns bytes
if not unit:
- m = re.match(r"(\d*[\.,]?\d+)(.*)", string.strip().lower())
+ m = re.match(r"([\d.,]+) *([a-zA-Z]*)", string.strip().lower())
if m:
traffic = float(m.group(1).replace(",", "."))
unit = m.group(2)
@@ -147,11 +168,17 @@ def parseFileSize(string, unit=None): #returns bytes
#ignore case
unit = unit.lower().strip()
- if unit in ("gb", "gig", "gbyte", "gigabyte", "gib", "g"):
+ if unit in ("eb", "ebyte", "exabyte", "eib", "e"):
+ traffic *= 1 << 60
+ elif unit in ("pb", "pbyte", "petabyte", "pib", "p"):
+ traffic *= 1 << 50
+ elif unit in ("tb", "tbyte", "terabyte", "tib", "t"):
+ traffic *= 1 << 40
+ elif unit in ("gb", "gbyte", "gigabyte", "gib", "g", "gig"):
traffic *= 1 << 30
elif unit in ("mb", "mbyte", "megabyte", "mib", "m"):
traffic *= 1 << 20
- elif unit in ("kb", "kib", "kilobyte", "kbyte", "k"):
+ elif unit in ("kb", "kbyte", "kilobyte", "kib", "k"):
traffic *= 1 << 10
return traffic
@@ -159,7 +186,7 @@ def parseFileSize(string, unit=None): #returns bytes
def lock(func):
def new(*args):
- #print "Handler: %s args: %s" % (func,args[1:])
+ #print "Handler: %s args: %s" % (func, args[1:])
args[0].lock.acquire()
try:
return func(*args)
@@ -195,7 +222,6 @@ 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")
+def versiontuple(v): #: By kindall (http://stackoverflow.com/a/11887825)
+ return tuple(map(int, (v.split("."))))