annotate scripts/comment_parser.py @ 1460:1b81ab727352

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