Advertisement
WolfieMario

Arguments Parser V2

Jun 5th, 2015
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.34 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.    specified by that mode, and the attributes necessary to derive them.
  23.    """
  24.    
  25.     class Mode(object):
  26.         _ALWAYS = set(['form', 'args_ctrl',
  27.                        'select_type', 'input_ctrl', 'query_ctrl', 'child_paths'])
  28.         _FIELDS = set(['link_ctrl', 'field_ctrls'])
  29.         _LINKS = set(['link_button', 'link_paths'])
  30.         _DELIMITERS = set(['prefix', 'delimiter'])
  31.         _INPUTS = set(['input', 'inputs'])
  32.         _COMPONENTS = set(['components'])
  33.        
  34.         SELECT = _ALWAYS | _FIELDS | _LINKS | _INPUTS | _COMPONENTS | set(['form_ops'])
  35.         QUERY = _ALWAYS | set(['substitutions', 'query'])
  36.         LINK_INPUTS = SELECT | set(['link_inputs'])
  37.         VISIBLE = _ALWAYS | _COMPONENTS | set(['link_ctrls'])
  38.         REFRESH = _ALWAYS | _FIELDS | _LINKS | _DELIMITERS
  39.        
  40.         ALL = SELECT | QUERY | LINK_INPUTS | VISIBLE | REFRESH
  41.    
  42.     @export(*Mode.ALL)
  43.     def __init__(self, form, args_ctrl, parse=Mode.SELECT):
  44.         # Always parsed
  45.         args, child_paths = extractArgs(args_ctrl)
  46.         select_type = SelectType.types[args[0]]
  47.         input_ctrl, query_ctrl = [getByPath(form, path) for path in args[1:3]]
  48.        
  49.         # Conditional parsing
  50.         if 'substitutions' in parse:
  51.             args.substitutions = safeSplit(args.query_ctrl.HiddenValue, LIST_SEP)[2:]
  52.         if 'query' in parse:
  53.             args.query = args.query_ctrl.Tag
  54.         if not parse.isdisjoint(Arguments.Mode._INPUTS):
  55.             input = input_ctrl.Text
  56.        
  57.         if select_type == SelectType.MULTIPLE and 'components' in parse:
  58.             components = []
  59.             for path in args[3:]:
  60.                 ctrl = getByPath(form, path)
  61.                 q_ctrl = getByPath(ctrl.Parent, ctrl.HiddenValue.split(LIST_SEP, 3)[2])
  62.                 components.append( (q_ctrl.HiddenValue.split(LIST_SEP, 1)[0], ctrl) )
  63.        
  64.         need_inputs = 'inputs' in parse
  65.         if not parse.isdisjoint(Arguments.Mode._DELIMITERS) or need_inputs:
  66.             prefix, delimiter = query_ctrl.HiddenValue.split(LIST_SEP, 2)[:2]
  67.        
  68.         need_fields = not parse.isdisjoint(Arguments.Mode._FIELDS)
  69.         need_links = not parse.isdisjoint(Arguments.Mode._LINKS)
  70.         need_link_ctrls = 'link_ctrls' in parse
  71.         need_link_inputs = 'link_inputs' in parse
  72.         type_link = select_type in (SelectType.LINKED, SelectType.LINKING)
  73.         if need_fields or need_links or need_link_ctrls or need_inputs or need_link_inputs:
  74.             ctrls = [getByPath(form, path) for path in args[3:]]
  75.             if type_link:
  76.                 link_ctrl, field_ctrls = ctrls[0], ctrls[1:]
  77.             else:
  78.                 field_ctrls = ctrls
  79.        
  80.         if type_link and (need_links or need_link_ctrls):
  81.             link_paths = safeSplit(link_ctrl.HiddenValue, LIST_SEP)
  82.             if select_type == SelectType.LINKING:
  83.                 link_button = getByPath(form, link_paths[0])
  84.                 link_paths = link_paths[1:]
  85.        
  86.         if type_link and need_link_ctrls:
  87.             link_ctrls = [getByPath(form, path) for path in link_paths]
  88.        
  89.         if need_inputs:
  90.             inputs = safeSplit(input[len(prefix):], delimiter,
  91.                                len(field_ctrls) - 1, empty_list=False)
  92.        
  93.         if 'form_ops' in parse:
  94.             # ThisComponent = XSCRIPTCONTEXT.getDocument()
  95.             form_ops = (XSCRIPTCONTEXT.getDocument().CurrentController
  96.                                       .getFormController(form).FormOperations)
  97.        
  98.         if type_link and need_link_inputs:
  99.             link_inputs = [getValue(getByPath(form, path)) for path in safeSplit(link_ctrl.Tag, LIST_SEP)]
  100.        
  101.         self._locals = locals()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement