def test22_copy_folder(self): vfs.copy('vfs', 'mem:/tmp/folder-copy') with vfs.open('mem:/tmp/folder-copy/hello.txt') as file: self.assertEqual(file.read(), 'hello world\n') vfs.make_folder('mem:/tmp/folder-dest') vfs.copy('vfs', 'mem:/tmp/folder-dest') with vfs.open('mem:/tmp/folder-dest/vfs/hello.txt') as file: self.assertEqual(file.read(), 'hello world\n')
def test21_copy_file(self): url = self.root + 'tmp/dir/' vfs.make_folder(url) vfs.copy(self.root + 'tmp/blah.txt', url) file = vfs.open(url + 'blah.txt') self.assertEqual(file.read(), 'BLAH!!!') vfs.copy(self.root + 'tmp/blah.txt', url + 'blah2.txt') file = vfs.open(url + 'blah2.txt') self.assertEqual(file.read(), 'BLAH!!!')
def test21_copy_file(self): url = self.root + "tmp/dir/" vfs.make_folder(url) vfs.copy(self.root + "tmp/blah.txt", url) file = vfs.open(url + "blah.txt") self.assertEqual(file.read(), "BLAH!!!") vfs.copy(self.root + "tmp/blah.txt", url + "blah2.txt") file = vfs.open(url + "blah2.txt") self.assertEqual(file.read(), "BLAH!!!")
def test12_append(self): file = vfs.open('mem:testfile.txt', vfs.APPEND) file.write("two\n") file.close() file = vfs.open('mem:testfile.txt') self.assertEqual(file.read(), 'one\ntwo\n') file = vfs.open('mem:testfile.txt', vfs.WRITE) file.write("three\n") file.close() file = vfs.open('mem:testfile.txt') self.assertEqual(file.read(), 'three\n')
def test12_append(self): file = vfs.make_file(self.root + "testfile.txt") file.write("one\n") file.close() file = vfs.open(self.root + "testfile.txt", vfs.APPEND) file.write("two\n") file.close() file = vfs.open(self.root + "testfile.txt") self.assertEqual(file.read(), "one\ntwo\n") file = vfs.open(self.root + "testfile.txt", vfs.WRITE) file.write("three\n") file.close() file = vfs.open(self.root + "testfile.txt") self.assertEqual(file.read(), "three\n")
def getFilePair(cls, url): fh = vfs.open(url) line = fh.read(4) if line == 'ENVI': # need to open the cube, not the header return (url, None) fh.close() headers = findHeaders(url) for header in headers: fh = vfs.open(header) line = fh.read(4) if line == 'ENVI': return (header, url) fh.close() return None
def getFilePair(cls, url): fh = vfs.open(url) line=fh.read(4) if line=='ENVI': # need to open the cube, not the header return (url, None) fh.close() headers = findHeaders(url) for header in headers: fh = vfs.open(header) line=fh.read(4) if line=='ENVI': return (header, url) fh.close() return None
def applyProjectSettings(cls, msg): """Publish/subscribe callback to load project view settings. This is called by the major mode loading process to set the default view parameters based on per-project defaults. """ mode = msg.data if not mode.project_info: return cls.dprint("Applying settings for %s" % mode) # Add 'project' keyword to Buffer object if the file belongs to a # project settings = cls.findProjectConfigObject(mode, cls.classprefs.settings_directory) if not settings: # Try the global settings cls.dprint("Trying default settings for project %s" % mode.project_info.project_name) base = mode.project_info.getSettingsRelativeURL() url = base.resolve2(ProjectPlugin.classprefs.default_settings_file_name) if vfs.exists(url): fh = vfs.open(url) settings = pickle.load(fh) fh.close() if settings and isinstance(settings, dict): # dprint(settings) # mode.applyFileLocalComments(settings) mode.classprefsUpdateLocals(settings) mode.applyDefaultSettings()
def getConfigFileHandle(cls, confdir, mode, url): """Get a file handle to data in the project's configuration directory. Files within the configuration directory should be based on the major mode name, and optionally for added specificity can append a filename extension. For instance, the template system used for projects uses the "$PROJROOT/.peppy- project/templates" directory by default, where $PROJROOT is the root directory of the project. Within that templates directory, the file "Python" is the default for python files, but "Python.pyx" could be used as a default for pyrex files. @param confdir: pathname of configuration directory @param mode: major mode instance @param url: url of file that is being created @return: file-like object to read the data, or None if not found """ filename = vfs.get_filename(url) names = [] if "." in filename: ext = filename.split(".")[-1] names.append(confdir.resolve2("%s.%s" % (mode.keyword, ext))) names.append(confdir.resolve2(mode.keyword)) for configname in names: try: cls.dprint("Trying to load file %s" % configname) fh = vfs.open(configname) return fh except: pass return None
def identify(cls, url, filename=None): fh = vfs.open(url) line=fh.read(160) cls.dprint(repr(line)) if line[0:30] =='SIMPLE = T' and line[80:90] == 'BITPIX = ': return True return False
def test20_move_file(self): vfs.copy(self.root + 'tmp/blah.txt', self.root + 'tmp/blah.txt.bak') vfs.move(self.root + 'tmp/blah.txt.bak', self.root + 'tmp/blah.txt.old') file = vfs.open(self.root + 'tmp/blah.txt.old') self.assertEqual(file.read(), 'BLAH!!!') self.assertEqual(vfs.exists(self.root + 'tmp/blah.txt.bak'), False)
def applyProjectSettings(cls, msg): """Publish/subscribe callback to load project view settings. This is called by the major mode loading process to set the default view parameters based on per-project defaults. """ mode = msg.data if not mode.project_info: return cls.dprint("Applying settings for %s" % mode) # Add 'project' keyword to Buffer object if the file belongs to a # project settings = cls.findProjectConfigObject( mode, cls.classprefs.settings_directory) if not settings: # Try the global settings cls.dprint("Trying default settings for project %s" % mode.project_info.project_name) base = mode.project_info.getSettingsRelativeURL() url = base.resolve2( ProjectPlugin.classprefs.default_settings_file_name) if vfs.exists(url): fh = vfs.open(url) settings = pickle.load(fh) fh.close() if settings and isinstance(settings, dict): #dprint(settings) #mode.applyFileLocalComments(settings) mode.classprefsUpdateLocals(settings) mode.applyDefaultSettings()
def getConfigFileHandle(cls, confdir, mode, url): """Get a file handle to data in the project's configuration directory. Files within the configuration directory should be based on the major mode name, and optionally for added specificity can append a filename extension. For instance, the template system used for projects uses the "$PROJROOT/.peppy- project/templates" directory by default, where $PROJROOT is the root directory of the project. Within that templates directory, the file "Python" is the default for python files, but "Python.pyx" could be used as a default for pyrex files. @param confdir: pathname of configuration directory @param mode: major mode instance @param url: url of file that is being created @return: file-like object to read the data, or None if not found """ filename = vfs.get_filename(url) names = [] if '.' in filename: ext = filename.split('.')[-1] names.append(confdir.resolve2("%s.%s" % (mode.keyword, ext))) names.append(confdir.resolve2(mode.keyword)) for configname in names: try: cls.dprint("Trying to load file %s" % configname) fh = vfs.open(configname) return fh except: pass return None
def identify(cls, url, filename=None): fh = vfs.open(url) line = fh.read(160) cls.dprint(repr(line)) if line[0:30] == 'SIMPLE = T' and line[ 80:90] == 'BITPIX = ': return True return False
def save(self, filename=None): if filename: fh = vfs.open(filename, vfs.WRITE) if fh: self.parser.write(fh) fh.close() self.setURL(filename) else: eprint("Couldn't save %s\n" % filename)
def getBufferedReader(self, size=1024): assert self.dprint(u"opening %s as %s" % (self.url, self.defaultmode)) if self.bfh is None: if vfs.exists(self.url): fh = vfs.open(self.url) self.bfh = BufferedReader(fh, size) if self.bfh: self.bfh.seek(0) return self.bfh
def save(self,filename=None): if filename: fh = vfs.open(filename, vfs.WRITE) if fh: self.parser.write(fh) fh.close() self.setURL(filename) else: eprint("Couldn't save %s\n" % filename)
def open(self,filename=None): """Open the header file, and if successful parse it.""" if self.headerurl: fh = vfs.open(self.headerurl) if fh: self.read(fh) fh.close() else: eprint("Couldn't open %s for reading.\n" % self.headerurl)
def open(self, filename=None): """Open the header file, and if successful parse it.""" if self.headerurl: fh = vfs.open(self.headerurl) if fh: self.read(fh) fh.close() else: eprint("Couldn't open %s for reading.\n" % self.headerurl)
def test20_append(self): # Initialize with vfs.make_file('vfs/toto.txt') as file: file.write('hello\n') # Test with vfs.open('vfs/toto.txt', vfs.APPEND) as file: file.write('bye\n') self.assertEqual(open('vfs/toto.txt').read(), 'hello\nbye\n') # Remove temporary file vfs.remove('vfs/toto.txt')
def test21_write(self): # Initialize with vfs.make_file('vfs/truncate.txt') as file: file.write('hello there\n') # Test with vfs.open('vfs/truncate.txt', vfs.WRITE) as file: file.write('bye\n') self.assertEqual(open('vfs/truncate.txt').read(), 'bye\n') # Remove temporary file vfs.remove('vfs/truncate.txt')
def identify(cls, url): fh = vfs.open(url) assert cls.dprint("checking for cube handler: %s" % dir(fh)) if fh and hasattr(fh, 'metadata') and hasattr(fh.metadata, 'getCube'): return fh.metadata url = vfs.normalize(url) matches = cls.identifyall(url) if len(matches)>0: return matches[0] return None
def identify(cls, url): fh = vfs.open(url) assert cls.dprint("checking for cube handler: %s" % dir(fh)) if fh and hasattr(fh, 'metadata') and hasattr(fh.metadata, 'getCube'): return fh.metadata url = vfs.normalize(url) matches = cls.identifyall(url) if len(matches) > 0: return matches[0] return None
def open(self, url=None): """Open the header file, and if successful parse it.""" if url: self.setURL(url) if self.url: #fh=self.url.getReader() fh = vfs.open(self.url) if fh: self.read(fh) fh.close()
def save(self,filename=None): if filename: url = vfs.normalize(filename) if vfs.is_file(url): fh = vfs.open(url, vfs.WRITE) else: fh = vfs.make_file(url) if fh: fh.write(str(self)) fh.close() else: eprint("Couldn't open %s for writing.\n" % filename)
def loadPrefs(self): self.project_config = self.project_settings_dir.resolve2(ProjectPlugin.classprefs.project_file) try: fh = vfs.open(self.project_config) if fh: self.readConfig(fh) for param in self.iterPrefs(): self.dprint("%s = %s" % (param.keyword, getattr(self, param.keyword))) self.dprint(self.configToText()) except LookupError: self.dprint("Project file not found -- using defaults.") self.setDefaultPrefs()
def save(self, filename=None): if filename: url = vfs.normalize(filename) if vfs.is_file(url): fh = vfs.open(url, vfs.WRITE) else: fh = vfs.make_file(url) if fh: fh.write(str(self)) fh.close() else: eprint("Couldn't open %s for writing.\n" % filename)
def action(self, index=-1, multiplier=1): stylesheet = wx.GetApp().fonts.getStyleFile() dlg = PeppyStyleEditor(self.frame, -1) self.setSampleText(dlg) retval = dlg.ShowModal() if retval == wx.ID_OK: # Find style name from controls within the dialog ctrl = dlg.FindWindowById(style_editor.ed_glob.ID_PREF_SYNTHEME) tag = ctrl.GetStringSelection() ctrl = dlg.FindWindowById(wx.ID_NEW) if ctrl.GetValue(): tag = "untitled" dlg2 = wx.TextEntryDialog( self.frame, caption="Save Style Sheet", message= "Enter name for style sheet.\n\nThis style sheet must be saved in your personal\nconfiguration directory before it can be used.\n\nIf you cancel, the style sheet changes will be lost.", defaultValue=tag, style=wx.OK | wx.CANCEL) retval = dlg2.ShowModal() tag = dlg2.GetValue() if not tag: tag = "untitled" dlg2.Destroy() if retval == wx.ID_OK: #dprint("Saving style to %s" % tag) filename = wx.GetApp().fonts.getStylePath(tag) styles = ed_style.MergeStyles( dlg.preview.BlankStyleDictionary(), dlg.styles_new) dlg.preview.SetStyles(filename, styles, True) EditraSTCMixin.global_style_set = filename sheet = dlg.GenerateStyleSheet() #dprint(sheet) fh = open(filename, 'wb') fh.write(sheet) wx.GetApp().fonts.classprefs.editra_style_theme = tag Publisher().sendMessage('peppy.preferences.changed') elif retval == wx.ID_SAVE: dprint("Save!") sheet = dlg.GenerateStyleSheet() dprint(sheet) self.export_count += 1 url = "mem:///style-sheet-%d.ess" % self.export_count fh = vfs.make_file(url) fh.write(sheet) fh.close() fh = vfs.open(url) dprint(fh.read()) self.frame.open(url) dlg.Destroy()
def loadPrefs(self): self.project_config = self.project_settings_dir.resolve2( ProjectPlugin.classprefs.project_file) try: fh = vfs.open(self.project_config) if fh: self.readConfig(fh) for param in self.iterPrefs(): self.dprint("%s = %s" % (param.keyword, getattr(self, param.keyword))) self.dprint(self.configToText()) except LookupError: self.dprint("Project file not found -- using defaults.") self.setDefaultPrefs()
def action(self, index=-1, multiplier=1): stylesheet = wx.GetApp().fonts.getStyleFile() dlg = PeppyStyleEditor(self.frame, -1) self.setSampleText(dlg) retval = dlg.ShowModal() if retval == wx.ID_OK: # Find style name from controls within the dialog ctrl = dlg.FindWindowById(style_editor.ed_glob.ID_PREF_SYNTHEME) tag = ctrl.GetStringSelection() ctrl = dlg.FindWindowById(wx.ID_NEW) if ctrl.GetValue(): tag = "untitled" dlg2 = wx.TextEntryDialog( self.frame, caption="Save Style Sheet", message="Enter name for style sheet.\n\nThis style sheet must be saved in your personal\nconfiguration directory before it can be used.\n\nIf you cancel, the style sheet changes will be lost.", defaultValue=tag, style=wx.OK|wx.CANCEL) retval = dlg2.ShowModal() tag = dlg2.GetValue() if not tag: tag = "untitled" dlg2.Destroy() if retval == wx.ID_OK: #dprint("Saving style to %s" % tag) filename = wx.GetApp().fonts.getStylePath(tag) styles = ed_style.MergeStyles(dlg.preview.BlankStyleDictionary(), dlg.styles_new) dlg.preview.SetStyles(filename, styles, True) EditraSTCMixin.global_style_set = filename sheet = dlg.GenerateStyleSheet() #dprint(sheet) fh = open(filename, 'wb') fh.write(sheet) wx.GetApp().fonts.classprefs.editra_style_theme = tag Publisher().sendMessage('peppy.preferences.changed') elif retval == wx.ID_SAVE: dprint("Save!") sheet = dlg.GenerateStyleSheet() dprint(sheet) self.export_count += 1 url = "mem:///style-sheet-%d.ess" % self.export_count fh = vfs.make_file(url) fh.write(sheet) fh.close() fh = vfs.open(url) dprint(fh.read()) self.frame.open(url) dlg.Destroy()
def revertEncoding(self, buffer, url=None, message=None, encoding=None, allow_undo=False): if url is None: url = buffer.url fh = vfs.open(url) if allow_undo: self.BeginUndoAction() self.ClearAll() self.readThreaded(fh, buffer, message) self.openSuccess(buffer, encoding=encoding) if allow_undo: self.EndUndoAction() else: self.EmptyUndoBuffer()
def load(cls, url): import cPickle as pickle # Note: because plugins are loaded using the execfile command, pickle # can't find classes that are in the global namespace. Have to supply # PythonScriptableMacro into the builtin namespace to get around this. import __builtin__ __builtin__.PythonScriptableMacro = PythonScriptableMacro if not vfs.exists(url): return fh = vfs.open(url) bytes = fh.read() fh.close() if bytes: version, data = pickle.loads(bytes) if version == 1: cls.unpackVersion1(data) else: raise RuntimeError("Unknown version of MacroSaveData in %s" % url)
def load(cls, url, bad=None, progress=None): """Find an HSI dataset instance corresponding to the url @param url: url to load @param bad: subclass of HSI.MetadataMixin that should be avoided. This is used to select a different dataset reader if an error occurs with the one specified here. @param progress: (optional) progress bar callback @return: instance of HSI.MetadataMixin that can read the file, or None if nothing is found. """ cls.discover() url = vfs.normalize(url) if bad: cls.dprint("EXCLUDING %s" % bad) # Check to see if there's a specific handler provided in the vfs fh = vfs.open(url) cls.dprint("checking for cube handler: %s" % dir(fh)) if fh and hasattr(fh, 'metadata') and hasattr(fh.metadata, 'getCube'): dataset = fh.metadata # Only return the dataset if it's not the same class we're trying # to avoid if dataset.__class__ != bad: return dataset # OK, that didn't return a result, so see if there's a HSI handler matches = cls.identifyall(url) for format in matches: if format == bad: cls.dprint("Skipping format %s" % format.format_name) continue cls.dprint("Loading %s format cube" % format.format_name) dataset = format(url, progress=progress) return dataset return None
def load(cls, urlinfo): fh = vfs.open(urlinfo) group = ENVITextROI(urlinfo) current = None index = -1 for line in fh: if line.startswith(';'): if line.startswith('; ROI '): name, val = line[6:].split(':') name = name.strip() val = val.strip() dprint("name=%s val=%s" % (name, val)) if name == 'name': current = ROI(val) group.addROI(current) elif name == 'npts': current.number = int(val) elif name == 'rgb value': current.setColor(val) else: dprint(current) current = None elif line.startswith('; ID'): index = 0 current = group.getROI(index) else: vals = line.split() if len(vals) > 0: # ENVI roi coordinates start from 1, not zero dprint(vals) current.addPoint(int(vals[0]), int(vals[1]) - 1, int(vals[2]) - 1) else: index += 1 current = group.getROI(index) dprint(group) return group
def parseCtags(self, filename): self.tags = {} tagre = re.compile('(.+)\t(.+)\t(.+);"\t(.+)') try: fh = vfs.open(filename) for line in fh: match = tagre.match(line) if match: tag = match.group(1) file = match.group(2) addr = match.group(3) fields = match.group(4) self.dprint("tag=%s file=%s addr=%s field=%s" % (tag, file, addr, str(fields))) if tag not in self.tags: self.tags[tag] = [] self.tags[tag].append((file, addr, fields)) #dprint(self.tags[tag]) else: self.dprint(line) except LookupError, e: self.dprint("Tag file %s not found" % filename) pass
def load(cls, urlinfo): fh = vfs.open(urlinfo) group = ENVITextROI(urlinfo) current = None index = -1 for line in fh: if line.startswith(';'): if line.startswith('; ROI '): name, val = line[6:].split(':') name = name.strip() val = val.strip() dprint("name=%s val=%s" % (name, val)) if name == 'name': current = ROI(val) group.addROI(current) elif name == 'npts': current.number = int(val) elif name == 'rgb value': current.setColor(val) else: dprint(current) current = None elif line.startswith('; ID'): index = 0 current = group.getROI(index) else: vals = line.split() if len(vals)>0: # ENVI roi coordinates start from 1, not zero dprint(vals) current.addPoint(int(vals[0]), int(vals[1])-1, int(vals[2])-1) else: index += 1 current = group.getROI(index) dprint(group) return group
def test_simple(self): vfs.make_folder(self.root + 'tmp') file = vfs.make_file(self.root + 'tmp/blah.txt') file.write("BLAH!!!") file.close() # Probably too loose of a test, but the modification time has a 10 # second window for correctness mtime = vfs.get_mtime(self.root + 'tmp/blah.txt') current = datetime.now() assert abs(mtime - current) < timedelta(10) file = vfs.open(self.root + 'tmp/blah.txt') self.assertEqual(file.read(), 'BLAH!!!') assert vfs.exists(self.root + 'tmp') names = vfs.get_names(self.root) assert "tmp" in names names = vfs.get_names(self.root + 'tmp') assert "blah.txt" in names vfs.remove(self.root + 'tmp') assert not vfs.exists(self.root + 'tmp') assert not vfs.exists(self.root + 'tmp/blah.txt')
def test_simple(self): vfs.make_folder(self.root + "tmp") file = vfs.make_file(self.root + "tmp/blah.txt") file.write("BLAH!!!") file.close() # Probably too loose of a test, but the modification time has a 10 # second window for correctness mtime = vfs.get_mtime(self.root + "tmp/blah.txt") current = datetime.now() assert abs(mtime - current) < timedelta(10) file = vfs.open(self.root + "tmp/blah.txt") self.assertEqual(file.read(), "BLAH!!!") assert vfs.exists(self.root + "tmp") names = vfs.get_names(self.root) assert "tmp" in names names = vfs.get_names(self.root + "tmp") assert "blah.txt" in names vfs.remove(self.root + "tmp") assert not vfs.exists(self.root + "tmp") assert not vfs.exists(self.root + "tmp/blah.txt")
def test11_reading(self): file = vfs.make_file(self.root + 'testfile.txt') file.write("one\n") file.close() file = vfs.open(self.root + 'testfile.txt') self.assertEqual(file.read(), 'one\n')
def test17_move_file(self): vfs.copy('vfs/hello.txt', 'vfs/hello.txt.bak') vfs.move('vfs/hello.txt.bak', 'vfs/hello.txt.old') file = vfs.open('vfs/hello.txt.old') self.assertEqual(file.read(), 'hello world\n') self.assertEqual(vfs.exists('vfs/hello.txt.bak'), False)
def open(self, buffer, progress_message): """Save the file handle, which is really the mpd connection""" self.pipe = vfs.open(buffer.url) self.readThreaded(self.pipe, buffer)
def test_copy_file(self): vfs.copy('vfs/hello.txt', 'vfs-tmp/hello.txt.bak') with vfs.open('vfs-tmp/hello.txt.bak') as file: self.assertEqual(file.read(), 'hello world\n')
def setUp(self): self.tests = vfs.open('vfs/')
def test_open(self): file = vfs.open('http://www.google.com') data = file.read() file.close() self.assertEqual('<html>' in data, True)
def test15_open_file(self): file = vfs.open('vfs/hello.txt') self.assertEqual(file.read(), 'hello world\n')
def test_copy_folder_to_folder(self): vfs.copy('vfs', 'vfs-tmp') with vfs.open('vfs-tmp/vfs/hello.txt') as file: self.assertEqual(file.read(), 'hello world\n')