本文整理汇总了Python中module.utils.fs_encode函数的典型用法代码示例。如果您正苦于以下问题:Python fs_encode函数的具体用法?Python fs_encode怎么用?Python fs_encode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了fs_encode函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: packageFinished
def packageFinished(self, pypack):
download_folder = save_join(self.config['general']['download_folder'], pypack.folder, "")
for link in pypack.getChildren().itervalues():
file_type = splitext(link["name"])[1][1:].lower()
#self.logDebug(link, file_type)
if file_type not in self.formats:
continue
hash_file = fs_encode(save_join(download_folder, link["name"]))
if not isfile(hash_file):
self.logWarning("File not found: %s" % link["name"])
continue
with open(hash_file) as f:
text = f.read()
for m in re.finditer(self.regexps.get(file_type, self.regexps['default']), text):
data = m.groupdict()
self.logDebug(link["name"], data)
local_file = fs_encode(save_join(download_folder, data["name"]))
algorithm = self.methods.get(file_type, file_type)
checksum = computeChecksum(local_file, algorithm)
if checksum == data["hash"]:
self.logInfo('File integrity of "%s" verified by %s checksum (%s).' % (data["name"],
algorithm,
checksum))
else:
self.logWarning("%s checksum for file %s does not match (%s != %s)" % (algorithm,
data["name"],
checksum,
data["hash"]))
示例2: package_finished
def package_finished(self, pypack):
download_folder = fs_join(self.pyload.config.get("general", "download_folder"), pypack.folder, "")
for link in pypack.getChildren().values():
file_type = os.path.splitext(link['name'])[1][1:].lower()
if file_type not in self.formats:
continue
hash_file = fs_encode(fs_join(download_folder, link['name']))
if not os.path.isfile(hash_file):
self.log_warning(_("File not found"), link['name'])
continue
with open(hash_file) as f:
text = f.read()
for m in re.finditer(self.regexps.get(file_type, self.regexps['default']), text):
data = m.groupdict()
self.log_debug(link['name'], data)
local_file = fs_encode(fs_join(download_folder, data['NAME']))
algorithm = self.methods.get(file_type, file_type)
checksum = computeChecksum(local_file, algorithm)
if checksum is data['HASH']:
self.log_info(_('File integrity of "%s" verified by %s checksum (%s)') %
(data['NAME'], algorithm, checksum))
else:
self.log_warning(_("%s checksum for file %s does not match (%s != %s)") %
(algorithm, data['NAME'], checksum, data['HASH']))
示例3: findDuplicates
def findDuplicates(self, pyfile):
""" Search all packages for duplicate links to "pyfile".
Duplicates are links that would overwrite "pyfile".
To test on duplicity the package-folder and link-name
of twolinks are compared (basename(link.name)).
So this method returns a list of all links with equal
package-folders and filenames as "pyfile", but except
the data for "pyfile" iotselöf.
It does MOT check the link's status.
"""
dups = []
pyfile_name = fs_encode(basename(pyfile.name))
# get packages (w/o files, as most file data is useless here)
queue = self.core.api.getQueue()
for package in queue:
# check if package-folder equals pyfile's package folder
if fs_encode(package.folder) == fs_encode(pyfile.package().folder):
# now get packaged data w/ files/links
pdata = self.core.api.getPackageData(package.pid)
if pdata.links:
for link in pdata.links:
link_name = fs_encode(basename(link.name))
# check if link name collides with pdata's name
if link_name == pyfile_name:
# at last check if it is not pyfile itself
if link.fid != pyfile.id:
dups.append(link)
return dups
示例4: _copyChunks
def _copyChunks(self):
init = fs_encode(self.info.getChunkName(0)) #initial chunk name
if self.info.getCount() > 1:
fo = open(init, "rb+") #first chunkfile
for i in range(1, self.info.getCount()):
#input file
fo.seek(
self.info.getChunkRange(i - 1)[1] + 1) #seek to beginning of chunk, to get rid of overlapping chunks
fname = fs_encode("%s.chunk%d" % (self.filename, i))
fi = open(fname, "rb")
buf = 32 * 1024
while True: #copy in chunks, consumes less memory
data = fi.read(buf)
if not data:
break
fo.write(data)
fi.close()
if fo.tell() < self.info.getChunkRange(i)[1]:
fo.close()
remove(init)
self.info.remove() #there are probably invalid chunks
raise Exception("Downloaded content was smaller than expected. Try to reduce download connections.")
remove(fname) #remove chunk
fo.close()
if self.nameDisposition and self.disposition:
self.filename = save_join(dirname(self.filename), self.nameDisposition)
move(init, fs_encode(self.filename))
self.info.remove() #remove info file
示例5: get_download
def get_download(path):
path = unquote(path).decode("utf8")
# @TODO some files can not be downloaded
root = PYLOAD.getConfigValue("general", "download_folder")
path = path.replace("..", "")
return static_file(fs_encode(path), fs_encode(root))
示例6: scan
def scan(self, pyfile, thread):
file = fs_encode(pyfile.plugin.last_download)
filename = os.path.basename(pyfile.plugin.last_download)
cmdfile = fs_encode(self.get_config('cmdfile'))
cmdargs = fs_encode(self.get_config('cmdargs').strip())
if not os.path.isfile(file) or not os.path.isfile(cmdfile):
return
thread.addActive(pyfile)
pyfile.setCustomStatus(_("virus scanning"))
pyfile.setProgress(0)
try:
p = subprocess.Popen([cmdfile, cmdargs, file], bufsize=-1, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = map(str.strip, p.communicate())
if out:
self.log_info(filename, out)
if err:
self.log_warning(filename, err)
if not self.get_config('ignore-err'):
self.log_debug("Delete/Quarantine task is aborted")
return
if p.returncode:
pyfile.error = _("Infected file")
action = self.get_config('action')
try:
if action == "Delete":
if not self.get_config('deltotrash'):
os.remove(file)
else:
try:
send2trash.send2trash(file)
except NameError:
self.log_warning(_("Send2Trash lib not found, moving to quarantine instead"))
pyfile.setCustomStatus(_("file moving"))
shutil.move(file, self.get_config('quardir'))
except Exception, e:
self.log_warning(_("Unable to move file to trash: %s, moving to quarantine instead") % e.message)
pyfile.setCustomStatus(_("file moving"))
shutil.move(file, self.get_config('quardir'))
else:
self.log_debug("Successfully moved file to trash")
elif action == "Quarantine":
pyfile.setCustomStatus(_("file moving"))
shutil.move(file, self.get_config('quardir'))
except (IOError, shutil.Error), e:
self.log_error(filename, action + " action failed!", e)
示例7: get_download
def get_download(path):
path = unquote(path).decode("utf8")
#@TODO some files can not be downloaded
root = PYLOAD.getConfigValue("general", "download_folder")
path = path.replace("..", "")
try:
return static_file(fs_encode(path), fs_encode(root))
except Exception, e:
print e
return HTTPError(404, "File not Found.")
示例8: list
def list(self, password=None):
command = "vb" if self.fullpath else "lb"
p = self.call_cmd(command, "-v", fs_encode(self.filename), password=password)
out, err = p.communicate()
if "Cannot open" in err:
raise ArchiveError(_("Cannot open file"))
if err.strip(): #: only log error at this point
self.manager.logError(err.strip())
result = set()
if not self.fullpath and self.VERSION.startswith('5'):
# NOTE: Unrar 5 always list full path
for f in fs_decode(out).splitlines():
f = save_join(self.out, os.path.basename(f.strip()))
if os.path.isfile(f):
result.add(save_join(self.out, os.path.basename(f)))
else:
for f in fs_decode(out).splitlines():
f = f.strip()
result.add(save_join(self.out, f))
return list(result)
示例9: downloads
def downloads():
root = PYLOAD.getConfigValue("general", "download_folder")
if not isdir(root):
return base([_("Download directory not found.")])
data = {"folder": [], "files": []}
items = listdir(fs_encode(root))
for item in sorted([fs_decode(x) for x in items]):
if isdir(safe_join(root, item)):
folder = {"name": item, "path": item, "files": []}
files = listdir(safe_join(root, item))
for file in sorted([fs_decode(x) for x in files]):
try:
if isfile(safe_join(root, item, file)):
folder["files"].append(file)
except:
pass
data["folder"].append(folder)
elif isfile(join(root, item)):
data["files"].append(item)
return render_to_response("downloads.html", {"files": data}, [pre_processor])
示例10: load
def load(name):
fs_name = fs_encode("%s.chunks" % name)
if not exists(fs_name):
raise IOError()
fh = codecs.open(fs_name, "r", "utf_8")
name = fh.readline()[:-1]
size = fh.readline()[:-1]
if name.startswith("name:") and size.startswith("size:"):
name = name[5:]
size = size[5:]
else:
fh.close()
raise WrongFormat()
ci = ChunkInfo(name)
ci.loaded = True
ci.setSize(size)
while True:
if not fh.readline(): #skip line
break
name = fh.readline()[1:-1]
range = fh.readline()[1:-1]
if name.startswith("name:") and range.startswith("range:"):
name = name[5:]
range = range[6:].split("-")
else:
raise WrongFormat()
ci.addChunk(name, (long(range[0]), long(range[1])))
fh.close()
return ci
示例11: process
def process(self, pyfile):
if not self.account:
self.logError(_("Please enter your premium4.me account or deactivate this plugin"))
self.fail("No premium4.me account provided")
self.logDebug("premium4.me: Old URL: %s" % pyfile.url)
tra = self.getTraffic()
#raise timeout to 2min
self.req.setOption("timeout", 120)
self.download("http://premium4.me/api/getfile.php?authcode=%s&link=%s" % (self.account.authcode, quote(pyfile.url, "")), disposition=True)
err = ''
if self.req.http.code == '420':
# Custom error code send - fail
lastDownload = fs_encode(self.lastDownload)
if exists(lastDownload):
f = open(lastDownload, "rb")
err = f.read(256).strip()
f.close()
remove(lastDownload)
else:
err = 'File does not exist'
trb = self.getTraffic()
self.logInfo("Filesize: %d, Traffic used %d, traffic left %d" % (pyfile.size, tra-trb, trb))
if err: self.fail(err)
示例12: downloads
def downloads():
root = PYLOAD.getConfigValue("general", "download_folder")
if not isdir(root):
return base([_('Download directory not found.')])
data = {
'folder': [],
'files': []
}
items = listdir(fs_encode(root))
for item in sorted([fs_decode(x) for x in items]):
if isdir(save_join(root, item)):
folder = {
'name': item,
'path': item,
'files': []
}
files = listdir(save_join(root, item))
for file in sorted([fs_decode(x) for x in files]):
try:
if isfile(save_join(root, item, file)):
folder['files'].append(file)
except:
pass
data['folder'].append(folder)
elif isfile(join(root, item)):
data['files'].append(item)
return render_to_response('downloads.html', {'files': data}, [pre_processor])
示例13: extract
def extract(self, password=None):
command = "x" if self.fullpath else "e"
p = self.call_cmd(command, fs_encode(self.filename), self.out, password=password)
renice(p.pid, self.renice)
#: Communicate and retrieve stderr
self._progress(p)
err = p.stderr.read().strip()
if err:
if self.re_wrongpwd.search(err):
raise PasswordError
elif self.re_wrongcrc.search(err):
raise CRCError(err)
else: #: Raise error if anything is on stderr
raise ArchiveError(err)
if p.returncode:
raise ArchiveError(_("Process return code: %d") % p.returncode)
self.files = self.list(password)
示例14: downloadFinished
def downloadFinished(self, pyfile):
"""
Compute checksum for the downloaded file and compare it with the hash provided by the hoster.
pyfile.plugin.check_data should be a dictionary which can contain:
a) if known, the exact filesize in bytes (e.g. "size": 123456789)
b) hexadecimal hash string with algorithm name as key (e.g. "md5": "d76505d0869f9f928a17d42d66326307")
"""
if hasattr(pyfile.plugin, "check_data") and (isinstance(pyfile.plugin.check_data, dict)):
data = pyfile.plugin.check_data.copy()
elif hasattr(pyfile.plugin, "api_data") and (isinstance(pyfile.plugin.api_data, dict)):
data = pyfile.plugin.api_data.copy()
else:
return
self.logDebug(data)
if not pyfile.plugin.lastDownload:
self.checkFailed(pyfile, None, "No file downloaded")
local_file = fs_encode(pyfile.plugin.lastDownload)
#download_folder = self.config['general']['download_folder']
#local_file = fs_encode(save_join(download_folder, pyfile.package().folder, pyfile.name))
if not isfile(local_file):
self.checkFailed(pyfile, None, "File does not exist")
# validate file size
if "size" in data:
api_size = int(data['size'])
file_size = getsize(local_file)
if api_size != file_size:
self.logWarning("File %s has incorrect size: %d B (%d expected)" % (pyfile.name, file_size, api_size))
self.checkFailed(pyfile, local_file, "Incorrect file size")
del data['size']
# validate checksum
if data and self.config['general']['checksum']:
if "checksum" in data:
data['md5'] = data['checksum']
for key in self.algorithms:
if key in data:
checksum = computeChecksum(local_file, key.replace("-", "").lower())
if checksum:
if checksum == data[key].lower():
self.logInfo('File integrity of "%s" verified by %s checksum (%s).' % (pyfile.name,
key.upper(),
checksum))
return
else:
self.logWarning("%s checksum for file %s does not match (%s != %s)" % (key.upper(),
pyfile.name,
checksum,
data[key]))
self.checkFailed(pyfile, local_file, "Checksums do not match")
else:
self.logWarning("Unsupported hashing algorithm: %s" % key.upper())
else:
self.logWarning("Unable to validate checksum for file %s" % pyfile.name)
示例15: verify
def verify(self):
with zipfile.ZipFile(fs_encode(self.filename), 'r', allowZip64=True) as z:
badfile = z.testzip()
if badfile:
raise CRCError(badfile)
else:
raise PasswordError