123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- #!/data/data/com.termux/files/usr/bin/python3
- import datetime
- import json
- import os
- import pytz
- import subprocess
- import sys
- import yaml
- def to_iso6801_basic(dt):
- return dt.isoformat().replace(':', '').replace('-', '') \
- .replace('+0000', 'Z')
- def compute(target_dir_path):
- loc_json = subprocess.check_output(['termux-location', '-p', 'network']) \
- .decode(sys.stdout.encoding)
- req_time = datetime.datetime.now(pytz.utc)
- loc_attr = yaml.load(loc_json)
- target_path = os.path.join(
- target_dir_path,
- '{}.yml'.format(to_iso6801_basic(req_time)),
- )
- with open(target_path, 'w') as f:
- f.write(yaml.dump(
- loc_attr,
- default_flow_style=False,
- ))
- def _init_argparser():
- import argparse
- argparser = argparse.ArgumentParser(description=None)
- argparser.add_argument(
- '--target-dir',
- metavar='PATH',
- dest='target_dir_path',
- default='.',
- )
- 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:]))
|