Minor refactor for the index page's table.
David Blume

David Blume commited on 2016-09-11 10:47:25
Showing 1 changed files, with 23 additions and 21 deletions.

... ...
@@ -45,6 +45,7 @@ def write_file(full_pathname, data):
45 45
 
46 46
 
47 47
 def map_ip_to_location(ip):
48
+    """ Use a meaningful location instead of an IP address where possible."""
48 49
     if ip == '67.188.28.83':
49 50
         return 'home'.ljust(13)
50 51
     elif ip in ('50.224.7.233', '50.224.7.248'):
... ...
@@ -52,6 +53,24 @@ def map_ip_to_location(ip):
52 53
     return ip
53 54
 
54 55
 
56
+device_prefixes = { '1G': '(Au)',
57
+                    '5S': '(Br)',
58
+                    'YW': '(FW)',
59
+                    'YY': '(Da)',
60
+                    '2N': '(Li)',
61
+                    '1R': '(Ty)',
62
+                    'YU': '(Lf)',
63
+                    'YP': '(C4)',
64
+                  }
65
+
66
+def map_key_to_prefix(k):
67
+    """ Return a hint about the key if we can."""
68
+    try:
69
+        return "%s " % device_prefixes[k[:2]]
70
+    except KeyError:
71
+        pass
72
+    return ""
73
+
55 74
 if __name__ == '__main__':
56 75
     localdir = os.path.abspath(os.path.dirname(sys.argv[0]))
57 76
     args = cgi.FieldStorage()
... ...
@@ -116,32 +135,15 @@ if __name__ == '__main__':
116 135
             # just the home page, then
117 136
             print "Content-type: text/plain; charset=UTF-8\n"
118 137
             wrote_response = True
119
-            print "key          \tvalue         \tcreated \tupdated \torigin      \tNotes"
120
-            print "------------ \t------------- \t----------\t----------\t-------------\t---------------------"
121
-            devices = { '1G': '(Au)',
122
-                        '5S': '(Br)',
123
-                        'YW': '(FW)',
124
-                        'YY': '(Da)',
125
-                        '2N': '(Li)',
126
-                        '1R': '(Ty)',
127
-                        'YU': '(Lf)',
128
-                        'YP': '(C4)',
129
-                      }
138
+            headers = ['key', 'value', 'created', 'updated', 'origin', 'notes']
139
+            print "\t".join([i.ljust(12) for i in headers])
140
+            print "\t".join([('-' * len(i)).ljust(12) for i in headers])
130 141
             for key in sorted(store, key=lambda k: store[k]['time'], reverse=True):
131
-                if key[:2] in devices:
132
-                    device = devices[key[:2]] + ' '
133
-                else:
134
-                    device = ''
142
+                device = map_key_to_prefix(key)
135 143
                 d = store[key]
136 144
                 td = texttime.stringify(datetime.timedelta(seconds=time.time()-d['time']))
137
-                if 'created' in d:
138 145
                 created = time.strftime('%Y-%m-%d', time.localtime(d['created']))
139
-                else:
140
-                    created = "          "
141
-                if 'origin' in d:
142 146
                 origin = map_ip_to_location(d['origin'])
143
-                else:
144
-                    origin = "               "
145 147
                 print '\t'.join((key.ljust(12), d['value'].ljust(13), created, str(d['time']), origin, "%supdated %s ago" % (device, td)))
146 148
             print
147 149
             print 'Tip:'
148 150