join-adjascent 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python3
  2. import sys
  3. import typing
  4. import pandas
  5. def _merge_adjacent(
  6. recordings: pandas.DataFrame, remove_tags: typing.Set[str]
  7. ) -> typing.Iterator[pandas.Series]:
  8. previous_recording: typing.Optional[pandas.Series] = None
  9. for _, next_recording in recordings.sort_values("start").iterrows():
  10. next_recording["tags"] = set(next_recording["tags"]).difference(remove_tags)
  11. if previous_recording is not None:
  12. if next_recording["start"] <= previous_recording["end"]:
  13. previous_recording["end"] = next_recording["end"]
  14. previous_recording["tags"].update(next_recording["tags"])
  15. else:
  16. yield previous_recording
  17. previous_recording = next_recording
  18. else:
  19. previous_recording = next_recording
  20. if previous_recording is not None:
  21. yield previous_recording
  22. def _main():
  23. pandas.set_option("display.width", None)
  24. pandas.set_option("display.max_rows", None)
  25. # https://timewarrior.net/docs/api/
  26. config_text, recordings_json = sys.stdin.read().split("\n\n", maxsplit=1)
  27. config = dict(o.split(": ", maxsplit=1) for o in config_text.splitlines())
  28. selected_tags = set(config["temp.report.tags"].split(","))
  29. print(selected_tags)
  30. recordings = pandas.DataFrame(
  31. _merge_adjacent(
  32. pandas.read_json(recordings_json, convert_dates=["start", "end"]).set_index(
  33. "id", verify_integrity=True
  34. ),
  35. remove_tags=selected_tags,
  36. )
  37. ).assign(
  38. start=lambda df: df["start"]
  39. .dt.tz_convert("Europe/Vienna")
  40. .dt.strftime("%Y-%m-%d %H:%M"),
  41. end=lambda df: df["end"].dt.tz_convert("Europe/Vienna").dt.strftime("%H:%M"),
  42. )
  43. print(recordings)
  44. if __name__ == "__main__":
  45. _main()