def dump_dir(c, src_path, dest_path): src_path = src_path.strip("/") # since some people may still be holding back progress with Python 2, I'll support # them for now and not use the Python 3 exist_ok option :( try: os.makedirs(dest_path) except OSError as e: pass # Access the list of vcards in the directory hdrs, cards = c.get(src_path, header_list=[headers.Type(b'x-bt/vcard-listing')]) # Parse the XML response to the previous request. # Extract a list of file names in the directory names = [] root = parse_xml(cards) dump_xml(root, "/".join([dest_path, "listing.xml"])) for card in root.findall("card"): names.append(card.attrib["handle"]) c.setpath(src_path) # get all the files for name in names: get_file(c, name, "/".join([dest_path, name]), folder_name=src_path) # return to the root directory depth = len([f for f in src_path.split("/") if len(f)]) for i in range(depth): c.setpath(to_parent=True)
def listdir(self, name="", xml=False): """listdir(self, name = "") Requests information about the contents of the directory with the specified name relative to the current directory for the session. If the name is omitted or an empty string is supplied, the contents of the current directory are typically listed by the server. If successful, the server will provide an XML folder listing. If the xml argument is true, the XML listing will be returned directly. Else, this function will parse the XML and return a tuple of two lists, the first list being the folder names, and the second list being file names. """ hdrs, data = self.get( name, header_list=[headers.Type(b"x-obex/folder-listing", False)]) if xml: return data tree = parse_xml(data) folders = [] files = [] for e in tree: if e.tag == "folder": folders.append(e.attrib["name"]) elif e.tag == "file": files.append(e.attrib["name"]) elif e.tag == "parent-folder": pass # ignore it else: sys.stderr.write("Unknown listing element %s\n" % e.tag) return folders, files