def _addGroupArgumentToDependencies(self, arg, ancestors, optional=False): # Add mutex choice group arg to dependency tree # group_arg contains members as dependency arguments options = arg.children[0].children p_node = self._getDependencyParentNode(ancestors) members = [ self._generateDependencyArgument(option) for option in options ] pretty_names = [member["name"] for member in members] names = [arg.name for arg in arg.children[0].children] gdesc = "Group key for mutex choices: {0}".format(" and ".join(names)) for name in [name for name in names if name in self.all_desc_and_type]: gdesc = gdesc.replace( name, "{0} ({1})".format(name, self.all_desc_and_type[name]['desc'])) gname = "<{0}>".format("_".join(pretty_names)) grp_arg = Argument(gname) grp_arg.parse(gname) self.all_desc_and_type[gname] = {'desc': gdesc} self._addArgumentToDependencies(grp_arg, ancestors=ancestors, optional=optional, members=members) ancestors.append(grp_arg.name) return ancestors
def _loadInputsFromUsage(self, usage): ancestors = [] for arg in usage.children: # Traverse usage args and add them to dependencies tree arg_type = type(arg).__name__ if hasattr(arg, "children"): fchild_type = type(arg.children[0]).__name__ # Has sub-arguments, maybe recurse into _loadRtrctnsFrmUsg # but have to deal with children in subtype if arg_type == "Optional" and fchild_type == "AnyOptions": for option in arg.children[0].children: self._addArgumentToDependencies(option, ancestors=ancestors, optional=True) elif arg_type == "OneOrMore": list_name = "<list_of_{0}>".format( self._getParamName(arg.children[0].name)) list_arg = Argument(list_name) list_arg.parse(list_name) self.all_desc_and_type[list_name] = { 'desc': "List of {0}".format( self._getParamName(arg.children[0].name)) } self._addArgumentToDependencies(list_arg, ancestors=ancestors, isList=True) ancestors.append(list_name) elif arg_type == "Optional" and fchild_type == "Option": for option in arg.children: self._addArgumentToDependencies(option, ancestors=ancestors, optional=True) elif arg_type == "Optional" and fchild_type == "Either": # Mutex choices group, add group to dependencies tree # add choices args to group ancestors = self._addGroupArgumentToDependencies( arg, ancestors, optional=True) elif arg_type == "Required" and fchild_type == "Either": # Mutex choices group, add group to dependencies tree # add choices args to group ancestors = self._addGroupArgumentToDependencies( arg, ancestors) elif arg_type == "Command": self._addArgumentToDependencies(arg, ancestors=ancestors) ancestors.append(arg.name) elif arg_type == "Argument": self._addArgumentToDependencies(arg, ancestors=ancestors) ancestors.append(arg.name) elif arg_type == "Option": self._addArgumentToDependencies(arg, ancestors=ancestors, optional=True) ancestors.append(arg.name) else: raise_error( ImportError, "Non implemented docopt arg.type: {0}".format(arg_type))
def loadDescriptionAndType(self): # using docopt code to extract description and type from args for line in (self._parse_section('arguments:', self.docopt_str) + self._parse_section('options:', self.docopt_str)): _, _, s = line.partition(':') # get rid of "options:" split = re.split(r'\n[ \t]*(-\S+?)', '\n' + s)[1:] if\ line in self._parse_section('options:', self.docopt_str) else\ re.split(r'\n[ \t]*(<\S+?)', '\n' + s)[1:] split = [s1 + s2 for s1, s2 in zip(split[::2], split[1::2])] # parse each line of Arguments and Options for arg_str in [ s for s in split if (s.startswith('-') or s.startswith('<')) ]: arg = Option.parse(arg_str) if arg_str.startswith('-')\ else Argument.parse(arg_str) arg_segs = arg_str.partition(' ') # Add desc and type to all_desc_and_type # key is initial id/name self.all_desc_and_type[arg.name] = { "desc": arg_segs[-1].replace('\n', ' ').replace(" ", '').strip() } if hasattr(arg, "value") and arg.value is not None and\ arg.value is not False: self.all_desc_and_type[arg.name]['default-value'] =\ arg.value if type(arg) is Option and arg.argcount > 0: for typ in [ seg for seg in arg_segs[0].replace(',', ' ').replace( '=', ' ').split() if seg[0] != "-" ]: self.all_desc_and_type[arg.name]["type"] = typ