コード例 #1
0
def test_load_log_plural():
    # create the string
    stringified = (
        '2014-08-01 20:27:44 Friday - activation - '
            '["life#00000", "days#00001", '
            '"day#Nf01s: July 31, 2014 (Thursday, yesterday)"]\n'
        '2044-08-01 20:27:44 Monday - activation - '
            '["life#00000", "days#00001", '
            '"day#Vdl4d: July 01, 2044 (Thursday, in a long time)"]\n'
    )
    # load the string
    log = load_log(stringified)
    # check the result
    assert log == [
        (
            [
                (u'00000', u'life', None),
                (u'00001', u'days', None),
                (u'Nf01s', u'day', u'July 31, 2014 (Thursday, yesterday)')
            ],
            u'activation',
            datetime.datetime(2014, 8, 1, 20, 27, 44)
        ),
        (
            [
                (u'00000', u'life', None),
                (u'00001', u'days', None),
                (u'Vdl4d', u'day', u'July 01, 2044 (Thursday, in a long time)')
            ],
            u'activation',
            datetime.datetime(2044, 8, 1, 20, 27, 44)
        )
    ]
コード例 #2
0
ファイル: tracker.py プロジェクト: kidaa/tree-of-life
    def deserialize(self, files):
        """
        files is a dictionary of filename to filedata.

        deserialize must allow for any key to be missing. If a file is not
        present in that dictionary, it should be like an empty string in that
        dictionary.
        """
        self.root = root = self.roottype(self, self.nodecreator,
                loading_in_progress=True)
        root.loading_in_progress = True

        log_data = files.get('log', u'')
        self.root.log = file_storage.load_log(log_data)

        stack = []
        lastnode = root
        lastindent = -1
        metadata_allowed_here = False

        error_context = ErrorContext()  # mutable thingy

        try:
            life_data = files.get('life', u'')
            parser = file_storage.parse_string(life_data)
            parser.error_context = error_context
            for indent, is_metadata, nodeid, node_type, text in parser:
                if node_type == "":
                    indent = lastindent
                    if lastnode is not None and lastnode.node_type != "":
                        indent += 1
                if indent > lastindent:
                    if indent > lastindent + 1:
                        raise LoadError("indented too far")
                    stack.append(lastnode)
                    metadata_allowed_here = True
                elif indent < lastindent:
                    stack = stack[:int(indent) + 1]
                lastindent = indent

                parent = stack[-1]

                if is_metadata:
                    if not metadata_allowed_here:
                        raise LoadError('metadata in the wrong place')
                    parent.setoption(node_type, text)
                    lastnode = None
                else:
                    if node_type != "-":
                        metadata_allowed_here = False

                    node = self.nodecreator.create(node_type, text, parent,
                            nodeid=nodeid)
                    if node is not None:
                        parent.addchild(node)
                    lastnode = node
        except LoadError as e:
            e.error_context = error_context
            raise
        except Exception as e:
            new_e = LoadError("UNHANDLED ERROR:\n %s" % traceback.format_exc())
            new_e.error_context = error_context
            raise new_e

        if self.make_skeleton:
            root.make_skeleton()

        root.load_finished()
        for depth, node in root.iter_flat_children():
            node.load_finished()

        # enable instant load_finished() on node creation
        root.loading_in_progress = False