Example #1
0
def string_literal():
  whitespace()
  quote = one_of('\'"')
  commit()
  st = u''.join(many1(p(string_char, quote)))
  one_of(quote)
  return ('str', st)
Example #2
0
    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
Example #3
0
File: forms.py Project: malthe/cvs
    def parse(self, keyword=None):
        roles = HealthRole.objects.all()
        by_keyword = dict((role.keyword, role) for role in roles)
        matches = difflib.get_close_matches(keyword.upper(), by_keyword)

        if not matches:
            raise FormatError(
                u"Did not understand the keyword: '%s'." % keyword)

        keyword = matches[0]
        result = {
            'role': by_keyword[keyword.upper()],
            }

        try:
            code = u"".join(pico.digits())
        except:
            raise FormatError(u"Expected an HMIS facility code (got: %s)." %
                             "".join(remaining()))

        try:
            facility = result['facility'] = Facility.objects.filter(code=code).get()
        except Facility.DoesNotExist:
            raise FormatError(u"No such HMIS facility code: %s." % code)

        whitespace()
        optional(pico.separator, None)

        # optionally provide a sub-village group
        name = "".join(remaining()).strip()
        if name:
            # get all (name, location) pairs of all child nodes of
            # groups that report to this facility
            policies = {}
            policy = None
            group = None

            for policy in facility.policies.all().select_related():
                policies[policy.group.name.upper()] = policy
                for descendant in policy.group.get_descendants():

                    try:
                        group_name = descendant.name.upper()
                        policies[group_name] = descendant.reporting_policy
                    except ReportingPolicy.DoesNotExist:
                        pass

            matches = difflib.get_close_matches(name.upper(), policies)
            if matches:
                name = matches[0]
                group = policies[name].group
            elif policy is not None:
                group = policy.group.add_child(
                    slug="added_by_user", name='"%s"' % name)
                facility.policies.create(group=group)

            result['group'] = group

        return result
Example #4
0
def identifier():
  whitespace()
  not_followed_by(p(choice, *[p(reserved, rw) for rw in reserved_words]))
  first = identifier_char1()
  commit()
  rest = many(identifier_char)
  name = u''.join([first] + rest)
  return ('ident', name)
Example #5
0
    def parse(cls):
        one_of('+')
        caseless_string('echo')
        whitespace()

        return {
            'echo': "".join(remaining())
            }
Example #6
0
def end_element(name):
    whitespace()
    string("</")
    commit()
    if name != xml_name():
        fail()
    whitespace()
    close_angle()
Example #7
0
def xmldecl():
    caseless_string("xml")
    whitespace()
    return (
        "xml",
        optional(partial(xmldecl_attr, "version", version_num), "1.0"),
        optional(partial(xmldecl_attr, "standalone", standalone), "yes"),
    )
Example #8
0
def end_element(name):
    whitespace()
    string("</")
    commit()
    if name != xml_name():
        fail()
    whitespace()
    close_angle()
Example #9
0
def number():
  whitespace()
  lead = u''.join(many1(digit))
  commit()
  if optional(p(one_of, '.')):
    trail = u''.join(many1(digit))
    return ('float', float(lead + '.' + trail))
  else:
    return ('int', int(lead))
Example #10
0
def bin_op():
    left = term()
    op = operator()
    commit()
    right = expression()
    whitespace()
    
    n = BinaryNode(left, op)
    return n.merge(right) 
Example #11
0
def bin_op():
    left = term()
    op = operator()
    commit()
    right = expression()
    whitespace()

    n = BinaryNode(left, op)
    return n.merge(right)
Example #12
0
def processing(parser = False):
    parser = parser or compose(build_string, partial(many, partial(not_one_of, '?')))

    string('<?')
    commit()
    result = parser()
    whitespace()
    
    string('?>')
    return result
Example #13
0
def processing(parser=False):
    parser = parser or compose(build_string, partial(many, partial(not_one_of, "?")))

    string("<?")
    commit()
    result = parser()
    whitespace()

    string("?>")
    return result
Example #14
0
 def _ParseOp(self):
   """Consumes one operation, defined by left term and right
   expression, which may be either another term or another ParseOp().
   """
   left = self._term_parser()
   op = self._operator()
   commit()
   right = self._expr_parser()
   whitespace()
   node = self._op_classes[op](self._schema, left)
   return node.Merge(right)
Example #15
0
 def _ParseIndexTerm(self):
   """Consumes an index term. If '_', creates a place-holder node
   (None); otherwise, creates an IndexTermNode.
   """
   whitespace()
   index_term = self._phrase()
   if index_term == '_':
     node = None
   else:
     node = IndexTermNode(
       self._schema, self._table, self._column, index_term)
   whitespace()
   return node
Example #16
0
 def unit():
     whitespace()
     unit = one_of_strings(
         'day',
         'week', 'wk',
         'month', 'mo',
         'year', 'yr',
         'd',
         'w',
         'm',
         'y', )[0]
     optional(partial(one_of, 'sS'), None)
     return unit
Example #17
0
def parenthetical():
    whitespace()
    one_of('(')
    commit()
    whitespace()
    v = expression()
    whitespace()
    one_of(')')
    whitespace()
    return ParentheticalNode(v)
Example #18
0
def parenthetical():
    whitespace()
    one_of('(')
    commit()
    whitespace()
    v = expression()
    whitespace()
    one_of(')')
    whitespace()
    return ParentheticalNode(v)
Example #19
0
 def _ParseParenthetical(self):
   """Consumes parenthetical expression."""
   whitespace()
   one_of('(')
   commit()
   whitespace()
   node = self._expr_parser()
   whitespace()
   one_of(')')
   whitespace()
   return Parenthetical(self._schema, node)
Example #20
0
    def parse(cls):
        one_of('+')
        caseless_string('epi')

        aggregates = {}

        if whitespace():
            while peek():
                try:
                    code = "".join(one_of_strings(*(tuple(cls.TOKENS) + tuple(cls.ALIAS))))
                    code = code.upper()
                except:
                    raise FormatError(
                        "Expected an epidemiological indicator "
                        "such as TB or MA.")

                # 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]+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
                whitespace()

        return {
            'aggregates': aggregates
            }
Example #21
0
 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
Example #22
0
def xmldecl():
    caseless_string('xml')
    whitespace()
    return ('xml', optional(partial(xmldecl_attr, 'version', version_num), "1.0"), 
                   optional(partial(xmldecl_attr, 'standalone', standalone), "yes"))
Example #23
0
    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
Example #24
0
File: forms.py Project: malthe/cvs
    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
Example #25
0
def prolog():
    whitespace()
    optional(tri(partial(processing, xmldecl)), None)
    many(partial(choice, processing, comment, whitespace1))
    optional(doctype, None)
    many(partial(choice, processing, comment, whitespace1))
Example #26
0
def reserved_op(name):
  assert name in reserved_operators
  whitespace()
  string(name)
  not_followed_by(operator_char)
  return name
Example #27
0
File: forms.py Project: malthe/cvs
    def parse(cls, keyword=None, keywords=None):
        if keywords is None:
            keywords = cls.KEYWORDS

        slug = keywords[keyword.lower()]
        kind = ReportKind.objects.get(slug=slug)

        observations = {}
        result = {
            'observations': observations,
            'kind': kind,
            }

        total = "".join(optional(pico.digits, ()))
        if total:
            result['total'] = int(total)
            many1(partial(one_of, ' ,;'))

        kinds = ObservationKind.objects.filter(slug__startswith="%s_" % slug).all()
        observation_kinds = dict((kind.slug, kind) for kind in kinds)
        codes = [kind.abbr for kind in kinds if kind.abbr]

        # we allow both the observation kinds and any aliases
        allowed_codes = tuple(codes) + tuple(cls.ALIASES)

        while peek():
            # look up observation kinds that double as user input
            # for the aggregate codes
            try:
                code = "".join(pico.one_of_strings(*allowed_codes)).lower()
            except:
                raise FormatError(
                    "Expected an indicator code "
                    "such as %s (got: %s)." % (
                        " or ".join(map(unicode.upper, codes[:2])),
                        "".join(remaining()).strip() or u"nothing"))

            # rewrite alias if required, then look up kind
            munged= "%s_%s" % (slug, code)
            munged = cls.ALIASES.get(munged, munged)
            kind = observation_kinds[munged]

            # guard against duplicate entries
            if kind.slug in observations:
                raise FormatError("Duplicate value for %s." % code)

            whitespace()

            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, kind.name))

            observations[kind.slug] = value
            many(partial(one_of, ' ,;.'))

        return result
Example #28
0
def operator():
  whitespace()
  not_followed_by(p(choice, *[p(reserved_op, op) for op in reserved_operators]))
  name = u''.join(many1(operator_char))
  return ('op', name)
Example #29
0
def special(name):
  whitespace()
  string(name)
  return name
Example #30
0
def program():
  prog = many(program_part)
  whitespace()
  eof()
  return ('prog', prog)
Example #31
0
def reserved(name):
  assert name in reserved_words
  whitespace()
  string(name)
  not_followed_by(identifier_char)
  return name
Example #32
0
def prolog():
    whitespace()
    optional(tri(partial(processing, xmldecl)), None)
    many(partial(choice, processing, comment, whitespace1))
    optional(doctype, None)    
    many(partial(choice, processing, comment, whitespace1))