def parse(cls): result = {} @tri def ident(): pico.hash() commit() return many1(any_token) @tri def number(): return many1(partial(one_of, string.digits + ' -+()')) ident = optional(partial(choice, ident, number), None) if ident is not None: result['ident'] = re.sub('[ \-+()]', '', "".join(ident)) else: name = optional(tri(pico.name), None) if name is not None: result['name'] = name whitespace() if peek() and not result: raise FormatError( "We did not understand: %s." % "".join(remaining())) return result
def parse(cls): result = {} try: identifiers = optional(tri(pico.ids), None) if identifiers: result['ids'] = [id.upper() for id in identifiers] else: result['name'] = pico.name() except: raise FormatError( "Expected a name, or a patient's health or tracking ID " "(got: %s)." % "".join(remaining())) if 'name' in result: try: many1(partial(one_of, ' ,;')) result['sex'] = pico.one_of_strings( 'male', 'female', 'm', 'f')[0].upper() except: raise FormatError( "Expected the infant's gender " "(\"male\", \"female\", or simply \"m\" or \"f\"), " "but received instead: %s." % "".join(remaining())) try: pico.separator() except: raise FormatError("Expected age or birthdate of patient.") try: result['age'] = choice(*map(tri, (pico.date, pico.timedelta))) except: raise FormatError("Expected age or birthdate of patient, but " "received %s." % "".join(remaining())) return result
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 parse(self, health_id=None): result = {} if health_id is None: try: part = optional(tri(pico.identifier), None) if part is not None: health_id = "".join(part) else: result['name'] = pico.name() except: raise FormatError("Expected a patient id or name.") if 'name' in result: try: pico.separator() result['sex'] = pico.one_of_strings( 'male', 'female', 'm', 'f')[0].upper() except: raise FormatError( "Expected either M or F " \ "to indicate the patient's gender (got: %s)." % "".join(remaining())) try: pico.separator() except: raise FormatError("Expected age or birthdate of patient.") try: result['age'] = choice(*map(tri, (pico.date, pico.timedelta))) except: raise FormatError("Expected age or birthdate of patient, but " "received %s." % "".join( remaining()).split(',')[0]) if health_id is not None: result['health_id'] = health_id try: whitespace() optional(pico.separator, None) reading = choice( partial(pico.one_of_strings, 'red', 'green', 'yellow', 'r', 'g', 'y'), pico.digits) try: reading = int("".join(reading)) except: result['category'] = reading[0].upper() else: whitespace() unit = optional(partial(pico.one_of_strings, 'mm', 'cm'), None) if unit is None: reading = self.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(partial(choice, tri(pico.separator), tri(whitespace)), None): if optional(partial( pico.one_of_strings, 'oedema', 'odema', 'oe'), None): result['oedema'] = True elif peek(): raise FormatError( "Specify \"oedema\" or \"oe\" if the patient shows " "signs of oedema, otherwise leave empty (got: %s)." % \ "".join(remaining())) return result
def make_caseless_literal(s): "returns a literal string, case independant parser." return partial(s, tri(caseless_string), s)
optional(CFWS) # specials = "(" / ")" / ; Special characters that do # "<" / ">" / ; not appear in atext # "[" / "]" / # ":" / ";" / # "@" / "\" / # "," / "." / # DQUOTE specials = partial(one_of, '()<>[]:;@\,."') # Section 3.2.5 Parsers - http://tools.ietf.org/html/rfc5322#section-3.2.5 # word = atom / quoted-string word = partial(choice, tri(atom), quoted_string) # phrase = 1*word / obs-phrase phrase = partial(choice, tri(partial(many1, word)), obs_phrase) # Section 3.4.1 Parsers - http://tools.ietf.org/html/rfc5322#section-3.4.1 # dtext = %d33-90 / ; Printable US-ASCII # %d94-126 / ; characters not including # obs-dtext ; "[", "]", or "\" dtext = partial(choice, partial(char_range, 33, 99), partial(char_range, 94, 126), obs_dtext) # domain-literal = [CFWS] "[" *([FWS] dtext) [FWS] "]" [CFWS] def domain_literal(): optional(CFWS) square()
def make_literal(s): "returns a literal parser" return partial(s, tri(string), s)
def comment(): string("<!--") commit() result, _ = many_until(any_token, tri(partial(string, "-->"))) return "COMMENT", build_string(result)
def prolog(): whitespace() optional(tri(partial(processing, xmldecl)), None) many(partial(choice, processing, comment, whitespace1)) optional(doctype, None) many(partial(choice, processing, comment, whitespace1))