Digitale bierlijst

cli.py 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. from typing import Optional
  2. import click
  3. from prettytable import PrettyTable
  4. from piket_client.model import (
  5. AardbeiActivity,
  6. ServerStatus,
  7. NetworkError,
  8. Consumption,
  9. AardbeiPeopleDiff,
  10. Person,
  11. Settlement,
  12. ConsumptionType,
  13. )
  14. @click.group()
  15. def cli():
  16. """Poke coco from the command line."""
  17. pass
  18. @cli.command()
  19. def status():
  20. """Show the current status of the server."""
  21. status = ServerStatus.is_server_running()
  22. if isinstance(status, NetworkError):
  23. print_error(f"Failed to get data from server, error {status.value}")
  24. return
  25. print_ok("Server is available.")
  26. open_consumptions = ServerStatus.unsettled_consumptions()
  27. if isinstance(open_consumptions, NetworkError):
  28. print_error(
  29. f"Failed to get unsettled consumptions, error {open_consumptions.value}"
  30. )
  31. return
  32. click.echo(f"There are {open_consumptions.amount} unsettled consumptions.")
  33. if open_consumptions.amount > 0:
  34. click.echo(f"First at: {open_consumptions.first_timestamp.strftime('%c')}")
  35. click.echo(f"Most recent at: {open_consumptions.last_timestamp.strftime('%c')}")
  36. @cli.group()
  37. def people():
  38. pass
  39. @people.command("list")
  40. @click.option("--active/--inactive", default=None)
  41. def list_people(active: bool) -> None:
  42. people = Person.get_all(active=active)
  43. if isinstance(people, NetworkError):
  44. print_error(f"Could not get people: {people.value}")
  45. return
  46. table = PrettyTable()
  47. table.field_names = ["ID", "Full name", "Display name", "Active"]
  48. table.align["ID"] = "r"
  49. table.align["Full name"] = "l"
  50. table.align["Display name"] = "l"
  51. table.sortby = "Full name"
  52. for p in people:
  53. table.add_row([p.person_id, p.full_name, p.display_name, p.active])
  54. print(table)
  55. @people.command("create")
  56. @click.option("--display-name", type=click.STRING)
  57. @click.argument("name", type=click.STRING)
  58. def create_person(name: str, display_name: str) -> None:
  59. """Create a person."""
  60. person = Person(full_name=name, display_name=display_name).create()
  61. if isinstance(person, NetworkError):
  62. print_error(f"Could not create Person: {person.value}")
  63. return
  64. print_ok(f'Created person "{name}" with ID {person.person_id}.')
  65. @people.command("rename")
  66. @click.argument("person-id", type=click.INT)
  67. @click.option("--new-full-name", type=click.STRING)
  68. @click.option("--new-display-name", type=click.STRING)
  69. def rename_person(
  70. person_id: int, new_full_name: Optional[str], new_display_name: Optional[str],
  71. ) -> None:
  72. person = Person.get(person_id)
  73. if person is None:
  74. raise click.UsageError(f"Cannot find Person {person_id}!")
  75. if new_full_name is None and new_display_name is None:
  76. raise click.UsageError("No new full name or display name specified!")
  77. new_person = person.rename(
  78. new_full_name=new_full_name, new_display_name=new_display_name
  79. )
  80. @cli.group()
  81. def settlements():
  82. pass
  83. @settlements.command("show")
  84. @click.argument("settlement_id", type=click.INT)
  85. def show_settlement(settlement_id: int) -> None:
  86. """Get and view the contents of a Settlement."""
  87. s = Settlement.get(settlement_id)
  88. if isinstance(s, NetworkError):
  89. print_error(f"Could not get Settlement: {s.value}")
  90. return
  91. output_settlement_info(s)
  92. @settlements.command("create")
  93. @click.argument("name")
  94. def create_settlement(name: str) -> None:
  95. """Create a new Settlement."""
  96. s = Settlement.create(name)
  97. if isinstance(s, NetworkError):
  98. print_error(f"Could not create Settlement: {s.value}")
  99. return
  100. output_settlement_info(s)
  101. def output_settlement_info(s: Settlement) -> None:
  102. click.echo(f'Settlement {s.settlement_id}, "{s.name}"')
  103. click.echo(f"Summary:")
  104. for key, value in s.consumption_summary.items():
  105. click.echo(f" - {value['count']} {value['name']} ({key})")
  106. ct_name_by_id = {key: value["name"] for key, value in s.consumption_summary.items()}
  107. table = PrettyTable()
  108. table.field_names = ["Name", *ct_name_by_id.values()]
  109. table.sortby = "Name"
  110. table.align = "r"
  111. table.align["Name"] = "l" # type: ignore
  112. zero_fields = {k: "" for k in ct_name_by_id.values()}
  113. for item in s.per_person_counts.values():
  114. r = {"Name": item["full_name"], **zero_fields}
  115. for key, value in item["counts"].items():
  116. r[ct_name_by_id[key]] = value
  117. table.add_row(r.values())
  118. print(table)
  119. @cli.group()
  120. def consumption_types():
  121. pass
  122. @consumption_types.command("list")
  123. def list_consumption_types() -> None:
  124. active = ConsumptionType.get_all(active=True)
  125. inactive = ConsumptionType.get_all(active=False)
  126. if isinstance(active, NetworkError) or isinstance(inactive, NetworkError):
  127. print_error("Could not get consumption types!")
  128. return
  129. table = PrettyTable()
  130. table.field_names = ["ID", "Name", "Active"]
  131. table.sortby = "ID"
  132. for ct in active + inactive:
  133. table.add_row([ct.consumption_type_id, ct.name, ct.active])
  134. print(table)
  135. @consumption_types.command("create")
  136. @click.argument("name")
  137. def create_consumption_type(name: str) -> None:
  138. ct = ConsumptionType(name=name).create()
  139. if not isinstance(ct, NetworkError):
  140. print_ok(f'Created consumption type "{name}" with ID {ct.consumption_type_id}.')
  141. @consumption_types.command("activate")
  142. @click.argument("consumption_type_id", type=click.INT)
  143. def activate_consumption_type(consumption_type_id: int) -> None:
  144. ct = ConsumptionType.get(consumption_type_id)
  145. if isinstance(ct, NetworkError):
  146. print_error(f"Could not get ConsumptionType: {ct.value}")
  147. return
  148. result = ct.set_active(True)
  149. if not isinstance(result, NetworkError):
  150. print_ok(
  151. f"Consumption type {ct.consumption_type_id} ({ct.name}) is now active."
  152. )
  153. @consumption_types.command("deactivate")
  154. @click.argument("consumption_type_id", type=click.INT)
  155. def deactivate_consumption_type(consumption_type_id: int) -> None:
  156. ct = ConsumptionType.get(consumption_type_id)
  157. if isinstance(ct, NetworkError):
  158. print_error(f"Could not get ConsumptionType: {ct.value}")
  159. return
  160. result = ct.set_active(False)
  161. if not isinstance(result, NetworkError):
  162. print_ok(
  163. f"Consumption type {ct.consumption_type_id} ({ct.name}) is now inactive."
  164. )
  165. def print_ok(msg: str) -> None:
  166. click.echo(click.style(msg, fg="green"))
  167. def print_error(msg: str) -> None:
  168. click.echo(click.style(msg, fg="red", bold=True), err=True)
  169. @cli.group()
  170. @click.option("--token", required=True, envvar="AARDBEI_TOKEN")
  171. @click.option("--endpoint", default="http://localhost:3000", envvar="AARDBEI_ENDPOINT")
  172. @click.pass_context
  173. def aardbei(ctx, token: str, endpoint: str) -> None:
  174. ctx.ensure_object(dict)
  175. ctx.obj["AardbeiToken"] = token
  176. ctx.obj["AardbeiEndpoint"] = endpoint
  177. @aardbei.group("activities")
  178. def aardbei_activities() -> None:
  179. pass
  180. @aardbei_activities.command("list")
  181. @click.pass_context
  182. def aardbei_list_activities(ctx) -> None:
  183. acts = AardbeiActivity.get_available(
  184. token=ctx.obj["AardbeiToken"], endpoint=ctx.obj["AardbeiEndpoint"]
  185. )
  186. if isinstance(acts, NetworkError):
  187. print_error(f"Could not get activities: {acts.value}")
  188. return
  189. table = PrettyTable()
  190. table.field_names = ["ID", "Name"]
  191. table.align = "l"
  192. for a in acts:
  193. table.add_row([a.aardbei_id, a.name])
  194. print(table)
  195. @aardbei_activities.command("apply")
  196. @click.argument("activity_id", type=click.INT)
  197. @click.pass_context
  198. def aardbei_apply_activity(ctx, activity_id: int) -> None:
  199. result = AardbeiActivity.apply_activity(
  200. token=ctx.obj["AardbeiToken"],
  201. endpoint=ctx.obj["AardbeiEndpoint"],
  202. activity_id=activity_id,
  203. )
  204. if isinstance(result, NetworkError):
  205. print_error("Failed to apply activity: {result.value}")
  206. return
  207. print_ok(f"Activity applied. There are now {result} active people.")
  208. @aardbei.group("people")
  209. def aardbei_people() -> None:
  210. pass
  211. @aardbei_people.command("diff")
  212. @click.pass_context
  213. def aardbei_diff_people(ctx) -> None:
  214. diff = AardbeiPeopleDiff.get_diff(
  215. token=ctx.obj["AardbeiToken"], endpoint=ctx.obj["AardbeiEndpoint"]
  216. )
  217. if isinstance(diff, NetworkError):
  218. print_error(f"Could not get differences: {diff.value}")
  219. return
  220. if diff.num_changes == 0:
  221. print_ok("There are no changes to apply.")
  222. return
  223. click.echo(f"There are {diff.num_changes} pending changes:")
  224. show_diff(diff)
  225. @aardbei_people.command("sync")
  226. @click.pass_context
  227. def aardbei_sync_people(ctx) -> None:
  228. diff = AardbeiPeopleDiff.sync(
  229. token=ctx.obj["AardbeiToken"], endpoint=ctx.obj["AardbeiEndpoint"]
  230. )
  231. if isinstance(diff, NetworkError):
  232. print_error(f"Could not apply differences: {diff.value}")
  233. return
  234. if diff.num_changes == 0:
  235. print_ok("There were no changes to apply.")
  236. return
  237. print_ok(f"Applied {diff.num_changes} pending changes:")
  238. show_diff(diff)
  239. def show_diff(diff: AardbeiPeopleDiff) -> None:
  240. for name in diff.new_people:
  241. click.echo(f" - Create local Person for {name}")
  242. for name in diff.link_existing:
  243. click.echo(f" - Link local and remote people for {name}")
  244. for name in diff.altered_name:
  245. click.echo(f" - Process name change for {name}")
  246. if __name__ == "__main__":
  247. cli()