Advertisement
WolfieMario

Arguments Parser V3

Jun 5th, 2015
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.88 KB | None | 0 0
  1. class Arguments(object):
  2.     """Selector-specific arguments.
  3.    
  4.    The parser used during construction limits parsing to only the attributes
  5.    required for the represented use case, and the attributes necessary to
  6.    derive those attributes.
  7.    """
  8.    
  9.     class Parser(object):
  10.         # Parse args control
  11.         def fields(args, temps): # link_ctrl, field_ctrls
  12.             if temps.type_multiple: return
  13.             ctrls = [getByPath(args.form, path) for path in temps.args[3:]]
  14.             if temps.type_link:
  15.                 args.link_ctrl, args.field_ctrls = ctrls[0], ctrls[1:]
  16.             else:
  17.                 args.field_ctrls = ctrls
  18.         def components(args, temps):
  19.             if not temps.type_multiple: return
  20.             args.components = []
  21.             for path in temps.args[3:]:
  22.                 ctrl = getByPath(args.form, path)
  23.                 q_ctrl = getByPath(ctrl.Parent, ctrl.HiddenValue.split(LIST_SEP, 3)[2])
  24.                 args.components.append( (q_ctrl.HiddenValue.split(LIST_SEP, 1)[0], ctrl) )
  25.        
  26.         # Parse query control
  27.         def delimiters(args, temps):
  28.             args.prefix, args.delimiter = args.query_ctrl.HiddenValue.split(LIST_SEP, 2)[:2]
  29.         def substitutions(args, temps):
  30.             args.substitutions = safeSplit(args.query_ctrl.HiddenValue, LIST_SEP)[2:]
  31.         def query(args, temps):
  32.             args.query = args.query_ctrl.Tag
  33.        
  34.         # Parse link control
  35.         def links(args, temps): #link_button, link_paths; @depends(fields)
  36.             if not temps.type_link: return
  37.             args.link_paths = safeSplit(args.link_ctrl.HiddenValue, LIST_SEP)
  38.             if args.select_type == SelectType.LINKING:
  39.                 args.link_button = getByPath(args.form, args.link_paths[0])
  40.                 args.link_paths = args.link_paths[1:]
  41.         def link_ctrls(args, temps): #@depends(links)
  42.             if not temps.type_link: return
  43.             args.link_ctrls = [getByPath(args.form, path) for path in args.link_paths]
  44.         def link_inputs(args, temps): #@depends(fields)
  45.             if not temps.type_link: return
  46.             args.link_inputs = [getValue(getByPath(args.form, path)) for path
  47.                                 in safeSplit(args.link_ctrl.Tag, LIST_SEP)]
  48.        
  49.         # Parse input control
  50.         def input(args, temps):
  51.             args.input = args.input_ctrl.Text
  52.         def inputs(args, temps): #@depends(input, delimiters, fields)
  53.             if temps.type_multiple: return
  54.             args.inputs = safeSplit(args.input[len(args.prefix):], args.delimiter,
  55.                                     len(args.field_ctrls) - 1, empty_list=False)
  56.        
  57.         # (Not actually parsing)
  58.         def form_ops(args, temps):
  59.             # ThisComponent = XSCRIPTCONTEXT.getDocument()
  60.             args.form_ops = (XSCRIPTCONTEXT.getDocument().CurrentController
  61.                             .getFormController(args.form).FormOperations)
  62.        
  63.         SELECT = [fields, links, input, delimiters, inputs, components, form_ops] # Depended: delimiters
  64.         QUERY = [substitutions, query]
  65.         LINK_INPUTS = SELECT + [link_inputs]
  66.         VISIBLE = [components, fields, links, link_ctrls] # Depended: fields, links
  67.         REFRESH = [fields, links, delimiters]
  68.    
  69.     def __init__(self, form, args_ctrl, parser=Parser.SELECT):
  70.         self.form = form
  71.         self.args_ctrl = args_ctrl
  72.         args, self.child_paths = extractArgs(args_ctrl)
  73.         self.select_type = SelectType.types[args[0]]
  74.         self.input_ctrl, self.query_ctrl = [getByPath(form, path) for path in args[1:3]]
  75.        
  76.         temps = object()
  77.         temps.args = args
  78.         temps.type_link = self.select_type in (SelectType.LINKED, SelectType.LINKING)
  79.         temps.type_multiple = (self.select_type == SelectType.MULTIPLE)
  80.        
  81.         for func in parser:
  82.             func(self, temps)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement