Created
April 23, 2015 10:41
-
-
Save jorge-lavin/b251bc55e3355dae4e79 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Parser for amazon appliances data | |
""" | |
#TODO: Remove trailing '\n' in entries | |
import pprint | |
import sys | |
FILE_NAME='maestroparseador.txt' | |
def remove_empty(list_to_process, value=''): | |
return [ entry for entry in list_to_process if entry != value ] | |
def process_line(line): | |
task_type = 'TaskType' | |
disk_image = 'DISKIMAGE' | |
# Process TaskType line | |
if line[0] == task_type: | |
keys = remove_empty(line)[0::2] | |
values = remove_empty(line)[1::2] | |
return dict(zip(keys, values)) | |
# Process DISKIMAGE line | |
elif line[0] == disk_image: | |
keys = remove_empty(line)[1::2] | |
values = remove_empty(line)[2::2] | |
return dict(zip(keys, values)) | |
elif line == ['\n']: | |
return dict() | |
else: | |
raise ValueError('Line does not begin with {0} or {1}'.format(task_type, disk_image)) | |
def find(app_name, app): | |
import_url = 'ImportManifestUrl' | |
try: | |
url = app[import_url] | |
if app_name in url: | |
return True | |
except KeyError: | |
return False | |
def print_usage(): | |
usage = """ | |
parser.py APPLIANCE | |
APPLIANCE appliance name to be found. Quote if whitespace is included""" | |
print(usage) | |
if __name__ == '__main__': | |
with open(FILE_NAME) as f: | |
appliances = [process_line(line.split(' ')) for line in f ] | |
try: | |
app_to_find = sys.argv[1] | |
except IndexError: | |
print_usage() | |
exit(1) | |
for app in appliances: | |
if find(app_to_find, app): | |
pprint.pprint(app) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment