vasm / plainui.py

#!/usr/bin/env python

#    This file is part of VASM.
#
#    VASM is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License v3 as published by
#    the Free Software Foundation.
#
#    VASM is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with VASM.  If not, see <http://www.gnu.org/licenses/>.

__author__ = "Moises Henriquez"
__author_email__ = "moc.liamg@xnl.E0M"[::-1]

import sys
import gtk
sys.path.append("./modules")
from support import widgets
from support import dialogs
import os
import imp



def get_category_image(category):
    """ Returns the path or icon name for the category provided """
    iconsdir = os.path.join(os.getcwd(),
                            "modules/support/data/icons")
    caticon = os.path.join(iconsdir, "category_%s.png"% category.lower())
    generic = os.path.join(iconsdir, "category_unknown.png")
    if os.path.exists(caticon):
        return caticon
    return generic

class VASM(gtk.Window):
    def __init__(self):
        gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
        self.connect("destroy", gtk.main_quit)
        self.body = gtk.VBox()
        self.viewers = gtk.VBox()
        self.categories = []        
        self.discover_modules()
        self.set_title("VASM")
        iconpath = os.path.join(os.getcwd(), "modules/support/data/icons/package-utilities.png")
        self.set_icon_from_file(iconpath)
        
        # create a hbox on top to display the vasmcc logo
        tophb = gtk.HBox()
        imgsensor = gtk.EventBox()
        imgsensor.connect("button-release-event", self.show_about_dialog)
        img = gtk.Image()
        img.set_from_pixbuf(
            gtk.gdk.pixbuf_new_from_file_at_size(
                os.path.join(os.getcwd(), "modules/support/data/vasmcc-logo.png"),
                100, 50))
        imgsensor.add(img)
        tophb.pack_end(imgsensor, False, False, 4)
        
        # Add the topbox to the body of the window
        self.body.pack_start(tophb, False, True, 2)
        
        # Add the viewers on a scrolled window just in case our space is too small to display all of them
        scroll = gtk.ScrolledWindow()
        scroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        scroll.add_with_viewport(self.viewers)
        scroll.set_shadow_type(gtk.SHADOW_NONE)
        # Add the scrolledwindow to the body of the main window
        self.body.pack_start(scroll, True, True, 0)
        self.set_size_request(640, 480)
        self.set_position(gtk.WIN_POS_CENTER)
        self.expand_last_view()
        self.add(self.body)
    
    def show_about_dialog(self, widget, event):
        if event.button != 1:
            return
        about = dialogs.AboutVASM(parent=self)
        if about.run():
            about.destroy()

    def expand_last_view(self):
        """ Expand the last iconview in the view area to fill the blank space """
        last = self.viewers.get_children()[-1]
        self.viewers.set_child_packing(
            last, True, True, 2, gtk.PACK_START)

    def add_option(self, module):
        """ Add module launcher to the appriopriate view """
        wholder = None
        for holder in self.categories:
            if holder.label == module.VASM_CATEGORY:
                wholder = holder
                break
        if wholder is None:
            wholder = widgets.SettingsHolder(
                label=module.VASM_CATEGORY,
                icon=get_category_image(module.VASM_CATEGORY),
                parent = self)
            # Append to the list of existing holders
            self.categories.append(wholder)
            # we need to pack this because it didn't exist
            self.viewers.pack_start(wholder, False, True, 2)
        # Now that we have the placeholder (view), add the launcher there.
        wholder.add_launcher(
            label=module.VASM_LABEL,
            icon=module.VASM_ICON,
            actuator=module.__run__)
    
    
    def discover_modules(self):
        """ Discover the modules pluggable into vasm """
        modulesdir = os.path.join(os.getcwd(), "modules")
        for fname in os.listdir(modulesdir):
            viewer = None
            if fname.endswith(".py") and fname !="__init__.py":
                pyname = fname[:-3]
                vasm_mod = self.load_module(pyname, fname, [modulesdir])
                if self.test_vasm_module(vasm_mod):
                    if vasm_mod.__vasm_test__():
                        # Module has paseed the test.  Add to the views
                        self.add_option(vasm_mod)

    def load_module(self, name, fname, path=["/home/vluser/devel/vasm/modules"]):
        """ return a named module found in the given path"""
        mod = imp.find_module(name, path)
        ret = imp.load_module(name, mod[0], fname, mod[2])
        return ret

    def test_vasm_module(self, module):
        """ Check if the module in question is a valid vasm module
        
        Returns True when a module can be loaded on to vasm.
        False otherwise. """
        if not hasattr(module, 'VASM_CATEGORY'):
            return False
        if not hasattr(module, "__vasm_test__"):
            return False
        if not hasattr(module, "__run__"):
            return False
        return True

if __name__ == '__main__':
    gtk.gdk.threads_init()
    w = VASM()
    w.show_all()
    gtk.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.