def _gatherLiveFileUrls(self, part, relativeDir, extradir): #_getImportConfig(self, recursive=0, type="makeFlat") if not part.live: return [] config = part._getImportConfig(recursive=1, type="useFolders") include = config[0] exclude = config[1] recursive = config[2] type = config[3] path = config[4] if not path: return [] importService = components.classes["@activestate.com/koFileImportingService;1"].\ getService(components.interfaces.koIFileImportingService) filenames = set(importService.findCandidateFiles(part, path, include, exclude, recursive)) flist = [] for name in filenames: diskfile = os.path.abspath(name) if not os.path.isfile(diskfile): continue url = uriparse.localPathToURI(diskfile) dest = uriparse.RelativizeURL(relativeDir, url) if extradir: dest = os.path.join(extradir, dest) flist.append((diskfile, dest)) return flist
def test_localpaths(self): paths = [ ("path", "Normal windows local path", r"c:\test\test.txt", "file:///c:/test/test.txt"), ("path", "Windows UNC path", r"\\planer\d\trentm\tmp\foo.txt", "file://///planer/d/trentm/tmp/foo.txt"), ] for test in paths: uri = uriparse.localPathToURI(test[2].replace("\\","/")) self.failUnlessSamePath(uri, test[3].replace("\\","/"))
def test_dirty(self): name = 'test.kpf' project = self._setupProject(name) dirname, basename = os.path.split(name) basename = os.path.splitext(basename)[0] project.load(uriparse.localPathToURI((os.path.abspath(name)))); assert project.isDirty project.save() assert not project.isDirty folder = components.classes["@activestate.com/koPart?type=folder;1"] \ .createInstance(components.interfaces.koIPart_folder) project.addChild(folder) assert project.isDirty
def findCandidateFiles(self, part, dirname, include, exclude, recursive): self.lastErrorSvc.setLastError(0, '') try: if include != '': include_pats = include.split(';') else: include_pats = None if exclude != '': exclude_pats = exclude.split(';') else: exclude_pats = None part = UnwrapObject(part) allnames = self.getCandidatesForDir(dirname, False, include_pats, exclude_pats) # now look at children if recursive: for subdirname in _dirwalker(dirname, exclude_pats): names = self.getCandidatesForDir(subdirname, part.live, include_pats, exclude_pats) names = [os.path.join(subdirname, name) for name in names\ if part.live or os.path.isfile(os.path.join(subdirname, name))] allnames.extend(names) #print "part._project._urlmap:", part._project._urlmap if not part.live: # XXX REMOVE ME when livefolder == folder # for now, support the js dialog for importing newfiles = [] for name in allnames: name = os.path.abspath(name) url = uriparse.localPathToURI(name) if not part._project._urlmap.get(url, None): newfiles.append(name) return newfiles # live folder import wants all the matching files return allnames except: # TODO: Catch OSError or appropriate specific exception log.exception("failed scanning file system") return []
def _populate_history(self, last_dir): this_dir = os.path.abspath(dirname(__file__)) data_dir = join(this_dir, "tmp", last_dir) if not exists(data_dir): os.mkdir(data_dir) fnames = [] uris = [] for i in range(1, 6): fname = join(data_dir, "f%d.txt" % (i,)) f = open(fname, "w") f.write("# Data\n"); f.close() fnames.append(fname) uris.append(uriparse.localPathToURI(fname)) line_num = 10 limit = 10 for i in range(limit): uri = uris[i % len(uris)] self.history.note_loc(Location(uri, line_num, 0)) line_num += 10 current_loc = Location(uris[0], line_num, 0) # idx 10, line 110 return fnames, uris, current_loc
def test_saveUntitledFile(self): text = "This is a test!" koDoc = self._koDocUntitled() koDoc.buffer = text prefs = components.classes["@activestate.com/koPrefService;1"] \ .createInstance(components.interfaces.koIPrefService).prefs cleanLineEnds = prefs.getBooleanPref("cleanLineEnds") cleanLineEnds_CleanCurrentLine = prefs.getBooleanPref( "cleanLineEnds_CleanCurrentLine") cleanLineEnds_ChangedLinesOnly = prefs.getBooleanPref( "cleanLineEnds_ChangedLinesOnly") ensureFinalEOL = prefs.getBooleanPref("ensureFinalEOL") prefs.setBooleanPref("cleanLineEnds", True) prefs.setBooleanPref("cleanLineEnds_CleanCurrentLine", True) prefs.setBooleanPref("cleanLineEnds_ChangedLinesOnly", True) prefs.setBooleanPref("ensureFinalEOL", True) try: path = tempfile.mktemp() import uriparse uri = uriparse.localPathToURI(path) newdocument = self._koDocFromPath(path, load=False) newdocument.setBufferAndEncoding( koDoc.buffer, koDoc.encoding.python_encoding_name) newdocument.language = koDoc.language newdocument.save(True) assert os.path.exists(path) f = open(path) t = f.read() f.close() assert t == text os.unlink(path) # clean up finally: prefs.setBooleanPref("cleanLineEnds", cleanLineEnds) prefs.setBooleanPref("cleanLineEnds_CleanCurrentLine", cleanLineEnds_CleanCurrentLine) prefs.setBooleanPref("cleanLineEnds_ChangedLinesOnly", cleanLineEnds_ChangedLinesOnly) prefs.setBooleanPref("ensureFinalEOL", ensureFinalEOL)
def _populate_history(self, last_dir): this_dir = os.path.abspath(dirname(__file__)) data_dir = join(this_dir, "tmp", last_dir) if not exists(data_dir): os.mkdir(data_dir) fnames = [] uris = [] for i in range(1, 6): fname = join(data_dir, "f%d.txt" % (i, )) f = open(fname, "w") f.write("# Data\n") f.close() fnames.append(fname) uris.append(uriparse.localPathToURI(fname)) line_num = 10 limit = 10 for i in range(limit): uri = uris[i % len(uris)] self.history.note_loc(Location(uri, line_num, 0)) line_num += 10 current_loc = Location(uris[0], line_num, 0) # idx 10, line 110 return fnames, uris, current_loc
def test_saveUntitledFile(self): text = "This is a test!" koDoc = self._koDocUntitled() koDoc.buffer = text prefs = components.classes["@activestate.com/koPrefService;1"] \ .createInstance(components.interfaces.koIPrefService).prefs cleanLineEnds = prefs.getBooleanPref("cleanLineEnds") cleanLineEnds_CleanCurrentLine = prefs.getBooleanPref("cleanLineEnds_CleanCurrentLine") cleanLineEnds_ChangedLinesOnly = prefs.getBooleanPref("cleanLineEnds_ChangedLinesOnly") ensureFinalEOL = prefs.getBooleanPref("ensureFinalEOL") prefs.setBooleanPref("cleanLineEnds", True) prefs.setBooleanPref("cleanLineEnds_CleanCurrentLine", True) prefs.setBooleanPref("cleanLineEnds_ChangedLinesOnly", True) prefs.setBooleanPref("ensureFinalEOL", True) try: path = tempfile.mktemp() import uriparse uri = uriparse.localPathToURI(path) newdocument = self._koDocFromPath(path, load=False) newdocument.setBufferAndEncoding(koDoc.buffer, koDoc.encoding.python_encoding_name) newdocument.language = koDoc.language newdocument.save(True) assert os.path.exists(path) f = open(path) t = f.read() f.close() assert t == text os.unlink(path) # clean up finally: prefs.setBooleanPref("cleanLineEnds", cleanLineEnds) prefs.setBooleanPref("cleanLineEnds_CleanCurrentLine", cleanLineEnds_CleanCurrentLine) prefs.setBooleanPref("cleanLineEnds_ChangedLinesOnly", cleanLineEnds_ChangedLinesOnly) prefs.setBooleanPref("ensureFinalEOL", ensureFinalEOL)
def test_package(self): packageDir = os.path.join(os.getcwd(),'tmp') packageName = 'test' packagePath = os.path.join(packageDir,packageName+'.kpz') if not os.path.exists(packageDir): os.makedirs(packageDir) packager = components.classes["@activestate.com/koProjectPackageService;1"] \ .getService(components.interfaces.koIProjectPackageService) packager = UnwrapObject(packager) #packager.debug = 1 project = components.classes["@activestate.com/koProject;1"] \ .createInstance(components.interfaces.koIProject) uri = uriparse.localPathToURI(os.path.join(os.getcwd(),'install','release','INSTALLDIR','samples','sample_test.kpf')) #uri = uriparse.localPathToURI(os.path.join(os.getcwd(),'install','release','INSTALLDIR','samples','toolbox.kpf')) project.load(uri) packager.packageProject(packagePath, project) assert os.path.exists(packagePath) packager.extractPackage(packagePath, packageDir) assert os.path.exists(os.path.join(packageDir,packageName,packageName+'.kpf')) otherproject = components.classes["@activestate.com/koProject;1"] \ .createInstance(components.interfaces.koIProject) packager.importPackage(packagePath, packageDir, otherproject) assert len(project.getChildren()) == len(otherproject.getChildren())
def getDragDataByFlavor(self, flavor): if flavor == "application/x-moz-file": return self.path elif flavor in ("text/x-moz-url", "text/uri-list"): return uriparse.localPathToURI(self.path) return self.getDragData()
def _koDocFromPath(self, path, load=True): """Return an intialized `KoDocument` instance for the given path.""" import uriparse uri = uriparse.localPathToURI(path) return self._koDocFromURI(uri, load=load)
def test_read(self): verbose = 0 name = 'test.kpf' project = self._setupProject(name) dirname, basename = os.path.split(name) basename = os.path.splitext(basename)[0] project.load(uriparse.localPathToURI((os.path.abspath(name)))); if verbose: project.dump(0) print project.getChildren(), len(project.getChildren()) self.failUnless(len(project.getChildren()) == 2, "didn't read the right number of children") project.prefset.setStringPref('pref1', u'valueofpref1') assert project.prefset.getStringPref('pref1') == u'valueofpref1' project.name = project.name + '2' self.failUnlessEqual(project.name, "test2") project.save() project = components.classes["@activestate.com/koProject;1"] \ .createInstance(components.interfaces.koIProject) name = 'test2.kpf' project.load(uriparse.localPathToURI((os.path.abspath(name)))); if verbose: project.dump(0) project.prefset.dump(0) print "Project %s has %d children" % (name, len(project.getChildren())) assert project.prefset.getStringPref('pref1') == u'valueofpref1' project.prefset.deletePref('pref1') file1 = project.getChildren()[0] if verbose: file1.dump(0) fileprefs = components.classes["@activestate.com/koPreferenceSet;1"] \ .createInstance(components.interfaces.koIPreferenceSet) fileprefs.setStringPref("foo", "bar") file1.prefset = fileprefs project.name = project.name[:-1] + '3' if verbose: project.dump(0) project.save() project = components.classes["@activestate.com/koProject;1"] \ .createInstance(components.interfaces.koIProject) name = 'test3.kpf' project.load(uriparse.localPathToURI((os.path.abspath(name)))); file1 = project.getChildren()[0] assert file1.prefset.getStringPref('foo') == 'bar' # check that we can add children newfile = components.classes["@activestate.com/koPart;1"] \ .createInstance(components.interfaces.koIPart) newfile.type = 'file' newfile.setStringAttribute('url', 'foo') project.addChild(newfile) assert 3 == len(project.getChildren()) # check that reverts do revert project.revert() assert 2 == len(project.getChildren()) assert not project.isDirty project.name = 'foo' assert project.isDirty project.revert() assert not project.isDirty assert project.hasAttribute('kpf_version') assert not project.hasAttribute('foobar')
def addSelectedFiles(self, folder, importType, basedir, filenames): #import time #t1 = time.clock() # XXX bug 38793 if basedir[-1]=='/': basedir = basedir[:-1] folder = UnwrapObject(folder) project = folder._project _folderCache = {} # See what type of import this is #print "basedir: %r" % (basedir) URI = URIParser() URI.URI = basedir if URI.scheme in RemoteURISchemeTypes: isRemote = True # Convert all filenames into remote URI's remotePrefix = "%s://%s" % (URI.scheme, URI.server) # XXX - This hack may be needed on Windows ?? #if sys.platform.startswith("win"): # # Windows, convert the filenames to a windows format # filenames_and_urls = [(os.path.normpath(filename), remotePrefix + filename) for filename in filenames] #else: filenames_and_urls = [(filename, remotePrefix + filename) for filename in filenames] else: isRemote = False # Convert all filenames into local URI's filenames_and_urls = [(filename, uriparse.localPathToURI(str(filename))) for filename in filenames] data = [(project.getPart(filename, url, project, folder.live), filename, url) for (filename,url) in filenames_and_urls] #c1 = time.clock() #print "addSelectedFiles c1 ", c1-t1 if importType == 'makeFlat': for part, filename, url in data: part.live = folder.live folder.children.append(part) part._parent = folder project._urlmap[url] = part part.assignId() elif importType == 'groupByType': registry = components.classes["@activestate.com/koLanguageRegistryService;1"].\ getService(components.interfaces.koILanguageRegistryService) for part, filename, url in data: language = registry.suggestLanguageForFile(filename) if not language: language = 'Other' if language in _folderCache and _folderCache[subdirname]: subfolder = _folderCache[language] else: subfolder = _folderCache[language] = folder.getLanguageFolder(language) part.live = subfolder.live subfolder.children.append(part) part._parent = subfolder project._urlmap[url] = part part.assignId() elif importType == 'useFolders': if not isRemote: basedir = uriparse.localPathToURI(basedir) baseuri = basedir for part, filename, url in data: diruri = os.path.dirname(url) if diruri in _folderCache and _folderCache[diruri]: subfolder = _folderCache[diruri] else: subfolder = _folderCache[diruri] = folder.getDirFolder(baseuri, diruri) if not subfolder: log.error("Unable to get subfolder for %s: %s", baseuri, diruri) part.live = subfolder.live subfolder.children.append(part) part._parent = subfolder project._urlmap[url] = part part.assignId() #c2 = time.clock() #print "addSelectedFiles c2 ", c2-c1 try: # let the file status service know we need to get status info obSvc = components.classes["@mozilla.org/observer-service;1"]\ .getService(components.interfaces.nsIObserverService) obSvc.notifyObservers(self,'file_changed',basedir) except: pass # no listener project.set_isDirty(len(data) > 0)