PageRenderTime 67ms CodeModel.GetById 64ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/mysql_watcher/browse_queries

https://bitbucket.org/lindenlab/apiary/
Python | 85 lines | 61 code | 13 blank | 11 comment | 12 complexity | af452f4df98b17651337893e6f6db797 MD5 | raw file
 1#!/usr/bin/python
 2
 3"""
 4Browse query result files, and view/edit metadata associated with queries
 5"""
 6import os.path
 7if os.path.exists("../setup-path.py"):
 8    execfile("../setup-path.py")
 9import getopt, sys
10
11import curses
12import curses.textpad
13import curses.wrapper
14import string
15import sys
16import time
17
18from llbase import llsd
19from dblibs.dbbrowser import *
20
21def sort_by_field(d, field):
22    "Returns the keys of dictionary d sorted by the value in the field"
23    items=d.items()
24    backitems=[ [v[1][field],v[0]] for v in items]
25    backitems.sort()
26    return [ backitems[i][1] for i in range(0,len(backitems))]
27
28LIST_HEIGHT = 20
29
30def dump_query_info(query_map, fn):
31    #
32    # Sort by the map key (so that there's some hope of merging
33    #
34    sorted_keys = query_map.keys()
35    sorted_keys.sort()
36
37    out = []
38    for key in sorted_keys:
39        row = query_map[key]
40        out_row = {}
41        out_row['query'] = row['query']
42        out_row['host'] = row['host']
43        out_row['notes'] = row['notes']
44        out.append(out_row)
45
46    f = open(fn, "w")
47    f.write(str(llsd.LLSD(out)))
48    f.close()
49
50
51def display_queries(stdscr, query_stats, query_metadata):
52    # Create the query browser
53    query_browser = LLQueryBrowser(stdscr, query_stats, query_metadata)
54    query_browser.redraw()
55
56    done = False
57    while not done:
58        c = stdscr.getch()
59        if query_browser.handleKey(c):
60            pass
61        elif c == ord('q'):
62            # Quit
63            query_metadata.save("./query_info.llsd")
64            done = True
65            break
66        if c == ord('w'):
67            query_metadata.save("./query_info.llsd")
68            pass
69        elif c == ord('s'):
70            query_browser.toggleSort()
71        elif c == ord('n'):
72            # Go forth and edit notes
73            query_browser.editSelectedText('notes')
74        else:
75            print "Pressed key %s" % c
76        query_browser.redraw()
77
78if __name__ == "__main__":
79    path = sys.argv[1]
80
81    # Read in metadata
82    query_metadata = LLQueryMetadata("./query_info.llsd")
83    query_stats = LLQueryStatMap(path, time.time())
84    query_stats.load(path)
85    curses.wrapper(display_queries, query_stats, query_metadata)