def test_updatePluginPlaces(self):
		class SpecificLocator(IPluginLocator):
			pass
		pm = PluginManager()
		pm.setPluginPlaces(["bla/bli"])
		pm.updatePluginPlaces(["mif/maf"])
		self.assertEqual(set(["bla/bli","mif/maf"]),set(pm.getPluginLocator().plugins_places))
Exemple #2
0
    def _tryLoad(self, schema_name, num_colors):
        plugin_manager = PluginManager()
        plugin_manager.getPluginLocator().setPluginInfoExtension("ini")
        plugin_manager.setPluginPlaces([paths.pluginsDir("user", "syntaxcolors"), paths.pluginsDir("system", "syntaxcolors")])
        plugin_manager.collectPlugins()

        for plugin_info in plugin_manager.getAllPlugins():

            # Useless, but let's activate them
            plugin_manager.activatePluginByName(plugin_info.name)

            plugin_object = plugin_info.plugin_object
            if plugin_object.name() == schema_name and plugin_object.supportsNumColors(num_colors):

                self._color_map.update(_parseColorSchema(plugin_object.colorSchema(num_colors)))
                return True

        return False
    def test_updatePluginPlaces(self):
        class SpecificLocator(IPluginLocator):
            pass

        pm = PluginManager()
        pm.setPluginPlaces(["bla/bli"])
        pm.updatePluginPlaces(["mif/maf"])
        self.assertEqual(set(["bla/bli", "mif/maf"]),
                         set(pm.getPluginLocator().plugins_places))
Exemple #4
0
def availableExtensions():

    # setup the categories

    categories = {'Extension': pluginTypes.IExtensionPlugin}

    # Build the manager, set load location, and then collect them

    extManager = PluginManager(categories_filter=categories)
    #pluginManager.setPluginPlaces()

    loc = extManager.getPluginLocator()
    loc.setPluginPlaces([path + '\\extensions'])

    extManager.locatePlugins()
    extManager.loadPlugins()

    extensions = startupExtensions(extManager)

    return extensions
Exemple #5
0
def availableInterfaces():
    # setup the categories

    categories = {
        'Interface': pluginTypes.IInterfacePlugin,
        'Techniques': pluginTypes.ITechniquePlugin
    }

    # Build the manager, set load location, and then collect them

    interfaceManager = PluginManager(categories_filter=categories)
    # pluginManager.setPluginPlaces()

    loc = interfaceManager.getPluginLocator()
    loc.setPluginPlaces([path + '\\interfaces'])

    interfaceManager.locatePlugins()
    interfaceManager.loadPlugins()

    interfaces = startupInterfaces(interfaceManager)

    return interfaces
	def test_init_with_plugin_locator(self):
		class SpecificLocator(IPluginLocator):
			pass
		pm = PluginManager(plugin_locator=SpecificLocator())
		self.assertEqual(["Default"],pm.getCategories())
		self.assertTrue(isinstance(pm.getPluginLocator(),SpecificLocator))
	def test_init_with_plugin_info_ext(self):
		pm = PluginManager(plugin_info_ext="bla")
		self.assertEqual(["Default"],pm.getCategories())
		self.assertTrue(isinstance(pm.getPluginLocator(),PluginFileLocator))
Exemple #8
0
class Phong(object):
    def __init__(self):
        self._log = logging.getLogger(
            "%s.%s" % (self.__module__, self.__class__.__name__))

        analyzer = PluginFileAnalyzerWithInfoFile('phong', 'phong-plugin')
        self._plugins = PluginManager()
        self._plugins.getPluginLocator().setAnalyzers([analyzer])
        self._plugins.setPluginPlaces([
            './plugins/',
            '/usr/lib/phong/plugins',
        ])
        self._config = ConfigParser.ConfigParser()
        self.loadConfig(
            os.path.sep.join((os.path.dirname(__file__), 'defaults.cfg')))
        self._wiki = None
        self._commands = []
        self._spiff = None

    @property
    def pluginManager(self):
        return self._plugins()

    @property
    def config(self):
        return self._config

    @property
    def spiff(self):
        if self._spiff is None:
            self._spiff = spiff.API(self.config.get('phong', 'spiff-api'))
        return self._spiff

    @property
    def wiki(self):
        if self._wiki is None:
            url = self._config.get('phong', 'mediawiki-url')
            api = self._config.get('phong', 'mediawiki-api-url')
            username = self._config.get('phong', 'mediawiki-username')
            password = self._config.get('phong', 'mediawiki-password')
            self._wiki = phong.wiki.Wiki(url, api, username, password)
        return self._wiki

    def buildContext(self):
        context = {}
        for plugin in self._plugins.getAllPlugins():
            cxt = plugin.plugin_object.buildContext(self)
            if isinstance(cxt, dict):
                context.update(cxt)
        return context

    def getTemplate(self,
                    name,
                    defaultContext={},
                    buildContext=True,
                    prefix=None):
        if buildContext:
            cxt = self.buildContext()
            cxt.update(defaultContext)
        else:
            cxt = defaultContext
        engines = [
            phong.templates.FileEngine(self),
            phong.templates.WikiEngine(self),
        ]
        for e in engines:
            if e.hasTemplate(name, prefix):
                return e.getTemplate(name, prefix, cxt)

    def renderTemplate(self, name, context={}):
        cxt = self.buildContext()
        cxt.update(context)
        return self.getTemplate(name).render(context)

    def loadPlugins(self):
        self._plugins.collectPlugins()

        for plugin in self._plugins.getAllPlugins():
            self._plugins.activatePluginByName(plugin.name)
            for cmd in plugin.plugin_object.availableCommands():
                self._commands.append(cmd(self, plugin))

    def loadConfig(self, path):
        self._log.debug("Loading configuration from %s", path)
        self._config.read(os.path.expanduser(path))

    def getState(self, name):
        dir = os.path.expanduser(self._config.get('phong', 'state-dir'))
        return phong.state.State(
            os.path.sep.join((dir, "%s.state.json" % (name))))

    def main(self, argv):
        parser = argparse.ArgumentParser(prog='phong', add_help=False)
        parser.add_argument('-c',
                            '--config',
                            help='Configuration file to use',
                            default="/etc/phong.cfg")
        parser.add_argument('-d',
                            '--dry-run',
                            help='Dont actually do anything',
                            default=False,
                            action='store_true')
        parser.add_argument('-D',
                            '--debug',
                            help='Print debug messages',
                            default=False,
                            action='store_true')
        args = parser.parse_known_args(argv)
        parser.add_argument('-h',
                            '--help',
                            help='show this help message and exit',
                            action='help')

        if args[0].debug:
            logging.basicConfig(level=logging.DEBUG)

        self.loadConfig(args[0].config)
        self.loadPlugins()
        subparser = parser.add_subparsers(help='action')
        for command in self._commands:
            pluginArgs = subparser.add_parser(command.name(),
                                              help=command.helpText())
            pluginArgs.set_defaults(command_obj=command)
            command.buildArgs(pluginArgs)
        args = parser.parse_args(argv)

        return args.command_obj.execute(args)
Exemple #9
0
 def test_init_with_category_filter(self):
     pm = PluginManager(categories_filter={"Mouf": IPlugin})
     self.assertEqual(["Mouf"], pm.getCategories())
     self.assertTrue(isinstance(pm.getPluginLocator(), PluginFileLocator))
Exemple #10
0
class EditorController:
    def __init__(self, editor, global_state, buffer_list):
        self._editor = editor
        self._global_state = global_state
        self._buffer_list = buffer_list
        self._lexer = Lexer()
        self._plugin_manager = PluginManager()
        self._plugin_manager.getPluginLocator().setPluginInfoExtension("ini")
        self._plugin_manager.setPluginPlaces([paths.pluginsDir("user", "commands"), paths.pluginsDir("system", "commands")])
        self._plugin_manager.collectPlugins()

        for plugin_info in self._plugin_manager.getAllPlugins():
            self._plugin_manager.activatePluginByName(plugin_info.name)

        # To speed up resolution, we cache the keyword->plugin association. It is filled lazy
        self._keyword_to_plugin_cache = {}

        self._buffer_list.currentBufferChanged.connect(self.registerCurrentBuffer)

    def registerCurrentBuffer(self, *args):
        self._editor.edit_area.buffer = self._buffer_list.current
        self._editor.status_bar_controller.buffer = self._buffer_list.current
        self._editor.side_ruler_controller.buffer = self._buffer_list.current
        self._editor.info_hover_box.buffer = self._buffer_list.current
        self._lexer.setModel(self._buffer_list.current.document)

    def forceQuit(self):
        for b in self._buffer_list.buffers:
            if b.document.documentMetaInfo("Filename").data() is None:
                continue

            models.EditorState.instance().setCursorPosForPath(
                    os.path.abspath(b.document.documentMetaInfo("Filename").data()),
                    b.cursor.pos)

        models.EditorState.instance().save()
        models.Configuration.save()
        gui.VApplication.vApp.exit()

    def doSave(self):
        self._doSave()
        self._doLint()

    def doSaveAs(self, filename):
        self._doSave(filename)
        self._doLint()

    def doInsertFile(self, filename):
        buffer = self._buffer_list.current

        command = commands.InsertFileCommand(buffer, filename)

        result = command.execute()
        if result.success:
            buffer.command_history.add(command)

    def tryQuit(self):
        if any([b.isModified() for b in self._buffer_list.buffers]):
            self._editor.status_bar.setMessage("Document has been modified. " +
                                               "Use :q! to quit without saving " +
                                               "or :qw to save and quit.", 3000)
        else:
            self.forceQuit()

    def searchForward(self, search_text):
        if search_text == '':
            if self._global_state.current_search is not None:
                search_text = self._global_state.current_search[0]

        if search_text != '':
            self._global_state.current_search = (search_text, Search.SearchDirection.FORWARD)
            Search.find(self._buffer_list.current, search_text, Search.SearchDirection.FORWARD)

    def searchBackward(self, search_text):
        if search_text == '':
            if self._global_state.current_search is not None:
                search_text = self._global_state.current_search[0]

        if search_text != '':
            self._global_state.current_search = (search_text, Search.SearchDirection.BACKWARD)
            Search.find(self._buffer_list.current, search_text, Search.SearchDirection.BACKWARD)

    def selectPrevBuffer(self):
        self._buffer_list.selectPrev()

    def selectNextBuffer(self):
        self._buffer_list.selectNext()

    def doSaveAndExit(self):
        self._doSave()
        self.forceQuit()

    def openFile(self, filename):
        buffer = self._buffer_list.bufferForFilename(filename)
        if buffer is not None:
            self._buffer_list.select(buffer)
            return

        current_buffer = self._buffer_list.current
        new_buffer = models.Buffer()
        status_bar = self._editor.status_bar

        try:
            with open(filename,'r') as f:
                new_buffer.document.read(f)
        except FileNotFoundError:
            status_bar.setMessage("%s [New file]" % filename, 3000)
        except Exception as e:
            status_bar.setMessage("%s [Error: %s]" % (filename, str(e)), 3000)

        new_buffer.document.documentMetaInfo("Filename").setData(filename)
        new_buffer.document.documentMetaInfo("Modified").setData(False)

        initial_md5 = None
        if not new_buffer.document.isEmpty():
            initial_md5 = hashlib.md5(new_buffer.document.documentText().encode("utf-8"))
        new_buffer.document.documentMetaInfo("InitialMD5").setData(initial_md5)

        if current_buffer.isEmpty() and not current_buffer.document.documentMetaInfo("Modified").data():
            self._buffer_list.replaceAndSelect(current_buffer, new_buffer)
        else:
            self._buffer_list.addAndSelect(new_buffer)

        recovered_cursor_pos = models.EditorState.instance().cursorPosForPath(os.path.abspath(filename))
        if recovered_cursor_pos is not None:
            new_buffer.cursor.toPos(recovered_cursor_pos)

        self._doLint()

    def createEmptyBuffer(self):
        self._buffer_list.addAndSelect(models.Buffer())

    def setMode(self, mode):
        self._global_state.edit_mode = mode

    def interpretCommandLine(self, command_line):
        """
        Interprets and dispatch the command line to the appropriate command execution routine,
        or the plugin system.
        command_line contains the full command line as specified by the user, as a string.
        """

        if len(command_line.strip()) == 0:
            return

        command_tokens = shlex.split(command_line)
        keyword = command_tokens[0]
        status_bar = self._editor.status_bar

        if keyword == 'q!':
            self.forceQuit()
        elif keyword == 'q':
            self.tryQuit()
        elif keyword == "w":
            if len(command_tokens) == 1:
                self.doSave()
            elif len(command_tokens) == 2:
                self.doSaveAs(command_tokens[1])
            else:
                status_bar.setMessage("Only one filename allowed at write",3000)
                return
        elif keyword == "r":
            if len(command_tokens) == 1:
                status_bar.setMessage("Specify filename",3000)
                return
            elif len(command_tokens) == 2:
                self.doInsertFile(command_tokens[1])
            else:
                status_bar.setMessage("Only one filename allowed", 3000)
                return
        elif keyword in ("wq", "x"):
            self.doSaveAndExit()
        elif keyword == "e":
            if len(command_tokens) == 1:
                status_bar.setMessage("Specify filename", 3000)
            elif len(command_tokens) == 2:
                self.openFile(command_tokens[1])
            else:
                status_bar.setMessage("Only one filename allowed", 3000)
                return
        elif keyword == "bp":
            self.selectPrevBuffer()
        elif keyword == "bn":
            self.selectNextBuffer()
        else:

            if keyword not in self._keyword_to_plugin_cache:
                for plugin_info in self._plugin_manager.getAllPlugins():
                    plugin_object = plugin_info.plugin_object
                    if plugin_object.keyword() == keyword:
                        self._keyword_to_plugin_cache[keyword] = plugin_object

            if keyword in self._keyword_to_plugin_cache:
                plugin_object = self._keyword_to_plugin_cache[keyword]
                plugin_object.execute(command_line)
            else:
                status_bar.setMessage("Unrecognized command", 3000)

    # Private

    def _doLint(self):
        document = self._buffer_list.current.document

        linter1 = linting.PyFlakesLinter(document)
        all_info = linter1.runOnce()

        meta_info = {}

        for info in all_info:
            meta_info[info.line] = info

        line_info = document.lineMetaInfo("LinterResult")
        line_info.clear()
        line_info.setDataForLines(meta_info)

    def _doSave(self, filename=None):
        status_bar = self._editor.status_bar
        document = self._buffer_list.current.document

        if filename is not None and len(filename) == 0:
            status_bar.setMessage("Error! Unspecified file name.", 3000)
            return

        status_bar.setMessage("Saving...")
        gui.VApplication.vApp.processEvents()

        if filename is None:
            filename = document.documentMetaInfo("Filename").data()

        if filename is None:
            status_bar.setMessage("Error! Cannot save unnamed file. Please specify a filename with :w filename", 3000)
            return

        try:
            with open(filename,'w') as f:
                document.write(f)
        except Exception as e:
            status_bar.setMessage("Error! Cannot save file. %s" % str(e), 3000)
            return
        else:
            status_bar.setMessage("Saved %s" % filename, 3000)

        document.documentMetaInfo("Filename").setData(filename)
        document.documentMetaInfo("Modified").setData(False)
        document.lineMetaInfo("Change").clear()
Exemple #11
0
class ModuleManager():
    def __init__(self, threads, kill_list, kill_list_lock, job_list, binpath):
        self.threads = threads
        self.kill_list = kill_list
        self.kill_list_lock = kill_list_lock
        self.job_list = job_list # Running jobs
        self.binpath = binpath

        self.root_path = os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..', '..'))
        self.module_bin_path = os.path.join(self.root_path, "module_bin")
        self.plugin_path = os.path.join(self.root_path, "lib", "assembly", "plugins")

        self.pmanager = PluginManager()
        locator = self.pmanager.getPluginLocator()
        locator.setPluginInfoExtension('asm-plugin')
        self.pmanager.setPluginPlaces([ self.plugin_path ])
        self.pmanager.collectPlugins()
        self.pmanager.locatePlugins()
        self.plugins = ['none']
        num_plugins = len(self.pmanager.getAllPlugins())
        if  num_plugins == 0:
            raise Exception("No Plugins Found!")

        plugins = []
        self.executables = {}
        for plugin in self.pmanager.getAllPlugins():
            plugin.threads = threads
            self.plugins.append(plugin.name)
            plugin.plugin_object.setname(plugin.name)
            ## Check for installed binaries
            try:
                version = plugin.details.get('Documentation', 'Version')
                executables = plugin.details.items('Executables')
                full_execs = [(k, self.get_executable_path(v)) for k,v in executables]
                for binary in full_execs:
                    if not os.path.exists(binary[1]):
                        if float(version) < 1:
                            print '[Warning]: {} (v{}) -- Binary does not exist for beta plugin -- {}'.format(plugin.name, version, binary[1])
                        else:
                            raise Exception('[ERROR]: {} (v{})-- Binary does not exist -- {}'.format(plugin.name, version, binary[1]))
                self.executables[plugin.name] = full_execs
            except ConfigParser.NoSectionError: pass
            plugins.append(plugin.name)
        print "Plugins found [{}]: {}".format(num_plugins, sorted(plugins))


    def run_proc(self, module, wlink, job_data, parameters):
        """ Run module adapter for wasp interpreter
        To support the Job_data mechanism, injects wlink
        """
        if not self.has_plugin(module):
            raise Exception("No plugin named {}".format(module))
        plugin = self.pmanager.getPluginByName(module)

        config_settings = plugin.details.items('Settings')
        config_settings = update_settings(config_settings, parameters)

        try:
            settings = {k:v for k,v in self.executables[module]}
            for k,v in config_settings: ## Don't override
                if not k in settings:
                    settings[k] = v
            settings = settings.items()
        except:
            # settings = config_settings
            raise Exception("Plugin Config not updated: {}!".format(module))

        #### Check input/output type compatibility
        if wlink['link']:
            for link in wlink['link']:
                if not link:
                    continue
                if link['module']:
                    try:
                        assert (self.output_type(link['module']) == self.input_type(module) or
                                self.output_type(link['module']) in self.input_type(module))
                    except AssertionError:
                        raise Exception('{} and {} have mismatched input/output types'.format(module, link['module']))
        #### Run
        job_data['wasp_chain'] = wlink
        output = plugin.plugin_object.base_call(settings, job_data, self)
        ot = self.output_type(module)
        wlink.insert_output(output, ot,
                            plugin.name)
        if not wlink.output:
            raise Exception('"{}" module failed to produce {}'.format(module, ot))


    def output_type(self, module):
        return self.pmanager.getPluginByName(module).plugin_object.OUTPUT

    def input_type(self, module):
        return self.pmanager.getPluginByName(module).plugin_object.INPUT

    def get_short_name(self, module):
        try:
            plugin = self.pmanager.getPluginByName(module)
            settings = plugin.details.items('Settings')
            for kv in settings:
                if kv[0] == 'short_name':
                    sn = kv[1]
                    break
            return sn
        except:
            return None

    def get_executable(self, module):
        try:
            plugin = self.pmanager.getPluginByName(module)
            settings = plugin.details.items('Settings')
            for kv in settings:
                if kv[0] == 'short_name':
                    sn = kv[1]
                    break
            return sn
        except:
            return None

    def verify_file(self, filename):
        if not os.path.exists(filename):
            raise Exception("File not found: %s" % filename)

    def get_executable_path(self, filename, verify=False):
        guess1 = os.path.join(self.module_bin_path, filename)
        guess2 = os.path.join(self.binpath, filename)
        fullname = guess1 if os.path.exists(guess1) else guess2
        if verify: verify_file(fullname)
        return fullname

    def has_plugin(self, plugin):
        if not plugin.lower() in self.plugins:
            logging.error("{} plugin not found".format(plugin))
            return False
        return True

    def valid_modules(self, l):
        """ Return filtered list of available modules """
        return [m for m in l if not m.startswith('?') and self.has_plugin(m)]

    def validate_pipe(self, pipe):
        for stage in pipe:
            for word in stage.replace('+', ' ').split(' '):
                if not (word.startswith('?') or self.has_plugin(word)):
                    raise Exception('Invalid pipeline command')


    def split_pipe(self, l):
        """ Splits a multi-module string in to bins
        Ex: 'kiki ?k=29 velvet' -> [[kiki, ?k=29], [velvet]]
        """
        bins = []
        for word in l:
            if not word.startswith('?'):
                bins.append([word])
            elif word.startswith('?'):
                bins[-1].append(word)
        return bins

    def parse_input(self, pipe):
        """
        Parses inital pipe and separates branching bins
        Ex: ['sga', '?p=True', 'kiki ?k=31 velvet', 'sspace']
        """
        stages = phelper.parse_branches(pipe)
        return stages
Exemple #12
0
class EditorController:
    def __init__(self, editor, global_state, buffer_list):
        self._editor = editor
        self._global_state = global_state
        self._buffer_list = buffer_list
        self._lexer = Lexer()
        self._plugin_manager = PluginManager()
        self._plugin_manager.getPluginLocator().setPluginInfoExtension("ini")
        self._plugin_manager.setPluginPlaces([
            paths.pluginsDir("user", "commands"),
            paths.pluginsDir("system", "commands")
        ])
        self._plugin_manager.collectPlugins()

        for plugin_info in self._plugin_manager.getAllPlugins():
            self._plugin_manager.activatePluginByName(plugin_info.name)

        # To speed up resolution, we cache the keyword->plugin association. It is filled lazy
        self._keyword_to_plugin_cache = {}

        self._buffer_list.currentBufferChanged.connect(
            self.registerCurrentBuffer)

    def registerCurrentBuffer(self, *args):
        self._editor.edit_area.buffer = self._buffer_list.current
        self._editor.status_bar_controller.buffer = self._buffer_list.current
        self._editor.side_ruler_controller.buffer = self._buffer_list.current
        self._editor.info_hover_box.buffer = self._buffer_list.current
        self._lexer.setModel(self._buffer_list.current.document)

    def forceQuit(self):
        for b in self._buffer_list.buffers:
            if b.document.documentMetaInfo("Filename").data() is None:
                continue

            models.EditorState.instance().setCursorPosForPath(
                os.path.abspath(
                    b.document.documentMetaInfo("Filename").data()),
                b.cursor.pos)

        models.EditorState.instance().save()
        models.Configuration.save()
        gui.VApplication.vApp.exit()

    def doSave(self):
        self._doSave()
        self._doLint()

    def doSaveAs(self, filename):
        self._doSave(filename)
        self._doLint()

    def doInsertFile(self, filename):
        buffer = self._buffer_list.current

        command = commands.InsertFileCommand(buffer, filename)

        result = command.execute()
        if result.success:
            buffer.command_history.add(command)

    def tryQuit(self):
        if any([b.isModified() for b in self._buffer_list.buffers]):
            self._editor.status_bar.setMessage(
                "Document has been modified. " +
                "Use :q! to quit without saving " + "or :qw to save and quit.",
                3000)
        else:
            self.forceQuit()

    def searchForward(self, search_text):
        if search_text == '':
            if self._global_state.current_search is not None:
                search_text = self._global_state.current_search[0]

        if search_text != '':
            self._global_state.current_search = (
                search_text, Search.SearchDirection.FORWARD)
            Search.find(self._buffer_list.current, search_text,
                        Search.SearchDirection.FORWARD)

    def searchBackward(self, search_text):
        if search_text == '':
            if self._global_state.current_search is not None:
                search_text = self._global_state.current_search[0]

        if search_text != '':
            self._global_state.current_search = (
                search_text, Search.SearchDirection.BACKWARD)
            Search.find(self._buffer_list.current, search_text,
                        Search.SearchDirection.BACKWARD)

    def selectPrevBuffer(self):
        self._buffer_list.selectPrev()

    def selectNextBuffer(self):
        self._buffer_list.selectNext()

    def doSaveAndExit(self):
        self._doSave()
        self.forceQuit()

    def openFile(self, filename):
        buffer = self._buffer_list.bufferForFilename(filename)
        if buffer is not None:
            self._buffer_list.select(buffer)
            return

        current_buffer = self._buffer_list.current
        new_buffer = models.Buffer()
        status_bar = self._editor.status_bar

        try:
            with open(filename, 'r') as f:
                new_buffer.document.read(f)
        except FileNotFoundError:
            status_bar.setMessage("%s [New file]" % filename, 3000)
        except Exception as e:
            status_bar.setMessage("%s [Error: %s]" % (filename, str(e)), 3000)

        new_buffer.document.documentMetaInfo("Filename").setData(filename)
        new_buffer.document.documentMetaInfo("Modified").setData(False)

        initial_md5 = None
        if not new_buffer.document.isEmpty():
            initial_md5 = hashlib.md5(
                new_buffer.document.documentText().encode("utf-8"))
        new_buffer.document.documentMetaInfo("InitialMD5").setData(initial_md5)

        if current_buffer.isEmpty(
        ) and not current_buffer.document.documentMetaInfo("Modified").data():
            self._buffer_list.replaceAndSelect(current_buffer, new_buffer)
        else:
            self._buffer_list.addAndSelect(new_buffer)

        recovered_cursor_pos = models.EditorState.instance().cursorPosForPath(
            os.path.abspath(filename))
        if recovered_cursor_pos is not None:
            new_buffer.cursor.toPos(recovered_cursor_pos)

        self._doLint()

    def createEmptyBuffer(self):
        self._buffer_list.addAndSelect(models.Buffer())

    def setMode(self, mode):
        self._global_state.edit_mode = mode

    def interpretCommandLine(self, command_line):
        """
        Interprets and dispatch the command line to the appropriate command execution routine,
        or the plugin system.
        command_line contains the full command line as specified by the user, as a string.
        """

        if len(command_line.strip()) == 0:
            return

        command_tokens = shlex.split(command_line)
        keyword = command_tokens[0]
        status_bar = self._editor.status_bar

        if keyword == 'q!':
            self.forceQuit()
        elif keyword == 'q':
            self.tryQuit()
        elif keyword == "w":
            if len(command_tokens) == 1:
                self.doSave()
            elif len(command_tokens) == 2:
                self.doSaveAs(command_tokens[1])
            else:
                status_bar.setMessage("Only one filename allowed at write",
                                      3000)
                return
        elif keyword == "r":
            if len(command_tokens) == 1:
                status_bar.setMessage("Specify filename", 3000)
                return
            elif len(command_tokens) == 2:
                self.doInsertFile(command_tokens[1])
            else:
                status_bar.setMessage("Only one filename allowed", 3000)
                return
        elif keyword in ("wq", "x"):
            self.doSaveAndExit()
        elif keyword == "e":
            if len(command_tokens) == 1:
                status_bar.setMessage("Specify filename", 3000)
            elif len(command_tokens) == 2:
                self.openFile(command_tokens[1])
            else:
                status_bar.setMessage("Only one filename allowed", 3000)
                return
        elif keyword == "bp":
            self.selectPrevBuffer()
        elif keyword == "bn":
            self.selectNextBuffer()
        else:

            if keyword not in self._keyword_to_plugin_cache:
                for plugin_info in self._plugin_manager.getAllPlugins():
                    plugin_object = plugin_info.plugin_object
                    if plugin_object.keyword() == keyword:
                        self._keyword_to_plugin_cache[keyword] = plugin_object

            if keyword in self._keyword_to_plugin_cache:
                plugin_object = self._keyword_to_plugin_cache[keyword]
                plugin_object.execute(command_line)
            else:
                status_bar.setMessage("Unrecognized command", 3000)

    # Private

    def _doLint(self):
        document = self._buffer_list.current.document

        linter1 = linting.PyFlakesLinter(document)
        all_info = linter1.runOnce()

        meta_info = {}

        for info in all_info:
            meta_info[info.line] = info

        line_info = document.lineMetaInfo("LinterResult")
        line_info.clear()
        line_info.setDataForLines(meta_info)

    def _doSave(self, filename=None):
        status_bar = self._editor.status_bar
        document = self._buffer_list.current.document

        if filename is not None and len(filename) == 0:
            status_bar.setMessage("Error! Unspecified file name.", 3000)
            return

        status_bar.setMessage("Saving...")
        gui.VApplication.vApp.processEvents()

        if filename is None:
            filename = document.documentMetaInfo("Filename").data()

        if filename is None:
            status_bar.setMessage(
                "Error! Cannot save unnamed file. Please specify a filename with :w filename",
                3000)
            return

        try:
            with open(filename, 'w') as f:
                document.write(f)
        except Exception as e:
            status_bar.setMessage("Error! Cannot save file. %s" % str(e), 3000)
            return
        else:
            status_bar.setMessage("Saved %s" % filename, 3000)

        document.documentMetaInfo("Filename").setData(filename)
        document.documentMetaInfo("Modified").setData(False)
        document.lineMetaInfo("Change").clear()
	def test_init_with_plugin_locator(self):
		class SpecificLocator(IPluginLocator):
			pass
		pm = PluginManager(plugin_locator=SpecificLocator())
		self.assertEqual(["Default"],pm.getCategories())
		self.assertTrue(isinstance(pm.getPluginLocator(),SpecificLocator))
Exemple #14
0
class Phong(object):
  def __init__(self):
    self._log = logging.getLogger("%s.%s"%(self.__module__,
      self.__class__.__name__))

    analyzer = PluginFileAnalyzerWithInfoFile('phong', 'phong-plugin')
    self._plugins = PluginManager()
    self._plugins.getPluginLocator().setAnalyzers([analyzer])
    self._plugins.setPluginPlaces([
      './plugins/',
      '/usr/lib/phong/plugins',
    ])
    self._config = ConfigParser.ConfigParser()
    self.loadConfig(os.path.sep.join((os.path.dirname(__file__),
      'defaults.cfg')))
    self._wiki = None
    self._commands = []
    self._spiff = None

  @property
  def pluginManager(self):
    return self._plugins()

  @property
  def config(self):
    return self._config

  @property
  def spiff(self):
    if spiff is not None and self._spiff is None:
      self._spiff = spiff.API(self.config.get('phong', 'spiff-api'))
    return self._spiff

  @property
  def wiki(self):
    if self._wiki is None:
      url = self._config.get('phong', 'mediawiki-url')
      api = self._config.get('phong', 'mediawiki-api-url')
      username = self._config.get('phong', 'mediawiki-username')
      password = self._config.get('phong', 'mediawiki-password')
      self._wiki = phong.wiki.Wiki(url, api, username, password)
    return self._wiki

  def buildContext(self):
    context = {
      'phong': templates.PhongTemplateAPI(self),
    }
    for plugin in self._plugins.getAllPlugins():
      cxt = plugin.plugin_object.buildContext(self)
      if isinstance(cxt, dict):
        context.update(cxt)
    return context

  def getTemplate(self, name, defaultContext={}, buildContext=True, prefix=None):
    if buildContext:
      cxt = self.buildContext()
      cxt.update(defaultContext)
    else:
      cxt = defaultContext

    engines = []

    if self.config.has_option('phong', 'template-paths'):
      for path in self.config.get('phong', 'template-paths').split(','):
        engines.append(phong.templates.FileEngine(self, path.strip()))

    engines.append(phong.templates.FileEngine(self, 'templates'))
    engines.append(phong.templates.WikiEngine(self))

    for e in engines:
      if e.hasTemplate(name, prefix):
        return e.getTemplate(name, prefix, cxt)

  def renderTemplate(self, name, context={}):
    cxt = self.buildContext()
    cxt.update(context)
    return self.getTemplate(name).render(context)

  def loadPlugins(self):
    self._plugins.collectPlugins()

    for plugin in self._plugins.getAllPlugins():
      self._plugins.activatePluginByName(plugin.name)
      for cmd in plugin.plugin_object.availableCommands():
        self._commands.append(cmd(self, plugin))

  def loadConfig(self, path):
    self._log.debug("Loading configuration from %s", path)
    self._config.read(os.path.expanduser(path))

  def getState(self, name):
    dir = os.path.expanduser(self._config.get('phong', 'state-dir'))
    return phong.state.State(os.path.sep.join((dir, "%s.state.json"%(name))))

  @property
  def argv(self):
    return self._argv

  @property
  def args(self):
    return self._args

  def main(self, argv):
    parser = argparse.ArgumentParser(prog='phong', add_help=False)
    parser.add_argument('-c', '--config', help='Configuration file to use. Can be specified multiple times. Later files override earlier ones.',
        action='append',
        default=["/etc/phong.cfg",])
    parser.add_argument('-d', '--dry-run', help='Dont actually do anything',
        default=False, action='store_true')
    parser.add_argument('-D', '--debug', help='Print debug messages',
        default=False, action='store_true')
    args = parser.parse_known_args(argv)
    parser.add_argument('-h', '--help', help='show this help message and exit', action='help')

    if args[0].debug:
      logging.basicConfig(level=logging.DEBUG)

    for config in args[0].config:
      self.loadConfig(config)
    self.loadPlugins()
    subparser = parser.add_subparsers(help='action')
    for command in self._commands:
      pluginArgs = subparser.add_parser(command.name(),
          help=command.helpText())
      pluginArgs.set_defaults(command_obj=command)
      command.buildArgs(pluginArgs)
    self._argv = argv
    self._args = parser.parse_args(argv)

    return self._args.command_obj.execute(self._args)
Exemple #15
0
class ModuleManager():
    def __init__(self, threads, kill_list, kill_list_lock, job_list, binpath,
                 modulebin):
        self.threads = threads
        self.kill_list = kill_list
        self.kill_list_lock = kill_list_lock
        self.job_list = job_list  # Running jobs
        self.binpath = binpath
        self.module_bin_path = modulebin

        self.root_path = os.path.abspath(
            os.path.join(os.path.dirname(__file__), '..', '..'))
        self.plugin_path = os.path.join(self.root_path, "lib", "assembly",
                                        "plugins")

        self.pmanager = PluginManager()
        locator = self.pmanager.getPluginLocator()
        locator.setPluginInfoExtension('asm-plugin')
        self.pmanager.setPluginPlaces([self.plugin_path])
        self.pmanager.collectPlugins()
        self.pmanager.locatePlugins()
        self.plugins = ['none']
        num_plugins = len(self.pmanager.getAllPlugins())
        if num_plugins == 0:
            raise Exception("No Plugins Found!")

        plugins = []
        self.executables = {}
        for plugin in self.pmanager.getAllPlugins():
            plugin.threads = threads
            self.plugins.append(plugin.name)
            plugin.plugin_object.setname(plugin.name)
            ## Check for installed binaries
            try:
                version = plugin.details.get('Documentation', 'Version')
                executables = plugin.details.items('Executables')
                full_execs = [(k, self.get_executable_path(v))
                              for k, v in executables]
                for binary in full_execs:
                    if not os.path.exists(binary[1]):
                        if float(version) < 1:
                            logger.warn(
                                'Third-party binary does not exist for beta plugin: {} (v{}) -- {}'
                                .format(plugin.name, version, binary[1]))
                        else:
                            raise Exception(
                                'ERROR: Third-party binary does not exist for beta plugin: {} (v{}) -- {}'
                                .format(plugin.name, version, binary[1]))
                self.executables[plugin.name] = full_execs
            except ConfigParser.NoSectionError:
                pass
            plugins.append(plugin.name)
        logger.info("Plugins found [{}]: {}".format(num_plugins,
                                                    sorted(plugins)))

    def run_proc(self, module, wlink, job_data, parameters):
        """ Run module adapter for wasp interpreter
        To support the Job_data mechanism, injects wlink
        """
        if not self.has_plugin(module):
            raise Exception("No plugin named {}".format(module))
        plugin = self.pmanager.getPluginByName(module)

        config_settings = plugin.details.items('Settings')
        config_settings = update_settings(config_settings, parameters)

        try:
            settings = {k: v for k, v in self.executables[module]}
            for k, v in config_settings:  ## Don't override
                if not k in settings:
                    settings[k] = v
            settings = settings.items()
        except:
            # settings = config_settings
            raise Exception("Plugin Config not updated: {}!".format(module))

        #### Check input/output type compatibility
        if wlink['link']:
            for link in wlink['link']:
                if not link:
                    continue
                if link['module']:
                    try:
                        assert (self.output_type(link['module'])
                                == self.input_type(module) or self.output_type(
                                    link['module']) in self.input_type(module))
                    except AssertionError:
                        raise Exception(
                            '{} and {} have mismatched input/output types'.
                            format(module, link['module']))
        #### Run
        job_data['wasp_chain'] = wlink
        output = plugin.plugin_object.base_call(settings, job_data, self)
        ot = self.output_type(module)
        wlink.insert_output(output, ot, plugin.name)
        if not wlink.output:
            raise Exception('"{}" module failed to produce {}'.format(
                module, ot))

        ### Store any output values in job_data
        data = {'module': module, 'module_output': output}
        job_data['plugin_output'].append(data)

    def output_type(self, module):
        return self.pmanager.getPluginByName(module).plugin_object.OUTPUT

    def input_type(self, module):
        return self.pmanager.getPluginByName(module).plugin_object.INPUT

    def get_short_name(self, module):
        try:
            plugin = self.pmanager.getPluginByName(module)
            settings = plugin.details.items('Settings')
            for kv in settings:
                if kv[0] == 'short_name':
                    sn = kv[1]
                    break
            return sn
        except:
            return None

    def get_executable(self, module):
        try:
            plugin = self.pmanager.getPluginByName(module)
            settings = plugin.details.items('Settings')
            for kv in settings:
                if kv[0] == 'short_name':
                    sn = kv[1]
                    break
            return sn
        except:
            return None

    def verify_file(self, filename):
        if not os.path.exists(filename):
            raise Exception("File not found: %s" % filename)

    def get_executable_path(self, filename, verify=False):
        guess1 = os.path.join(self.module_bin_path, filename)
        guess2 = os.path.join(self.binpath, filename)
        fullname = guess1 if os.path.exists(guess1) else guess2
        if verify: verify_file(fullname)
        return fullname

    def has_plugin(self, plugin):
        if not plugin.lower() in self.plugins:
            logger.error("{} plugin not found".format(plugin))
            return False
        return True

    def valid_modules(self, l):
        """ Return filtered list of available modules """
        return [m for m in l if not m.startswith('?') and self.has_plugin(m)]

    def validate_pipe(self, pipe):
        for stage in pipe:
            for word in stage.replace('+', ' ').split(' '):
                if not (word.startswith('?') or self.has_plugin(word)):
                    raise Exception('Invalid pipeline command')

    def split_pipe(self, l):
        """ Splits a multi-module string in to bins
        Ex: 'kiki ?k=29 velvet' -> [[kiki, ?k=29], [velvet]]
        """
        bins = []
        for word in l:
            if not word.startswith('?'):
                bins.append([word])
            elif word.startswith('?'):
                bins[-1].append(word)
        return bins

    def parse_input(self, pipe):
        """
        Parses inital pipe and separates branching bins
        Ex: ['sga', '?p=True', 'kiki ?k=31 velvet', 'sspace']
        """
        stages = phelper.parse_branches(pipe)
        return stages
Exemple #16
0
 def test_default_init(self):
     pm = PluginManager()
     self.assertEqual(["Default"], pm.getCategories())
     self.assertTrue(isinstance(pm.getPluginLocator(), PluginFileLocator))
	def test_default_init(self):
		pm = PluginManager()
		self.assertEqual(["Default"],pm.getCategories())
		self.assertTrue(isinstance(pm.getPluginLocator(),PluginFileLocator))
Exemple #18
0
 def test_init_with_plugin_info_ext(self):
     pm = PluginManager(plugin_info_ext="bla")
     self.assertEqual(["Default"], pm.getCategories())
     self.assertTrue(isinstance(pm.getPluginLocator(), PluginFileLocator))
	def test_init_with_category_filter(self):
		pm = PluginManager(categories_filter={"Mouf": IPlugin})
		self.assertEqual(["Mouf"],pm.getCategories())
		self.assertTrue(isinstance(pm.getPluginLocator(),PluginFileLocator))