diff options
author | Alasdair Colley <alnessy@hotmail.com> | 2014-07-17 16:59:04 +0200 |
---|---|---|
committer | Alasdair Colley <alnessy@hotmail.com> | 2014-07-17 16:59:04 +0200 |
commit | 9b48fe3a73af2e453363af3d7c84d30bb39f3745 (patch) | |
tree | 94f73e79643b522d652e640db342415b4cc3e3c1 |
initial commit
-rwxr-xr-x | saucypy | 66 |
1 files changed, 66 insertions, 0 deletions
@@ -0,0 +1,66 @@ +#! /usr/bin/env python + +import difflib +import json +import optparse +import sys + +VERSION = "1.0.0" + +ENTRIES_FILE = "blasource.json" + +items = { + "regex": "regexes are useful", + "regex-pal": "regex-pal url", + "regex flash": "regex flash url" +} + +def find( key, entries ): + return difflib.get_close_matches( key, entries.keys() ) + +def parse_args(): + parser = optparse.OptionParser() + parser.add_option( "-a", "--add", action = "store", nargs = 2 ) + parser.add_option( "-i", "--index", action = "store", type = int ) + parser.add_option( "-v", "--version", action = "store_true" ) + return parser.parse_args() + +def main(): + options, args = parse_args() + + if options.version: + print VERSION + sys.exit( 0 ) + + try: + entries = json.load( open( ENTRIES_FILE ) ) + except IOError: + entries = {} + + if options.add: + key, value = options.add + entries[key] = value + + json.dump( entries, open( ENTRIES_FILE, "w" ), indent = 4, sort_keys = True ) + else: + if not args: + print "Expected a search argument, got nothing." + sys.exit( 1 ) + + matches = find( args[0], entries ) + if len( matches ) > 1: + if options.index is not None: + try: + print entries[matches[options.index]] + except IndexError: + print "Match index out of range." + sys.exit( 1 ) + else: + print "Matches (use -i option to pick one) -> {0}".format( ", ".join( "{0}: {1}".format( i, m ) for i, m in enumerate( matches ) ) ) + elif matches: + print entries[matches[0]] + else: + print "No match found." + +if __name__ == "__main__": + main() |