1 from budget_sync
.ordered_set
import OrderedSet
2 from bugzilla
.bug
import Bug
3 from typing
import Callable
, Set
, Dict
, Iterable
, Optional
, List
, Tuple
, Union
, Any
4 from budget_sync
.util
import BugStatus
, PrettyPrinter
5 from budget_sync
.money
import Money
6 from budget_sync
.config
import Config
, Person
, Milestone
10 from collections
import deque
11 from datetime
import date
, time
, datetime
13 from functools
import cached_property
14 except ImportError: # :nocov:
15 # compatibility with python < 3.8
16 from cached_property
import cached_property
# :nocov:
19 class BudgetGraphBaseError(Exception):
23 class BudgetGraphParseError(BudgetGraphBaseError
):
24 def __init__(self
, bug_id
: int):
28 class BudgetGraphPayeesParseError(BudgetGraphParseError
):
29 def __init__(self
, bug_id
: int, msg
: str):
30 super().__init
__(bug_id
)
34 return f
"Failed to parse cf_payees_list field of " \
35 f
"bug #{self.bug_id}: {self.msg}"
38 class BudgetGraphUnknownAssignee(BudgetGraphParseError
):
39 def __init__(self
, bug_id
: int, assignee
: str):
40 super().__init
__(bug_id
)
41 self
.assignee
= assignee
44 return f
"Bug #{self.bug_id} is assigned to an unknown person: " \
48 class BudgetGraphLoopError(BudgetGraphBaseError
):
49 def __init__(self
, bug_ids
: List
[int]):
50 self
.bug_ids
= bug_ids
53 retval
= f
"Detected Loop in Budget Graph: #{self.bug_ids[-1]} -> "
54 retval
+= " -> ".join((f
"#{i}" for i
in self
.bug_ids
))
58 class _NodeSimpleReprWrapper
:
59 def __init__(self
, node
: "Node"):
63 return f
"#{self.node.bug.id}"
65 def __lt__(self
, other
):
67 return self
.node
.bug
.id < other
.node
.bug
.id
70 class PayeeState(enum
.Enum
):
71 NotYetSubmitted
= "not yet submitted"
72 Submitted
= "submitted"
76 _Date
= Union
[date
, datetime
]
79 def _parse_money_from_toml(value
: Any
) -> Money
:
80 if not isinstance(value
, (int, str)):
81 msg
= f
"monetary amount is not a string or integer " \
82 f
"(to use fractional amounts such as 123.45, write " \
83 f
"\"123.45\"): {value!r}"
88 def _parse_date_time_or_none_from_toml(value
: Any
) -> Optional
[_Date
]:
89 if value
is None or isinstance(value
, (date
, datetime
)):
91 elif isinstance(value
, time
):
92 msg
= f
"just a time of day by itself is not enough," \
93 f
" a date must be included: {str(value)}"
95 elif isinstance(value
, bool):
96 msg
= f
"invalid date: {str(value).lower()}"
98 elif isinstance(value
, (str, int, float)):
99 msg
= f
"invalid date: {value!r}"
100 raise ValueError(msg
)
102 msg
= f
"invalid date"
103 raise ValueError(msg
)
111 paid
: Optional
[_Date
],
112 submitted
: Optional
[_Date
]):
114 self
.payee_key
= payee_key
117 self
.submitted
= submitted
120 def payee(self
) -> Person
:
122 return self
.node
.graph
.config
.all_names
[self
.payee_key
]
124 msg
= f
"unknown payee name: {self.payee_key!r} is not the name " \
125 f
"or an alias of any known person"
126 raise BudgetGraphPayeesParseError(self
.node
.bug
.id, msg
) \
127 .with_traceback(sys
.exc_info()[2])
131 if self
.paid
is not None:
132 return PayeeState
.Paid
133 if self
.submitted
is not None:
134 return PayeeState
.Submitted
135 return PayeeState
.NotYetSubmitted
138 def _from_toml(node
: "Node", payee_key
: str, toml_value
: Any
) -> "Payment":
141 known_keys
= ("paid", "submitted", "amount")
142 if isinstance(toml_value
, dict):
144 amount
= toml_value
['amount']
146 msg
= f
"value for key {payee_key!r} is missing the " \
147 f
"`amount` field which is required"
148 raise BudgetGraphPayeesParseError(node
.bug
.id, msg
) \
149 .with_traceback(sys
.exc_info()[2])
150 for k
, v
in toml_value
.items():
151 if k
in ("paid", "submitted"):
153 parsed_value
= _parse_date_time_or_none_from_toml(v
)
154 except ValueError as e
:
155 msg
= f
"failed to parse `{k}` field for" \
156 f
" key {payee_key!r}: {e}"
157 raise BudgetGraphPayeesParseError(
159 .with_traceback(sys
.exc_info()[2])
163 assert k
== "submitted"
164 submitted
= parsed_value
165 if k
not in known_keys
:
166 msg
= f
"value for key {payee_key!r} has an unknown" \
168 raise BudgetGraphPayeesParseError(node
.bug
.id, msg
) \
169 .with_traceback(sys
.exc_info()[2])
171 paid
= _parse_date_time_or_none_from_toml(
172 toml_value
.get('paid'))
173 except ValueError as e
:
174 msg
= f
"failed to parse `paid` field for" \
175 f
" key {payee_key!r}: {e}"
176 raise BudgetGraphPayeesParseError(
178 .with_traceback(sys
.exc_info()[2])
180 submitted
= _parse_date_time_or_none_from_toml(
181 toml_value
.get('submitted'))
182 except ValueError as e
:
183 msg
= f
"failed to parse `submitted` field for" \
184 f
" key {payee_key!r}: {e}"
185 raise BudgetGraphPayeesParseError(
187 .with_traceback(sys
.exc_info()[2])
188 elif isinstance(toml_value
, (int, str, float)):
189 # float included for better error messages
192 msg
= f
"value for key {payee_key!r} is invalid -- it should " \
193 f
"either be a monetary value or a table"
194 raise BudgetGraphPayeesParseError(node
.bug
.id, msg
)
196 amount
= _parse_money_from_toml(amount
)
197 except ValueError as e
:
198 msg
= f
"failed to parse monetary amount for key {payee_key!r}: {e}"
199 raise BudgetGraphPayeesParseError(
201 .with_traceback(sys
.exc_info()[2])
202 return Payment(node
=node
, payee_key
=payee_key
, amount
=amount
,
203 paid
=paid
, submitted
=submitted
)
207 payee
= f
"Person<{self.payee.identifier!r}>"
208 except BudgetGraphBaseError
:
209 payee
= "<unknown person>"
210 return (f
"Payment(node={_NodeSimpleReprWrapper(self.node)}, "
212 f
"payee_key={self.payee_key!r}, "
213 f
"amount={self.amount}, "
214 f
"state={self.state.name}, "
215 f
"paid={str(self.paid)}, "
216 f
"submitted={str(self.submitted)})")
220 class PaymentSummaryState(enum
.Enum
):
221 Submitted
= PayeeState
.Submitted
222 Paid
= PayeeState
.Paid
223 NotYetSubmitted
= PayeeState
.NotYetSubmitted
227 class PaymentSummary
:
228 total_submitted
: Money
229 """includes amount paid"""
231 def __init__(self
, payments
: Iterable
[Payment
]):
232 self
.payments
= tuple(payments
)
233 self
.total
= Money(0)
234 self
.total_paid
= Money(0)
235 self
.total_submitted
= Money(0)
236 self
.submitted_date
= None
237 self
.paid_date
= None
238 self
.not_submitted
= []
240 for payment
in self
.payments
:
241 if summary_state
is None:
242 summary_state
= PaymentSummaryState(payment
.state
)
243 self
.submitted_date
= payment
.submitted
244 self
.paid_date
= payment
.paid
245 elif summary_state
!= PaymentSummaryState(payment
.state
) \
246 or self
.submitted_date
!= payment
.submitted \
247 or self
.paid_date
!= payment
.paid
:
248 summary_state
= PaymentSummaryState
.Inconsistent
249 self
.paid_date
= None
250 self
.submitted_date
= None
251 self
.total
+= payment
.amount
252 if payment
.state
is PayeeState
.Submitted
:
253 self
.total_submitted
+= payment
.amount
254 elif payment
.state
is PayeeState
.Paid
:
255 self
.total_submitted
+= payment
.amount
256 self
.total_paid
+= payment
.amount
258 assert payment
.state
is PayeeState
.NotYetSubmitted
259 self
.not_submitted
.append(payment
.node
.bug
.id)
260 if summary_state
is None:
261 self
.state
= PaymentSummaryState
.NotYetSubmitted
263 self
.state
= summary_state
265 def get_not_submitted(self
):
266 return self
.not_submitted
268 def __repr__(self
) -> str:
269 return (f
"PaymentSummary(total={self.total}, "
270 f
"total_paid={self.total_paid}, "
271 f
"total_submitted={self.total_submitted}, "
272 f
"submitted_date={self.submitted_date}, "
273 f
"paid_date={self.paid_date}, "
274 f
"state={self.state}, "
275 f
"payments={self.payments})")
277 def __pretty_print__(self
, pp
: PrettyPrinter
):
278 with pp
.type_pp("PaymentSummary") as tpp
:
279 tpp
.field("total", self
.total
)
280 tpp
.field("total_submitted", self
.total_submitted
)
281 tpp
.field("submitted_date", self
.submitted_date
)
282 tpp
.field("paid_date", self
.paid_date
)
283 tpp
.field("state", self
.state
)
284 tpp
.field("payments", self
.payments
)
287 class BudgetGraphUnknownMilestone(BudgetGraphParseError
):
288 def __init__(self
, bug_id
: int, milestone_str
: str):
289 super().__init
__(bug_id
)
290 self
.milestone_str
= milestone_str
293 return f
"failed to parse cf_nlnet_milestone field of bug " \
294 f
"#{self.bug_id}: unknown milestone: {self.milestone_str!r}"
297 class BudgetGraphUnknownStatus(BudgetGraphParseError
):
298 def __init__(self
, bug_id
: int, status_str
: str):
299 super().__init
__(bug_id
)
300 self
.status_str
= status_str
303 return f
"failed to parse status field of bug " \
304 f
"#{self.bug_id}: unknown status: {self.status_str!r}"
310 parent_id
: Optional
[int]
311 immediate_children
: OrderedSet
["Node"]
312 budget_excluding_subtasks
: Money
313 budget_including_subtasks
: Money
314 fixed_budget_excluding_subtasks
: Money
315 fixed_budget_including_subtasks
: Money
316 milestone_str
: Optional
[str]
318 def __init__(self
, graph
: "BudgetGraph", bug
: Bug
):
321 self
.parent_id
= getattr(bug
, "cf_budget_parent", None)
322 self
.immediate_children
= OrderedSet()
323 self
.budget_excluding_subtasks
= Money
.from_str(bug
.cf_budget
)
324 self
.fixed_budget_excluding_subtasks
= self
.budget_excluding_subtasks
325 self
.budget_including_subtasks
= Money
.from_str(bug
.cf_total_budget
)
326 self
.fixed_budget_including_subtasks
= self
.budget_including_subtasks
327 self
.milestone_str
= bug
.cf_nlnet_milestone
328 if self
.milestone_str
== "---":
329 self
.milestone_str
= None
332 def status(self
) -> BugStatus
:
334 return BugStatus
.cast(self
.bug
.status
)
336 new_err
= BudgetGraphUnknownStatus(self
.bug
.id, self
.bug
.status
)
337 raise new_err
.with_traceback(sys
.exc_info()[2])
340 def assignee(self
) -> Person
:
342 return self
.graph
.config
.all_names
[self
.bug
.assigned_to
]
344 raise BudgetGraphUnknownAssignee(self
.bug
.id,
345 self
.bug
.assigned_to
) \
346 .with_traceback(sys
.exc_info()[2])
349 def bug_url(self
) -> str:
350 return f
"{self.graph.config.bugzilla_url_stripped}/show_bug.cgi?" \
354 def milestone(self
) -> Optional
[Milestone
]:
355 if self
.milestone_str
is None:
358 return self
.graph
.config
.milestones
[self
.milestone_str
]
360 new_err
= BudgetGraphUnknownMilestone(
361 self
.bug
.id, self
.milestone_str
)
362 raise new_err
.with_traceback(sys
.exc_info()[2])
365 def payments(self
) -> Dict
[str, Payment
]:
367 parsed
= toml
.loads(self
.bug
.cf_payees_list
)
368 except toml
.TomlDecodeError
as e
:
369 new_err
= BudgetGraphPayeesParseError(
370 self
.bug
.id, f
"TOML parse error: {e}")
371 raise new_err
.with_traceback(sys
.exc_info()[2])
373 for key
, value
in parsed
.items():
374 if not isinstance(key
, str):
375 raise BudgetGraphPayeesParseError(
376 self
.bug
.id, f
"key is not a string: {key!r}")
377 retval
[key
] = Payment
._from
_toml
(self
, key
, value
)
381 def resolved_payments(self
) -> Dict
[Person
, List
[Payment
]]:
382 retval
: Dict
[Person
, List
[Payment
]] = {}
383 for payment
in self
.payments
.values():
384 if payment
.payee
not in retval
:
385 retval
[payment
.payee
] = []
386 retval
[payment
.payee
].append(payment
)
390 def payment_summaries(self
) -> Dict
[Person
, PaymentSummary
]:
391 return {person
: PaymentSummary(payments
)
392 for person
, payments
in self
.resolved_payments
.items()}
395 def submitted_excluding_subtasks(self
) -> Money
:
397 for payment
in self
.payments
.values():
398 if payment
.submitted
is not None or payment
.paid
is not None:
399 retval
+= payment
.amount
403 def paid_excluding_subtasks(self
) -> Money
:
405 for payment
in self
.payments
.values():
406 if payment
.paid
is not None:
407 retval
+= payment
.amount
411 def submitted_including_subtasks(self
) -> Money
:
412 retval
= self
.submitted_excluding_subtasks
413 for i
in self
.immediate_children
:
414 retval
+= i
.submitted_including_subtasks
418 def paid_including_subtasks(self
) -> Money
:
419 retval
= self
.paid_excluding_subtasks
420 for i
in self
.immediate_children
:
421 retval
+= i
.paid_including_subtasks
425 def parent(self
) -> Optional
["Node"]:
426 if self
.parent_id
is not None:
427 return self
.graph
.nodes
[self
.parent_id
]
430 def parents(self
) -> Iterable
["Node"]:
432 while parent
is not None:
434 parent
= parent
.parent
436 def _raise_loop_error(self
):
438 for parent
in self
.parents():
439 bug_ids
.append(parent
.bug
.id)
442 raise BudgetGraphLoopError(bug_ids
)
445 def root(self
) -> "Node":
446 # also checks for loop errors
448 for parent
in self
.parents():
451 self
._raise
_loop
_error
()
455 def is_in_nlnet_mou(self
):
456 """returns true if this bugreport is an immediate child of a top-level
457 milestone. it does *not* return true for the top-level bug itself
458 because only the immediate children comprise the MoU.
461 if self
.parent
is not None and self
.milestone
is not None:
462 return self
.parent
.bug
.id == self
.milestone
.canonical_bug_id
463 except BudgetGraphBaseError
:
468 def closest_bug_in_mou(self
) -> Optional
["Node"]:
469 """returns the closest bug that is in a NLNet MoU, searching only in
470 this bug and parents.
472 if self
.is_in_nlnet_mou
:
474 for parent
in self
.parents():
475 if parent
.is_in_nlnet_mou
:
479 def children(self
) -> Iterable
["Node"]:
480 def visitor(node
: Node
) -> Iterable
[Node
]:
481 for i
in node
.immediate_children
:
483 yield from visitor(i
)
486 def children_breadth_first(self
) -> Iterable
["Node"]:
487 q
= deque(self
.immediate_children
)
493 q
.extend(node
.immediate_children
)
496 def __eq__(self
, other
):
497 if isinstance(other
, Node
):
498 return self
.bug
.id == other
.bug
.id
499 return NotImplemented
504 def __pretty_print__(self
, pp
: PrettyPrinter
):
505 with pp
.type_pp("Node") as tpp
:
506 tpp
.field("graph", ...)
507 tpp
.field("id", _NodeSimpleReprWrapper(self
))
508 tpp
.try_field("root",
509 lambda: _NodeSimpleReprWrapper(self
.root
),
510 BudgetGraphLoopError
)
511 parent
= f
"#{self.parent_id}" if self
.parent_id
is not None else None
512 tpp
.field("parent", parent
)
513 tpp
.field("budget_excluding_subtasks",
514 self
.budget_excluding_subtasks
)
515 tpp
.field("budget_including_subtasks",
516 self
.budget_including_subtasks
)
517 tpp
.field("fixed_budget_excluding_subtasks",
518 self
.fixed_budget_excluding_subtasks
)
519 tpp
.field("fixed_budget_including_subtasks",
520 self
.fixed_budget_including_subtasks
)
521 tpp
.field("milestone_str", self
.milestone_str
)
522 tpp
.field("is_in_nlnet_mou", self
.is_in_nlnet_mou
)
523 tpp
.try_field("milestone", lambda: self
.milestone
,
524 BudgetGraphBaseError
)
525 immediate_children
= [_NodeSimpleReprWrapper(i
)
526 for i
in self
.immediate_children
]
527 tpp
.field("immediate_children", immediate_children
)
528 tpp
.try_field("payments",
529 lambda: list(self
.payments
.values()),
530 BudgetGraphBaseError
)
532 status
= repr(self
.status
)
533 except BudgetGraphBaseError
:
534 status
= f
"<unknown status: {self.bug.status!r}>"
535 tpp
.field("status", status
)
537 assignee
= f
"Person<{self.assignee.identifier!r}>"
538 except BudgetGraphBaseError
:
539 assignee
= f
"<unknown assignee: {self.bug.assigned_to!r}>"
540 tpp
.field("assignee", assignee
)
541 tpp
.try_field("resolved_payments",
542 lambda: self
.resolved_payments
,
543 BudgetGraphBaseError
)
544 tpp
.try_field("payment_summaries",
545 lambda: self
.payment_summaries
,
546 BudgetGraphBaseError
)
550 root
= _NodeSimpleReprWrapper(self
.root
)
551 except BudgetGraphLoopError
:
552 root
= "<loop error>"
554 milestone
= repr(self
.milestone
)
555 except BudgetGraphBaseError
:
556 milestone
= "<unknown milestone>"
558 status
= repr(self
.status
)
559 except BudgetGraphBaseError
:
560 status
= f
"<unknown status: {self.bug.status!r}>"
562 assignee
= f
"Person<{self.assignee.identifier!r}>"
563 except BudgetGraphBaseError
:
564 assignee
= f
"<unknown assignee: {self.bug.assigned_to!r}>"
565 immediate_children
= []
566 for i
in self
.immediate_children
:
567 immediate_children
.append(_NodeSimpleReprWrapper(i
))
568 immediate_children
.sort()
569 parent
= f
"#{self.parent_id}" if self
.parent_id
is not None else None
570 payments
= list(self
.payments
.values())
571 resolved_payments
= self
.resolved_payments
572 payment_summaries
= self
.payment_summaries
573 return (f
"Node(graph=..., "
574 f
"id={_NodeSimpleReprWrapper(self)}, "
577 f
"budget_excluding_subtasks={self.budget_excluding_subtasks}, "
578 f
"budget_including_subtasks={self.budget_including_subtasks}, "
579 f
"fixed_budget_excluding_subtasks={self.fixed_budget_excluding_subtasks}, "
580 f
"fixed_budget_including_subtasks={self.fixed_budget_including_subtasks}, "
581 f
"milestone_str={self.milestone_str!r}, "
582 f
"is_in_nlnet_mou={self.is_in_nlnet_mou!r}, "
583 f
"milestone={milestone}, "
584 f
"immediate_children={immediate_children!r}, "
585 f
"payments={payments!r}, "
587 f
"assignee={assignee}, "
588 f
"resolved_payments={resolved_payments!r}, "
589 f
"payment_summaries={payment_summaries!r})")
592 class BudgetGraphError(BudgetGraphBaseError
):
593 def __init__(self
, bug_id
: int, root_bug_id
: int):
595 self
.root_bug_id
= root_bug_id
598 class BudgetGraphMoneyWithNoMilestone(BudgetGraphError
):
600 return (f
"Bug assigned money but without"
601 f
" any assigned milestone: #{self.bug_id}")
604 class BudgetGraphMilestoneMismatch(BudgetGraphError
):
606 return (f
"Bug's assigned milestone doesn't match the milestone "
607 f
"assigned to the root bug: descendant bug"
608 f
" #{self.bug_id}, root bug"
609 f
" #{self.root_bug_id}")
612 class BudgetGraphMoneyMismatchForBudgetExcludingSubtasks(BudgetGraphError
):
613 def __init__(self
, bug_id
: int, root_bug_id
: int,
614 expected_budget_excluding_subtasks
: Money
):
615 super().__init
__(bug_id
, root_bug_id
)
616 self
.expected_budget_excluding_subtasks
= \
617 expected_budget_excluding_subtasks
620 return (f
"Budget assigned to task excluding subtasks "
621 f
"(cf_budget field) doesn't match calculated value: "
622 f
"bug #{self.bug_id}, calculated value"
623 f
" {self.expected_budget_excluding_subtasks}")
626 class BudgetGraphMoneyMismatchForBudgetIncludingSubtasks(BudgetGraphError
):
627 def __init__(self
, bug_id
: int, root_bug_id
: int,
628 expected_budget_including_subtasks
: Money
):
629 super().__init
__(bug_id
, root_bug_id
)
630 self
.expected_budget_including_subtasks
= \
631 expected_budget_including_subtasks
634 return (f
"Budget assigned to task including subtasks "
635 f
"(cf_total_budget field) doesn't match calculated value: "
636 f
"bug #{self.bug_id}, calculated value"
637 f
" {self.expected_budget_including_subtasks}")
640 class BudgetGraphNegativeMoney(BudgetGraphError
):
642 return (f
"Budget assigned to task is less than zero: "
643 f
"bug #{self.bug_id}")
646 class BudgetGraphPayeesMoneyMismatch(BudgetGraphError
):
647 def __init__(self
, bug_id
: int, root_bug_id
: int, payees_total
: Money
,
648 expected_payees_total
: Money
):
649 super().__init
__(bug_id
, root_bug_id
)
650 self
.payees_total
= payees_total
651 self
.expected_payees_total
= expected_payees_total
654 return (f
"Total budget assigned to payees (cf_payees_list) doesn't "
655 f
"match expected value: bug #{self.bug_id}, calculated total "
656 f
"{self.payees_total}, expected value "
657 f
"{self.expected_payees_total}")
660 class BudgetGraphNegativePayeeMoney(BudgetGraphError
):
661 def __init__(self
, bug_id
: int, root_bug_id
: int, payee_key
: str):
662 super().__init
__(bug_id
, root_bug_id
)
663 self
.payee_key
= payee_key
666 return (f
"Budget assigned to payee for task is less than zero: "
667 f
"bug #{self.bug_id}, payee {self.payee_key!r}")
670 class BudgetGraphIncorrectRootForMilestone(BudgetGraphError
):
671 def __init__(self
, bug_id
: int, milestone
: str, milestone_canonical_bug_id
: int):
672 super().__init
__(bug_id
, bug_id
)
673 self
.milestone
= milestone
674 self
.milestone_canonical_bug_id
= milestone_canonical_bug_id
677 return (f
"Bug #{self.bug_id} is not the canonical root bug for "
678 f
"assigned milestone {self.milestone!r} but has no parent "
679 f
"bug set: the milestone's canonical root bug is "
680 f
"#{self.milestone_canonical_bug_id}")
684 nodes
: Dict
[int, Node
]
686 def __init__(self
, bugs
: Iterable
[Bug
], config
: Config
):
690 self
.nodes
[bug
.id] = Node(self
, bug
)
691 for node
in self
.nodes
.values():
692 if node
.parent
is None:
694 node
.parent
.immediate_children
.add(node
)
695 # useful debug prints
697 # node = self.nodes[bug.id]
698 # print ("bug added", bug.id, node, node.parent.immediate_children)
701 def roots(self
) -> OrderedSet
[Node
]:
703 for node
in self
.nodes
.values():
704 # calling .root also checks for loop errors
709 def _get_node_errors(self
, root
: Node
, node
: Node
,
710 errors
: List
[BudgetGraphBaseError
]):
711 if node
.milestone_str
is None:
712 if node
.budget_including_subtasks
!= 0 \
713 or node
.budget_excluding_subtasks
!= 0:
714 errors
.append(BudgetGraphMoneyWithNoMilestone(
715 node
.bug
.id, root
.bug
.id))
718 # check for milestone errors
720 if root
== node
and node
.milestone
is not None:
721 if node
.milestone
.canonical_bug_id
!= node
.bug
.id:
722 if node
.budget_including_subtasks
!= 0 \
723 or node
.budget_excluding_subtasks
!= 0:
724 errors
.append(BudgetGraphIncorrectRootForMilestone(
725 node
.bug
.id, node
.milestone
.identifier
,
726 node
.milestone
.canonical_bug_id
728 except BudgetGraphBaseError
as e
:
732 # check for status errors
734 except BudgetGraphBaseError
as e
:
738 # check for assignee errors
740 except BudgetGraphBaseError
as e
:
743 if node
.milestone_str
!= root
.milestone_str
:
744 errors
.append(BudgetGraphMilestoneMismatch(
745 node
.bug
.id, root
.bug
.id))
747 if node
.budget_excluding_subtasks
< 0 \
748 or node
.budget_including_subtasks
< 0:
749 errors
.append(BudgetGraphNegativeMoney(
750 node
.bug
.id, root
.bug
.id))
753 subtasks_total
= Money(0)
754 for child
in node
.immediate_children
:
755 subtasks_total
+= child
.fixed_budget_including_subtasks
756 childlist
.append(child
.bug
.id)
757 # useful debug prints
758 # print ("subtask total", node.bug.id, root.bug.id, subtasks_total,
761 payees_total
= Money(0)
762 payee_payments
: Dict
[Person
, List
[Payment
]] = {}
763 for payment
in node
.payments
.values():
764 if payment
.amount
< 0:
765 errors
.append(BudgetGraphNegativePayeeMoney(
766 node
.bug
.id, root
.bug
.id, payment
.payee_key
))
767 payees_total
+= payment
.amount
769 # check for payee errors
771 previous_payment
= payee_payments
.get(payment
.payee
)
772 if previous_payment
is not None:
773 payee_payments
[payment
.payee
].append(payment
)
775 payee_payments
[payment
.payee
] = [payment
]
776 except BudgetGraphBaseError
as e
:
779 def set_including_from_excluding_and_error():
780 node
.fixed_budget_including_subtasks
= \
781 node
.budget_excluding_subtasks
+ subtasks_total
783 BudgetGraphMoneyMismatchForBudgetIncludingSubtasks(
784 node
.bug
.id, root
.bug
.id,
785 node
.fixed_budget_including_subtasks
))
787 def set_including_from_payees_and_error():
788 node
.fixed_budget_including_subtasks
= \
789 payees_total
+ subtasks_total
791 BudgetGraphMoneyMismatchForBudgetIncludingSubtasks(
792 node
.bug
.id, root
.bug
.id,
793 node
.fixed_budget_including_subtasks
))
795 def set_excluding_from_including_and_error():
796 v
= node
.budget_including_subtasks
- subtasks_total
798 return set_including_from_excluding_and_error()
799 node
.fixed_budget_excluding_subtasks
= v
801 BudgetGraphMoneyMismatchForBudgetExcludingSubtasks(
802 node
.bug
.id, root
.bug
.id,
803 node
.fixed_budget_excluding_subtasks
))
805 def set_excluding_from_payees_and_error():
806 node
.fixed_budget_excluding_subtasks
= \
809 BudgetGraphMoneyMismatchForBudgetExcludingSubtasks(
810 node
.bug
.id, root
.bug
.id,
811 node
.fixed_budget_excluding_subtasks
))
813 def set_payees_from_including_and_error():
814 fixed_payees_total
= \
815 node
.budget_including_subtasks
- subtasks_total
816 errors
.append(BudgetGraphPayeesMoneyMismatch(
817 node
.bug
.id, root
.bug
.id, payees_total
, fixed_payees_total
))
819 def set_payees_from_excluding_and_error():
820 fixed_payees_total
= \
821 node
.budget_excluding_subtasks
822 errors
.append(BudgetGraphPayeesMoneyMismatch(
823 node
.bug
.id, root
.bug
.id, payees_total
, fixed_payees_total
))
825 payees_matches_including
= \
826 node
.budget_including_subtasks
- subtasks_total
== payees_total
827 payees_matches_excluding
= \
828 node
.budget_excluding_subtasks
== payees_total
829 including_matches_excluding
= \
830 node
.budget_including_subtasks
- subtasks_total \
831 == node
.budget_excluding_subtasks
833 if payees_matches_including \
834 and payees_matches_excluding \
835 and including_matches_excluding
:
837 elif payees_matches_including
:
838 # can't have 2 match without all 3 matching
839 assert not payees_matches_excluding
840 assert not including_matches_excluding
841 if node
.budget_including_subtasks
== 0 and len(node
.payments
) == 0:
842 set_including_from_excluding_and_error()
844 set_excluding_from_including_and_error()
845 elif payees_matches_excluding
:
846 # can't have 2 match without all 3 matching
847 assert not payees_matches_including
848 assert not including_matches_excluding
849 if node
.budget_excluding_subtasks
== 0 and len(node
.payments
) == 0:
850 if node
.budget_including_subtasks
== 0:
851 set_including_from_excluding_and_error()
853 set_excluding_from_including_and_error()
855 set_including_from_excluding_and_error()
856 elif including_matches_excluding
:
857 # can't have 2 match without all 3 matching
858 assert not payees_matches_including
859 assert not payees_matches_excluding
860 if len(node
.payments
) == 0:
861 pass # no error -- payees is just not set
862 elif node
.budget_excluding_subtasks
== 0 \
863 and node
.budget_including_subtasks
== 0:
864 set_excluding_from_payees_and_error()
865 set_including_from_payees_and_error()
867 set_payees_from_excluding_and_error()
870 if len(node
.payments
) == 0:
871 # payees unset -- don't need to set payees
872 if node
.budget_including_subtasks
== 0:
873 set_including_from_excluding_and_error()
875 set_excluding_from_including_and_error()
876 elif node
.budget_excluding_subtasks
== 0 \
877 and node
.budget_including_subtasks
== 0:
878 set_excluding_from_payees_and_error()
879 set_including_from_payees_and_error()
880 elif node
.budget_excluding_subtasks
== 0:
881 set_excluding_from_including_and_error()
882 set_payees_from_including_and_error()
883 elif node
.budget_including_subtasks
== 0:
884 set_including_from_excluding_and_error()
885 set_payees_from_excluding_and_error()
887 set_including_from_excluding_and_error()
888 set_payees_from_excluding_and_error()
890 def get_errors(self
) -> List
[BudgetGraphBaseError
]:
894 except BudgetGraphBaseError
as e
:
900 for child
in reversed(list(root
.children_breadth_first())):
902 self
._get
_node
_errors
(root
, child
, errors
)
903 except BudgetGraphBaseError
as e
:
905 self
._get
_node
_errors
(root
, root
, errors
)
906 except BudgetGraphBaseError
as e
:
911 def assigned_nodes(self
) -> Dict
[Person
, List
[Node
]]:
912 retval
: Dict
[Person
, List
[Node
]]
913 retval
= {person
: [] for person
in self
.config
.people
.values()}
914 for node
in self
.nodes
.values():
915 retval
[node
.assignee
].append(node
)
919 def assigned_nodes_for_milestones(self
) -> Dict
[Milestone
, List
[Node
]]:
920 retval
: Dict
[Milestone
, List
[Node
]]
921 retval
= {milestone
: []
922 for milestone
in self
.config
.milestones
.values()}
923 for node
in self
.nodes
.values():
924 if node
.milestone
is not None:
925 retval
[node
.milestone
].append(node
)
929 def milestone_payments(self
) -> Dict
[Milestone
, List
[Payment
]]:
930 retval
: Dict
[Milestone
, List
[Payment
]] = {
931 milestone
: [] for milestone
in self
.config
.milestones
.values()
933 for node
in self
.nodes
.values():
934 if node
.milestone
is not None:
935 retval
[node
.milestone
].extend(node
.payments
.values())
939 def payments(self
) -> Dict
[Person
, Dict
[Milestone
, List
[Payment
]]]:
940 retval
: Dict
[Person
, Dict
[Milestone
, List
[Payment
]]] = {
943 for milestone
in self
.config
.milestones
.values()
945 for person
in self
.config
.people
.values()
947 for node
in self
.nodes
.values():
948 if node
.milestone
is not None:
949 for payment
in node
.payments
.values():
950 retval
[payment
.payee
][node
.milestone
].append(payment
)
954 def milestone_people(self
) -> Dict
[Milestone
, OrderedSet
[Person
]]:
955 """get a list of people associated with each milestone
957 payments
= list(self
.payments
) # just activate the payments
959 for milestone
in self
.milestone_payments
.keys():
960 retval
[milestone
] = OrderedSet()
961 for milestone
, payments
in self
.milestone_payments
.items():
962 for payment
in payments
:
963 retval
[milestone
].add(payment
.payee
)
966 def __pretty_print__(self
, pp
: PrettyPrinter
):
967 with pp
.type_pp("BudgetGraph") as tpp
:
968 tpp
.field("nodes", self
.nodes
)
969 tpp
.try_field("roots",
970 lambda: [_NodeSimpleReprWrapper(i
)
971 for i
in self
.roots
],
972 BudgetGraphBaseError
)
973 tpp
.try_field("assigned_nodes",
976 _NodeSimpleReprWrapper(node
)
979 for person
, nodes
in self
.assigned_nodes
.items()
981 BudgetGraphBaseError
)
982 tpp
.try_field("assigned_nodes_for_milestones",
985 _NodeSimpleReprWrapper(node
)
988 for milestone
, nodes
in self
.assigned_nodes_for_milestones
.items()
990 BudgetGraphBaseError
)
991 tpp
.try_field("payments",
992 lambda: self
.payments
, BudgetGraphBaseError
)
993 tpp
.try_field("milestone_people",
994 lambda: self
.milestone_people
,
995 BudgetGraphBaseError
)
998 nodes
= [*self
.nodes
.values()]
1000 def repr_or_failed(f
: Callable
[[], Any
]) -> str:
1003 except BudgetGraphBaseError
:
1007 roots
= [_NodeSimpleReprWrapper(i
) for i
in self
.roots
]
1009 roots_str
= repr(roots
)
1010 except BudgetGraphBaseError
:
1011 roots_str
= "<failed>"
1012 assigned_nodes
= repr_or_failed(lambda: {
1014 _NodeSimpleReprWrapper(node
)
1017 for person
, nodes
in self
.assigned_nodes
.items()
1019 assigned_nodes_for_milestones
= repr_or_failed(lambda: {
1021 _NodeSimpleReprWrapper(node
)
1024 for milestone
, nodes
in self
.assigned_nodes_for_milestones
.items()
1026 milestone_payments
= repr_or_failed(lambda: self
.milestone_payments
)
1027 payments
= repr_or_failed(lambda: self
.payments
)
1028 milestone_people
= repr_or_failed(lambda: self
.milestone_people
)
1029 return (f
"BudgetGraph{{nodes={nodes!r}, "
1031 f
"assigned_nodes={assigned_nodes}, "
1032 f
"assigned_nodes_for_milestones={assigned_nodes_for_milestones}, "
1033 f
"milestone_payments={milestone_payments}, "
1034 f
"payments={payments}, "
1035 f
"milestone_people={milestone_people}}}")