log-location 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/data/data/com.termux/files/usr/bin/python3
  2. import os
  3. import sys
  4. import termux_log_location as tll
  5. LAST_YAML_SYMLINK_NAME = 'last.yml'
  6. def symlink(src, dst, force=False):
  7. if force and os.path.lexists(dst):
  8. os.unlink(dst)
  9. os.symlink(src=src, dst=dst)
  10. def load_last(target_dir_path):
  11. last_path = os.path.join(target_dir_path, LAST_YAML_SYMLINK_NAME)
  12. if os.path.exists(last_path):
  13. return tll.Position.load_yaml(
  14. path=os.path.realpath(last_path),
  15. )
  16. else:
  17. return None
  18. def compute(target_dir_path, distance_threshold_metres):
  19. cur = tll.Position.from_termux_location()
  20. if cur:
  21. last = load_last(target_dir_path)
  22. if not last or tll.Position.haversine_distance_metres(last, cur) > distance_threshold_metres:
  23. cur_path = cur.save_yaml(target_dir_path)
  24. symlink(
  25. src=os.path.relpath(cur_path, start=target_dir_path),
  26. dst=os.path.join(target_dir_path, LAST_YAML_SYMLINK_NAME),
  27. force=True,
  28. )
  29. def _init_argparser():
  30. import argparse
  31. argparser = argparse.ArgumentParser(description=None)
  32. argparser.add_argument(
  33. '--target-dir',
  34. metavar='PATH',
  35. dest='target_dir_path',
  36. default='.',
  37. )
  38. argparser.add_argument(
  39. '--distance-threshold',
  40. metavar='METRES',
  41. dest='distance_threshold_metres',
  42. default=0,
  43. type=float,
  44. )
  45. return argparser
  46. def main(argv):
  47. argparser = _init_argparser()
  48. try:
  49. import argcomplete
  50. argcomplete.autocomplete(argparser)
  51. except ImportError:
  52. pass
  53. args = argparser.parse_args(argv)
  54. compute(**vars(args))
  55. return 0
  56. if __name__ == "__main__":
  57. sys.exit(main(sys.argv[1:]))