def string_literal(): whitespace() quote = one_of('\'"') commit() st = u''.join(many1(p(string_char, quote))) one_of(quote) return ('str', st)
def parse(cls): one_of('+') pico.one_of_strings('register', 'reg') result = {} @tri def ident(): whitespace1() pico.hash() commit() return many1(any_token) @tri def number(): whitespace1() return many1(partial(one_of, string.digits + ' -+()')) @tri def name(): whitespace1() return pico.name() ident = optional(partial(choice, ident, number), None) if ident is not None: result['ident'] = re.sub('[ \-+()]', '', "".join(ident)) else: name = optional(name, None) if name is not None: result['name'] = name return result
def parse(cls): one_of('+') caseless_string('echo') whitespace() return { 'echo': "".join(remaining()) }
def _ParseParam(self): """Consumes parameter keys for lookup in a parameter dictionary passed in with the query. Parameter keys may consist of letters, digits and underscores (_). Returns the value that the parameter key maps to. """ one_of('{') param_name = ''.join(many_until1(p(one_of, letters + digits + '_'), p(one_of, '}'))[0]) return Parameter(param_name)
def parenthetical(): whitespace() one_of('(') commit() whitespace() v = expression() whitespace() one_of(')') whitespace() return ParentheticalNode(v)
def _ParseParenthetical(self): """Consumes parenthetical expression.""" whitespace() one_of('(') commit() whitespace() node = self._expr_parser() whitespace() one_of(')') whitespace() return Parenthetical(self._schema, node)
def _ParsePhrase(self): """Consumes a key range specification of the form <table>.<column>=<maybe quoted value>. """ whitespace() table = self._token().lower() one_of('.') commit() column = self._token().lower() whitespace() one_of('=') whitespace() phrase = self._param_parser() node = PhraseNode(self._schema, table, column, phrase) whitespace() return node
def parse(cls): one_of('+') caseless_string('epi') aggregates = {} if whitespace(): while peek(): try: code = "".join(pico.one_of_strings(*( tuple(cls.TOKENS) + tuple(cls.ALIAS)))) code = code.upper() except: raise FormatError( "Expected an epidemiological indicator " "such as TB or MA (got: %s)." % \ "".join(remaining())) # rewrite alias code = cls.ALIAS.get(code, code) if code in aggregates: raise FormatError("Duplicate value for %s." % code) whitespace1() try: minus = optional(partial(one_of, '-'), '') value = int("".join([minus]+pico.digits())) except: raise FormatError("Expected a value for %s." % code) if value < 0: raise FormatError("Got %d for %s. You must " "report a positive value." % ( value, cls.TOKENS[code].lower())) aggregates[code] = value many(partial(one_of, ' ,;.')) return { 'aggregates': aggregates }
def parse(): one_of('+') one_of_strings('register', 'reg') result = {} @tri def ident(): whitespace1() one_of('#') commit() result['ident'] = "".join(many1(partial(not_one_of, ','))) @tri def name(): whitespace1() result['name'] = "".join(many1(partial(not_one_of, ','))) optional(partial(choice, ident, name), None) return result
def char_spec_range(): one_of("[") low = char_spec_range_char() one_of('-') high = char_spec_range_char() one_of("]") return partial(satisfies, lambda c: low <= c <= high)
def char_spec_range(): one_of("[") low = char_spec_range_char() one_of("-") high = char_spec_range_char() one_of("]") return partial(satisfies, lambda c: low <= c <= high)
def _ParsePhrase(self): """Reads one attribute col_name=value. Creates a DB update in self._updates. """ col_name = self._token().lower() col_def = self._table.GetColumn(col_name) one_of('=') commit() phrase = self._phrase() if phrase: value = eval(phrase) if col_def.value_type == 'N': value = int(value) if self._raw: self._updates[col_def.key] = db_client.UpdateAttr(value, 'PUT') else: self._updates[col_name] = value else: if self._raw: self._updates[col_def.key] = db_client.UpdateAttr(None, 'DELETE') else: self._updates[col_name] = None return None
def _ParsePhrase(self): """Reads one attribute col_name=value. Creates a DB update in self._updates. """ col_name = self._token().lower() col_def = self._table.GetColumn(col_name) one_of('=') commit() phrase = self._phrase() if phrase: value = eval(phrase) if col_def.value_type == 'N': value = int(value) if self._raw: self._updates[col_def.key] = db_client.UpdateAttr(value, 'PUT') else: self._updates[col_name] = value else: if self._raw: self._updates[col_def.key] = db_client.UpdateAttr( None, 'DELETE') else: self._updates[col_name] = None return None
def bracketed(): one_of('[') v = expression() one_of(']') return ['bracket', v]
def float_value(): whole_part = digits() one_of('.') commit() decimal_part = digits() return float('%s.%s' % (whole_part, decimal_part))
def entity(): one_of('&') ent = choice(named_entity, numeric_entity) one_of(';') return ent
def parse(cls): result = {} prefix = optional(tri(identifier), None) if prefix is not None: result['patient_id'] = "".join(prefix) whitespace() one_of('+') caseless_string('muac') if prefix is None: try: whitespace1() part = optional(tri(identifier), None) if part is not None: result['patient_id'] = "".join(part) else: result['name'] = name() except: raise FormatError("Expected a patient id or name.") if 'name' in result: try: separator() result['sex'] = one_of('MmFf').upper() except: raise FormatError("Expected either M or F to indicate the patient's gender.") try: separator() except: raise FormatError("Expected age or birthdate of patient.") try: result['age'] = choice(*map(tri, (date, timedelta))) except: received, stop = many_until(any_token, comma) raise FormatError("Expected age or birthdate of patient, but " "received %s." % "".join(received)) try: if prefix is None: separator() else: whitespace1() reading = choice( partial(one_of_strings, 'red', 'green', 'yellow', 'r', 'g', 'y'), digits) try: reading = int("".join(reading)) except: reading = reading[0].upper() else: whitespace() unit = optional(partial(one_of_strings, 'mm', 'cm'), None) if unit is None: reading = cls.get_reading_in_mm(reading) elif "".join(unit) == 'cm': reading = reading * 10 result['reading'] = reading except: raise FormatError( "Expected MUAC reading (either green, yellow or red), but " "received %s." % "".join(remaining())) if optional(separator, None): result['tags'] = tags() return result
def braced(): one_of('{') v = expression() one_of('}') return ['brace', v]
def numeric_entity(): one_of("#") return choice(hex_entity, dec_entity)
def hex_entity(): one_of("x") return unichr(hex_value())
def entity(): one_of("&") ent = choice(named_entity, numeric_entity) one_of(";") return ent
def operator_char(): return one_of(operator_chars)
def ident(): whitespace1() one_of('#') commit() result['ident'] = "".join(many1(partial(not_one_of, ',')))
def matched(start, stop, name): one_of(start) v = expression() one_of(stop) return [name, v]
def parse_tracking_id(): return pico.digit() + pico.digit() + \ one_of(TRACKING_ID_LETTERS) + \ one_of(TRACKING_ID_LETTERS) + \ pico.digit() + pico.digit()
def parse(cls): one_of('+') caseless_string('error') raise FormatError("error")
def parse(cls): one_of('+') caseless_string('break')
def numeric_entity(): one_of('#') return choice(hex_entity, dec_entity)
def parse(cls): one_of('+') caseless_string('bad') from django.core.exceptions import ImproperlyConfigured raise ImproperlyConfigured("".join(remaining()))
def hex_entity(): one_of('x') return unichr(hex_value())
def parse(cls): one_of('+') caseless_string('hello')
def parened(): one_of('(') v = expression() one_of(')') return ['paren', v]