Mercurial > p > roundup > code
annotate roundup/mlink_expr.py @ 7800:2d4684e4702d
fix: enhancement to history command output and % template fix.
Rather than using the key field, use the label field for descriptions.
Call cls.labelprop(default_to_id=True) so it returns id rather than
the first sorted property name.
If labelprop() returns 'id' or 'title', we return nothing. 'id' means
there is no label set and no properties named 'name' or 'title'. So
have the caller do whatever it wants (prepend classname for example)
when there is no human readable name. This prevents %(name)s%(key)s
from producing: 23(23).
Also don't accept the 'title' property. Titles can be too
long. Arguably we could: '%(name)20s' to limit the title
length. However without ellipses or something truncating the title
might be confusing. So again pretend there is no human readable name.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Tue, 12 Mar 2024 11:52:17 -0400 |
| parents | 87af08c75695 |
| children | 741ea8a86012 |
| rev | line source |
|---|---|
|
6401
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
1 # |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
2 # Copyright: 2010 Intevation GmbH. |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
3 # 2021 Ralf Schlatterbeck, rsc@runtux.com. |
|
6960
7b090bb35c25
flake8 fixes - exception handling changed
John Rouillard <rouilj@ieee.org>
parents:
6412
diff
changeset
|
4 # |
|
6401
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
5 |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
6 # This module is Free Software under the Roundup licensing, |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
7 # see the COPYING.txt file coming with Roundup. |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
8 # |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
9 |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
10 class Binary: |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
11 |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
12 def __init__(self, x, y): |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
13 self.x = x |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
14 self.y = y |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
15 |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
16 def visit(self, visitor): |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
17 self.x.visit(visitor) |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
18 self.y.visit(visitor) |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
19 |
|
6960
7b090bb35c25
flake8 fixes - exception handling changed
John Rouillard <rouilj@ieee.org>
parents:
6412
diff
changeset
|
20 |
|
6401
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
21 class Unary: |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
22 |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
23 def __init__(self, x): |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
24 self.x = x |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
25 |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
26 def generate(self, atom): |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
27 return atom(self) |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
28 |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
29 def visit(self, visitor): |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
30 self.x.visit(visitor) |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
31 |
|
6960
7b090bb35c25
flake8 fixes - exception handling changed
John Rouillard <rouilj@ieee.org>
parents:
6412
diff
changeset
|
32 |
|
6401
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
33 class Equals(Unary): |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
34 |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
35 def evaluate(self, v): |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
36 return self.x in v |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
37 |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
38 def visit(self, visitor): |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
39 visitor(self) |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
40 |
|
6960
7b090bb35c25
flake8 fixes - exception handling changed
John Rouillard <rouilj@ieee.org>
parents:
6412
diff
changeset
|
41 |
|
6401
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
42 class Empty(Unary): |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
43 |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
44 def evaluate(self, v): |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
45 return not v |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
46 |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
47 def visit(self, visitor): |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
48 visitor(self) |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
49 |
|
6960
7b090bb35c25
flake8 fixes - exception handling changed
John Rouillard <rouilj@ieee.org>
parents:
6412
diff
changeset
|
50 |
|
6401
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
51 class Not(Unary): |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
52 |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
53 def evaluate(self, v): |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
54 return not self.x.evaluate(v) |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
55 |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
56 def generate(self, atom): |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
57 return "NOT(%s)" % self.x.generate(atom) |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
58 |
|
6960
7b090bb35c25
flake8 fixes - exception handling changed
John Rouillard <rouilj@ieee.org>
parents:
6412
diff
changeset
|
59 |
|
6401
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
60 class Or(Binary): |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
61 |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
62 def evaluate(self, v): |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
63 return self.x.evaluate(v) or self.y.evaluate(v) |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
64 |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
65 def generate(self, atom): |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
66 return "(%s)OR(%s)" % ( |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
67 self.x.generate(atom), |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
68 self.y.generate(atom)) |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
69 |
|
6960
7b090bb35c25
flake8 fixes - exception handling changed
John Rouillard <rouilj@ieee.org>
parents:
6412
diff
changeset
|
70 |
|
6401
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
71 class And(Binary): |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
72 |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
73 def evaluate(self, v): |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
74 return self.x.evaluate(v) and self.y.evaluate(v) |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
75 |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
76 def generate(self, atom): |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
77 return "(%s)AND(%s)" % ( |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
78 self.x.generate(atom), |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
79 self.y.generate(atom)) |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
80 |
|
6960
7b090bb35c25
flake8 fixes - exception handling changed
John Rouillard <rouilj@ieee.org>
parents:
6412
diff
changeset
|
81 |
|
6401
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
82 def compile_expression(opcodes): |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
83 |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
84 stack = [] |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
85 push, pop = stack.append, stack.pop |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
86 for opcode in opcodes: |
|
6960
7b090bb35c25
flake8 fixes - exception handling changed
John Rouillard <rouilj@ieee.org>
parents:
6412
diff
changeset
|
87 if opcode == -1: push(Empty(opcode)) # noqa: E271,E701 |
|
7b090bb35c25
flake8 fixes - exception handling changed
John Rouillard <rouilj@ieee.org>
parents:
6412
diff
changeset
|
88 elif opcode == -2: push(Not(pop())) # noqa: E701 |
|
7b090bb35c25
flake8 fixes - exception handling changed
John Rouillard <rouilj@ieee.org>
parents:
6412
diff
changeset
|
89 elif opcode == -3: push(And(pop(), pop())) # noqa: E701 |
|
7b090bb35c25
flake8 fixes - exception handling changed
John Rouillard <rouilj@ieee.org>
parents:
6412
diff
changeset
|
90 elif opcode == -4: push(Or(pop(), pop())) # noqa: E701 |
|
7b090bb35c25
flake8 fixes - exception handling changed
John Rouillard <rouilj@ieee.org>
parents:
6412
diff
changeset
|
91 else: push(Equals(opcode)) # noqa: E701 |
|
6401
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
92 |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
93 return pop() |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
94 |
|
6960
7b090bb35c25
flake8 fixes - exception handling changed
John Rouillard <rouilj@ieee.org>
parents:
6412
diff
changeset
|
95 |
|
6401
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
96 class Expression: |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
97 |
|
6412
a0c0ee3ed8b1
Tests for Link expressions
Ralf Schlatterbeck <rsc@runtux.com>
parents:
6401
diff
changeset
|
98 def __init__(self, v, is_link=False): |
|
6401
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
99 try: |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
100 opcodes = [int(x) for x in v] |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
101 if min(opcodes) >= -1: |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
102 raise ValueError() |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
103 |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
104 compiled = compile_expression(opcodes) |
|
6412
a0c0ee3ed8b1
Tests for Link expressions
Ralf Schlatterbeck <rsc@runtux.com>
parents:
6401
diff
changeset
|
105 if is_link: |
|
a0c0ee3ed8b1
Tests for Link expressions
Ralf Schlatterbeck <rsc@runtux.com>
parents:
6401
diff
changeset
|
106 self.evaluate = lambda x: compiled.evaluate( |
|
a0c0ee3ed8b1
Tests for Link expressions
Ralf Schlatterbeck <rsc@runtux.com>
parents:
6401
diff
changeset
|
107 x and [int(x)] or []) |
|
a0c0ee3ed8b1
Tests for Link expressions
Ralf Schlatterbeck <rsc@runtux.com>
parents:
6401
diff
changeset
|
108 else: |
|
a0c0ee3ed8b1
Tests for Link expressions
Ralf Schlatterbeck <rsc@runtux.com>
parents:
6401
diff
changeset
|
109 self.evaluate = lambda x: compiled.evaluate([int(y) for y in x]) |
|
6964
87af08c75695
catch type error as well.
John Rouillard <rouilj@ieee.org>
parents:
6960
diff
changeset
|
110 except (ValueError, TypeError): |
|
6412
a0c0ee3ed8b1
Tests for Link expressions
Ralf Schlatterbeck <rsc@runtux.com>
parents:
6401
diff
changeset
|
111 if is_link: |
|
a0c0ee3ed8b1
Tests for Link expressions
Ralf Schlatterbeck <rsc@runtux.com>
parents:
6401
diff
changeset
|
112 v = [None if x == '-1' else x for x in v] |
|
a0c0ee3ed8b1
Tests for Link expressions
Ralf Schlatterbeck <rsc@runtux.com>
parents:
6401
diff
changeset
|
113 self.evaluate = lambda x: x in v |
|
a0c0ee3ed8b1
Tests for Link expressions
Ralf Schlatterbeck <rsc@runtux.com>
parents:
6401
diff
changeset
|
114 elif '-1' in v: |
|
6401
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
115 v = [x for x in v if int(x) > 0] |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
116 self.evaluate = lambda x: bool(set(x) & set(v)) or not x |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
117 else: |
|
8bc5faeb7677
Make rev multilink expressions work for anydbm
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
118 self.evaluate = lambda x: bool(set(x) & set(v)) |
|
6960
7b090bb35c25
flake8 fixes - exception handling changed
John Rouillard <rouilj@ieee.org>
parents:
6412
diff
changeset
|
119 except BaseException: |
|
7b090bb35c25
flake8 fixes - exception handling changed
John Rouillard <rouilj@ieee.org>
parents:
6412
diff
changeset
|
120 raise |
