Repository navigation
Expand file tree
/
Copy path__init__.py
More file actions
261 lines (194 loc) · 6.71 KB
/
Copy path__init__.py
File metadata and controls
261 lines (194 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# -*- coding: utf-8 -*-
# @author: vuolter
""" Store all useful functions here """
import bitmath
import htmlentitydefs
import os
import re
import string
import sys
import time
import urllib
from pyload.utils import pylgettext as gettext
# abstraction layer for json operations
try:
import simplejson as json
except ImportError:
import json
from bottle import json_loads, json_dumps
def decode(string):
""" Decode string to unicode with utf8 """
if type(string) == str:
return string.decode("utf8", "replace")
else:
return string
def encode(string):
""" Decode string to utf8 """
if type(string) == unicode:
return string.encode("utf8", "replace")
else:
return string
def remove_chars(string, repl):
""" removes all chars in repl from string"""
if type(repl) == unicode:
for badc in list(repl):
string = string.replace(badc, "")
return string
else:
if type(string) == str:
return string.translate(string.maketrans("", ""), repl)
elif type(string) == unicode:
return string.translate(dict((ord(s), None) for s in repl))
def safe_filename(name):
""" remove bad chars """
name = urllib.unquote(name).encode('ascii', 'replace') #: Non-ASCII chars usually breaks file saving. Replacing.
if os.name == 'nt':
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, u'\0\\"')
#: Deprecated method
def save_path(name):
return safe_filename(name)
def fs_join(*args):
""" joins a path, encoding aware """
return fs_encode(os.path.join(*[x if type(x) == unicode else decode(x) for x in args]))
#: Deprecated method
def save_join(*args):
return fs_join(*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):
return safe_filename(encode(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
return False
def formatSize(size):
"""formats size of bytes"""
return bitmath.Byte(int(size)).best_prefix()
def formatSpeed(speed):
return formatSize(speed) + "/s"
def freeSpace(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:
s = os.statvfs(folder)
return s.f_frsize * s.f_bavail
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_frsize
def uniqify(seq): #: Originally by Dave Kirby
""" Remove duplicates from list preserving 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.,]+) *([a-zA-Z]*)", 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 ("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", "kbyte", "kilobyte", "kib", "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 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(htmlentitydefs.name2codepoint[name])
except KeyError:
pass
return text #: leave as is
def has_method(obj, name):
""" Check if "name" was defined in obj, (false if it was inhereted) """
return hasattr(obj, '__dict__') and name in obj.__dict__
def html_unescape(text):
"""Removes HTML or XML character references and entities from a text string"""
return re.sub("&#?\w+;", fixup, text)
def versiontuple(v): #: By kindall (http://stackoverflow.com/a/11887825)
return tuple(map(int, (v.split("."))))
def load_translation(name, locale, default="en"):
""" Load language and return its translation object or None """
import traceback
try:
gettext.setpaths([os.path.join(os.sep, "usr", "share", "pyload", "locale"), None])
translation = gettext.translation(name, os.path.join(pypath, "locale"),
languages=[locale, default], fallback=True)
except Exception:
traceback.print_exc()
return None
else:
translation.install(True)
return translation