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 budget_sync
.config
import Config
, Person
, Milestone
7 from functools
import cached_property
11 from collections
import deque
12 from datetime
import date
, time
, datetime
15 class BudgetGraphBaseError(Exception):
19 class BudgetGraphParseError(BudgetGraphBaseError
):
20 def __init__(self
, bug_id
: int):
24 class BudgetGraphPayeesParseError(BudgetGraphParseError
):
25 def __init__(self
, bug_id
: int, msg
: str):
26 super().__init
__(bug_id
)
30 return f
"Failed to parse cf_payees_list field of " \
31 f
"bug #{self.bug_id}: {self.msg}"
34 class BudgetGraphLoopError(BudgetGraphBaseError
):
35 def __init__(self
, bug_ids
: List
[int]):
36 self
.bug_ids
= bug_ids
39 retval
= f
"Detected Loop in Budget Graph: #{self.bug_ids[-1]} -> "
40 retval
+= " -> ".join((f
"#{i}" for i
in self
.bug_ids
))
44 class _NodeSimpleReprWrapper
:
45 def __init__(self
, node
: "Node"):
49 return f
"#{self.node.bug.id}"
51 def __lt__(self
, other
):
53 return self
.node
.bug
.id < other
.node
.bug
.id
56 class PayeeState(enum
.Enum
):
57 NotYetSubmitted
= "not yet submitted"
58 Submitted
= "submitted"
62 _Date
= Union
[date
, datetime
]
65 def _parse_money_from_toml(value
: Any
) -> Money
:
66 if not isinstance(value
, (int, str)):
67 msg
= f
"monetary amount is not a string or integer " \
68 f
"(to use fractional amounts such as 123.45, write " \
69 f
"\"123.45\"): {value!r}"
74 def _parse_date_time_or_none_from_toml(value
: Any
) -> Optional
[_Date
]:
75 if value
is None or isinstance(value
, (date
, datetime
)):
77 elif isinstance(value
, time
):
78 msg
= f
"just a time of day by itself is not enough," \
79 f
" a date must be included: {str(value)}"
81 elif isinstance(value
, bool):
82 msg
= f
"invalid date: {str(value).lower()}"
84 elif isinstance(value
, (str, int, float)):
85 msg
= f
"invalid date: {value!r}"
97 paid
: Optional
[_Date
],
98 submitted
: Optional
[_Date
]):
100 self
.payee_key
= payee_key
103 self
.submitted
= submitted
106 def payee(self
) -> Person
:
108 return self
.node
.graph
.config
.all_names
[self
.payee_key
]
110 msg
= f
"unknown payee name: {self.payee_key!r} is not the name " \
111 f
"or an alias of any known person"
112 raise BudgetGraphPayeesParseError(self
.node
.bug
.id, msg
) \
113 .with_traceback(sys
.exc_info()[2])
117 if self
.paid
is not None:
118 return PayeeState
.Paid
119 if self
.submitted
is not None:
120 return PayeeState
.Submitted
121 return PayeeState
.NotYetSubmitted
124 def _from_toml(node
: "Node", payee_key
: str, toml_value
: Any
) -> "Payment":
127 known_keys
= ("paid", "submitted", "amount")
128 if isinstance(toml_value
, dict):
130 amount
= toml_value
['amount']
132 msg
= f
"value for key {payee_key!r} is missing the " \
133 f
"`amount` field which is required"
134 raise BudgetGraphPayeesParseError(node
.bug
.id, msg
) \
135 .with_traceback(sys
.exc_info()[2])
136 for k
, v
in toml_value
.items():
137 if k
in ("paid", "submitted"):
139 parsed_value
= _parse_date_time_or_none_from_toml(v
)
140 except ValueError as e
:
141 msg
= f
"failed to parse `{k}` field for" \
142 f
" key {payee_key!r}: {e}"
143 raise BudgetGraphPayeesParseError(
145 .with_traceback(sys
.exc_info()[2])
149 assert k
== "submitted"
150 submitted
= parsed_value
151 if k
not in known_keys
:
152 msg
= f
"value for key {payee_key!r} has an unknown" \
154 raise BudgetGraphPayeesParseError(node
.bug
.id, msg
) \
155 .with_traceback(sys
.exc_info()[2])
157 paid
= _parse_date_time_or_none_from_toml(
158 toml_value
.get('paid'))
159 except ValueError as e
:
160 msg
= f
"failed to parse `paid` field for" \
161 f
" key {payee_key!r}: {e}"
162 raise BudgetGraphPayeesParseError(
164 .with_traceback(sys
.exc_info()[2])
166 submitted
= _parse_date_time_or_none_from_toml(
167 toml_value
.get('submitted'))
168 except ValueError as e
:
169 msg
= f
"failed to parse `submitted` field for" \
170 f
" key {payee_key!r}: {e}"
171 raise BudgetGraphPayeesParseError(
173 .with_traceback(sys
.exc_info()[2])
174 elif isinstance(toml_value
, (int, str, float)):
175 # float included for better error messages
178 msg
= f
"value for key {payee_key!r} is invalid -- it should " \
179 f
"either be a monetary value or a table"
180 raise BudgetGraphPayeesParseError(node
.bug
.id, msg
)
182 amount
= _parse_money_from_toml(amount
)
183 except ValueError as e
:
184 msg
= f
"failed to parse monetary amount for key {payee_key!r}: {e}"
185 raise BudgetGraphPayeesParseError(
187 .with_traceback(sys
.exc_info()[2])
188 return Payment(node
=node
, payee_key
=payee_key
, amount
=amount
,
189 paid
=paid
, submitted
=submitted
)
193 payee
= f
"Person<{self.payee.identifier!r}>"
194 except BudgetGraphBaseError
:
195 payee
= "<unknown person>"
196 return (f
"Payment(node={_NodeSimpleReprWrapper(self.node)}, "
198 f
"payee_key={self.payee_key!r}, "
199 f
"amount={self.amount}, "
200 f
"state={self.state.name}, "
201 f
"paid={str(self.paid)}, "
202 f
"submitted={str(self.submitted)})")
205 class BudgetGraphUnknownMilestone(BudgetGraphParseError
):
206 def __init__(self
, bug_id
: int, milestone_str
: str):
207 super().__init
__(bug_id
)
208 self
.milestone_str
= milestone_str
211 return f
"failed to parse cf_nlnet_milestone field of bug " \
212 f
"#{self.bug_id}: unknown milestone: {self.milestone_str!r}"
218 parent_id
: Optional
[int]
219 immediate_children
: Set
["Node"]
220 budget_excluding_subtasks
: Money
221 budget_including_subtasks
: Money
222 fixed_budget_excluding_subtasks
: Money
223 fixed_budget_including_subtasks
: Money
224 milestone_str
: Optional
[str]
226 def __init__(self
, graph
: "BudgetGraph", bug
: Bug
):
229 self
.parent_id
= getattr(bug
, "cf_budget_parent", None)
230 self
.immediate_children
= set()
231 self
.budget_excluding_subtasks
= Money
.from_str(bug
.cf_budget
)
232 self
.fixed_budget_excluding_subtasks
= self
.budget_excluding_subtasks
233 self
.budget_including_subtasks
= Money
.from_str(bug
.cf_total_budget
)
234 self
.fixed_budget_including_subtasks
= self
.budget_including_subtasks
235 self
.milestone_str
= bug
.cf_nlnet_milestone
236 if self
.milestone_str
== "---":
237 self
.milestone_str
= None
240 def milestone(self
) -> Optional
[Milestone
]:
242 if self
.milestone_str
is not None:
243 return self
.graph
.config
.milestones
[self
.milestone_str
]
246 new_err
= BudgetGraphUnknownMilestone(
247 self
.bug
.id, self
.milestone_str
)
248 raise new_err
.with_traceback(sys
.exc_info()[2])
251 def payments(self
) -> Dict
[str, Payment
]:
253 parsed
= toml
.loads(self
.bug
.cf_payees_list
)
254 except toml
.TomlDecodeError
as e
:
255 new_err
= BudgetGraphPayeesParseError(
256 self
.bug
.id, f
"TOML parse error: {e}")
257 raise new_err
.with_traceback(sys
.exc_info()[2])
259 for key
, value
in parsed
.items():
260 if not isinstance(key
, str):
261 raise BudgetGraphPayeesParseError(
262 self
.bug
.id, f
"key is not a string: {key!r}")
263 retval
[key
] = Payment
._from
_toml
(self
, key
, value
)
267 def parent(self
) -> Optional
["Node"]:
268 if self
.parent_id
is not None:
269 return self
.graph
.nodes
[self
.parent_id
]
272 def parents(self
) -> Iterable
["Node"]:
274 while parent
is not None:
276 parent
= parent
.parent
278 def _raise_loop_error(self
):
280 for parent
in self
.parents():
281 bug_ids
.append(parent
.bug
.id)
284 raise BudgetGraphLoopError(bug_ids
)
287 def root(self
) -> "Node":
288 # also checks for loop errors
290 for parent
in self
.parents():
293 self
._raise
_loop
_error
()
296 def children(self
) -> Iterable
["Node"]:
297 def visitor(node
: Node
) -> Iterable
[Node
]:
298 for i
in node
.immediate_children
:
300 yield from visitor(i
)
303 def children_breadth_first(self
) -> Iterable
["Node"]:
304 q
= deque(self
.immediate_children
)
310 q
.extend(node
.immediate_children
)
313 def __eq__(self
, other
):
314 return self
.bug
.id == other
.bug
.id
321 root
= _NodeSimpleReprWrapper(self
.root
)
322 except BudgetGraphLoopError
:
323 root
= "<loop error>"
325 milestone
= repr(self
.milestone
)
326 except BudgetGraphBaseError
:
327 milestone
= "<unknown milestone>"
328 immediate_children
= []
329 for i
in self
.immediate_children
:
330 immediate_children
.append(_NodeSimpleReprWrapper(i
))
331 immediate_children
.sort()
332 parent
= f
"#{self.parent_id}" if self
.parent_id
is not None else None
333 payments
= list(self
.payments
.values())
334 return (f
"Node(graph=..., "
335 f
"id={_NodeSimpleReprWrapper(self)}, "
338 f
"budget_excluding_subtasks={self.budget_excluding_subtasks}, "
339 f
"budget_including_subtasks={self.budget_including_subtasks}, "
340 f
"fixed_budget_excluding_subtasks={self.fixed_budget_excluding_subtasks}, "
341 f
"fixed_budget_including_subtasks={self.fixed_budget_including_subtasks}, "
342 f
"milestone_str={self.milestone_str!r}, "
343 f
"milestone={milestone}, "
344 f
"immediate_children={immediate_children!r}, "
345 f
"payments={payments!r}")
348 class BudgetGraphError(BudgetGraphBaseError
):
349 def __init__(self
, bug_id
: int, root_bug_id
: int):
351 self
.root_bug_id
= root_bug_id
354 class BudgetGraphMoneyWithNoMilestone(BudgetGraphError
):
356 return (f
"Bug assigned money but without"
357 f
" any assigned milestone: #{self.bug_id}")
360 class BudgetGraphMilestoneMismatch(BudgetGraphError
):
362 return (f
"Bug's assigned milestone doesn't match the milestone "
363 f
"assigned to the root bug: descendant bug"
364 f
" #{self.bug_id}, root bug"
365 f
" #{self.root_bug_id}")
368 class BudgetGraphMoneyMismatchForBudgetExcludingSubtasks(BudgetGraphError
):
369 def __init__(self
, bug_id
: int, root_bug_id
: int,
370 expected_budget_excluding_subtasks
: Money
):
371 super().__init
__(bug_id
, root_bug_id
)
372 self
.expected_budget_excluding_subtasks
= \
373 expected_budget_excluding_subtasks
376 return (f
"Budget assigned to task excluding subtasks "
377 f
"(cf_budget field) doesn't match calculated value: "
378 f
"bug #{self.bug_id}, calculated value"
379 f
" {self.expected_budget_excluding_subtasks}")
382 class BudgetGraphMoneyMismatchForBudgetIncludingSubtasks(BudgetGraphError
):
383 def __init__(self
, bug_id
: int, root_bug_id
: int,
384 expected_budget_including_subtasks
: Money
):
385 super().__init
__(bug_id
, root_bug_id
)
386 self
.expected_budget_including_subtasks
= \
387 expected_budget_including_subtasks
390 return (f
"Budget assigned to task including subtasks "
391 f
"(cf_total_budget field) doesn't match calculated value: "
392 f
"bug #{self.bug_id}, calculated value"
393 f
" {self.expected_budget_including_subtasks}")
396 class BudgetGraphNegativeMoney(BudgetGraphError
):
398 return (f
"Budget assigned to task is less than zero: "
399 f
"bug #{self.bug_id}")
402 class BudgetGraphPayeesMoneyMismatch(BudgetGraphError
):
403 def __init__(self
, bug_id
: int, root_bug_id
: int, payees_total
: Money
,
404 expected_payees_total
: Money
):
405 super().__init
__(bug_id
, root_bug_id
)
406 self
.payees_total
= payees_total
407 self
.expected_payees_total
= expected_payees_total
410 return (f
"Total budget assigned to payees (cf_payees_list) doesn't "
411 f
"match expected value: bug #{self.bug_id}, calculated total "
412 f
"{self.payees_total}, expected value "
413 f
"{self.expected_payees_total}")
416 class BudgetGraphNegativePayeeMoney(BudgetGraphError
):
417 def __init__(self
, bug_id
: int, root_bug_id
: int, payee_key
: str):
418 super().__init
__(bug_id
, root_bug_id
)
419 self
.payee_key
= payee_key
422 return (f
"Budget assigned to payee for task is less than zero: "
423 f
"bug #{self.bug_id}, payee {self.payee_key!r}")
426 class BudgetGraphDuplicatePayeesForTask(BudgetGraphError
):
427 def __init__(self
, bug_id
: int, root_bug_id
: int, payee1_key
: str, payee2_key
: str):
428 super().__init
__(bug_id
, root_bug_id
)
429 self
.payee1_key
= payee1_key
430 self
.payee2_key
= payee2_key
433 return (f
"Budget assigned to multiple aliases of the same person in "
434 f
"a single task: bug #{self.bug_id}, budget assigned to both "
435 f
"{self.payee1_key!r} and {self.payee2_key!r}")
438 class BudgetGraphIncorrectRootForMilestone(BudgetGraphError
):
439 def __init__(self
, bug_id
: int, milestone
: str, milestone_canonical_bug_id
: int):
440 super().__init
__(bug_id
, bug_id
)
441 self
.milestone
= milestone
442 self
.milestone_canonical_bug_id
= milestone_canonical_bug_id
445 return (f
"Bug #{self.bug_id} is not the canonical root bug for "
446 f
"assigned milestone {self.milestone!r} but has no parent "
447 f
"bug set: the milestone's canonical root bug is "
448 f
"#{self.milestone_canonical_bug_id}")
452 nodes
: Dict
[int, Node
]
454 def __init__(self
, bugs
: Iterable
[Bug
], config
: Config
):
458 self
.nodes
[bug
.id] = Node(self
, bug
)
459 for node
in self
.nodes
.values():
460 if node
.parent
is None:
462 node
.parent
.immediate_children
.add(node
)
465 def roots(self
) -> Set
[Node
]:
467 for node
in self
.nodes
.values():
468 # calling .root also checks for loop errors
473 def _get_node_errors(self
, root
: Node
, node
: Node
,
474 errors
: List
[BudgetGraphBaseError
]):
475 if node
.milestone_str
is None:
476 if node
.budget_including_subtasks
!= 0 \
477 or node
.budget_excluding_subtasks
!= 0:
478 errors
.append(BudgetGraphMoneyWithNoMilestone(
479 node
.bug
.id, root
.bug
.id))
482 # check for milestone errors
484 if root
== node
and node
.milestone
is not None \
485 and node
.milestone
.canonical_bug_id
!= node
.bug
.id:
486 if node
.budget_including_subtasks
!= 0 \
487 or node
.budget_excluding_subtasks
!= 0:
488 errors
.append(BudgetGraphIncorrectRootForMilestone(
489 node
.bug
.id, node
.milestone
.identifier
,
490 node
.milestone
.canonical_bug_id
492 except BudgetGraphBaseError
as e
:
495 if node
.milestone_str
!= root
.milestone_str
:
496 errors
.append(BudgetGraphMilestoneMismatch(
497 node
.bug
.id, root
.bug
.id))
499 if node
.budget_excluding_subtasks
< 0 \
500 or node
.budget_including_subtasks
< 0:
501 errors
.append(BudgetGraphNegativeMoney(
502 node
.bug
.id, root
.bug
.id))
504 subtasks_total
= Money(0)
505 for child
in node
.immediate_children
:
506 subtasks_total
+= child
.fixed_budget_including_subtasks
508 payees_total
= Money(0)
510 for payment
in node
.payments
.values():
511 if payment
.amount
< 0:
512 errors
.append(BudgetGraphNegativePayeeMoney(
513 node
.bug
.id, root
.bug
.id, payment
.payee_key
))
514 payees_total
+= payment
.amount
516 # check for payee errors
518 previous_payment
= payee_payments
.get(payment
.payee
)
519 if previous_payment
is not None:
520 errors
.append(BudgetGraphDuplicatePayeesForTask(
521 node
.bug
.id, root
.bug
.id,
522 previous_payment
.payee_key
, payment
.payee_key
524 payee_payments
[payment
.payee
] = payment
525 except BudgetGraphBaseError
as e
:
528 def set_including_from_excluding_and_error():
529 node
.fixed_budget_including_subtasks
= \
530 node
.budget_excluding_subtasks
+ subtasks_total
532 BudgetGraphMoneyMismatchForBudgetIncludingSubtasks(
533 node
.bug
.id, root
.bug
.id,
534 node
.fixed_budget_including_subtasks
))
536 def set_including_from_payees_and_error():
537 node
.fixed_budget_including_subtasks
= \
538 payees_total
+ subtasks_total
540 BudgetGraphMoneyMismatchForBudgetIncludingSubtasks(
541 node
.bug
.id, root
.bug
.id,
542 node
.fixed_budget_including_subtasks
))
544 def set_excluding_from_including_and_error():
545 node
.fixed_budget_excluding_subtasks
= \
546 node
.budget_including_subtasks
- subtasks_total
548 BudgetGraphMoneyMismatchForBudgetExcludingSubtasks(
549 node
.bug
.id, root
.bug
.id,
550 node
.fixed_budget_excluding_subtasks
))
552 def set_excluding_from_payees_and_error():
553 node
.fixed_budget_excluding_subtasks
= \
556 BudgetGraphMoneyMismatchForBudgetExcludingSubtasks(
557 node
.bug
.id, root
.bug
.id,
558 node
.fixed_budget_excluding_subtasks
))
560 def set_payees_from_including_and_error():
561 fixed_payees_total
= \
562 node
.budget_including_subtasks
- subtasks_total
563 errors
.append(BudgetGraphPayeesMoneyMismatch(
564 node
.bug
.id, root
.bug
.id, payees_total
, fixed_payees_total
))
566 def set_payees_from_excluding_and_error():
567 fixed_payees_total
= \
568 node
.budget_excluding_subtasks
569 errors
.append(BudgetGraphPayeesMoneyMismatch(
570 node
.bug
.id, root
.bug
.id, payees_total
, fixed_payees_total
))
572 payees_matches_including
= \
573 node
.budget_including_subtasks
- subtasks_total
== payees_total
574 payees_matches_excluding
= \
575 node
.budget_excluding_subtasks
== payees_total
576 including_matches_excluding
= \
577 node
.budget_including_subtasks
- subtasks_total \
578 == node
.budget_excluding_subtasks
580 if payees_matches_including \
581 and payees_matches_excluding \
582 and including_matches_excluding
:
584 elif payees_matches_including
:
585 # can't have 2 match without all 3 matching
586 assert not payees_matches_excluding
587 assert not including_matches_excluding
588 if node
.budget_including_subtasks
== 0 and len(node
.payments
) == 0:
589 set_including_from_excluding_and_error()
591 set_excluding_from_including_and_error()
592 elif payees_matches_excluding
:
593 # can't have 2 match without all 3 matching
594 assert not payees_matches_including
595 assert not including_matches_excluding
596 if node
.budget_excluding_subtasks
== 0 and len(node
.payments
) == 0:
597 if node
.budget_including_subtasks
== 0:
598 set_including_from_excluding_and_error()
600 set_excluding_from_including_and_error()
602 set_including_from_excluding_and_error()
603 elif including_matches_excluding
:
604 # can't have 2 match without all 3 matching
605 assert not payees_matches_including
606 assert not payees_matches_excluding
607 if len(node
.payments
) == 0:
608 pass # no error -- payees is just not set
609 elif node
.budget_excluding_subtasks
== 0 \
610 and node
.budget_including_subtasks
== 0:
611 set_excluding_from_payees_and_error()
612 set_including_from_payees_and_error()
614 set_payees_from_excluding_and_error()
617 if len(node
.payments
) == 0:
618 # payees unset -- don't need to set payees
619 if node
.budget_including_subtasks
== 0:
620 set_including_from_excluding_and_error()
622 set_excluding_from_including_and_error()
623 elif node
.budget_excluding_subtasks
== 0 \
624 and node
.budget_including_subtasks
== 0:
625 set_excluding_from_payees_and_error()
626 set_including_from_payees_and_error()
627 elif node
.budget_excluding_subtasks
== 0:
628 set_excluding_from_including_and_error()
629 set_payees_from_including_and_error()
630 elif node
.budget_including_subtasks
== 0:
631 set_including_from_excluding_and_error()
632 set_payees_from_excluding_and_error()
634 set_including_from_excluding_and_error()
635 set_payees_from_excluding_and_error()
637 def get_errors(self
) -> List
[BudgetGraphBaseError
]:
641 except BudgetGraphBaseError
as e
:
647 for child
in reversed(list(root
.children_breadth_first())):
649 self
._get
_node
_errors
(root
, child
, errors
)
650 except BudgetGraphBaseError
as e
:
652 self
._get
_node
_errors
(root
, root
, errors
)
653 except BudgetGraphBaseError
as e
:
658 def payments(self
) -> Dict
[Person
, Dict
[Milestone
, List
[Payment
]]]:
660 for person
in self
.config
.people
.values():
661 milestone_payments
= {}
662 for milestone
in self
.config
.milestones
.values():
663 milestone_payments
[milestone
] = []
664 retval
[person
] = milestone_payments
665 for node
in self
.nodes
.values():
666 if node
.milestone
is not None:
667 for payment
in node
.payments
.values():
668 retval
[payment
.payee
][node
.milestone
].append(payment
)