def _reloadPyScripts( self ): self._scriptCmdTracker.clearCommands() commandFiles = [self._scriptFilename] + self._getCommandFiles() print(commandFiles) for f in commandFiles: try: text = open( f, "r" ).read() except: continue allGlobals = self._getGlobalsFromSourceCode( text, f ) if allGlobals is not None: category = os.path.splitext(os.path.basename(f))[0].replace("_", " ") if "CATEGORY" in allGlobals: category = allGlobals["CATEGORY"] for fn in allGlobals: if callable(allGlobals[fn]) \ and fn.startswith(cmdretriever.SCRIPT_PREFIX): allGlobals[fn].category = category allGlobals[fn].cmdFile = f infos = cmdretriever.getCommandsFromObjects( allGlobals ) self._scriptCmdTracker.registerNewCommands( infos ) self._registerDependencies( allGlobals )
def _reloadPyScripts(self, files=None): self._scriptCmdTracker.clearCommands(files) commandFiles = [self._scriptFilename] if files: commandFiles = commandFiles + files print "Files to reload: ", commandFiles else: commandFiles = commandFiles + self._getCommandFiles() logging.debug(commandFiles) for f in commandFiles: try: text = open(f, "r").read() except: continue allGlobals = self._getGlobalsFromSourceCode(text, f) if allGlobals is not None: infos = cmdretriever.getCommandsFromObjects(allGlobals) #print f, infos self._scriptCmdTracker.registerNewCommands(infos) self._registerDependencies(allGlobals) self._commandsInFile[f] = infos
def _reloadPyScripts( self ): text = open( self._scriptFilename, "r" ).read() allGlobals = self._getGlobalsFromSourceCode( text, self._scriptFilename ) if allGlobals is not None: infos = cmdretriever.getCommandsFromObjects( allGlobals ) self._scriptCmdTracker.registerNewCommands( infos ) self._registerDependencies( allGlobals )
def _reloadPyScripts(self, files=None): if files: for file_name in files: cmd_infos = self._commandsInFile.get(file_name, None) if cmd_infos: self._scriptCmdTracker.clearCommands(cmd_infos) else: self._scriptCmdTracker.clearCommands() commandFiles = [self._scriptFilename] if files: commandFiles = commandFiles + files else: commandFiles = commandFiles + self._getCommandFiles() assert logging.debug(commandFiles) or True for file_name in commandFiles: try: with open(file_name, "r") as fd: file_contents = fd.read().replace('\r\n', '\n') + "\n" except IOError as e: if file_name == SCRIPTS_FILE_NAME: do_once( logging.warning, "Legacy script file %s not found" % SCRIPTS_FILE_NAME) else: logging.error(e) continue except Exception as e: logging.error(e) continue # Do not bother to parse files which does not contain command definitions if not COMMAND_FILE_CHECK.search(file_contents): logging.warning( "Skipping file %s as it does not contain any command definitions", file_name) continue allGlobals = self._getGlobalsFromSourceCode( file_contents, file_name) if allGlobals is not None: infos = cmdretriever.getCommandsFromObjects(allGlobals) self._scriptCmdTracker.registerNewCommands(infos) self._registerDependencies(allGlobals) self._commandsInFile[file_name] = infos logging.info( "Scriptotron registered commands from '%s': [%s]" % (basename(file_name), ", ".join(info["cmdName"] for info in infos)))
def install_command_from_url(command_url): try: fp = urllib.urlopen(command_url) except: msg = "Couldn't install that command" displayMessage(msg) return text = fp.read() fp.close() lines = text.split("\n") if len(lines) < 3: msg = "There was no command to install!" displayMessage(msg) return while lines[0].strip() == "": lines.pop(0) command_file_name = command_url.split("/")[-1] if not command_file_name.endswith(".py"): msg = "Couldn't install this command %s" % command_file_name displayMessage(msg) return from enso.providers import getInterface cmd_folder = getInterface("scripts_folder")() command_file_path = os.path.join(cmd_folder, command_file_name) shortname = os.path.splitext(command_file_name)[0] if os.path.exists(command_file_path): msg = "You already have a command named %s" % shortname displayMessage(msg) return allGlobals = {} # normalise text for crlf text = text.replace('\r\n', '\n').replace('\r', '\n') code = compile(text, command_file_path, "exec") exec code in allGlobals installed_commands = [ x["cmdName"] for x in cmdretriever.getCommandsFromObjects(allGlobals) ] if len(installed_commands) == 1: install_message = "%s is now a command" % installed_commands[0] else: install_message = "%s are now commands" % ", ".join(installed_commands) # Use binary mode for writing so endlines are not converted to "\r\n" on win32 fp = open(command_file_path, "wb") fp.write(text) fp.close() displayMessage(install_message)
def install_command_from_url(command_url): try: fp = urllib.urlopen(command_url) except: msg = "Couldn't install that command" displayMessage(msg) return text = fp.read() fp.close() lines = text.split("\n") if len(lines) < 3: msg = "There was no command to install!" displayMessage(msg) return while lines[0].strip() == "": lines.pop(0) command_file_name = command_url.split("/")[-1] if not command_file_name.endswith(".py"): msg = "Couldn't install this command %s" % command_file_name displayMessage(msg) return from enso.providers import getInterface cmd_folder = getInterface("scripts_folder")() command_file_path = os.path.join(cmd_folder, command_file_name) shortname = os.path.splitext(command_file_name)[0] if os.path.exists(command_file_path): msg = "You already have a command named %s" % shortname displayMessage(msg) return allGlobals = {} # normalise text for crlf text = text.replace('\r\n','\n').replace('\r','\n') code = compile( text, command_file_path, "exec" ) exec code in allGlobals installed_commands = [x["cmdName"] for x in cmdretriever.getCommandsFromObjects(allGlobals)] if len(installed_commands) == 1: install_message = "%s is now a command" % installed_commands[0] else: install_message = "%s are now commands" % ", ".join(installed_commands) # Use binary mode for writing so endlines are not converted to "\r\n" on win32 fp = open(command_file_path, "wb") fp.write(text) fp.close() displayMessage(install_message)
def _reloadPyScripts(self): self._scriptCmdTracker.clearCommands() commandFiles = [self._scriptFilename] + self._getCommandFiles() print commandFiles for f in commandFiles: try: text = open(f, "r").read() except: continue allGlobals = self._getGlobalsFromSourceCode(text, f) if allGlobals is not None: infos = cmdretriever.getCommandsFromObjects(allGlobals) self._scriptCmdTracker.registerNewCommands(infos) self._registerDependencies(allGlobals)
def _reloadPyScripts(self): self._scriptCmdTracker.clearCommands() commandFiles = [self._scriptFilename] + self._getCommandFiles() print commandFiles for f in commandFiles: try: text = open(f, "r").read() except: continue allGlobals = self._getGlobalsFromSourceCode(text, f) if allGlobals is not None: infos = cmdretriever.getCommandsFromObjects(allGlobals) self._scriptCmdTracker.registerNewCommands(infos) self._registerDependencies(allGlobals)
def command_manager(): command_manager = CommandManager().get() # Register named command 'enso help' named_help_cmd = HelpCommand command_manager.registerCommand(named_help_cmd.NAME, named_help_cmd(None)) # Register arbitrary postfix command 'enso' with valid argument 'help' execGlobals = {} exec dedent(""" def cmd_enso(ensoapi, what): pass cmd_enso.valid_args = ['about', 'help'] """) in execGlobals commands = getCommandsFromObjects(execGlobals) assert len(commands) == 1 script_help_cmd = makeCommandFromInfo(commands[0], None, None) assert script_help_cmd is not None command_manager.registerCommand(script_help_cmd.NAME, script_help_cmd) yield command_manager
def get_commands_from_object(text, filename): allGlobals = {} code = compile( text, filename, "exec" ) exec code in allGlobals return cmdretriever.getCommandsFromObjects(allGlobals)
def get_commands_from_object(text, filename): allGlobals = {} code = compile(text, filename, "exec") exec code in allGlobals return cmdretriever.getCommandsFromObjects(allGlobals)