def loadFile(self, filename): # creates the new XdkItem filename = nativestring(filename) basename = os.path.basename(filename) name = os.path.splitext(basename)[0] temp_dir = nativestring(QDir.tempPath()) temp_path = os.path.join(temp_dir, 'xdk/%s' % name) # remove existing files from the location if os.path.exists(temp_path): try: shutil.rmtree(temp_path, True) except: pass # make sure we have the temp location available if not os.path.exists(temp_path): try: os.makedirs(temp_path) except: pass # extract the zip files to the temp location zfile = zipfile.ZipFile(filename, 'r') zfile.extractall(temp_path) zfile.close() # load the table of contents self.loadingFinished.emit(filename)
def __init__(self, xdk_filename): # create the new XdkItem super(XdkItem, self).__init__(None, xdk_filename) # creates the new XdkItem basename = os.path.basename(nativestring(xdk_filename)) name = os.path.splitext(basename)[0] temppath = nativestring(QDir.tempPath()) temp_path = os.path.join(temppath, 'xdk/%s' % name) # define custom properties self._tempFilepath = temp_path self._searchurls = {} # set the options self.setChildIndicatorPolicy(self.ShowIndicator) self.setIcon(0, QIcon(projexui.resources.find('img/sdk.png'))) toc_file = os.path.join(temp_path, 'toc.xml') toc_xml = None if toc_file: try: toc_xml = ElementTree.parse(toc_file).getroot()[0] except: pass if toc_xml is not None: self._url = 'file:///%s/index.html' % temp_path.strip('/') self.setText(0, toc_xml.get('title', self.text(0))) self.loadFromXml(toc_xml, temp_path) else: # load the url information for this entry for name in sorted(os.listdir(temp_path)): # ignore 'hidden' folders if name.startswith('_') and not name.startswith('__'): continue # ignore special cases (only want modules for this) if '-' in name or name == 'toc.xml': continue # use the index or __init__ information if name == '__init__.html': self._url = 'file:///%s/%s' % (temp_path, name) continue elif name == 'index.html': self._url = 'file:///%s/%s' % (temp_path, name) # otherwise, load a childitem filepath = os.path.join(temp_path, name) folder = os.path.isdir(filepath) XdkEntryItem(self, filepath, folder=folder)
def dropEvent( self, event ): """ Handles a drop event. """ url = event.mimeData().urls()[0] url_path = nativestring(url.toString()) # download an icon from the web if ( not url_path.startswith('file:') ): filename = os.path.basename(url_path) temp_path = os.path.join(nativestring(QDir.tempPath()), filename) try: urllib.urlretrieve(url_path, temp_path) except IOError: return self.setFilepath(temp_path) else: self.setFilepath(url_path.replace('file://', ''))