示例#1
0
    def __iter__(self):
        start, fields = self.__schema
        proto = dict((k, dv) for k, (_, dv) in fields.iteritems())
        default_field = (lambda x: x, None)

        def record(rec):
            rec_ = proto.copy()
            rec_.update(rec)
            for fn, err in ifilter(lambda i: isinstance(i[1], ErrorLevel),
                                   rec_.iteritems()):
                if err:
                    self._error(err, err.msg, rec, rec.name, fn)
                    rec_[fn] = None
            return rec_

        def accum(db, m):
            valuator = fields.get(m.name, default_field)
            try:
                field = (m.name, valuator[0](m[0].rstrip() if m else ''))
            except Exception, err:
                self._error(level.Content,
                            err.msg if hasattr(err, 'msg') else unicode(err),
                            m)
                field = (m.name, valuator[1])
            if m.name == start:
                if isinstance(field[1], ErrorLevel):
                    self._error(err, err.msg, m, m.name)
                    field = (m.name, '')
                db.append(sfm.element(field[1], m.pos, content=[field]))
            else:
                db[-1].append(field)
            return db
示例#2
0
 def __iter__(self):
     start,fields = self.__schema
     proto = dict((k,dv) for k,(_,dv) in fields.iteritems())
     default_field = (lambda x:x, None)
     def record(rec):
         rec_ = proto.copy()
         rec_.update(rec)
         fn,err = next(ifilter(lambda i: isinstance(i[1], ErrorLevel),rec_.iteritems()),('',None))
         if err: 
             self._error(err, err.msg, rec, rec.name, fn)
             rec_[fn] = None
         return rec_
     
     def accum(db, m):
         valuator = fields.get(m.name, default_field)
         try:
             field = (m.name, valuator[0](m[0].rstrip() if m else ''))
         except Exception, err:
             self._error(level.Content, err.msg if hasattr(err,'msg') else unicode(err), m)
             field = (m.name, valuator[1])
         if m.name == start:
             if isinstance(field[1], ErrorLevel): 
                 self._error(err, err.msg, m, m.name)
                 field = (m.name, '')
             db.append(sfm.element(field[1], m.pos, content=[field]))
         else:
             db[-1].append(field)
         return db
示例#3
0
    def _ChapterNumber_(self, chapter_marker):
        tok = next(self._tokens)
        chapter = self.numeric_re.match(tok)
        if not chapter:
            self._error(level.Content, 'missing chapter number after \\c',
                        chapter_marker)
            chapter_marker.args = [u'\uFFFD']
        else:
            chapter_marker.args = [
                unicode(tok[chapter.start(1):chapter.end(1)])
            ]
            tok = tok[chapter.end():]
        if tok and not self.sep_re.match(tok):
            self._error(level.Content,
                        'missing space after chapter number \'{chapter}\'',
                        tok,
                        chapter=chapter_marker.args[0])
        tok = tok.lstrip()
        if tok:
            if tok[0] == '\\':
                self._tokens.put_back(tok)
            else:
                self._error(
                    level.Structure,
                    'text cannot follow chapter marker \'{0}\'',
                    tok,
                    chapter_marker,
                )
                chapter_marker.append(
                    sfm.element(None, meta=self.default_meta, content=[tok]))
                tok = None

        return self._default_(chapter_marker)
示例#4
0
def elem(name, *content):
    name, args = (name[0], list(name[1:])) if type(name) == tuple else (name,
                                                                        [])
    e = sfm.element(name,
                    args=args,
                    meta=usfm.default_stylesheet.get(name, {}))
    e.extend(content)
    return e
示例#5
0
 def accum(db, m):
     valuator = fields.get(m.name, default_field)
     try:
         field = (m.name, valuator[0](m[0].rstrip() if m else ''))
     except Exception as err:
         self._error(level.Content,
                     err.msg if hasattr(err, 'msg') else unicode(err),
                     m)
         field = (m.name, valuator[1])
     if m.name == start:
         if isinstance(field[1], ErrorLevel):
             self._error(err.level, err.msg, m, m.name)
             field = (m.name, '')
         db.append(sfm.element(field[1], m.pos, content=[field]))
     else:
         db[-1].append(field)
     return db
示例#6
0
 def _ChapterNumber_(self, chapter_marker):
     tok = next(self._tokens)
     chapter = self.numeric_re.match(tok)
     if not chapter:
         self._error(level.Content, 'missing chapter number after \\c', 
                                  chapter_marker)
         chapter_marker.args = [u'\uFFFD']
     else:
         chapter_marker.args = [unicode(tok[chapter.start(1):chapter.end(1)])]
         tok = tok[chapter.end():]
     if tok and not self.sep_re.match(tok):
         self._error(level.Content, 'missing space after chapter number \'{chapter}\'',
                                 tok, chapter=chapter_marker.args[0])
     tok  = tok.lstrip()
     if tok:
         if tok[0] == '\\': 
             self._tokens.put_back(tok)
         else:
             self._error(level.Structure, 'text cannot follow chapter marker \'{0}\'', tok, chapter_marker, )
             chapter_marker.append(sfm.element(None, meta=self.default_meta, content=[tok]))
             tok = None
             
     return self._default_(chapter_marker)
示例#7
0
class parser(sfm.parser):
    '''
    >>> from pprint import pprint
    >>> import warnings
    >>> doc = """\\Marker toc1
    ...          \\Name toc1 - File - Long Table of Contents Text
    ...          \\OccursUnder h h1 h2 h3
    ...          \\FontSize 12
    ...          \\Bold"""
    >>> with warnings.catch_warnings():
    ...     warnings.simplefilter("ignore")
    ...     pprint(list(parser(doc.splitlines(True), schema('Marker',{}))))
    [{},
     {u'Bold': '',
      u'FontSize': text(u'12'),
      u'Marker': text(u'toc1'),
      u'Name': text(u'toc1 - File - Long Table of Contents Text'),
      u'OccursUnder': text(u'h h1 h2 h3')}]
    >>> demo_schema = schema('Marker', 
    ...     {'Marker' : (str, UnrecoverableError('Start of record marker: {0} missing')),
    ...      'Name'   : (str, StructureError('Marker {0} defintion missing: {1}')),
    ...      'Description'    : (str, ''),
    ...      'OccursUnder'    : (unique(sequence(str)), [None]),
    ...      'FontSize'       : (int,                   None),
    ...      'Bold'           : (flag,                  False)})
    >>> with warnings.catch_warnings():
    ...     warnings.simplefilter("error")
    ...     pprint(list(parser(doc.splitlines(True), demo_schema)))
    [{},
     {'Bold': True,
      'Description': '',
      'FontSize': 12,
      'Marker': 'toc1',
      'Name': 'toc1 - File - Long Table of Contents Text',
      'OccursUnder': set(['h', 'h1', 'h2', 'h3'])}]
    >>> with warnings.catch_warnings():
    ...     warnings.simplefilter("error")
    ...     pprint(list(parser("""
    ... \\Description this goes in the header since it's before the 
    ... key marker Marker as does the following marker.
    ... \\FontSize 15
    ... \\Marker toc1
    ... \\Name toc1 - File - Long Table of Contents Text
    ... \\FontSize 12""".splitlines(True), demo_schema)))
    [{u'Description': "this goes in the header since it's before the \\nkey marker Marker as does the following marker.",
      u'FontSize': 15},
     {'Bold': False,
      'Description': '',
      'FontSize': 12,
      'Marker': 'toc1',
      'Name': 'toc1 - File - Long Table of Contents Text',
      'OccursUnder': [None]}]
    >>> with warnings.catch_warnings():
    ...     warnings.simplefilter("error")
    ...     pprint(list(parser("""\Marker toc1
    ...                           \FontSize 12""".splitlines(True), demo_schema)))
    Traceback (most recent call last):
    ...
    SyntaxError: <string>: line 1,1: Marker toc1 defintion missing: Name
    '''
    def __init__(self, source, schema, error_level=level.Content):
        if not isinstance(schema, _schema):
            raise TypeError('arg 2 must a \'schema\' not {0!r}'.format(schema))
        metas = dict(
            (k, super(parser, self).default_meta) for k in schema.fields)
        super(parser, self).__init__(source, metas, error_level=error_level)
        self.__schema = schema

    def __iter__(self):
        start, fields = self.__schema
        proto = dict((k, dv) for k, (_, dv) in fields.iteritems())
        default_field = (lambda x: x, None)

        def record(rec):
            rec_ = proto.copy()
            rec_.update(rec)
            for fn, err in ifilter(lambda i: isinstance(i[1], ErrorLevel),
                                   rec_.iteritems()):
                if err:
                    self._error(err, err.msg, rec, rec.name, fn)
                    rec_[fn] = None
            return rec_

        def accum(db, m):
            valuator = fields.get(m.name, default_field)
            try:
                field = (m.name, valuator[0](m[0].rstrip() if m else ''))
            except Exception, err:
                self._error(level.Content,
                            err.msg if hasattr(err, 'msg') else unicode(err),
                            m)
                field = (m.name, valuator[1])
            if m.name == start:
                if isinstance(field[1], ErrorLevel):
                    self._error(err, err.msg, m, m.name)
                    field = (m.name, '')
                db.append(sfm.element(field[1], m.pos, content=[field]))
            else:
                db[-1].append(field)
            return db

        es = super(parser, self).__iter__()
        fs = ifilter(lambda v: isinstance(v, sfm.element), es)
        fgs = reduce(accum, fs, [sfm.element('header')])
        return chain((dict(fgs[0]), ), imap(record, fgs[1:]))
示例#8
0
def elem(name, *content):
    name, args = (name[0],list(name[1:])) if type(name) == tuple else (name, [])
    e = sfm.element(name, args=args, meta=usfm.default_stylesheet.get(name,{}))
    e.extend(content)
    return e