Exemple #1
0
 def LoadElements(self,res):        
     for name in res:
         Type,vs=res[name]
         elt=FieldFromValues(Type,vs)
         elt.name=name
         self.AddElt(elt,fromFile=True)
         p=self.elts[-1]
         for cname in vs:
             p.GetPropertyByName(cname).SetValue(vs[cname])
Exemple #2
0
    def OnMSEExport(self,evt,startfile=True):
        skipped_list=("Set","StaticImage")
        import os, os.path, MSE
        from widgets import FieldFromValues
        safe_title=self.Title
        if self.Title[-1]=='*':
            safe_title=self.Title[:-1]
        if not self.file:
            import os
            dlg = wx.DirDialog(self, "Choose a destination directory for Template Export:", style=wx.DD_DEFAULT_STYLE, defaultPath=self.dir)

            # If the user selects OK, then we process the dialog's data.
            # This is done by getting the path data from the dialog - BEFORE
            # we destroy it. 
            if dlg.ShowModal() == wx.ID_OK:
                path=dlg.GetPath()
                self.dir=path
                # Only destroy a dialog after you're done with it.
                dlg.Destroy()
            else:
                return
        else:
            path=os.path.split(self.file)[0]
        objs=self.elts
        tmp=objs[0]
        name=tmp.GetValues()['short name']
        sname=tmp.GetValues()['style short name']
        pg=os.path.join(path,'%s.mse-game'%name)
        ps=os.path.join(path,'%s-%s.mse-style'%(name,sname))
        if not os.path.isdir(pg):
            os.mkdir(pg)
        if not os.path.isdir(ps):
            os.mkdir(ps)
        game_parts=[]
        style_parts=[]
        for page in objs:
            #MSE.CopyImage(page,pg,ps)
            if page.Type in skipped_list:
                continue
            _obj=FieldFromValues(page.Type,page)
            game_parts.append(obj.export('game').encode('cp1252'))
            style_parts.append(obj.export('style').encode('cp1252'))
            MSE.CopyImage(page,pg,ps)
        file(os.path.join(pg,'game'),'wb').write(os.linesep.join(game_parts))
        file(os.path.join(ps,'style'),'wb').write(os.linesep.join(style_parts))
        if startfile:
            os.startfile(path)
            self.statusbar.SetStatusText('Template Exported!')        
Exemple #3
0
def make_template(template_filename):
    import os.path, cPickle
    from properties import typematch
    from  widgets import FieldFromValues
    tdir,filename=os.path.split(template_filename)
    script=""
    #Helper Function
    def getorder(element):
        t,v=elts[element]
        order= v.get('z index',0)
        if order is None:
            order=0
        return order
    IsPackage=template_filename.endswith('.bgp')
    #convert to list. otherwise, it is a generator, which may not be re-used
    #Disinction between simple tmpl file=>pickle & .zip file=>load them
    if not IsPackage:
        elts=cPickle.load(file(template_filename,'rb'))
        filename_no_ext=os.path.splitext(filename)[0]
        #filename_no_ext='script'
        script_name=os.path.join(tdir,filename_no_ext+'.py')
        if os.path.isfile(script_name):
            script=file(script_name,'rb')
    else:
        from package import Package
        p=Package(template_filename)
        elts=cPickle.load(p.template)
        script=p.script
    #it is a dict mapping elt name with (type, values)
    a=Template()
    #Show that this is a real template and not just some code
    a.isTemplate=True
    #if filename is inside the card folder, replace by relative path
    if template_filename.startswith(config.card_folder):
        a.name=os.path.relpath(template_filename,start=config.card_folder)
    else:
        a.name=template_filename
    a.help='Template from file %s'%filename
    a.column=dict()
    a.dir=tdir
    a.widgets=OrderedDict()
    a.choices=dict()#create a dict to contains all choices list for a given elements
    for x in sorted(elts, key=lambda x:getorder(x)):
        Type,vs=elts[x]
        obj=FieldFromValues(Type,vs)
        a.widgets[obj.name]=obj
        #Now link the widget to the template, somehow
        obj._template=a
    #Now, create script columns for default actions view
    for name in elts:
        Type,values=elts[name]
        if Type.lower()=='template':
            #Create a fitting size based on template size - only if one is defined
            a.fitting_size=(values['card width'],values['card height'])
            continue
        #Only loop on printable elts. For each of these, let user choose the changeable parameter: text for text, image path for image, color for color, chocie for choice
        tp=typematch.get(Type.capitalize(),None)
        if tp is None:#Not a changeable element
            continue
        if tp=="choice":
            a.column[values['name']]="new_choice",values['default']
            a.choices[values['name']]=values['choices']
            continue
        #If field is NOT editable, just do not offer to edit it
        if values['editable']==False:
            continue
        a.column[values["name"]]=tp,values['default']
        #Give a link to element template
    skipped=('Set','Info','StaticImage')
    #Apply script on each widgets
    ##Exec script first
    if script:
        import new
        script_vars=dict()
        exec script.read()in script_vars
        ##Then bind it to widget
        for wName in a.widgets:
            w=a.widgets[wName]
            if 'on_%s_displaying'%w.name in script_vars:
                w.OnDisplaying=new.instancemethod(script_vars['on_%s_displaying'%w.name],w,w.__class__)
            if 'on_%s_displayed'%w.name in script_vars:
                w.OnDisplayed=new.instancemethod(script_vars['on_%s_displayed'%w.name],w,w.__class__)
            if 'on_%s_create'%w.name in script_vars:
                print 'useless: object is already created when link happens'
                w.OnCreate=new.instancemethod(script_vars['on_%s_create'%w.name],w,w.__class__)
                print w.name,'matched in creation'
            if 'on_%s_change'%w.name in script_vars:
                print 'useless: the only important change is the one in display with gridvalue...'
                w.OnChange=new.instancemethod(script_vars['on_%s_change'%w.name],w,w.__class__)                
                print w.name,'matched in change'
    def process(dc, values):
        for objName in a.widgets: #getattr and not Xx. to have Tempalte element not revert to a bug
            obj=a.widgets[objName]            
            #Type,vs=elts[elt]
            Type=obj.Type
            if Type=='template':
                #Fill the stuff with card background, it color & size
                #~ dc.SetBrush(wx.Brush(vs['card background'],wx.SOLID))
                #~ dc.DrawRectangle(0,0,*a.fitting_size)
                #~ dc.SetBrush( wx.NullBrush )            
                continue
            if Type in skipped:
                continue
            #Change elt values with propgrid values
            #Only loop on exportable parameter and not the onefixed in the template file
            #If case the element is not editable, there is not values[elt]=>just give empty string (none not working with Render)
            obj.Render(values.get(obj.name,""),dc,a,values)
    import new
    a.process=new.instancemethod(process,a,Template)
    a.process=process
    return a
Exemple #4
0
def make_template(template_filename):
    import os.path, cPickle
    from properties import typematch
    from widgets import FieldFromValues
    tdir, filename = os.path.split(template_filename)
    script = ""

    #Helper Function
    def getorder(element):
        t, v = elts[element]
        order = v.get('z index', 0)
        if order is None:
            order = 0
        return order

    IsPackage = template_filename.endswith('.bgp')
    #convert to list. otherwise, it is a generator, which may not be re-used
    #Disinction between simple tmpl file=>pickle & .zip file=>load them
    if not IsPackage:
        elts = cPickle.load(file(template_filename, 'rb'))
        filename_no_ext = os.path.splitext(filename)[0]
        #filename_no_ext='script'
        script_name = os.path.join(tdir, filename_no_ext + '.py')
        if os.path.isfile(script_name):
            script = file(script_name, 'rb')
    else:
        from package import Package
        p = Package(template_filename)
        elts = cPickle.load(p.template)
        script = p.script
    #it is a dict mapping elt name with (type, values)
    a = Template()
    #Show that this is a real template and not just some code
    a.isTemplate = True
    #if filename is inside the card folder, replace by relative path
    if template_filename.startswith(config.card_folder):
        a.name = os.path.relpath(template_filename, start=config.card_folder)
    else:
        a.name = template_filename
    a.help = 'Template from file %s' % filename
    a.column = dict()
    a.dir = tdir
    a.widgets = OrderedDict()
    a.choices = dict(
    )  #create a dict to contains all choices list for a given elements
    for x in sorted(elts, key=lambda x: getorder(x)):
        Type, vs = elts[x]
        obj = FieldFromValues(Type, vs)
        a.widgets[obj.name] = obj
        #Now link the widget to the template, somehow
        obj._template = a
    #Now, create script columns for default actions view
    for name in elts:
        Type, values = elts[name]
        if Type.lower() == 'template':
            #Create a fitting size based on template size - only if one is defined
            a.fitting_size = (values['card width'], values['card height'])
            continue
        #Only loop on printable elts. For each of these, let user choose the changeable parameter: text for text, image path for image, color for color, chocie for choice
        tp = typematch.get(Type.capitalize(), None)
        if tp is None:  #Not a changeable element
            continue
        if tp == "choice":
            a.column[values['name']] = "new_choice", values['default']
            a.choices[values['name']] = values['choices']
            continue
        #If field is NOT editable, just do not offer to edit it
        if values['editable'] == False:
            continue
        a.column[values["name"]] = tp, values['default']
        #Give a link to element template
    skipped = ('Set', 'Info', 'StaticImage')
    #Apply script on each widgets
    ##Exec script first
    if script:
        import new
        script_vars = dict()
        exec script.read() in script_vars
        ##Then bind it to widget
        for wName in a.widgets:
            w = a.widgets[wName]
            if 'on_%s_displaying' % w.name in script_vars:
                w.OnDisplaying = new.instancemethod(
                    script_vars['on_%s_displaying' % w.name], w, w.__class__)
            if 'on_%s_displayed' % w.name in script_vars:
                w.OnDisplayed = new.instancemethod(
                    script_vars['on_%s_displayed' % w.name], w, w.__class__)
            if 'on_%s_create' % w.name in script_vars:
                print 'useless: object is already created when link happens'
                w.OnCreate = new.instancemethod(
                    script_vars['on_%s_create' % w.name], w, w.__class__)
                print w.name, 'matched in creation'
            if 'on_%s_change' % w.name in script_vars:
                print 'useless: the only important change is the one in display with gridvalue...'
                w.OnChange = new.instancemethod(
                    script_vars['on_%s_change' % w.name], w, w.__class__)
                print w.name, 'matched in change'

    def process(dc, values):
        for objName in a.widgets:  #getattr and not Xx. to have Tempalte element not revert to a bug
            obj = a.widgets[objName]
            #Type,vs=elts[elt]
            Type = obj.Type
            if Type == 'template':
                #Fill the stuff with card background, it color & size
                #~ dc.SetBrush(wx.Brush(vs['card background'],wx.SOLID))
                #~ dc.DrawRectangle(0,0,*a.fitting_size)
                #~ dc.SetBrush( wx.NullBrush )
                continue
            if Type in skipped:
                continue
            #Change elt values with propgrid values
            #Only loop on exportable parameter and not the onefixed in the template file
            #If case the element is not editable, there is not values[elt]=>just give empty string (none not working with Render)
            obj.Render(values.get(obj.name, ""), dc, a, values)

    import new
    a.process = new.instancemethod(process, a, Template)
    a.process = process
    return a