1 from bugzilla
.bug
import Bug
2 from bugzilla
import Bugzilla
3 from typing
import Set
, Dict
, Iterable
, Optional
, List
, Union
, Any
4 from budget_sync
.util
import all_bugs
5 from budget_sync
.money
import Money
6 from functools
import cached_property
10 from collections
import deque
11 from datetime
import date
, time
, datetime
14 class BudgetGraphBaseError(Exception):
18 class BudgetGraphParseError(BudgetGraphBaseError
):
19 def __init__(self
, bug_id
: int):
23 class BudgetGraphPayeesParseError(BudgetGraphParseError
):
24 def __init__(self
, bug_id
: int, msg
: str):
25 super().__init
__(bug_id
)
29 return f
"Failed to parse cf_payees_list field of bug #{self.bug_id}: {self.msg}"
32 class BudgetGraphLoopError(BudgetGraphBaseError
):
33 def __init__(self
, bug_ids
: List
[int]):
34 self
.bug_ids
= bug_ids
37 retval
= f
"Detected Loop in Budget Graph: #{self.bug_ids[-1]} -> "
38 retval
+= " -> ".join((f
"#{i}" for i
in self
.bug_ids
))
42 class _NodeSimpleReprWrapper
:
43 def __init__(self
, node
: "Node"):
47 return f
"#{self.node.bug.id}"
49 def __lt__(self
, other
):
51 return self
.node
.bug
.id < other
.node
.bug
.id
54 class PayeeState(enum
.Enum
):
55 NotYetSubmitted
= "not yet submitted"
56 Submitted
= "submitted"
60 _Date
= Union
[date
, datetime
]
63 def _parse_money_from_toml(value
: Any
) -> Money
:
64 if not isinstance(value
, (int, str)):
65 msg
= f
"monetary amount is not a string or integer " \
66 f
"(to use fractional amounts such as 123.45, write " \
67 f
"\"123.45\"): {value!r}"
72 def _parse_date_time_or_none_from_toml(value
: Any
) -> Optional
[_Date
]:
73 if value
is None or isinstance(value
, (date
, datetime
)):
75 elif isinstance(value
, time
):
76 msg
= f
"just a time of day by itself is not enough," \
77 f
" a date must be included: {str(value)}"
79 elif isinstance(value
, bool):
80 msg
= f
"invalid date: {str(value).lower()}"
82 elif isinstance(value
, (str, int, float)):
83 msg
= f
"invalid date: {value!r}"
95 paid
: Optional
[_Date
],
96 submitted
: Optional
[_Date
]):
98 self
.payee_key
= payee_key
101 self
.submitted
= submitted
105 if self
.paid
is not None:
106 return PayeeState
.Paid
107 if self
.submitted
is not None:
108 return PayeeState
.Submitted
109 return PayeeState
.NotYetSubmitted
112 def _from_toml(node
: "Node", payee_key
: str, toml_value
: Any
) -> "Payment":
115 known_keys
= ("paid", "submitted", "amount")
116 if isinstance(toml_value
, dict):
118 amount
= toml_value
['amount']
120 msg
= f
"value for key {payee_key!r} is missing the " \
121 f
"`amount` field which is required"
122 raise BudgetGraphPayeesParseError(node
.bug
.id, msg
) \
123 .with_traceback(sys
.exc_info()[2])
124 for k
, v
in toml_value
.items():
125 if k
in ("paid", "submitted"):
127 parsed_value
= _parse_date_time_or_none_from_toml(v
)
128 except ValueError as e
:
129 msg
= f
"failed to parse `{k}` field for" \
130 f
" key {payee_key!r}: {e}"
131 raise BudgetGraphPayeesParseError(
133 .with_traceback(sys
.exc_info()[2])
137 assert k
== "submitted"
138 submitted
= parsed_value
139 if k
not in known_keys
:
140 msg
= f
"value for key {payee_key!r} has an unknown" \
142 raise BudgetGraphPayeesParseError(node
.bug
.id, msg
) \
143 .with_traceback(sys
.exc_info()[2])
145 paid
= _parse_date_time_or_none_from_toml(
146 toml_value
.get('paid'))
147 except ValueError as e
:
148 msg
= f
"failed to parse `paid` field for" \
149 f
" key {payee_key!r}: {e}"
150 raise BudgetGraphPayeesParseError(
152 .with_traceback(sys
.exc_info()[2])
154 submitted
= _parse_date_time_or_none_from_toml(
155 toml_value
.get('submitted'))
156 except ValueError as e
:
157 msg
= f
"failed to parse `submitted` field for" \
158 f
" key {payee_key!r}: {e}"
159 raise BudgetGraphPayeesParseError(
161 .with_traceback(sys
.exc_info()[2])
162 elif isinstance(toml_value
, (int, str, float)):
163 # float included for better error messages
166 msg
= f
"value for key {payee_key!r} is invalid -- it should " \
167 f
"either be a monetary value or a table"
168 raise BudgetGraphPayeesParseError(node
.bug
.id, msg
)
170 amount
= _parse_money_from_toml(amount
)
171 except ValueError as e
:
172 msg
= f
"failed to parse monetary amount for key {payee_key!r}: {e}"
173 raise BudgetGraphPayeesParseError(
175 .with_traceback(sys
.exc_info()[2])
176 return Payment(node
=node
, payee_key
=payee_key
, amount
=amount
,
177 paid
=paid
, submitted
=submitted
)
180 return (f
"Payment(node={_NodeSimpleReprWrapper(self.node)}, "
181 f
"payee_key={self.payee_key!r}, "
182 f
"amount={self.amount}, "
183 f
"state={self.state.name}, "
184 f
"paid={str(self.paid)}, "
185 f
"submitted={str(self.submitted)})")
191 parent_id
: Optional
[int]
192 immediate_children
: Set
["Node"]
193 budget_excluding_subtasks
: Money
194 budget_including_subtasks
: Money
195 fixed_budget_excluding_subtasks
: Money
196 fixed_budget_including_subtasks
: Money
197 nlnet_milestone
: Optional
[str]
199 def __init__(self
, graph
: "BudgetGraph", bug
: Bug
):
202 self
.parent_id
= getattr(bug
, "cf_budget_parent", None)
203 self
.immediate_children
= set()
204 self
.budget_excluding_subtasks
= Money
.from_str(bug
.cf_budget
)
205 self
.fixed_budget_excluding_subtasks
= self
.budget_excluding_subtasks
206 self
.budget_including_subtasks
= Money
.from_str(bug
.cf_total_budget
)
207 self
.fixed_budget_including_subtasks
= self
.budget_including_subtasks
208 self
.nlnet_milestone
= bug
.cf_nlnet_milestone
209 if self
.nlnet_milestone
== "---":
210 self
.nlnet_milestone
= None
213 def payments(self
) -> Dict
[str, Payment
]:
215 parsed
= toml
.loads(self
.bug
.cf_payees_list
)
216 except toml
.TomlDecodeError
as e
:
217 new_err
= BudgetGraphPayeesParseError(
218 self
.bug
.id, f
"TOML parse error: {e}")
219 raise new_err
.with_traceback(sys
.exc_info()[2])
221 for key
, value
in parsed
.items():
222 if not isinstance(key
, str):
223 raise BudgetGraphPayeesParseError(
224 self
.bug
.id, f
"key is not a string: {key!r}")
225 retval
[key
] = Payment
._from
_toml
(self
, key
, value
)
229 def parent(self
) -> Optional
["Node"]:
230 if self
.parent_id
is not None:
231 return self
.graph
.nodes
[self
.parent_id
]
234 def parents(self
) -> Iterable
["Node"]:
236 while parent
is not None:
238 parent
= parent
.parent
240 def _raise_loop_error(self
):
242 for parent
in self
.parents():
243 bug_ids
.append(parent
.bug
.id)
246 raise BudgetGraphLoopError(bug_ids
)
249 def root(self
) -> "Node":
250 # also checks for loop errors
252 for parent
in self
.parents():
255 self
._raise
_loop
_error
()
258 def children(self
) -> Iterable
["Node"]:
259 def visitor(node
: Node
) -> Iterable
[Node
]:
260 for i
in node
.immediate_children
:
262 yield from visitor(i
)
265 def children_breadth_first(self
) -> Iterable
["Node"]:
266 q
= deque(self
.immediate_children
)
272 q
.extend(node
.immediate_children
)
275 def __eq__(self
, other
):
276 return self
.bug
.id == other
.bug
.id
283 root
= _NodeSimpleReprWrapper(self
.root
)
284 except BudgetGraphLoopError
:
285 root
= "<loop error>"
286 immediate_children
= []
287 for i
in self
.immediate_children
:
288 immediate_children
.append(_NodeSimpleReprWrapper(i
))
289 immediate_children
.sort()
290 parent
= f
"#{self.parent_id}" if self
.parent_id
is not None else None
291 payments
= list(self
.payments
.values())
292 return (f
"Node(graph=..., "
293 f
"id={_NodeSimpleReprWrapper(self)}, "
296 f
"budget_excluding_subtasks={self.budget_excluding_subtasks}, "
297 f
"budget_including_subtasks={self.budget_including_subtasks}, "
298 f
"fixed_budget_excluding_subtasks={self.fixed_budget_excluding_subtasks}, "
299 f
"fixed_budget_including_subtasks={self.fixed_budget_including_subtasks}, "
300 f
"nlnet_milestone={self.nlnet_milestone!r}, "
301 f
"immediate_children={immediate_children!r}, "
302 f
"payments={payments!r}")
305 class BudgetGraphError(BudgetGraphBaseError
):
306 def __init__(self
, bug_id
: int, root_bug_id
: int):
308 self
.root_bug_id
= root_bug_id
311 class BudgetGraphMoneyWithNoMilestone(BudgetGraphError
):
313 return (f
"Bug assigned money but without"
314 f
" any assigned milestone: #{self.bug_id}")
317 class BudgetGraphMilestoneMismatch(BudgetGraphError
):
319 return (f
"Bug's assigned milestone doesn't match the milestone "
320 f
"assigned to the root bug: descendant bug"
321 f
" #{self.bug_id}, root bug"
322 f
" #{self.root_bug_id}")
325 class BudgetGraphMoneyMismatchForBudgetExcludingSubtasks(BudgetGraphError
):
326 def __init__(self
, bug_id
: int, root_bug_id
: int,
327 expected_budget_excluding_subtasks
: Money
):
328 super().__init
__(bug_id
, root_bug_id
)
329 self
.expected_budget_excluding_subtasks
= \
330 expected_budget_excluding_subtasks
333 return (f
"Budget assigned to task excluding subtasks "
334 f
"(cf_budget field) doesn't match calculated value: "
335 f
"bug #{self.bug_id}, calculated value"
336 f
" {self.expected_budget_excluding_subtasks}")
339 class BudgetGraphMoneyMismatchForBudgetIncludingSubtasks(BudgetGraphError
):
340 def __init__(self
, bug_id
: int, root_bug_id
: int,
341 expected_budget_including_subtasks
: Money
):
342 super().__init
__(bug_id
, root_bug_id
)
343 self
.expected_budget_including_subtasks
= \
344 expected_budget_including_subtasks
347 return (f
"Budget assigned to task including subtasks "
348 f
"(cf_total_budget field) doesn't match calculated value: "
349 f
"bug #{self.bug_id}, calculated value"
350 f
" {self.expected_budget_including_subtasks}")
353 class BudgetGraphNegativeMoney(BudgetGraphError
):
355 return (f
"Budget assigned to task is less than zero: "
356 f
"bug #{self.bug_id}")
359 class BudgetGraphPayeesMoneyMismatch(BudgetGraphError
):
360 def __init__(self
, bug_id
: int, root_bug_id
: int, payees_total
: Money
,
361 expected_payees_total
: Money
):
362 super().__init
__(bug_id
, root_bug_id
)
363 self
.payees_total
= payees_total
364 self
.expected_payees_total
= expected_payees_total
367 return (f
"Total budget assigned to payees (cf_payees_list) doesn't "
368 f
"match expected value: bug #{self.bug_id}, calculated total "
369 f
"{self.payees_total}, expected value "
370 f
"{self.expected_payees_total}")
373 class BudgetGraphNegativePayeeMoney(BudgetGraphError
):
374 def __init__(self
, bug_id
: int, root_bug_id
: int, payee_key
: str):
375 super().__init
__(bug_id
, root_bug_id
)
376 self
.payee_key
= payee_key
379 return (f
"Budget assigned to payee for task is less than zero: "
380 f
"bug #{self.bug_id}, payee {self.payee_key!r}")
384 nodes
: Dict
[int, Node
]
386 def __init__(self
, bugs
: Iterable
[Bug
]):
389 self
.nodes
[bug
.id] = Node(self
, bug
)
390 for node
in self
.nodes
.values():
391 if node
.parent
is None:
393 node
.parent
.immediate_children
.add(node
)
396 def roots(self
) -> Set
[Node
]:
398 for node
in self
.nodes
.values():
399 # calling .root also checks for loop errors
404 def _get_node_errors(self
, root
: Node
, node
: Node
,
405 errors
: List
[BudgetGraphBaseError
]):
406 if node
.nlnet_milestone
is None:
407 if node
.budget_including_subtasks
!= 0 \
408 or node
.budget_excluding_subtasks
!= 0:
409 errors
.append(BudgetGraphMoneyWithNoMilestone(
410 node
.bug
.id, root
.bug
.id))
412 if node
.nlnet_milestone
!= root
.nlnet_milestone
:
413 errors
.append(BudgetGraphMilestoneMismatch(
414 node
.bug
.id, root
.bug
.id))
416 if node
.budget_excluding_subtasks
< 0 \
417 or node
.budget_including_subtasks
< 0:
418 errors
.append(BudgetGraphNegativeMoney(
419 node
.bug
.id, root
.bug
.id))
421 subtasks_total
= Money(0)
422 for child
in node
.immediate_children
:
423 subtasks_total
+= child
.fixed_budget_including_subtasks
425 payees_total
= Money(0)
426 for payment
in node
.payments
.values():
427 if payment
.amount
< 0:
428 errors
.append(BudgetGraphNegativePayeeMoney(
429 node
.bug
.id, root
.bug
.id, payment
.payee_key
))
430 payees_total
+= payment
.amount
432 def set_including_from_excluding_and_error():
433 node
.fixed_budget_including_subtasks
= \
434 node
.budget_excluding_subtasks
+ subtasks_total
436 BudgetGraphMoneyMismatchForBudgetIncludingSubtasks(
437 node
.bug
.id, root
.bug
.id,
438 node
.fixed_budget_including_subtasks
))
440 def set_including_from_payees_and_error():
441 node
.fixed_budget_including_subtasks
= \
442 payees_total
+ subtasks_total
444 BudgetGraphMoneyMismatchForBudgetIncludingSubtasks(
445 node
.bug
.id, root
.bug
.id,
446 node
.fixed_budget_including_subtasks
))
448 def set_excluding_from_including_and_error():
449 node
.fixed_budget_excluding_subtasks
= \
450 node
.budget_including_subtasks
- subtasks_total
452 BudgetGraphMoneyMismatchForBudgetExcludingSubtasks(
453 node
.bug
.id, root
.bug
.id,
454 node
.fixed_budget_excluding_subtasks
))
456 def set_excluding_from_payees_and_error():
457 node
.fixed_budget_excluding_subtasks
= \
460 BudgetGraphMoneyMismatchForBudgetExcludingSubtasks(
461 node
.bug
.id, root
.bug
.id,
462 node
.fixed_budget_excluding_subtasks
))
464 def set_payees_from_including_and_error():
465 fixed_payees_total
= \
466 node
.budget_including_subtasks
- subtasks_total
467 errors
.append(BudgetGraphPayeesMoneyMismatch(
468 node
.bug
.id, root
.bug
.id, payees_total
, fixed_payees_total
))
470 def set_payees_from_excluding_and_error():
471 fixed_payees_total
= \
472 node
.budget_excluding_subtasks
473 errors
.append(BudgetGraphPayeesMoneyMismatch(
474 node
.bug
.id, root
.bug
.id, payees_total
, fixed_payees_total
))
476 payees_matches_including
= \
477 node
.budget_including_subtasks
- subtasks_total
== payees_total
478 payees_matches_excluding
= \
479 node
.budget_excluding_subtasks
== payees_total
480 including_matches_excluding
= \
481 node
.budget_including_subtasks
- subtasks_total \
482 == node
.budget_excluding_subtasks
484 if payees_matches_including \
485 and payees_matches_excluding \
486 and including_matches_excluding
:
488 elif payees_matches_including
:
489 # can't have 2 match without all 3 matching
490 assert not payees_matches_excluding
491 assert not including_matches_excluding
492 if node
.budget_including_subtasks
== 0 and len(node
.payments
) == 0:
493 set_including_from_excluding_and_error()
495 set_excluding_from_including_and_error()
496 elif payees_matches_excluding
:
497 # can't have 2 match without all 3 matching
498 assert not payees_matches_including
499 assert not including_matches_excluding
500 if node
.budget_excluding_subtasks
== 0 and len(node
.payments
) == 0:
501 if node
.budget_including_subtasks
== 0:
502 set_including_from_excluding_and_error()
504 set_excluding_from_including_and_error()
506 set_including_from_excluding_and_error()
507 elif including_matches_excluding
:
508 # can't have 2 match without all 3 matching
509 assert not payees_matches_including
510 assert not payees_matches_excluding
511 if len(node
.payments
) == 0:
512 pass # no error -- payees is just not set
513 elif node
.budget_excluding_subtasks
== 0 \
514 and node
.budget_including_subtasks
== 0:
515 set_excluding_from_payees_and_error()
516 set_including_from_payees_and_error()
518 set_payees_from_excluding_and_error()
521 if len(node
.payments
) == 0:
522 # payees unset -- don't need to set payees
523 if node
.budget_including_subtasks
== 0:
524 set_including_from_excluding_and_error()
526 set_excluding_from_including_and_error()
527 elif node
.budget_excluding_subtasks
== 0 \
528 and node
.budget_including_subtasks
== 0:
529 set_excluding_from_payees_and_error()
530 set_including_from_payees_and_error()
531 elif node
.budget_excluding_subtasks
== 0:
532 set_excluding_from_including_and_error()
533 set_payees_from_including_and_error()
534 elif node
.budget_including_subtasks
== 0:
535 set_including_from_excluding_and_error()
536 set_payees_from_excluding_and_error()
538 set_including_from_excluding_and_error()
539 set_payees_from_excluding_and_error()
541 def get_errors(self
) -> List
[BudgetGraphBaseError
]:
545 except BudgetGraphBaseError
as e
:
551 for child
in reversed(list(root
.children_breadth_first())):
553 self
._get
_node
_errors
(root
, child
, errors
)
554 except BudgetGraphBaseError
as e
:
556 self
._get
_node
_errors
(root
, root
, errors
)
557 except BudgetGraphBaseError
as e
:
562 def payee_keys(self
) -> Set
[str]:
564 for node
in self
.nodes
.values():
565 for payee_key
in node
.payments
.keys():
566 retval
.add(payee_key
)