def initLogging(mode, force=False): global ag_debugLogger, loggingInitialized if (force or not loggingInitialized): loggingInitialized = True configFile = None if (mode == LOG_MODE_IDE): configFile = os.getenv("AG_LOGCONFIG_IDE") elif (mode == LOG_MODE_TESTRUN): configFile = os.getenv("AG_LOGCONFIG_PYTESTRUN") else: configFile = os.getenv("AG_LOGCONFIG_RUN") if ((configFile == None) or not os.path.exists(configFile)): if (mode == LOG_MODE_IDE): configFile = "IDELog" elif (mode == LOG_MODE_TESTRUN): configFile = "TestRunLog" else: configFile = "RunLog" configFile = os.path.join(appdirs.getSystemDir(appdirs.AG_LOGS_DIR), "py" + configFile + ".ini") if (os.path.exists(configFile)): print "Using logging configuration file: %s" % configFile fileConfig(configFile) else: print "*** Cannot find logging configuration file (%s) -- setting default logging level to WARN ***" % (configFile) defaultStream = sys.stderr if (mode == LOG_MODE_RUN): defaultStream = sys.stdout handler = logging.StreamHandler(defaultStream) handler.setLevel(logging.DEBUG) handler.setFormatter(logging.Formatter("%(asctime)s %(name)s %(levelname)s: %(message)s")) logging.getLogger().addHandler(handler) logging.getLogger().setLevel(logging.WARN) ag_debugLogger = logging.getLogger("tools.bgui.debug") ag_debugLogger.setLevel(logging.DEBUG) return configFile
def CreateDirectoryControl( parent, fileLabel=_("File Name:"), dirLabel=_("Directory:"), fileExtension="*", startingName="", startingDirectory=None, choiceDirs=None, appDirDefaultStartDir=False, returnAll=False, useDirDialog=False): if not choiceDirs: choiceDirs = [] projectDirs = [] if appDirDefaultStartDir: appDirectory = wx.ConfigBase_Get().Read(tools.bgui.tool.projecteditor.PROJECT_DIRECTORY_KEY, bdec.gui.tool.projecteditor.NEW_PROJECT_DIRECTORY_DEFAULT) else: appDirectory = wx.ConfigBase_Get().Read(tools.bgui.tool.projecteditor.PROJECT_DIRECTORY_KEY) if appDirectory: choiceDirs.append(appDirectory) if appDirDefaultStartDir and not startingDirectory: startingDirectory = appDirectory projectService = wx.GetApp().GetService(tools.bgui.tool.projecteditor.ProjectService) if projectService: curProjectDoc = projectService.GetCurrentProject() if curProjectDoc: homeDir = curProjectDoc.GetAppDocMgr().homeDir if homeDir and (homeDir not in choiceDirs): choiceDirs.append(homeDir) if not startingDirectory: startingDirectory = homeDir for projectDoc in projectService.GetOpenProjects(): if projectDoc == curProjectDoc: continue homeDir = projectDoc.GetAppDocMgr().homeDir if homeDir and (homeDir not in projectDirs): projectDirs.append(homeDir) projectDirs.sort(CaseInsensitiveCompare) for projectDir in projectDirs: if projectDir not in choiceDirs: choiceDirs.append(projectDir) if startingDirectory and (startingDirectory not in choiceDirs): choiceDirs.insert(0, startingDirectory) if os.getcwd() not in choiceDirs: choiceDirs.append(os.getcwd()) if appdirs.getSystemDir() not in choiceDirs: choiceDirs.append(appdirs.getSystemDir()) if not startingDirectory: startingDirectory = os.getcwd() nameControl = wx.TextCtrl(parent, -1, startingName, size=(-1,-1)) nameLabelText = wx.StaticText(parent, -1, fileLabel) dirLabelText = wx.StaticText(parent, -1, dirLabel) dirControl = wx.ComboBox(parent, -1, startingDirectory, size=(-1,-1), choices=choiceDirs) dirControl.SetToolTipString(startingDirectory) button = wx.Button(parent, -1, _("Browse...")) allControls = [nameControl, nameLabelText, dirLabelText, dirControl, button] def OnFindDirClick(event): name = "" nameCtrlValue = nameControl.GetValue() if nameCtrlValue: root, ext = os.path.splitext( nameCtrlValue ) if ext == '.' + fileExtension: name = nameCtrlValue else: name = _("%s.%s") % (nameCtrlValue, fileExtension) if not useDirDialog: dlg = wx.FileDialog(parent, _("Choose a filename and directory"), defaultDir = dirControl.GetValue().strip(), defaultFile = name, wildcard= "*.%s" % fileExtension, style=wx.SAVE|wx.CHANGE_DIR) else: dlg = wx.DirDialog(wx.GetApp().GetTopWindow(), _("Choose a directory:"), defaultPath=dirControl.GetValue().strip(), style=wx.DD_DEFAULT_STYLE|wx.DD_NEW_DIR_BUTTON) if dlg.ShowModal() != wx.ID_OK: dlg.Destroy() return path = dlg.GetPath() dlg.Destroy() if path: if not useDirDialog: dir, filename = os.path.split(path) if dirControl.FindString(dir) == wx.NOT_FOUND: dirControl.Insert(dir, 0) dirControl.SetValue(dir) dirControl.SetToolTipString(dir) nameControl.SetValue(filename) else: dirControl.SetValue(path) dirControl.SetToolTipString(path) parent.Bind(wx.EVT_BUTTON, OnFindDirClick, button) def Validate(allowOverwriteOnPrompt=False, infoString='', validClassName=False, ignoreFileConflicts=False): projName = nameControl.GetValue().strip() if projName == "": wx.MessageBox(_("Please provide a %sfile name.") % infoString, _("Provide a File Name")) return False if projName.find(' ') != -1: wx.MessageBox(_("Please provide a %sfile name that does not contains spaces.") % infoString, _("Spaces in File Name")) return False if validClassName: if projName[0].isdigit(): wx.MessageBox(_("File name cannot start with a number. Please enter a different name."), _("Invalid File Name")) return False if projName.endswith(".agp"): projName2 = projName[:-4] else: projName2 = projName if not projName2.replace("_", "a").isalnum(): # [a-zA-Z0-9_] note '_' is allowed and ending '.agp'. wx.MessageBox(_("Name must be alphanumeric ('_' allowed). Please enter a valid name."), _("Project Name")) return False dirName = dirControl.GetValue().strip() if dirName == "": wx.MessageBox(_("No directory. Please provide a directory."), _("Provide a Directory")) return False if os.sep == "\\" and dirName.find("/") != -1: wx.MessageBox(_("Wrong delimiter '/' found in directory path. Use '%s' as delimiter.") % os.sep, _("Provide a Valid Directory")) return False if not os.path.exists(dirName): wx.MessageBox(_("That %sdirectory does not exist. Please choose an existing directory.") % infoString, _("Provide a Valid Directory")) return False if not ignoreFileConflicts: filePath = os.path.join(dirName, MakeNameEndInExtension(projName, "." + fileExtension)) if os.path.exists(filePath): if allowOverwriteOnPrompt: res = wx.MessageBox(_("That %sfile already exists. Would you like to overwrite it.") % infoString, "File Exists", style=wx.YES_NO|wx.NO_DEFAULT) return (res == wx.YES) else: wx.MessageBox(_("That %sfile already exists. Please choose a different name.") % infoString, "File Exists") return False return True HALF_SPACE = 5 flexGridSizer = wx.FlexGridSizer(cols = 3, vgap = HALF_SPACE, hgap = HALF_SPACE) flexGridSizer.AddGrowableCol(1,1) if not useDirDialog: flexGridSizer.Add(nameLabelText, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_LEFT) flexGridSizer.Add(nameControl, 2, flag=wx.ALIGN_CENTER_VERTICAL|wx.EXPAND) flexGridSizer.Add(button, flag=wx.ALIGN_RIGHT|wx.LEFT, border=HALF_SPACE) flexGridSizer.Add(dirLabelText, flag=wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_LEFT) flexGridSizer.Add(dirControl, 2, flag=wx.ALIGN_CENTER_VERTICAL|wx.EXPAND) flexGridSizer.Add(wx.StaticText(parent, -1, ""), 0) else: flexGridSizer.Add(nameLabelText, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_LEFT) flexGridSizer.Add(nameControl, 2, flag=wx.ALIGN_CENTER_VERTICAL|wx.EXPAND) flexGridSizer.Add(wx.StaticText(parent, -1, ""), 0) flexGridSizer.Add(dirLabelText, flag=wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_LEFT) flexGridSizer.Add(dirControl, 2, flag=wx.ALIGN_CENTER_VERTICAL|wx.EXPAND) flexGridSizer.Add(button, flag=wx.ALIGN_RIGHT|wx.LEFT, border=HALF_SPACE) if returnAll: return nameControl, dirControl, flexGridSizer, Validate, allControls else: return nameControl, dirControl, flexGridSizer, Validate
def CreateDirectoryOnlyControl( parent, dirLabel=_("Location:"), startingDirectory=None, choiceDirs=None, appDirDefaultStartDir=False): if not choiceDirs: choiceDirs = [] projectDirs = [] if appDirDefaultStartDir: appDirectory = wx.ConfigBase_Get().Read(tools.bgui.tool.projecteditor.PROJECT_DIRECTORY_KEY, bdec.gui.tool.projecteditorProjectEditor.NEW_PROJECT_DIRECTORY_DEFAULT) else: appDirectory = wx.ConfigBase_Get().Read(tools.bgui.tool.projecteditor.PROJECT_DIRECTORY_KEY) if appDirectory: choiceDirs.append(appDirectory) if appDirDefaultStartDir and not startingDirectory: startingDirectory = appDirectory projectService = wx.GetApp().GetService(tools.bgui.tool.projecteditor.ProjectService) if projectService: curProjectDoc = projectService.GetCurrentProject() if curProjectDoc: homeDir = curProjectDoc.GetAppDocMgr().homeDir if homeDir and (homeDir not in choiceDirs): choiceDirs.append(homeDir) if not startingDirectory: startingDirectory = homeDir for projectDoc in projectService.GetOpenProjects(): if projectDoc == curProjectDoc: continue homeDir = projectDoc.GetAppDocMgr().homeDir if homeDir and (homeDir not in projectDirs): projectDirs.append(homeDir) projectDirs.sort(CaseInsensitiveCompare) for projectDir in projectDirs: if projectDir not in choiceDirs: choiceDirs.append(projectDir) if startingDirectory and (startingDirectory not in choiceDirs): choiceDirs.insert(0, startingDirectory) if os.getcwd() not in choiceDirs: choiceDirs.append(os.getcwd()) if appdirs.getSystemDir() not in choiceDirs: choiceDirs.append(appdirs.getSystemDir()) if not startingDirectory: startingDirectory = os.getcwd() dirLabelText = wx.StaticText(parent, -1, dirLabel) dirControl = wx.ComboBox(parent, -1, startingDirectory, size=(-1,-1), choices=choiceDirs) dirControl.SetToolTipString(startingDirectory) button = wx.Button(parent, -1, _("Browse...")) def OnFindDirClick(event): dlg = wx.DirDialog(wx.GetApp().GetTopWindow(), _("Choose a directory:"), defaultPath=dirControl.GetValue().strip(), style=wx.DD_DEFAULT_STYLE|wx.DD_NEW_DIR_BUTTON) dlg.CenterOnParent() if dlg.ShowModal() == wx.ID_OK: dir = dlg.GetPath() if dirControl.FindString(dir) == wx.NOT_FOUND: dirControl.Insert(dir, 0) dirControl.SetValue(dir) dirControl.SetToolTipString(dir) dlg.Destroy() parent.Bind(wx.EVT_BUTTON, OnFindDirClick, button) def Validate(allowOverwriteOnPrompt=False): dirName = dirControl.GetValue().strip() if dirName == "": wx.MessageBox(_("Please provide a directory."), _("Provide a Directory")) return False if os.sep == "\\" and dirName.find("/") != -1: wx.MessageBox(_("Wrong delimiter '/' found in directory path. Use '%s' as delimiter.") % os.sep, _("Provide a Valid Directory")) return False if not os.path.exists(dirName): wx.MessageBox(_("That directory does not exist. Please choose an existing directory."), _("Provide a Valid Directory")) return False return True HALF_SPACE = 5 flexGridSizer = wx.FlexGridSizer(cols = 3, vgap = HALF_SPACE, hgap = HALF_SPACE) flexGridSizer.AddGrowableCol(1,1) flexGridSizer.Add(dirLabelText, flag=wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_LEFT|wx.RIGHT, border=HALF_SPACE) flexGridSizer.Add(dirControl, 2, flag=wx.ALIGN_CENTER_VERTICAL|wx.EXPAND, border=HALF_SPACE) flexGridSizer.Add(button, flag=wx.ALIGN_RIGHT|wx.LEFT, border=HALF_SPACE) return dirControl, flexGridSizer, Validate