Use list comprehension.
David Blume

David Blume commited on 2016-09-11 01:23:13
Showing 1 changed files, with 5 additions and 20 deletions.

... ...
@@ -56,7 +56,6 @@ if __name__ == '__main__':
56 56
     localdir = os.path.abspath(os.path.dirname(sys.argv[0]))
57 57
     args = cgi.FieldStorage()
58 58
     key = None
59
-    extra = None
60 59
     auth = None
61 60
     get_key = False
62 61
     auth_in_get = False
... ...
@@ -83,28 +82,13 @@ if __name__ == '__main__':
83 82
         if not auth_in_get and "auth" in args:
84 83
             auth = args["auth"].value
85 84
 
86
-    if "extra" in args:
87
-        extra = args["extra"].value
88
-
89 85
     store = read_file(os.path.join(localdir, store_name))
90 86
     if get_key:
91 87
         keys = key.split(',')
92
-        if len(keys) > 1:
93
-            devices = set()
94
-            for k in store:
95
-                d = store[k]
96
-                if k in keys:
97
-                    devices.add((d['time'], key, d['value']))
98
-            if devices:  # not empty
99
-                print "Content-type: text/plain; charset=utf-8\n"
100
-                sys.stdout.write(sorted(devices, reverse=True)[0][2])
101
-            else:
102
-                print 'Status: 404 Not Found\n'
103
-        elif key in store:
88
+        matches = sorted([(store[x]['time'], store[x]['value']) for x in keys if x in store], reverse=True)
89
+        if matches:
104 90
             print "Content-type: text/plain; charset=utf-8\n"
105
-            sys.stdout.write(store[key]['value'])
106
-            if extra is not None:
107
-                sys.stdout.write('\n%s' % (store[key]['time'],))
91
+            sys.stdout.write(matches[0][1])
108 92
         else:
109 93
             print 'Status: 404 Not Found\n'
110 94
     else:
... ...
@@ -113,7 +97,7 @@ if __name__ == '__main__':
113 97
             authorization = f.read().strip()
114 98
         if doing_post and auth == authorization:
115 99
             for new_key in args:
116
-                if new_key not in ('auth', 'extra') and \
100
+                if new_key not in ('auth', ) and \
117 101
                     len(new_key) < 80 and len(args[new_key].value) < 140:
118 102
                     if new_key in store and 'created' in store[new_key]:
119 103
                         created_time = store[new_key]['created']
... ...
@@ -163,6 +147,7 @@ if __name__ == '__main__':
163 147
             print 'Tip:'
164 148
             print 'sync_ip () { export ENV_VAR=$(curl -s "%s?k=2FN50L002036,2FN44N010911"); }' % os.environ['SCRIPT_URI']
165 149
             print 'curl --data "`hostname -s`=`hostname -i`&auth=$AUTH" "%s"' % os.environ['SCRIPT_URI']
150
+            print 'Source code at http://git.dlma.com/kvs.git/'
166 151
 
167 152
         if not wrote_response:
168 153
             print 'Status: 400 Bad Request\n'
169 154