#!/data/data/com.termux/files/usr/bin/python3 import os import sys import termux_log_location as tll LAST_YAML_SYMLINK_NAME = 'last.yml' def symlink(src, dst, force=False): if force and os.path.lexists(dst): os.unlink(dst) os.symlink(src=src, dst=dst) def load_last(target_dir_path): last_path = os.path.join(target_dir_path, LAST_YAML_SYMLINK_NAME) if os.path.exists(last_path): return tll.Position.load_yaml( path=os.path.realpath(last_path), ) else: return None def compute(target_dir_path, distance_threshold_metres): cur = tll.Position.from_termux_location() if cur: last = load_last(target_dir_path) if not last or tll.Position.haversine_distance_metres(last, cur) > distance_threshold_metres: cur_path = cur.save_yaml(target_dir_path) symlink( src=os.path.relpath(cur_path, start=target_dir_path), dst=os.path.join(target_dir_path, LAST_YAML_SYMLINK_NAME), force=True, ) def _init_argparser(): import argparse argparser = argparse.ArgumentParser(description=None) argparser.add_argument( '--target-dir', metavar='PATH', dest='target_dir_path', default='.', ) argparser.add_argument( '--distance-threshold', metavar='METRES', dest='distance_threshold_metres', default=0, type=float, ) return argparser def main(argv): argparser = _init_argparser() try: import argcomplete argcomplete.autocomplete(argparser) except ImportError: pass args = argparser.parse_args(argv) compute(**vars(args)) return 0 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))