Advertisement
WolfieMario

Arguments Parser V1

Jun 5th, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.64 KB | None | 0 0
  1. def export(*names):
  2.     """A decorator to export the specified local variables.
  3.    
  4.    Before returning, set self._locals = locals()
  5.    self._locals will be deleted by the decorator.
  6.    """
  7.     def wrapper(func):
  8.         def export(self, *args, **kwargs):
  9.             func(self, *args, **kwargs)
  10.             for name in names:
  11.                 if name in self._locals:
  12.                     self.__dict__[name] = self._locals[name]
  13.             del self._locals
  14.         return export
  15.     return wrapper
  16.  
  17.  
  18. class Arguments(object):
  19.     """Selector-specific arguments.
  20.    
  21.    The mode used during construction limits parsing to only the attributes
  22.    required by that mode.
  23.    """
  24.    
  25.     class Mode(object):
  26.         SELECT = 0
  27.         QUERY = 1
  28.         LINK_INPUTS = 2
  29.         VISIBLE = 3
  30.         REFRESH = 4
  31.    
  32.     @export("form", "args_ctrl",
  33.             "select_type", "input_ctrl", "query_ctrl", "link_ctrl",
  34.             "field_ctrls", "components", "child_paths",
  35.             "prefix", "delimiter", "substitutions", "query",
  36.             "link_button", "link_paths", "link_inputs", "link_ctrls",
  37.             "input", "inputs", "form_ops")
  38.     def __init__(self, form, args_ctrl, mode=0): #Arguments.Mode.SELECT
  39.         args, child_paths = extractArgs(args_ctrl)
  40.         select_type = SelectType.types[args[0]]
  41.         input_ctrl, query_ctrl = [getByPath(form, path) for path in args[1:3]]
  42.        
  43.         if mode == Arguments.Mode.QUERY:
  44.             substitutions = safeSplit(query_ctrl.HiddenValue, LIST_SEP)[2:]
  45.             query = query_ctrl.Tag
  46.         else:
  47.             input = input_ctrl.Text
  48.            
  49.             if select_type == SelectType.MULTIPLE:
  50.                 if mode != Arguments.Mode.REFRESH:
  51.                     components = []
  52.                     for path in args[3:]:
  53.                         ctrl = getByPath(form, path)
  54.                         q_ctrl = getByPath(ctrl.Parent, ctrl.HiddenValue.split(LIST_SEP, 3)[2])
  55.                         components.append( (q_ctrl.HiddenValue.split(LIST_SEP, 1)[0], ctrl) )
  56.             else:
  57.                 ctrls = [getByPath(form, path) for path in args[3:]]
  58.                 if select_type in (SelectType.LINKED, SelectType.LINKING):
  59.                     link_ctrl, field_ctrls = ctrls[0], ctrls[1:]
  60.                    
  61.                     link_paths = safeSplit(link_ctrl.HiddenValue, LIST_SEP)
  62.                     if select_type == SelectType.LINKING:
  63.                         link_button = getByPath(form, link_paths[0])
  64.                         link_paths = link_paths[1:]
  65.                    
  66.                     if mode == Arguments.Mode.VISIBLE:
  67.                         link_ctrls = [getByPath(form, path) for path in link_paths]
  68.                 else:
  69.                     field_ctrls = ctrls
  70.                
  71.                 if mode != Arguments.Mode.VISIBLE:
  72.                     prefix, delimiter = query_ctrl.HiddenValue.split(LIST_SEP, 2)[:2]
  73.                    
  74.                     if mode != Arguments.Mode.REFRESH:
  75.                         inputs = safeSplit(input[len(prefix):], delimiter,
  76.                                                 len(field_ctrls) - 1, empty_list=False)
  77.                        
  78.                         # ThisComponent = XSCRIPTCONTEXT.getDocument()
  79.                         form_ops = (XSCRIPTCONTEXT.getDocument().CurrentController
  80.                                                        .getFormController(form).FormOperations)
  81.        
  82.         if mode == Arguments.Mode.LINK_INPUTS:
  83.             link_inputs = [getValue(getByPath(form, path)) for path in safeSplit(link_ctrl.Tag, LIST_SEP)]
  84.        
  85.         self._locals = locals()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement