Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- coding: utf-8 -*- # # Copyright (C) Damien Churchill 2008-2009 <damoxc@gmail.com> # Copyright (C) Andrew Resch 2009 <andrewresch@gmail.com> # # This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with # the additional special exception to link portions of this program with the OpenSSL library. # See LICENSE for more details. #
The ui common module contains methods and classes that are deemed useful for all the interfaces. """
# Dummy tranlation dict so Torrent states text is available for Translators # All entries in deluge.common.TORRENT_STATE should be here. It does not need importing # as the string matches the translation text so using the _() function is enough. "All": _("All"), "Active": _("Active"), "Allocating": _("Allocating"), "Checking": _("Checking"), "Downloading": _("Downloading"), "Seeding": _("Seeding"), "Paused": _("Paused"), "Checking": _("Checking"), "Queued": _("Queued"), "Error": _("Error"), }
"Error": _("Error"), "Warning": _("Warning"), "Announce OK": _("Announce OK"), "Announce Sent": _("Announce Sent") }
""" Collects information about a torrent file.
:param filename: The path to the torrent :type filename: string
""" # Get the torrent data from the torrent file except Exception as ex: log.warning("Unable to open %s: %s", filename, ex) raise ex
# Get encoding from torrent file if available self.encoding = str(self.__m_metadata["codepage"])
# Check if 'name.utf-8' is in the torrent and if not try to decode the string # using the encoding found. else:
# Get list of files from torrent info
path = os.path.join(prefix, *f["path.utf-8"]) del f["path.utf-8"] else: self.encoding)), self.encoding) f["sha1"] = f["sha1"].encode('hex') f["ed2k"] = f["ed2k"].encode('hex')
def walk(path, item): if item["type"] == "dir": item.update(dirs[path]) else: item.update(paths[path]) item["download"] = True
file_tree = FileTree2(paths.keys()) file_tree.walk(walk) else:
else: self.__m_files_tree = { "contents": { self.__m_name: { "type": "file", "index": 0, "length": self.__m_metadata["info"]["length"], "download": True } } } else: self.__m_name: (0, self.__m_metadata["info"]["length"], True) }
'path': f["path"], 'size': f["length"], 'download': True }) else: "path": self.__m_name, "size": self.__m_metadata["info"]["length"], "download": True })
""" Return the torrent info as a dictionary, only including the passed in keys.
:param keys: a number of key strings :type keys: string """ return dict([(key, getattr(self, key)) for key in keys])
def name(self): """ The name of the torrent.
:rtype: string """ return self.__m_name
def info_hash(self): """ The torrents info_hash
:rtype: string """ return self.__m_info_hash
def files(self): """ A list of the files that the torrent contains.
:rtype: list """ return self.__m_files
def files_tree(self): """ A dictionary based tree of the files.
::
{ "some_directory": { "some_file": (index, size, download) } }
:rtype: dictionary """
def metadata(self): """ The torrents metadata.
:rtype: dictionary """ return self.__m_metadata
def filedata(self): """ The torrents file data. This will be the bencoded dictionary read from the torrent file.
:rtype: string """ return self.__m_filedata
""" Converts a list of paths in to a file tree.
:param paths: The paths to be converted :type paths: list """
self.tree = {"contents": {}, "type": "dir"}
def get_parent(path): parent = self.tree while "/" in path: directory, path = path.split("/", 1) child = parent["contents"].get(directory) if child is None: parent["contents"][directory] = { "type": "dir", "contents": {} } parent = parent["contents"][directory] return parent, path
for path in paths: if path[-1] == "/": path = path[:-1] parent, path = get_parent(path) parent["contents"][path] = { "type": "dir", "contents": {} } else: parent, path = get_parent(path) parent["contents"][path] = { "type": "file" }
""" Return the tree.
:returns: the file tree. :rtype: dictionary """ return self.tree
""" Walk through the file tree calling the callback function on each item contained.
:param callback: The function to be used as a callback, it should have the signature func(item, path) where item is a `tuple` for a file and `dict` for a directory. :type callback: function """ def walk(directory, parent_path): for path in directory["contents"].keys(): full_path = path_join(parent_path, path) if directory["contents"][path]["type"] == "dir": directory["contents"][path] = callback(full_path, directory["contents"][path] ) or directory["contents"][path] walk(directory["contents"][path], full_path) else: directory["contents"][path] = callback(full_path, directory["contents"][path] ) or directory["contents"][path] walk(self.tree, "")
lines = []
def write(path, item): depth = path.count("/") path = os.path.basename(path) path = path + "/" if item["type"] == "dir" else path lines.append(" " * depth + path) self.walk(write) return "\n".join(lines)
""" Convert a list of paths in a file tree.
:param paths: The paths to be converted. :type paths: list """
path = path[:-1] parent, path = get_parent(path) parent[path] = {} else:
""" Return the tree, after first converting all file lists to a tuple.
:returns: the file tree. :rtype: dictionary """
""" Walk through the file tree calling the callback function on each item contained.
:param callback: The function to be used as a callback, it should have the signature func(item, path) where item is a `tuple` for a file and `dict` for a directory. :type callback: function """ else:
lines = []
def write(path, item): depth = path.count("/") path = os.path.basename(path) path = type(item) is dict and path + "/" or path lines.append(" " * depth + path) self.walk(write) return "\n".join(lines)
""" Grabs the localclient auth line from the 'auth' file and creates a localhost uri
:returns: with the username and password to login as :rtype: tuple """ from deluge.common import create_localclient_account create_localclient_account()
# This is a comment line continue
username, password = lsplit else: log.error("Your auth file is malformed: Incorrect number of fields!") continue
|