def OnDrop(self, files): """Opens dropped files @param files: list of file paths @postcondition: all files that could be properly opend are added to the notebook """ # Check file properties and make a "clean" list of file(s) to open valid_files = list() for fname in files: self.LOG("[ed_pages][evt] File(s) Dropped: %s" % fname) if not os.path.exists(fname): self.frame.PushStatusText(_("Invalid file: %s") % fname, \ ed_glob.SB_INFO) elif os.path.isdir(fname): dcnt = glob.glob(os.path.join(fname, '*')) dcnt = util.FilterFiles(dcnt) dlg = None if not len(dcnt): dlg = wx.MessageDialog(self, _("There are no files that Editra" " can open in %s") % fname, _("No Valid Files to Open"), style=wx.OK | wx.CENTER | \ wx.ICON_INFORMATION) elif len(dcnt) > 5: # Warn when the folder contains many files dlg = wx.MessageDialog(self, _("Do you wish to open all %d files" " in this directory?\n\nWarning" " opening many files at once may" " cause the editor to temporarly " " freeze.") % len(dcnt), _("Open Directory?"), style=wx.YES | wx.NO | \ wx.ICON_INFORMATION) if dlg is not None: result = dlg.ShowModal() dlg.Destroy() else: result = wx.ID_YES if result == wx.ID_YES: valid_files.extend(dcnt) else: pass else: valid_files.append(fname) for fname in valid_files: pathname = util.GetPathName(fname) the_file = util.GetFileName(fname) self.OpenPage(pathname, the_file) self.frame.PushStatusText(_("Opened file: %s") % fname, \ ed_glob.SB_INFO) return
def DoOpen(self, evt, fname=u''): """ Do the work of opening a file and placing it in a new notebook page. @keyword fname: can be optionally specified to open a file without opening a FileDialog @type fname: string """ try: e_id = evt.GetId() except AttributeError: e_id = evt if e_id == ID_OPEN: fdir = self.GetNotebook().GetCurrentCtrl().GetFileName() if len(fdir): fdir = os.path.dirname(fdir) elif not hasattr(sys, 'frozen'): fdir = os.curdir dlg = wx.FileDialog(self, _("Choose a File"), fdir, "", ''.join(syntax.GenFileFilters()), wx.OPEN | wx.MULTIPLE) dlg.SetFilterIndex(_PGET('FFILTER', 'int', 0)) if dlg.ShowModal() == wx.ID_OK: _PSET('FFILTER', dlg.GetFilterIndex()) for path in dlg.GetPaths(): if _PGET('OPEN_NW', default=False): wx.GetApp().OpenNewWindow(path) else: self.nb.OpenPage(util.GetPathName(path), util.GetFileName(path)) self.nb.GoCurrentPage() dlg.Destroy() else: self.LOG("[ed_main][info] CMD Open File: %s" % fname) self.nb.OpenPage(util.GetPathName(fname), util.GetFileName(fname), quiet=True) self.Raise()
def DoOpen(self, evt, fname=u''): """ Do the work of opening a file and placing it in a new notebook page. @keyword fname: can be optionally specified to open a file without opening a FileDialog @type fname: string """ result = wx.ID_CANCEL try: e_id = evt.GetId() except AttributeError: e_id = evt if e_id == ID_OPEN: dlg = wx.FileDialog(self, _("Choose a File"), '', "", ''.join(syntax.GenFileFilters()), wx.OPEN | wx.MULTIPLE) dlg.SetFilterIndex(_PGET('FFILTER', 'int', 0)) result = dlg.ShowModal() _PSET('FFILTER', dlg.GetFilterIndex()) paths = dlg.GetPaths() dlg.Destroy() if result != wx.ID_OK: self.LOG('[mainw][info] Canceled Opening File') else: for path in paths: if _PGET('OPEN_NW', default=False): wx.GetApp().OpenNewWindow(path) else: dirname = util.GetPathName(path) filename = util.GetFileName(path) self.nb.OpenPage(dirname, filename) self.nb.GoCurrentPage() else: self.LOG("[mainw][info] CMD Open File: %s" % fname) filename = util.GetFileName(fname) dirname = util.GetPathName(fname) self.nb.OpenPage(dirname, filename)
def testGetPathName(self): """Test that getting the path name from a string returns the correct string. """ roots = (("Home", "foo", "projects"), ("usr", "bin"), ("Users", "bar", "Desktop")) fname = "test.py" paths = [os.sep.join(root) for root in roots] for path in paths: tmp = os.path.join(path, fname) self.assertEqual(path, util.GetPathName(tmp), "util.GetPathName(%s) != %s" % (tmp, path))
def EditCommand(self, cmd): """Perform an edit related command @param cmd: command string to execute """ # e fname: edit file cmd = cmd[1:].strip() frame = self.GetTopLevelParent() if not os.path.isabs(cmd): cmd = os.path.join(self._curdir, cmd) if os.path.exists(cmd): frame.DoOpen(ed_glob.ID_COMMAND_LINE_OPEN, cmd) else: frame.nb.OpenPage(util.GetPathName(cmd), util.GetFileName(cmd))
def OpenPreviewFile(self, file_lbl): """Opens a file using the names in the Syntax Files choice control as a search query. @param file_lbl: name of file to open in test data directory """ fname = file_lbl.replace(u" ", u"_").replace(u"/", u"_").lower() fname = fname.replace('#', 'sharp') try: # fname = glob.glob(ed_glob.CONFIG['TEST_DIR'] + fname + ".*")[0] fname = glob.glob( os.path.join(util.GetResourceDir('tests'), fname) + ".*")[0] except IndexError: self.LOG('[style_editor][err] File %s Does not exist' % fname) return False #self.preview.SetFileName(fname) self.preview.dirname = util.GetPathName(fname) self.preview.filename = util.GetFileName(fname) self.preview.ClearAll() self.preview.LoadFile(fname) self.preview.FindLexer() self.preview.EmptyUndoBuffer() return True