test=False Log=False if len(sys.argv)>2: if sys.argv[2]=="-D" or sys.argv[2]=="-d": Debug=True print "wim: DEBUG mode" if len(sys.argv)>3: if sys.argv[3]=="-L" or sys.argv[3]=="-l": Log=True print "wim: gps LOG mode" if len(sys.argv)>4: if sys.argv[4]=="-T" or sys.argv[4]=="-t": test=True print "wim: gps TEST mode" doc=xml2obj(open(sys.argv[1])) gpslayers=[] #catch invalid xml try: for i in doc.gps.index: #catch invalid xml try: g=indexlayer(i.file,i.ll1,i.ll2) for t in i.trigger: g.triggers.append(trigger(int(t.id),t.command,t.param)) gpslayers.append(g) except: print "wim: error parsing xml index entry" except: print "wim: no index layers found"
def prepare_book(self, filepath): #Clear any old files from the cache if os.path.exists(self.cache_path): shutil.rmtree(self.cache_path) #Extract new book zipfile.ZipFile(filepath).extractall(path=self.cache_path) #Set permissions os.system("chmod 700 " + self.cache_path) #Find opf file if os.path.exists(self.cache_path + "META-INF/container.xml"): container_data = xml2obj( open(self.cache_path + "META-INF/container.xml", "r")) opf_file_path = container_data.rootfiles.rootfile.full_path #Load opf metadata = xml2obj(open(self.cache_path + opf_file_path, "r")) self.oebps = os.path.split(opf_file_path)[0] #Find ncx file for x in metadata.manifest.item: if x.media_type == "application/x-dtbncx+xml": ncx_file_path = self.cache_path + "/" + self.oebps + "/" + x.href #Load titles and filepaths self.titles = [] self.files = [] if os.access(ncx_file_path, os.R_OK): #Check if ncx is accessible #Parse ncx file pat = re.compile('-(.*)-') for line in open(ncx_file_path): line = line.strip() if "<text>" in line: out = line.replace("<text>", "") out = out.replace("</text>", "") out = out.replace("<content", "") self.titles.append(out) if "<content" in line: out = line.replace("<content src=\"", "") out = out.replace("\"", "") out = out.replace("/>", "") self.files.append(out) while not len(self.titles) == len(self.files): self.titles.remove(self.titles[0]) #Validate files if not os.path.exists(self.cache_path + "/" + self.oebps + "/" + self.files[0]): #Reload files self.files = [] for x in metadata.manifest.item: if x.media_type == "application/xhtml+xml": self.files.append(x.href) self.titles = [] i = 1 while not len(self.titles) == len(self.files): self.titles.append("Chapter " + str(i)) i += 1 #Calculate MD5 of book (for bookmarks) md5 = hashlib.md5() with open(filepath, 'rb') as f: for chunk in iter(lambda: f.read(128 * md5.block_size), ''): md5.update(chunk) #Metadata self.book_name = unicode( metadata.metadata.dc_title).encode("utf-8") self.book_author = unicode( metadata.metadata.dc_creator).encode("utf-8") self.book_md5 = md5.hexdigest() #Add book to config (used for bookmarks) if not self.config.has_section(self.book_md5): self.config.add_section(self.book_md5) self.config.set(self.book_md5, "count", 0) self.config.set(self.book_md5, "chapter", 0) self.config.set(self.book_md5, "pos", 0.0) self.config.set(self.book_md5, "stylesheet", "") #End of preparations self.ready = True return True else: #Else show an error dialog error_dialog = Gtk.MessageDialog(self.window, 0, Gtk.MessageType.ERROR, Gtk.ButtonsType.OK, "Could not open book.") error_dialog.format_secondary_text( "Make sure the book you are trying to open is in supported format and try again." ) error_dialog.run() error_dialog.destroy() self.ready = False return False
def prepare_book(self, filepath): #Clear any old files from the cache if os.path.exists(self.cache_path): shutil.rmtree(self.cache_path) #Extract new book zipfile.ZipFile(filepath).extractall(path=self.cache_path) #Set permissions os.system("chmod 700 "+self.cache_path) #Find opf file if os.path.exists(self.cache_path+"META-INF/container.xml"): container_data = xml2obj(open(self.cache_path+"META-INF/container.xml", "r")) opf_file_path = container_data.rootfiles.rootfile.full_path #Load opf metadata = xml2obj(open(self.cache_path+opf_file_path, "r")) self.oebps = os.path.split(opf_file_path)[0] #Find ncx file for x in metadata.manifest.item: if x.media_type == "application/x-dtbncx+xml": ncx_file_path = self.cache_path+"/"+self.oebps+"/"+x.href #Load titles and filepaths self.titles = [] self.files = [] if os.access(ncx_file_path, os.R_OK): #Check if ncx is accessible #Parse ncx file pat=re.compile('-(.*)-') for line in open(ncx_file_path): line=line.strip() if "<text>" in line: out = line.replace("<text>", "") out = out.replace("</text>", "") out = out.replace("<content", "") self.titles.append(out) if "<content" in line: out = line.replace("<content src=\"", "") out = out.replace("\"", "") out = out.replace("/>", "") self.files.append(out) while not len(self.titles) == len(self.files): self.titles.remove(self.titles[0]) #Validate files if not os.path.exists(self.cache_path+"/"+self.oebps+"/"+self.files[0]): #Reload files self.files = [] for x in metadata.manifest.item: if x.media_type == "application/xhtml+xml": self.files.append(x.href) self.titles = [] i = 1 while not len(self.titles) == len(self.files): self.titles.append("Chapter "+str(i)) i += 1 #Calculate MD5 of book (for bookmarks) md5 = hashlib.md5() with open(filepath,'rb') as f: for chunk in iter(lambda: f.read(128*md5.block_size), ''): md5.update(chunk) #Metadata self.book_name = unicode(metadata.metadata.dc_title).encode("utf-8") self.book_author = unicode(metadata.metadata.dc_creator).encode("utf-8") self.book_md5 = md5.hexdigest() #Add book to config (used for bookmarks) if not self.config.has_section(self.book_md5): self.config.add_section(self.book_md5) self.config.set(self.book_md5, "count", 0) self.config.set(self.book_md5, "chapter", 0) self.config.set(self.book_md5, "pos", 0.0) self.config.set(self.book_md5, "stylesheet","") #End of preparations self.ready = True return True else: #Else show an error dialog error_dialog = Gtk.MessageDialog(self.window, 0, Gtk.MessageType.ERROR, Gtk.ButtonsType.OK, "Could not open book.") error_dialog.format_secondary_text("Make sure the book you are trying to open is in supported format and try again.") error_dialog.run() error_dialog.destroy() self.ready = False return False
def decodeBody(self,charset): return xml2obj(super(XMLResponse, self).decodeBody(charset))