Example #1
0
def parse_interface(interfacename):
    tree = xmlparse("views" + QDir.separator() + interfacename + ".xml").getroot() # get the tree
    Compiler.current_interfacename = interfacename # set the current interfacename
    imported = [] # this is the list of interfaces to parse after this one
    
    Compiler.code += "from PySide.QtGui import *\nfrom PySide.QtCore import *\n" # import PySide
    Compiler.code += "from oxide.widgets.all import *\n" # import Oxide
    if interfacename == Compiler.start: # if this is the start interface, create a QApplication
        Compiler.code += "import sys\n"         # using sys.argv,
        Compiler.code += "app = OxideApplication(sys.argv)\n" # create a QApplication
        
    add_util_funcs() # add the utility functions
    
    add_model(interfacename,"__main__") # now add the model code to the output file
    
    for element in tree: # iterate through the elements in the tree
        if element.tag.lower() == "animations": # here we handle the special 'animations' tag
            Compiler.to_be_own_class = False # there is no way that we're in the resources tag here
            for animation in element: # go through each animation
                parse_animation(animation) # and parse it
        
        elif element.tag.lower() == "window": # here we handle the window
            Compiler.to_be_own_class = False # there is no way that we're in the resources tag here
            parse_toplevel(element)       # using parse_window
            
        elif element.tag.lower() == "resources":
            Compiler.to_be_own_class = True
            
            for child in element:
                Compiler.direct_resources_child = True
                parse_abstract_element(child)
                Compiler.direct_resources_child = False
                    
            Compiler.direct_resources_child = False
            
        elif element.tag.lower() == "import":
            Compiler.code += "import " + element.get('interface') + "\n"
            imported.append(element.get('interface'))
                    
    if interfacename == Compiler.start: # to finish up, if this is the start file
        Compiler.code += "sys.exit(app.exec_())\n" # run the application
    
    # now it has come time to put the code on the filesystem        
    if not QDir("bin").exists(): # if the 'bin' folder doesn't exist
        QDir().mkdir("bin")     # create it
    interfacefile = open(Compiler.gendir + QDir.separator() + interfacename + ".py", 'w')
    interfacefile.write(Compiler.code) # write Compiler.code to the file
    
    Compiler.code = "" # reset Compiler.code
    
    for interface in imported:
        parse_interface(interface)
Example #2
0
def base_object(element):
    element_tag = element.tag.lower()
    
    # check that the element represents a known widget type
    try: WidgetRegister.types[element_tag]
    except KeyError: raise UnkownWidgetError("'" + element.tag + "' is not a known widget type")
    
    # here we put in the constructor and check that this is a known widget type
    if Compiler.direct_resources_child is False: # the code should not be wrapped in a class
        Compiler.code += "\n"
        if element.get("id") is None: # if the id has been omitted
            addDefaultId(element)  # generate a default one and add it to the element
        if Compiler.to_be_own_class is True: element.set("id","self." + element.get('id'))
            
        indent()
        Compiler.code += element.get("id") + " = " + WidgetRegister.types[element_tag]['widgetName'] + "("
        if WidgetRegister.types[element_tag].get('isqt',False) is False: # if this is an oxide widget
            Compiler.code += "'" + prepStr(element.get("styletype","default")) + "'"
        Compiler.code += ")\n"
        
    else: # the code should be wrapped in a class
        if element.get('name') is None: raise MissingAttributeError(element.tag + " has no name.")
        if element.get('name').startswith("__"): raise ForbiddenNameError("'name' attribute may not begin with '__'")
        Compiler.code += "class " + element.get("name") + "(" + WidgetRegister.types[element_tag]['widgetName'] + "):\n"
        element.set("id","self")
        
        add_class_funcs(WidgetRegister.types[element_tag]['widgetName']) # add any utility functions for the class
        
        # add the model code
        add_model(Compiler.current_interfacename,element.get('name'))
        
        # __init__ function definition
        Compiler.code += Compiler.model_indent + "def __init__(self, " + ", ".join(WidgetRegister.types[element_tag].get('init_args',[])) + "):\n" 

        Compiler.code += Compiler.model_indent*2 + WidgetRegister.types[element_tag]['widgetName'] + ".__init__(self, " # basic constructor 
        if WidgetRegister.types[element_tag].get('isqt',False) is False: # if this is an oxide widget
            Compiler.code += "'" + prepStr(element.get("styletype","default")) + "'" # the add the style type
        Compiler.code +=  ", ".join(WidgetRegister.types[element_tag].get('init_args',[])) + ")\n" # give it the necessary __init__ arguments
        
        
    handleProps(WidgetRegister.types[element_tag],element) # handle properites
    handleSlots(WidgetRegister.types[element_tag], element) # handle events/signals/slots
    
    if WidgetRegister.types[element_tag].get('isqt',False) is False: # if this is an oxide widget
        handleStyleProps(WidgetRegister.types[element_tag],element)