def main(): options = cliargs.get_options(args) if options['input'] is '-': input_handle = sys.stdin else: try: input_handle = open(options['input'], 'rb') except: die("Couldn't open file '{}'".format(options['input'])) if options['output'] is '-': output_handle = sys.stdout else: try: output_handle = open(options['output'], 'wb') except: die("Couldn't write to file '{}'".format(options['output'])) text = input_handle.read() # cProfile.run('locations = geodict_lib.find_locations_in_text(text)') locations = geodict_lib.find_locations_in_text(text) output_string = '' if options['format'].lower() == 'json': output_string = json.dumps(locations) output_handle.write(output_string) elif format.lower() == 'text': for location in locations: found_tokens = location['found_tokens'] start_index = found_tokens[0]['start_index'] end_index = found_tokens[len(found_tokens) - 1]['end_index'] output_string += text[start_index:(end_index + 1)] output_string += "\n" output_handle.write(output_string) elif options['format'].lower() == 'csv': writer = csv.writer(output_handle) writer.writerow(['location', 'type', 'lat', 'lon']) for location in locations: found_tokens = location['found_tokens'] start_index = found_tokens[0]['start_index'] end_index = found_tokens[len(found_tokens) - 1]['end_index'] writer.writerow([ text[start_index:(end_index + 1)], found_tokens[0]['type'].lower(), found_tokens[0]['lat'], found_tokens[0]['lon'] ]) else: print("Unknown output format '{}'".format(format)) exit()
'default': '' }, 'queryurl': { 'short': 'q', 'type': 'optional', 'description': 'This is an optional argument, if -q/--queryurl is not specified, the default value of "empty" will be set into the result array', 'default': 'http://example.com' }, 'dohost': { 'short': 'h', 'type': 'switch', 'description': 'Switches default to false if they\'re not included on the command line, or true if they are present (you don\'t specify a value)', } } options = cliargs.get_options(args) filename = options['filename'] queryurl = options['queryurl'] dohost = options['dohost'] if filename == 'bad': print "You gave me a filename I didn't like: '"+filename+"'"; cliargs.print_usage_and_exit(args); output = "All arguments set: filename='"+filename+"', queryurl='"+queryurl+"', dohost='"; if dohost: output += "True" else: output += "False" output += "'"
'short': 'o', 'type': 'optional', 'description': 'The name of the file to write the location data to. If none is set, will write to STDOUT', 'default': '-' }, 'format': { 'short': 'f', 'type': 'optional', 'description': 'The format to use to output information about any locations found. By default it will write out location names separated by newlines, but specifying "json" will give more detailed information', 'default': 'text' } } options = cliargs.get_options(args) input = options['input'] output = options['output'] format = options['format'] if input is '-': input_handle = sys.stdin else: try: input_handle = open(input, 'rb') except: die("Couldn't open file '" + input + "'") if output is '-': output_handle = sys.stdout