From b4fdd76d9f21afc2c8ce5198fcf5761b307d3da2 Mon Sep 17 00:00:00 2001 From: makkalot Date: Mon, 9 Jun 2008 01:06:22 +0300 Subject: initial design for widget input factory --- funcweb/funcweb/widget_automation.py | 124 +++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 funcweb/funcweb/widget_automation.py diff --git a/funcweb/funcweb/widget_automation.py b/funcweb/funcweb/widget_automation.py new file mode 100644 index 0000000..7fec309 --- /dev/null +++ b/funcweb/funcweb/widget_automation.py @@ -0,0 +1,124 @@ +#the purpose of that module is to make widget automation +#for registered minion modules so we dont hace to write +#that same boring stuff for every added module ! + +from turbogears.widgets.base import Widget,WidgetsList + +class WidgetListFactory(object): + """ + The class is responsible for taking the method arguments + and converting them to the appropriate widget equivalents + + Examples : + """ + #which type matches for which InputWidget + __convert_table={ + 'int':{ + 'default_value':"TextField", + 'range':"SingleSelectField"}, + 'string':{ + 'default_value':"TextField", + 'options':"SingleSelectField"}, + 'boolean':{' + default_value':"CheckBox"}, + 'float':{ + 'default_value':"TextField", + 'range':"SingleSelectField"}, + 'hash':{ + 'default_value':"TextArea"}, + 'list':{ + 'default_value':"TextArea"} + } + + #will contain the input widget created in that class + __widget_list={} + + def __init__(self,argument_dict): + """ + Initiated with argument_dict of a method to return back + a WidgetsList object to be placed into a form object + + @param:argument_dict : The structure we got here is like + {'arg1':{'type':'int'..},'arg2':{'min':111} ...} + """ + + self.__argument_dict = argument_dict + + def __add_general_widget(self): + + #key is the argument_name and the argument are options + for key,argument in self.__argument_dict.iteritems(): + #get the type of the argument + current_type = argument['type'] + + act_special = False #if it has some special parameters + #it should be passed to its specialized method,if it has + #for example options in its key it should be shown as a + #SingleSelectField not a TextFiled + + for type_match in self.__convert_table[current_type].keys(): + if type_match!='default_value' and argument.has_key(type_match): + act_special = True + + print key,argument + + if act_special: + getattr(self,"__add_%s_widget")(argument['type']) #call the appropriate one + else: + temp_object = getattr(widgets,self.__convert_table[current_type]['default_value']) + #add common options to it + self.__add_commons_to_object(temp_object,argument,key) + #add a new entry to final list + self.__widget_list[key]=temp_object + + def __add_commons_to_object(self,object,argument,argument_name): + """ + As it was thought all input widgets have the same + common parameters they take so that method will add + them to instantiated object for ex (TextField) if they + occur into the argument ... + + @param object : instantiated inputwidget object + @param method argument to lookup {type:'int','max':12 ...} + @return :None + """ + #firstly set the name of the argument + setattr(object,"name",argument_name) + + if argument.has_key('default'): + setattr(object,"default",argument["default"]) + if argument.has_key('optional') and argument['optional']: + setattr(object,'is_required',False) + else: + setattr(object,'is_required',True) + if argument.has_key('description'): + setattr(object,'help_text',argument['description']) + + + def __add_string_widget(self,arg_dict): + print "Called with act special" + pass + + def __add_int_widget(self,arg_dict): + print "Called with act special" + pass + + def __add_boolean_widget(self,arg_dict): + print "Called with act special" + pass + + def __add_hash_widget(self,arg_dict): + print "Called with act special" + pass + + def __add_list_widget(self,arg_dict): + print "Called with act special" + pass + + def get_widgetlist(self): + """ + Return the final list back + """ + #compute the list + self.__add_general_widget() + return self.__widget_list -- cgit