Python pyparsing.Optional() Examples

The following are 30 code examples of pyparsing.Optional(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pyparsing , or try the search function .
Example #1
Source File: username.py    From ccat with GNU General Public License v3.0 6 votes vote down vote up
def _globalParse___username_attributes(line):
    username_dict = {}

    username       = (Word(printables))                                         ('user')
    privilege      = (Suppress('privilege') + Word(nums))                       ('priv_num')
    password_type  = (Suppress(MatchFirst(['secret', 'password'])) + Word(nums))('pass_type')

    parse_username = username + Optional(privilege) + password_type + Suppress(restOfLine)

    result = parse_username.parseString(line)

    username_dict[result.user] = {}
    username_dict[result.user]['password_type'] = result.pass_type.asList()[0]

    try:
        username_dict[result.user]['privilege'] = result.priv_num.asList()[0]
    except AttributeError:
        pass

    return username_dict 
Example #2
Source File: storm_control.py    From ccat with GNU General Public License v3.0 6 votes vote down vote up
def __ifaceAttributes___storm_check(storm,dct):

    parse_level  = Word(alphas)        + Suppress('level ')            + restOfLine
    parse_action = Suppress('action ') + Word(alphas)
    parse_type   = Word(alphas)        + Suppress(Optional("include")) + Word(alphas)
    try:
        value = parse_level.parseString(storm).asList()
        if 'level' in dct:
            dct['level'].append(value)
        else:
            dct['level'] = [value]
        return dct
    except ParseException:
        pass
    try:
        return util.int_dict_parse(parse_action, storm, 'action', dct)
    except ParseException:
        pass
    try:
        return util.int_dict_parse(parse_type,   storm, 'type',   dct)
    except ParseException:
        pass 
Example #3
Source File: rule.py    From CWR-DataApi with MIT License 6 votes vote down vote up
def _apply_modifiers(rule, modifiers):
        if 'grouped' in modifiers:
            rule = pp.Group(rule)

        if 'optional' in modifiers:
            rule = pp.Optional(rule)
        else:
            for modifier in modifiers:
                if modifier.startswith('at_least'):
                    times = rule_at_least.parseString(modifier)[0]
                    if times > 0:
                        rule_multiple = rule
                        for _ in range(1, times):
                            rule_multiple = rule_multiple + rule
                        rule = rule_multiple + pp.ZeroOrMore(rule)
                    else:
                        rule = pp.Optional(pp.ZeroOrMore(rule))

        return rule 
Example #4
Source File: protein_modification.py    From pybel with MIT License 6 votes vote down vote up
def get_protein_modification_language(concept_qualified: ParserElement) -> ParserElement:
    """Build a protein modification parser."""
    pmod_concept = MatchFirst([
        concept_qualified,
        pmod_default_ns,
        pmod_legacy_ns,
    ])

    return pmod_tag + nest(
        Group(pmod_concept)(CONCEPT)
        + Optional(
            WCW
            + amino_acid(PMOD_CODE)
            + Optional(WCW + ppc.integer(PMOD_POSITION)),
        ),
    ) 
Example #5
Source File: sql_graphviz.py    From auptimizer with GNU General Public License v3.0 6 votes vote down vote up
def grammar():
    parenthesis = Forward()
    parenthesis <<= "(" + ZeroOrMore(CharsNotIn("()") | parenthesis) + ")"

    field_def = OneOrMore(Word(alphanums + "_\"'`:-") | parenthesis)
    field_def.setParseAction(field_act)

    tablename_def = ( Word(alphas + "`_") | QuotedString("\"") )

    field_list_def = field_def + ZeroOrMore(Suppress(",") + field_def)
    field_list_def.setParseAction(field_list_act)

    create_table_def = Literal("CREATE") + "TABLE" + tablename_def.setResultsName("tableName") + "(" + field_list_def.setResultsName("fields") + ")" + ";"
    create_table_def.setParseAction(create_table_act)

    add_fkey_def = Literal("FOREIGN") + "KEY" + "(" + Word(alphanums).setResultsName("keyName") + ")" + "REFERENCES" + Word(alphanums).setResultsName("fkTable") + "(" + Word(alphanums + "_").setResultsName("fkCol") + ")" + Optional(Literal("DEFERRABLE")) + ";"
    add_fkey_def.setParseAction(add_fkey_act)

    other_statement_def = OneOrMore(CharsNotIn(";")) + ";"
    other_statement_def.setParseAction(other_statement_act)

    comment_def = "--" + ZeroOrMore(CharsNotIn("\n"))
    comment_def.setParseAction(other_statement_act)

    return OneOrMore(comment_def | create_table_def | add_fkey_def | other_statement_def) 
Example #6
Source File: parseTabularData.py    From pyparsing with MIT License 6 votes vote down vote up
def tableValue(expr, colstart, colend):
    empty_cell_is_zero = False
    if empty_cell_is_zero:
        return Optional(
            expr.copy().addCondition(
                mustMatchCols(colstart, colend), message="text not in expected columns"
            ),
            default=0,
        )
    else:
        return Optional(
            expr.copy().addCondition(
                mustMatchCols(colstart, colend), message="text not in expected columns"
            )
        )


# define the grammar for this simple table 
Example #7
Source File: __init__.py    From pyparsing with MIT License 6 votes vote down vote up
def __init__(
        self,
        element: pyparsing.ParserElement,
        converted: EditablePartial,
        parent: EditablePartial,
        number: int,
        name: str = None,
        index: Optional[int] = None,
    ):
        #: The pyparsing element that this represents
        self.element = element  # type: pyparsing.ParserElement
        #: The name of the element
        self.name = name  # type: str
        #: The output Railroad element in an unconverted state
        self.converted = converted  # type: EditablePartial
        #: The parent Railroad element, which we store so that we can extract this if it's duplicated
        self.parent = parent  # type: EditablePartial
        #: The order in which we found this element, used for sorting diagrams if this is extracted into a diagram
        self.number = number  # type: int
        #: The index of this inside its parent
        self.parent_index = index  # type: Optional[int]
        #: If true, we should extract this out into a subdiagram
        self.extract = False  # type: bool
        #: If true, all of this element's chilren have been filled out
        self.complete = False  # type: bool 
Example #8
Source File: interpreter.py    From design-patterns with MIT License 6 votes vote down vote up
def parse(input_string):
        word = Word(alphanums)
        command = Group(OneOrMore(word))
        token = Suppress("->")
        device = Group(OneOrMore(word))
        argument = Group(OneOrMore(word))
        event = command + token + device + Optional(token + argument)
        parse_results = event.parseString(input_string)
        cmd = parse_results[0]
        dev = parse_results[1]
        cmd_str = "_".join(cmd)
        dev_str = "_".join(dev)
        try:
            val = parse_results[2]
        except IndexError:
            val_str = None
        else:
            val_str = "_".join(val)
        return cmd_str, dev_str, val_str 
Example #9
Source File: c_parser.py    From rekall with GNU General Public License v2.0 6 votes vote down vote up
def _maybe_attributes(self):
        """Possibly match some attributes.

        The syntax of attributes is described here:
        https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html
        """
        return pyparsing.Group(
            pyparsing.ZeroOrMore(
                _ATTRIBUTE
                + _DOUBLE_OPEN_PARENTHESIS
                + pyparsing.delimitedList(
                    pyparsing.Group(
                        self._identifier()("name")
                        + pyparsing.Optional(
                            _OPEN_PARENTHESIS
                            + parsers.anything_beetween("()")("args")
                            + _CLOSE_PARENTHESIS
                        )
                    )
                )
                + _DOUBLE_CLOSE_PARENTHESIS
            ).setParseAction(self._make_attribute)
        )("attributes") 
Example #10
Source File: terms.py    From plyse with MIT License 5 votes vote down vote up
def __init__(self, field, values, parse_method=None):
        super(Term, self).__init__(Optional(field) + concatenate(values, operator="LONGEST_OR"))

        self.field = field
        self.values = values

        if parse_method:
            self.setParseAction(parse_method) 
Example #11
Source File: aaa.py    From ccat with GNU General Public License v3.0 5 votes vote down vote up
def _globalParse___aaa_attributes(line, type, count_aaa):
    aaa_dict = {}

    authentication_list    = (Suppress('login')                     + Word(printables))  ('authent_list')
    authentication_groups  = (OneOrMore(Optional(Suppress('group')) + Word(printables))) ('authent_methods')

    parse_authentication   = authentication_list + authentication_groups

    # parse_authorization_options  = MatchFirst(['exec', 'login']) + Word(printables) + OneOrMore(Optional(Suppress('group')) + Word(printables))

    accounting_login   = (MatchFirst(['exec', 'network', 'connection', 'commands'])) ('acc_login')
    accounting_list    = (Optional(Word(nums)) + Word(printables))                   ('acc_list')
    accounting_record  = (MatchFirst(['start-stop', 'stop-only', 'stop']))           ('acc_record')
    accounting_methods = (OneOrMore(Optional(Suppress('group')) + Word(printables))) ('acc_methods')

    parse_accounting   = accounting_login + accounting_list + accounting_record + accounting_methods

    if   type == 'authentication':
        result = parse_authentication.parseString(line)
        aaa_dict.update({'login' + str(count_aaa): {}})
        aaa_dict['login' + str(count_aaa)]['list']    = result.authent_list[0]
        aaa_dict['login' + str(count_aaa)]['methods'] = result.authent_methods.asList()
    # elif type == 'authorization':
    #     result = parse_authorization_options.parseString(line)
    #     aaa_dict.update({'login' + str(count_aaa): {}})
    #     aaa_dict['login' + str(count_aaa)]['login']   = result.pop(0)
    #     aaa_dict['login' + str(count_aaa)]['list']    = result.pop(0)
    #     aaa_dict['login' + str(count_aaa)]['methods'] = result.asList()
    elif type == 'accounting':
        result = parse_accounting.parseString(line)
        aaa_dict.update({'login' + str(count_aaa): {}})
        aaa_dict['login' + str(count_aaa)]['login']   = result.acc_login
        aaa_dict['login' + str(count_aaa)]['list']    = result.acc_list.asList()
        aaa_dict['login' + str(count_aaa)]['record']  = result.acc_record
        aaa_dict['login' + str(count_aaa)]['methods'] = result.acc_methods.asList()

    return aaa_dict 
Example #12
Source File: google_logging.py    From plaso with Apache License 2.0 5 votes vote down vote up
def __init__(self, data_type=DATA_TYPE):
    """Initializes event data.

    Args:
      data_type (Optional[str]): event data type indicator.
    """
    super(GoogleLogEventData, self).__init__(data_type=data_type)
    self.file_name = None
    self.line_number = None
    self.message = None
    self.priority = None
    self.thread_identifier = None 
Example #13
Source File: syslog.py    From plaso with Apache License 2.0 5 votes vote down vote up
def __init__(self, data_type=DATA_TYPE):
    """Initializes an event data attribute container.

    Args:
      data_type (Optional[str]): event data type indicator.
    """
    super(SyslogLineEventData, self).__init__(data_type=data_type)
    self.body = None
    self.hostname = None
    self.pid = None
    self.reporter = None
    self.severity = None 
Example #14
Source File: safe_eval.py    From qkeras with Apache License 2.0 5 votes vote down vote up
def GetParams(s):
  """Extracts args and kwargs from string."""
  # modified from https://stackoverflow.com/questions/38799223/parse-string-to-identify-kwargs-and-args  # pylint: disable=line-too-long

  _lparen = Suppress("(")  # pylint: disable=invalid-name
  _rparen = Suppress(")")  # pylint: disable=invalid-name
  _eq = Suppress("=")  # pylint: disable=invalid-name

  data = (_lparen + Optional(
      delimitedList(
          Group(Regex(r"[^=,)\s]+") + Optional(_eq + Regex(u"[^,)]*")))
          )
      ) + _rparen)

  items = data.parseString(s).asList()

  # need to make sure that kwargs only happen after args are processed
  args = [Num(i[0]) for i in items if len(i) == 1]
  kwargs = {i[0]: Num(i[1]) for i in items if len(i) == 2}

  # check for syntax error
  for i in range(1, len(items)):
    if (len(items[i]) == 1) and (len(items[i-1]) == 2):
      raise SyntaxError

  return args, kwargs 
Example #15
Source File: modulefilter.py    From wfuzz with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self):
        if PYPARSING:
            category = Word(alphas + "_-*", alphanums + "_-*")
            operator = oneOf("and or ,")
            neg_operator = "not"
            elementRef = category
            definition = elementRef + ZeroOrMore(operator + elementRef)
            nestedformula = Group(Suppress(Optional(Literal("("))) + definition + Suppress(Optional(Literal(")"))))
            neg_nestedformula = Optional(neg_operator) + nestedformula
            self.finalformula = neg_nestedformula + ZeroOrMore(operator + neg_nestedformula)

            elementRef.setParseAction(self.__compute_element)
            neg_nestedformula.setParseAction(self.__compute_neg_formula)
            nestedformula.setParseAction(self.__compute_formula)
            self.finalformula.setParseAction(self.__myreduce) 
Example #16
Source File: parser.py    From pyprover with Apache License 2.0 5 votes vote down vote up
def tokenlist(sep, item):
    """Creates a list of tokens matching the item."""
    return item + ZeroOrMore(sep + item) + Optional(sep) 
Example #17
Source File: fragment.py    From pybel with MIT License 5 votes vote down vote up
def get_fragment_language() -> ParserElement:
    """Build a protein fragment parser."""
    _fragment_value_inner = fragment_range | missing_fragment(FRAGMENT_MISSING)
    _fragment_value = _fragment_value_inner | And([Suppress('"'), _fragment_value_inner, Suppress('"')])
    parser_element = fragment_tag + nest(_fragment_value + Optional(WCW + quote(FRAGMENT_DESCRIPTION)))
    return parser_element 
Example #18
Source File: ast.py    From gcl with MIT License 5 votes vote down vote up
def listMembers(sep, expr):
  return p.Optional(p.delimitedList(expr, sep) + p.Optional(sep).suppress()) 
Example #19
Source File: ast.py    From gcl with MIT License 5 votes vote down vote up
def bracketedList(l, r, sep, expr, allow_missing_close=False):
  """Parse bracketed list.

  Empty list is possible, as is a trailing separator.
  """
  # We may need to backtrack for lists, because of list comprehension, but not for
  # any of the other lists
  strict = l != '['
  closer = sym(r) if not allow_missing_close else p.Optional(sym(r))
  if strict:
    return sym(l) - listMembers(sep, expr) - closer
  else:
    return sym(l) + listMembers(sep, expr) + closer 
Example #20
Source File: rule.py    From CWR-DataApi with MIT License 5 votes vote down vote up
def _process_rules_group(self, rules):
        group = None

        group_type = rules.list_type
        data = rules.rules

        if group_type == 'sequence':
            group = self._process_rules(data, pp.And)
        elif group_type == 'option':
            group = self._process_rules(data, pp.MatchFirst)
        elif group_type == 'optional':
            group = pp.Optional(self._process_rules(data, pp.And))

        return group 
Example #21
Source File: special.py    From CWR-DataApi with MIT License 5 votes vote down vote up
def audio_visual_key(name=None):
    """
    Creates the grammar for an Audio Visual Key code.

    This is a variation on the ISAN (International Standard Audiovisual Number)

    :param name: name for the field
    :return: grammar for an ISRC field
    """

    if name is None:
        name = 'AVI Field'

    society_code = basic.numeric(3)
    society_code = society_code.setName('Society Code') \
        .setResultsName('society_code')

    av_number = basic.alphanum(15, extended=True, isLast=True)
    field_empty = pp.Regex('[ ]{15}')
    field_empty.setParseAction(pp.replaceWith(''))
    av_number = av_number | field_empty
    av_number = av_number.setName('Audio-Visual Number') \
        .setResultsName('av_number')

    field = pp.Group(society_code + pp.Optional(av_number))

    field.setParseAction(lambda v: _to_avi(v[0]))

    field = field.setName(name)

    return field.setResultsName('audio_visual_key') 
Example #22
Source File: entity_relationship_diagram.py    From pyagram with MIT License 5 votes vote down vote up
def lexical_analysis(self, src):
        delimited = re.sub(r'\s+', ' ', ' '.join(src.strip().split('\n'))).split(';')
        result = []
        for stmt in delimited:
            if stmt == '':
                return result
            string = pp.Regex('[a-zA-Z0-9=_]+')
            nums = pp.Regex('[0-9]+')
            ws = pp.OneOrMore(pp.White()).suppress()
            lp = pp.Regex('[(]').suppress()
            rp = pp.Regex('[)]').suppress()
            c = pp.Regex('[,]').suppress()
            q = pp.Regex("[']").suppress()
    
            table_name = string.setResultsName('table_name')
            create_table = (pp.Keyword('CREATE', caseless = True) + ws + pp.Keyword('TABLE', caseless = True) + ws + pp.Optional(pp.Keyword('IF', caseless = True) + ws + pp.Keyword('NOT', caseless = True) + ws + pp.Keyword('EXISTS', caseless = True))).suppress() + table_name + lp 
    
            column_name = string.setResultsName('column_name')
            data_type = string.setResultsName('data_type')
            length = lp + nums.setResultsName('length') + rp
            nullable = (pp.Optional(pp.Keyword('NOT', caseless = True) + ws) + pp.Keyword('NULL', caseless = True)).setResultsName('nullable')
            default_value = pp.Keyword('DEFAULT', caseless = True).suppress() + ws + string.setResultsName('default_value')
            auto_increment = pp.Keyword('AUTO_INCREMENT', caseless = True).setResultsName('auto_increment')
            column = pp.Optional(ws) + column_name + ws + data_type + pp.Optional(pp.MatchFirst([length, ws + nullable, ws + default_value, ws + auto_increment])) + pp.Optional(pp.MatchFirst([ws + nullable, ws + default_value, ws + auto_increment])) + pp.Optional(pp.MatchFirst([ws + default_value, ws + auto_increment])) + pp.Optional(ws + auto_increment) + pp.Optional(ws) + c
    
            primary_key = pp.Keyword('PRIMARY KEY', caseless = True).suppress() + lp + pp.OneOrMore(q + string.setResultsName('primary_key') + q + pp.Optional(c)) + rp + pp.Optional(c)
            key = pp.Keyword('KEY', caseless = True).suppress() + lp + q + string.setResultsName('key') + q + pp.Optional(c) + rp + pp.Optional(c)
    
            parser = create_table + pp.OneOrMore(pp.Group(column)) + pp.Optional(primary_key) + pp.Optional(key) + rp + pp.OneOrMore(ws + string).suppress()
            result.append(parser.parseString(stmt, parseAll=True))
        return result 
Example #23
Source File: header_parsing.py    From dm_control with Apache License 2.0 5 votes vote down vote up
def _nested_if_else(if_, pred, else_, endif, match_if_true, match_if_false):
  """Constructs a parser for (possibly nested) if...(else)...endif blocks."""
  ifelse = pp.Forward()
  ifelse << pp.Group(  # pylint: disable=expression-not-assigned
      if_ +
      pred("predicate") +
      pp.ZeroOrMore(match_if_true | ifelse)("if_true") +
      pp.Optional(else_ +
                  pp.ZeroOrMore(match_if_false | ifelse)("if_false")) +
      endif)
  return ifelse


# Some common string patterns to suppress.
# ------------------------------------------------------------------------------ 
Example #24
Source File: util.py    From ccat with GNU General Public License v3.0 5 votes vote down vote up
def get_attributes(config):
    options_list = []
    option = White(exact=1) + Suppress(Optional(White())) + restOfLine
    next_line = config.readline()
    try:
        option_parse = option.parseString(next_line)
        while option_parse[0] == ' ':
            options_list.append(option_parse[-1])
            next_line = config.readline()
            option_parse = option.parseString(next_line)
    except:
        pass
    return options_list, next_line 
Example #25
Source File: api.py    From gnocchi with Apache License 2.0 5 votes vote down vote up
def ResourceSchema(schema):
    base_schema = {
        voluptuous.Optional('started_at'): utils.to_datetime,
        voluptuous.Optional('ended_at'): utils.to_datetime,
        voluptuous.Optional('user_id'): voluptuous.Any(None, six.text_type),
        voluptuous.Optional('project_id'): voluptuous.Any(None, six.text_type),
        voluptuous.Optional('metrics'): MetricsSchema,
    }
    base_schema.update(schema)
    return base_schema 
Example #26
Source File: api.py    From gnocchi with Apache License 2.0 5 votes vote down vote up
def BackwardCompatibleMeasuresList(v):
        v = voluptuous.Schema(
            voluptuous.Any(MeasuresListSchema,
                           {voluptuous.Optional("archive_policy_name"):
                            six.text_type,
                            voluptuous.Optional("unit"):
                            six.text_type,
                            "measures": MeasuresListSchema}),
            required=True)(v)
        if isinstance(v, dict):
            return v
        else:
            # Old format
            return {"measures": v} 
Example #27
Source File: expansions.py    From pyjsgf with MIT License 5 votes vote down vote up
def _make_matcher_element(self):
        return self._set_matcher_element_attributes(
            pyparsing.Optional(self.child.matcher_element)
        ) 
Example #28
Source File: case_parser.py    From psst with MIT License 5 votes vote down vote up
def parse_line(attribute, string):

    Grammar = Suppress(Keyword('mpc.{}'.format(attribute)) + Keyword('=')) + String('data') + Suppress(Literal(';') + Optional(Comments))
    result, i, j = Grammar.scanString(string).next()

    return [int_else_float_except_string(s) for s in result['data'].asList()] 
Example #29
Source File: case_parser.py    From psst with MIT License 5 votes vote down vote up
def parse_table(attribute, string):
    Line = OneOrMore(Float)('data') + Literal(';') + Optional(Comments, default='')('name')
    Grammar = Suppress(Keyword('mpc.{}'.format(attribute)) + Keyword('=') + Keyword('[') + Optional(Comments)) + OneOrMore(Group(Line)) + Suppress(Keyword(']') + Optional(Comments))

    result, i, j = Grammar.scanString(string).next()

    _list = list()
    for r in result:
        _list.append([int_else_float_except_string(s) for s in r['data'].asList()])

    return _list 
Example #30
Source File: preprocessing_parser.py    From rekall with GNU General Public License v2.0 5 votes vote down vote up
def _define_function_like(self):
        return (
            (_IDENTIFIER.setResultsName("name")
             + _OPEN_PARENTHESES).leaveWhitespace()
            + pyparsing.Optional(
                pyparsing.delimitedList(
                    _IDENTIFIER
                    | pyparsing.Literal("...")  # vararg macro.
                )).setResultsName("arguments")
            + _CLOSE_PARENTHESES
            + pyparsing.restOfLine.setResultsName("replacement")
        ).setParseAction(self._add_function_like)