Fabian Peter Hammerle 6 years ago
parent
commit
e135bbaa6c
1 changed files with 26 additions and 21 deletions
  1. 26 21
      log-location

+ 26 - 21
log-location

@@ -14,6 +14,12 @@ def to_iso6801_basic(dt):
         .replace('+0000', 'Z')
 
 
+def symlink(src, dst, force=False):
+    if force and os.path.lexists(dst):
+        os.unlink(dst)
+    os.symlink(src=src, dst=dst)
+
+
 class Position:
 
     def __init__(self, timestamp, latitude, longitude, altitude):
@@ -31,7 +37,6 @@ class Position:
         if not loc_attr:
             return None
         else:
-            print(loc_attr)
             return cls(
                 timestamp=req_ts -
                     dt.timedelta(milliseconds=loc_attr['elapsedMs']),
@@ -40,30 +45,30 @@ class Position:
                 altitude=loc_attr['altitude'],
             )
 
-
-def symlink(src, dst, force=False):
-    if force and os.path.lexists(dst):
-        os.unlink(dst)
-    os.symlink(src=src, dst=dst)
+    def save_yaml(self, target_dir_path):
+        path = os.path.join(
+            target_dir_path,
+            '{}.yml'.format(to_iso6801_basic(self.timestamp)),
+        )
+        attr = {
+            'lat': self.latitude,
+            'lon': self.longitude,
+            'alt': self.altitude,
+        }
+        with open(path, 'w') as f:
+            f.write(yaml.dump(attr, default_flow_style=False))
+        return path
 
 
 def compute(target_dir_path):
     cur = Position.from_termux_location()
-    target_name = '{}.yml'.format(to_iso6801_basic(cur.timestamp))
-    with open(os.path.join(target_dir_path, target_name), 'w') as f:
-        f.write(yaml.dump(
-            {
-                'lat': cur.latitude,
-                'lon': cur.longitude,
-                'alt': cur.altitude,
-            },
-            default_flow_style=False,
-        ))
-    symlink(
-        src=target_name,
-        dst=os.path.join(target_dir_path, 'last.yml'),
-        force=True,
-    )
+    if cur:
+        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.yml'),
+            force=True,
+        )
 
 
 def _init_argparser():