# CELTX CHARACTER + SCENE INVOLVEMENT SCRIPT
# ######################################################################
'''
This program opens a .celtx file, and reads the script. It finds all of
the unique characters in the said script, then puts together a list of
characters. It then compiles a list of all the scenes, and lists which
characters speak in each scene.
Celtx does not reindex characters in the main catalogue, making it a
poor tool to track characters. This script provides an alternate way
to track characters.
'''
# ######################################################################
from tkFileDialog import askopenfilename
from Tkinter import Tk
from sys import exit
import zipfile
# ######################## LOAD CELTX FILE #############################
# Ask the user to select a .celtx file to open.
Tk().withdraw()
filename = askopenfilename(title='Choose a Celtx file', filetypes=[('Celtx File','*.celtx'), ('Any File','*.*')])
# If the user does not choose a file, quit.
if filename == "":
exit("No file selected. Quitting")
# Unzip the file to a temporary directory.
celtxfile = zipfile.ZipFile(filename)
celtxfile.extractall("./temp_celtx/")
celtxfile.close()
# Print the file name for the user.
print ":: " + filename[filename.rfind("/")+1:filename.find(".celtx")].upper() + " CELTX FILE"
print ""
# ######################## READ PROJECT FILE ###########################
# characterList.append(character name, description)
characterList = list()
# Open the project in the temporary directory
projectfile = open("./temp_celtx/project.rdf")
line = projectfile.readline()
# Read through the file line by line.
while line:
if line.find(" -1:
description = ""
name = ""
while True:
if line.find("dc:title=") > -1:
name = line[line.find("dc:title=\"")+len("dc:title=\""):line.find("\"", line.find("dc:title=\"")+len("dc:title=\""))]
if line.find("dc:description=") > -1:
description = line[line.find("dc:description=\"")+len("dc:description=\""):line.find("\"", line.find("dc:description=\"")+len("dc:description=\""))]
if line.find("") > -1 or line.find("/>") > -1:
break
line = projectfile.readline()
characterList.append([name.upper(), description])
# Grab the next line.
line = projectfile.readline()
# Close the project file.
projectfile.close()
# ##########
# ##########
# ##########
# Print the list of characters.
print "COMPLETE LIST OF CHARACTERS (" + str(len(characterList)) + " Total)"
print "#####################################################"
for character in sorted(characterList):
print " " + character[0]
print ""
# Print the list of characters.
print "COMPLETE LIST OF CHARACTERS WITH DESCRIPTIONS"
print "#####################################################"
for character in sorted(characterList):
print " " + character[0]
if len(character[1].strip()) > 0:
print " " + character[1]
print ""
print ""
# ######################### READ SCRIPT FILE ###########################
# Lists in memory
# characterList.append(character name)
# sceneList.append( ['scene number', 'scene name', ['character #1',
# 'character #2']])
characterList = list()
sceneList = list()
# Temporary Variables
currentSceneNumber = ""
currentSceneName = ""
currentSceneCharacterList = list()
# ##########
# ##########
# ##########
# Open the script in the temporary directory
scriptfile = open("./temp_celtx/script-HU6.html")
line = scriptfile.readline()
# Read through the file line by line.
while line:
# New scene heading
if line[0:len("
")+1:line.find("
")].strip()
currentSceneNumber = line[line.find("scenenumber=\"")+len("scenenumber=\""):line.find("\"", line.find("scenenumber=\"")+len("scenenumber=\""))]
if line.find("
") < 0:
line = scriptfile.readline()
currentSceneName += " " + line[:line.find("
")].strip()
currentSceneName = currentSceneName.upper().replace(" ", " ").replace("
", "").strip()
# Clear the current character list.
currentSceneCharacterList = list()
# Add the character listed
if line[0:len("")] == "
":
character = line[line.find("
")+len("
"):].upper()
character = character.replace("
", "").replace("
", "").replace("&", "&").strip()
currentSceneCharacterList.append(character)
characterList.append(character)
# Grab the next line.
line = scriptfile.readline()
# Do the final scene.
sceneList.append([currentSceneNumber, currentSceneName, currentSceneCharacterList])
# Close the script file.
scriptfile.close()
# ##########
# ##########
# ##########
# Print the list out of characters.
print "CHARACTERS WITH LINES OF DIALOG (" + str(len(set(characterList))) + " Total)"
print "#####################################################"
for character in sorted(set(characterList)):
print " " + character
# Print the list out of scenes and characters involved.
print "\r\n\r\nSCENES / CHARACTERS w. DIALOG"
print "#####################################################"
for scene in sceneList:
print "\r\n"
print scene[0] + ": " + scene[1]
for character in sorted(set(scene[2])):
print " - " + character