annotate scripts/comment_parser.py @ 1384:7cd2a8dcdc51

Safari does not support Views having the forEach function. Implemented work around.
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Mon, 15 Feb 2016 13:48:24 +0000
parents 279930a008ca
children 1b6fa37d46a4 235594325b84 b5bf2f57187c
rev   line source
nickjillings@1316 1 #!/usr/bin/python
nickjillings@1316 2 # -*- coding: utf-8 -*-
nickjillings@1316 3
nickjillings@1316 4 import xml.etree.ElementTree as ET
nickjillings@1316 5 import os
nickjillings@1316 6 import csv
nickjillings@1316 7 import sys
nickjillings@1316 8
nickjillings@1316 9 # COMMAND LINE ARGUMENTS
nickjillings@1316 10
nickjillings@1316 11 assert len(sys.argv)<3, "comment_parser takes at most 1 command line argument\n"+\
nickjillings@1316 12 "Use: python score_parser.py [rating_folder_location]"
nickjillings@1316 13
nickjillings@1316 14 # XML results files location
nickjillings@1316 15 if len(sys.argv) == 1:
nickjillings@1316 16 folder_name = "../saves" # Looks in 'saves/' folder from 'scripts/' folder
nickjillings@1316 17 print "Use: python comment_parser.py [XML_files_location]"
nickjillings@1316 18 print "Using default path: " + folder_name
nickjillings@1316 19 elif len(sys.argv) == 2:
nickjillings@1316 20 folder_name = sys.argv[1] # First command line argument is folder
nickjillings@1316 21
nickjillings@1316 22 # check if folder_name exists
nickjillings@1316 23 if not os.path.exists(folder_name):
nickjillings@1316 24 #the file is not there
nickjillings@1316 25 print "Folder '"+folder_name+"' does not exist."
nickjillings@1316 26 sys.exit() # terminate script execution
nickjillings@1316 27 elif not os.access(os.path.dirname(folder_name), os.W_OK):
nickjillings@1316 28 #the file does exist but write privileges are not given
nickjillings@1316 29 print "No write privileges in folder '"+folder_name+"'."
nickjillings@1316 30
nickjillings@1316 31
nickjillings@1316 32 # CODE
nickjillings@1316 33
nickjillings@1316 34 # remember which files have been opened this time
nickjillings@1316 35 file_history = []
nickjillings@1316 36
nickjillings@1316 37 # get every XML file in folder
nickjillings@1316 38 for file in os.listdir(folder_name):
nickjillings@1316 39 if file.endswith(".xml"):
nickjillings@1316 40 tree = ET.parse(folder_name + '/' + file)
nickjillings@1316 41 root = tree.getroot()
nickjillings@1316 42
nickjillings@1316 43 # get list of all page names
nickjillings@1316 44 for audioholder in root.findall("./audioholder"): # iterate over pages
nickjillings@1316 45 page_name = audioholder.get('id') # get page name
nickjillings@1316 46
nickjillings@1316 47 if page_name is None: # ignore 'empty' audio_holders
nickjillings@1316 48 print "WARNING: " + file + " contains empty audio holder. (comment_parser.py)"
nickjillings@1316 49 break
nickjillings@1316 50
nickjillings@1316 51 # create folder [page_name] if not yet created
nickjillings@1316 52 if not os.path.exists(folder_name + "/" + page_name):
nickjillings@1316 53 os.makedirs(folder_name + "/" + page_name)
nickjillings@1316 54
nickjillings@1316 55 # for page [page_name], print comments related to fragment [id]
nickjillings@1316 56 for audioelement in audioholder.findall("./audioelement"):
nickjillings@1316 57 if audioelement is not None: # Check it exists
nickjillings@1316 58 audio_id = str(audioelement.get('id'))
nickjillings@1316 59
nickjillings@1316 60 csv_name = folder_name +'/' + page_name+'/'+page_name+'-comments-'+audio_id+'.csv'
nickjillings@1316 61
nickjillings@1316 62 # If file hasn't been opened yet this time, empty
nickjillings@1316 63 if csv_name not in file_history:
nickjillings@1316 64 csvfile = open(csv_name, 'w')
nickjillings@1316 65 file_history.append(csv_name) # remember this file has been written to this time around
nickjillings@1316 66 else:
nickjillings@1316 67 # append (!) to file [page_name]/[page_name]-comments-[id].csv
nickjillings@1316 68 csvfile = open(csv_name, 'a')
nickjillings@1316 69 writer = csv.writer(csvfile,
nickjillings@1316 70 delimiter=',',
nickjillings@1316 71 dialect="excel",
nickjillings@1316 72 quoting=csv.QUOTE_ALL)
nickjillings@1316 73 commentstr = audioelement.find("./comment/response").text
nickjillings@1316 74
nickjillings@1316 75 if commentstr is None:
nickjillings@1316 76 commentstr = ''
nickjillings@1316 77
nickjillings@1316 78 # anonymous comments:
nickjillings@1316 79 #writer.writerow([commentstr.encode("utf-8")])
nickjillings@1316 80 # comments with (file) name:
nickjillings@1316 81 writer.writerow([file[:-4]] + [commentstr.encode("utf-8")])
nickjillings@1316 82
nickjillings@1316 83 #TODO Replace 'new line' in comment with something else?
nickjillings@1316 84
nickjillings@1316 85 # PRO TIP: Change from csv to txt by running this in bash:
nickjillings@1316 86 # $ cd folder_where_csvs_are/
nickjillings@1316 87 # $ for i in *.csv; do mv "$i" "${i/.csv}".txt; done