def fmt(self, val): """ Format the values for common CSV output """ if type(val) in self.QUOTABLE_TYPES: s = decode_string(val) return u"{0}{1}{2}".format(self.quotechar, s, self.quotechar) else: return decode_string(str(val))
def read(self, line): """Parse a line of text input and return a tuple with the key(str) and value(list|dict) """ k, v = line.split('\t', 1) k_str = decode_string(k) v_obj = json.loads(decode_string(v)) if type(v_obj) not in self.READ_WRITE_TYPES: raise ValueError("Value is not an acceptable type") return (k_str, v_obj)
def read(self, line): """ read a line of csv data and output a list of values converts to unicode using common.decode_string """ l = csv.reader(StringIO.StringIO(line), delimiter=self.delimiter, quotechar=self.quotechar, skipinitialspace=True) for r in l: data = [decode_string(f).strip() for f in r] break return (None, data)
def encodestring(s, encoding=None): if encoding: s = common.decode_string(s, encoding) return s
class TorrentInfo(object): """ Collects information about a torrent file. :param filename: The path to the torrent :type filename: string """ def __init__(self, filename, filetree=1): # Get the torrent data from the torrent file try: #log.debug("Attempting to open %s.", filename) self.__m_filedata = open(filename, "rb").read() self.__m_metadata = bencode.bdecode(self.__m_filedata) except Exception, e: #log.warning("Unable to open %s: %s", filename, e) raise e print "Invalid Torrent" return self.__m_info_hash = sha(bencode.bencode( self.__m_metadata["info"])).hexdigest() # Get encoding from torrent file if available self.encoding = "UTF-8" if "encoding" in self.__m_metadata: self.encoding = self.__m_metadata["encoding"] elif "codepage" in self.__m_metadata: 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. if "name.utf-8" in self.__m_metadata["info"]: self.__m_name = decode_string( self.__m_metadata["info"]["name.utf-8"]) else: self.__m_name = decode_string(self.__m_metadata["info"]["name"], self.encoding) # Get list of files from torrent info paths = {} dirs = {} if self.__m_metadata["info"].has_key("files"): prefix = "" if len(self.__m_metadata["info"]["files"]) > 1: prefix = self.__m_name for index, f in enumerate(self.__m_metadata["info"]["files"]): if "path.utf-8" in f: path = os.path.join(prefix, *f["path.utf-8"]) else: path = decode_string( os.path.join( prefix, decode_string(os.path.join(*f["path"]), self.encoding)), self.encoding) f["index"] = index paths[path] = f dirname = os.path.dirname(path) while dirname: dirinfo = dirs.setdefault(dirname, {}) dirinfo["length"] = dirinfo.get("length", 0) + f["length"] dirname = os.path.dirname(dirname) if filetree == 2: 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: def walk(path, item): if type(item) is dict: return item return [paths[path]["index"], paths[path]["length"], True] file_tree = FileTree(paths) file_tree.walk(walk) self.__m_files_tree = file_tree.get_tree() else: if filetree == 2: self.__m_files_tree = { "contents": { self.__m_name: { "type": "file", "index": 0, "length": self.__m_metadata["info"]["length"], "download": True } } } else: self.__m_files_tree = { self.__m_name: (0, self.__m_metadata["info"]["length"], True) } self.__m_files = [] if self.__m_metadata["info"].has_key("files"): prefix = "" if len(self.__m_metadata["info"]["files"]) > 1: prefix = self.__m_name for f in self.__m_metadata["info"]["files"]: if "path.utf-8" in f: path = os.path.join(prefix, *f["path.utf-8"]) else: path = decode_string( os.path.join( prefix, decode_string(os.path.join(*f["path"]), self.encoding)), self.encoding) self.__m_files.append({ 'path': path, 'size': f["length"], 'download': True }) else: self.__m_files.append({ "path": self.__m_name, "size": self.__m_metadata["info"]["length"], "download": True })