mercurial/dagparser.py
author Saurabh Singh <singhsrb@fb.com>
Fri, 01 Sep 2017 12:34:36 -0700
changeset 34085 e8a7c1a0565a
parent 33716 c91013452b33
child 34131 0fa781320203
permissions -rw-r--r--
cmdutil: remove the redundant commit during amend There was an extra commit made during the amend operation to track the changes to the working copy. However, this logic was written a long time back and newer API's make this extra commit redundant. Therefore, I am removing the extra commit. After this change, I noticed that - Execution time of the cmdutil.amend improved by over 40%. - Execution time of "hg commit --amend" improved by over 20%. Test Plan: I ensured that the all the hg tests passed after the change. I had to fix a few tests which were aware of the extra commit made during the amend. Differential Revision: https://phab.mercurial-scm.org/D636
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11335
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
     1
# dagparser.py - parser and generator for concise description of DAGs
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
     2
#
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
     3
# Copyright 2010 Peter Arrenbrecht <peter@arrenbrecht.ch>
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
     4
#
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
     7
25941
a75cda2dfc19 dagparser: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25170
diff changeset
     8
from __future__ import absolute_import
a75cda2dfc19 dagparser: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25170
diff changeset
     9
a75cda2dfc19 dagparser: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25170
diff changeset
    10
import re
a75cda2dfc19 dagparser: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25170
diff changeset
    11
import string
a75cda2dfc19 dagparser: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25170
diff changeset
    12
a75cda2dfc19 dagparser: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25170
diff changeset
    13
from .i18n import _
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25941
diff changeset
    14
from . import error
11335
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    15
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    16
def parsedag(desc):
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    17
    '''parses a DAG from a concise textual description; generates events
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    18
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    19
    "+n" is a linear run of n nodes based on the current default parent
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    20
    "." is a single node based on the current default parent
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    21
    "$" resets the default parent to -1 (implied at the start);
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    22
        otherwise the default parent is always the last node created
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    23
    "<p" sets the default parent to the backref p
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    24
    "*p" is a fork at parent p, where p is a backref
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    25
    "*p1/p2/.../pn" is a merge of parents p1..pn, where the pi are backrefs
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    26
    "/p2/.../pn" is a merge of the preceding node and p2..pn
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    27
    ":name" defines a label for the preceding node; labels can be redefined
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    28
    "@text" emits an annotation event for text
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    29
    "!command" emits an action event for the current node
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    30
    "!!my command\n" is like "!", but to the end of the line
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    31
    "#...\n" is a comment up to the end of the line
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    32
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    33
    Whitespace between the above elements is ignored.
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    34
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    35
    A backref is either
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    36
     * a number n, which references the node curr-n, where curr is the current
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    37
       node, or
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    38
     * the name of a label you placed earlier using ":name", or
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    39
     * empty to denote the default parent.
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    40
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    41
    All string valued-elements are either strictly alphanumeric, or must
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    42
    be enclosed in double quotes ("..."), with "\" as escape character.
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    43
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    44
    Generates sequence of
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    45
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    46
      ('n', (id, [parentids])) for node creation
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    47
      ('l', (id, labelname)) for labels on nodes
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    48
      ('a', text) for annotations
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    49
      ('c', command) for actions (!)
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    50
      ('C', command) for line actions (!!)
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    51
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    52
    Examples
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    53
    --------
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    54
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    55
    Example of a complex graph (output not shown for brevity):
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    56
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    57
        >>> len(list(parsedag("""
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    58
        ...
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    59
        ... +3         # 3 nodes in linear run
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    60
        ... :forkhere  # a label for the last of the 3 nodes from above
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    61
        ... +5         # 5 more nodes on one branch
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    62
        ... :mergethis # label again
17500
8ac8db8dc346 en-us: labeled
timeless@mozdev.org
parents: 16683
diff changeset
    63
        ... <forkhere  # set default parent to labeled fork node
11335
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    64
        ... +10        # 10 more nodes on a parallel branch
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    65
        ... @stable    # following nodes will be annotated as "stable"
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    66
        ... +5         # 5 nodes in stable
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    67
        ... !addfile   # custom command; could trigger new file in next node
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    68
        ... +2         # two more nodes
17500
8ac8db8dc346 en-us: labeled
timeless@mozdev.org
parents: 16683
diff changeset
    69
        ... /mergethis # merge last node with labeled node
11335
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    70
        ... +4         # 4 more nodes descending from merge node
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    71
        ...
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    72
        ... """)))
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    73
        34
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    74
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    75
    Empty list:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    76
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    77
        >>> list(parsedag(""))
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    78
        []
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    79
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    80
    A simple linear run:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    81
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    82
        >>> list(parsedag("+3"))
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    83
        [('n', (0, [-1])), ('n', (1, [0])), ('n', (2, [1]))]
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    84
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    85
    Some non-standard ways to define such runs:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    86
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    87
        >>> list(parsedag("+1+2"))
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    88
        [('n', (0, [-1])), ('n', (1, [0])), ('n', (2, [1]))]
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    89
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    90
        >>> list(parsedag("+1*1*"))
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    91
        [('n', (0, [-1])), ('n', (1, [0])), ('n', (2, [1]))]
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    92
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    93
        >>> list(parsedag("*"))
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    94
        [('n', (0, [-1]))]
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    95
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    96
        >>> list(parsedag("..."))
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    97
        [('n', (0, [-1])), ('n', (1, [0])), ('n', (2, [1]))]
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    98
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    99
    A fork and a join, using numeric back references:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   100
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   101
        >>> list(parsedag("+2*2*/2"))
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   102
        [('n', (0, [-1])), ('n', (1, [0])), ('n', (2, [0])), ('n', (3, [2, 1]))]
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   103
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   104
        >>> list(parsedag("+2<2+1/2"))
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   105
        [('n', (0, [-1])), ('n', (1, [0])), ('n', (2, [0])), ('n', (3, [2, 1]))]
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   106
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   107
    Placing a label:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   108
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   109
        >>> list(parsedag("+1 :mylabel +1"))
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   110
        [('n', (0, [-1])), ('l', (0, 'mylabel')), ('n', (1, [0]))]
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   111
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   112
    An empty label (silly, really):
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   113
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   114
        >>> list(parsedag("+1:+1"))
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   115
        [('n', (0, [-1])), ('l', (0, '')), ('n', (1, [0]))]
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   116
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   117
    Fork and join, but with labels instead of numeric back references:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   118
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   119
        >>> list(parsedag("+1:f +1:p2 *f */p2"))
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   120
        [('n', (0, [-1])), ('l', (0, 'f')), ('n', (1, [0])), ('l', (1, 'p2')),
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   121
         ('n', (2, [0])), ('n', (3, [2, 1]))]
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   122
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   123
        >>> list(parsedag("+1:f +1:p2 <f +1 /p2"))
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   124
        [('n', (0, [-1])), ('l', (0, 'f')), ('n', (1, [0])), ('l', (1, 'p2')),
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   125
         ('n', (2, [0])), ('n', (3, [2, 1]))]
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   126
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   127
    Restarting from the root:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   128
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   129
        >>> list(parsedag("+1 $ +1"))
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   130
        [('n', (0, [-1])), ('n', (1, [-1]))]
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   131
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   132
    Annotations, which are meant to introduce sticky state for subsequent nodes:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   133
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   134
        >>> list(parsedag("+1 @ann +1"))
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   135
        [('n', (0, [-1])), ('a', 'ann'), ('n', (1, [0]))]
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   136
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   137
        >>> list(parsedag('+1 @"my annotation" +1'))
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   138
        [('n', (0, [-1])), ('a', 'my annotation'), ('n', (1, [0]))]
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   139
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   140
    Commands, which are meant to operate on the most recently created node:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   141
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   142
        >>> list(parsedag("+1 !cmd +1"))
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   143
        [('n', (0, [-1])), ('c', 'cmd'), ('n', (1, [0]))]
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   144
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   145
        >>> list(parsedag('+1 !"my command" +1'))
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   146
        [('n', (0, [-1])), ('c', 'my command'), ('n', (1, [0]))]
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   147
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   148
        >>> list(parsedag('+1 !!my command line\\n +1'))
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   149
        [('n', (0, [-1])), ('C', 'my command line'), ('n', (1, [0]))]
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   150
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   151
    Comments, which extend to the end of the line:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   152
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   153
        >>> list(parsedag('+1 # comment\\n+1'))
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   154
        [('n', (0, [-1])), ('n', (1, [0]))]
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   155
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   156
    Error:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   157
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   158
        >>> try: list(parsedag('+1 bad'))
33716
c91013452b33 dagparser: make print statement in doctest Py3 portable
Augie Fackler <augie@google.com>
parents: 26587
diff changeset
   159
        ... except Exception, e: print(e)
11335
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   160
        invalid character in dag description: bad...
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   161
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   162
    '''
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   163
    if not desc:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   164
        return
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   165
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   166
    wordchars = string.ascii_letters + string.digits
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   167
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   168
    labels = {}
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   169
    p1 = -1
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   170
    r = 0
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   171
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   172
    def resolve(ref):
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   173
        if not ref:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   174
            return p1
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   175
        elif ref[0] in string.digits:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   176
            return r - int(ref)
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   177
        else:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   178
            return labels[ref]
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   179
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   180
    chiter = (c for c in desc)
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   181
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   182
    def nextch():
25170
c69f4f7fe01a dagparser: use 'next' instead of try/except for default value
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 17500
diff changeset
   183
        return next(chiter, '\0')
11335
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   184
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   185
    def nextrun(c, allow):
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   186
        s = ''
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   187
        while c in allow:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   188
            s += c
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   189
            c = nextch()
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   190
        return c, s
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   191
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   192
    def nextdelimited(c, limit, escape):
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   193
        s = ''
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   194
        while c != limit:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   195
            if c == escape:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   196
                c = nextch()
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   197
            s += c
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   198
            c = nextch()
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   199
        return nextch(), s
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   200
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   201
    def nextstring(c):
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   202
        if c == '"':
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   203
            return nextdelimited(nextch(), '"', '\\')
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   204
        else:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   205
            return nextrun(c, wordchars)
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   206
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   207
    c = nextch()
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   208
    while c != '\0':
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   209
        while c in string.whitespace:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   210
            c = nextch()
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   211
        if c == '.':
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   212
            yield 'n', (r, [p1])
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   213
            p1 = r
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   214
            r += 1
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   215
            c = nextch()
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   216
        elif c == '+':
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   217
            c, digs = nextrun(nextch(), string.digits)
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   218
            n = int(digs)
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   219
            for i in xrange(0, n):
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   220
                yield 'n', (r, [p1])
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   221
                p1 = r
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   222
                r += 1
12387
4f8067c94729 cleanup: use x in (a, b) instead of x == a or x == b
Brodie Rao <brodie@bitheap.org>
parents: 12134
diff changeset
   223
        elif c in '*/':
11335
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   224
            if c == '*':
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   225
                c = nextch()
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   226
            c, pref = nextstring(c)
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   227
            prefs = [pref]
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   228
            while c == '/':
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   229
                c, pref = nextstring(nextch())
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   230
                prefs.append(pref)
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   231
            ps = [resolve(ref) for ref in prefs]
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   232
            yield 'n', (r, ps)
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   233
            p1 = r
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   234
            r += 1
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   235
        elif c == '<':
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   236
            c, ref = nextstring(nextch())
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   237
            p1 = resolve(ref)
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   238
        elif c == ':':
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   239
            c, name = nextstring(nextch())
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   240
            labels[name] = p1
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   241
            yield 'l', (p1, name)
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   242
        elif c == '@':
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   243
            c, text = nextstring(nextch())
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   244
            yield 'a', text
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   245
        elif c == '!':
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   246
            c = nextch()
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   247
            if c == '!':
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   248
                cmd = ''
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   249
                c = nextch()
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   250
                while c not in '\n\r\0':
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   251
                    cmd += c
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   252
                    c = nextch()
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   253
                yield 'C', cmd
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   254
            else:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   255
                c, cmd = nextstring(c)
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   256
                yield 'c', cmd
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   257
        elif c == '#':
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   258
            while c not in '\n\r\0':
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   259
                c = nextch()
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   260
        elif c == '$':
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   261
            p1 = -1
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   262
            c = nextch()
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   263
        elif c == '\0':
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   264
            return # in case it was preceded by whitespace
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   265
        else:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   266
            s = ''
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   267
            i = 0
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   268
            while c != '\0' and i < 10:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   269
                s += c
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   270
                i += 1
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   271
                c = nextch()
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25941
diff changeset
   272
            raise error.Abort(_('invalid character in dag description: '
16683
525fdb738975 cleanup: eradicate long lines
Brodie Rao <brodie@sf.io>
parents: 12387
diff changeset
   273
                               '%s...') % s)
11335
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   274
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   275
def dagtextlines(events,
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   276
                 addspaces=True,
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   277
                 wraplabels=False,
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   278
                 wrapannotations=False,
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   279
                 wrapcommands=False,
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   280
                 wrapnonlinear=False,
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   281
                 usedots=False,
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   282
                 maxlinewidth=70):
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   283
    '''generates single lines for dagtext()'''
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   284
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   285
    def wrapstring(text):
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   286
        if re.match("^[0-9a-z]*$", text):
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   287
            return text
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   288
        return '"' + text.replace('\\', '\\\\').replace('"', '\"') + '"'
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   289
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   290
    def gen():
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   291
        labels = {}
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   292
        run = 0
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   293
        wantr = 0
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   294
        needroot = False
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   295
        for kind, data in events:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   296
            if kind == 'n':
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   297
                r, ps = data
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   298
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   299
                # sanity check
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   300
                if r != wantr:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25941
diff changeset
   301
                    raise error.Abort(_("expected id %i, got %i") % (wantr, r))
11335
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   302
                if not ps:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   303
                    ps = [-1]
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   304
                else:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   305
                    for p in ps:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   306
                        if p >= r:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25941
diff changeset
   307
                            raise error.Abort(_("parent id %i is larger than "
12134
26d86a6cf2af dagparser: translate and lowercase error messages
Martin Geisler <mg@aragost.com>
parents: 11879
diff changeset
   308
                                               "current id %i") % (p, r))
11335
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   309
                wantr += 1
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   310
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   311
                # new root?
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   312
                p1 = r - 1
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   313
                if len(ps) == 1 and ps[0] == -1:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   314
                    if needroot:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   315
                        if run:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   316
                            yield '+' + str(run)
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   317
                            run = 0
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   318
                        if wrapnonlinear:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   319
                            yield '\n'
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   320
                        yield '$'
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   321
                        p1 = -1
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   322
                    else:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   323
                        needroot = True
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   324
                if len(ps) == 1 and ps[0] == p1:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   325
                    if usedots:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   326
                        yield "."
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   327
                    else:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   328
                        run += 1
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   329
                else:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   330
                    if run:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   331
                        yield '+' + str(run)
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   332
                        run = 0
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   333
                    if wrapnonlinear:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   334
                        yield '\n'
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   335
                    prefs = []
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   336
                    for p in ps:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   337
                        if p == p1:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   338
                            prefs.append('')
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   339
                        elif p in labels:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   340
                            prefs.append(labels[p])
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   341
                        else:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   342
                            prefs.append(str(r - p))
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   343
                    yield '*' + '/'.join(prefs)
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   344
            else:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   345
                if run:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   346
                    yield '+' + str(run)
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   347
                    run = 0
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   348
                if kind == 'l':
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   349
                    rid, name = data
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   350
                    labels[rid] = name
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   351
                    yield ':' + name
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   352
                    if wraplabels:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   353
                        yield '\n'
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   354
                elif kind == 'c':
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   355
                    yield '!' + wrapstring(data)
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   356
                    if wrapcommands:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   357
                        yield '\n'
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   358
                elif kind == 'C':
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   359
                    yield '!!' + data
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   360
                    yield '\n'
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   361
                elif kind == 'a':
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   362
                    if wrapannotations:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   363
                        yield '\n'
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   364
                    yield '@' + wrapstring(data)
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   365
                elif kind == '#':
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   366
                    yield '#' + data
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   367
                    yield '\n'
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   368
                else:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25941
diff changeset
   369
                    raise error.Abort(_("invalid event type in dag: %s")
11344
32a9744acf1e dagparser: replace format with str
Martin Geisler <mg@aragost.com>
parents: 11335
diff changeset
   370
                                     % str((type, data)))
11335
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   371
        if run:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   372
            yield '+' + str(run)
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   373
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   374
    line = ''
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   375
    for part in gen():
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   376
        if part == '\n':
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   377
            if line:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   378
                yield line
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   379
                line = ''
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   380
        else:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   381
            if len(line) + len(part) >= maxlinewidth:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   382
                yield line
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   383
                line = ''
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   384
            elif addspaces and line and part != '.':
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   385
                line += ' '
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   386
            line += part
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   387
    if line:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   388
        yield line
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   389
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   390
def dagtext(dag,
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   391
            addspaces=True,
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   392
            wraplabels=False,
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   393
            wrapannotations=False,
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   394
            wrapcommands=False,
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   395
            wrapnonlinear=False,
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   396
            usedots=False,
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   397
            maxlinewidth=70):
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   398
    '''generates lines of a textual representation for a dag event stream
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   399
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   400
    events should generate what parsedag() does, so:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   401
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   402
      ('n', (id, [parentids])) for node creation
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   403
      ('l', (id, labelname)) for labels on nodes
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   404
      ('a', text) for annotations
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   405
      ('c', text) for commands
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   406
      ('C', text) for line commands ('!!')
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   407
      ('#', text) for comment lines
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   408
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   409
    Parent nodes must come before child nodes.
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   410
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   411
    Examples
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   412
    --------
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   413
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   414
    Linear run:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   415
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   416
        >>> dagtext([('n', (0, [-1])), ('n', (1, [0]))])
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   417
        '+2'
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   418
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   419
    Two roots:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   420
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   421
        >>> dagtext([('n', (0, [-1])), ('n', (1, [-1]))])
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   422
        '+1 $ +1'
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   423
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   424
    Fork and join:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   425
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   426
        >>> dagtext([('n', (0, [-1])), ('n', (1, [0])), ('n', (2, [0])),
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   427
        ...          ('n', (3, [2, 1]))])
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   428
        '+2 *2 */2'
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   429
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   430
    Fork and join with labels:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   431
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   432
        >>> dagtext([('n', (0, [-1])), ('l', (0, 'f')), ('n', (1, [0])),
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   433
        ...          ('l', (1, 'p2')), ('n', (2, [0])), ('n', (3, [2, 1]))])
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   434
        '+1 :f +1 :p2 *f */p2'
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   435
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   436
    Annotations:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   437
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   438
        >>> dagtext([('n', (0, [-1])), ('a', 'ann'), ('n', (1, [0]))])
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   439
        '+1 @ann +1'
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   440
16683
525fdb738975 cleanup: eradicate long lines
Brodie Rao <brodie@sf.io>
parents: 12387
diff changeset
   441
        >>> dagtext([('n', (0, [-1])),
525fdb738975 cleanup: eradicate long lines
Brodie Rao <brodie@sf.io>
parents: 12387
diff changeset
   442
        ...          ('a', 'my annotation'),
525fdb738975 cleanup: eradicate long lines
Brodie Rao <brodie@sf.io>
parents: 12387
diff changeset
   443
        ...          ('n', (1, [0]))])
11335
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   444
        '+1 @"my annotation" +1'
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   445
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   446
    Commands:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   447
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   448
        >>> dagtext([('n', (0, [-1])), ('c', 'cmd'), ('n', (1, [0]))])
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   449
        '+1 !cmd +1'
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   450
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   451
        >>> dagtext([('n', (0, [-1])), ('c', 'my command'), ('n', (1, [0]))])
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   452
        '+1 !"my command" +1'
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   453
16683
525fdb738975 cleanup: eradicate long lines
Brodie Rao <brodie@sf.io>
parents: 12387
diff changeset
   454
        >>> dagtext([('n', (0, [-1])),
525fdb738975 cleanup: eradicate long lines
Brodie Rao <brodie@sf.io>
parents: 12387
diff changeset
   455
        ...          ('C', 'my command line'),
525fdb738975 cleanup: eradicate long lines
Brodie Rao <brodie@sf.io>
parents: 12387
diff changeset
   456
        ...          ('n', (1, [0]))])
11335
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   457
        '+1 !!my command line\\n+1'
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   458
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   459
    Comments:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   460
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   461
        >>> dagtext([('n', (0, [-1])), ('#', ' comment'), ('n', (1, [0]))])
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   462
        '+1 # comment\\n+1'
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   463
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   464
        >>> dagtext([])
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   465
        ''
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   466
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   467
    Combining parsedag and dagtext:
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   468
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   469
        >>> dagtext(parsedag('+1 :f +1 :p2 *f */p2'))
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   470
        '+1 :f +1 :p2 *f */p2'
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   471
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   472
    '''
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   473
    return "\n".join(dagtextlines(dag,
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   474
                                  addspaces,
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   475
                                  wraplabels,
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   476
                                  wrapannotations,
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   477
                                  wrapcommands,
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   478
                                  wrapnonlinear,
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   479
                                  usedots,
3201ff1459dd dagparser: parses and formats DAGs as concise text
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   480
                                  maxlinewidth))