|
# SBPlanets -- A Crappy Starbound script for keeping track of planets using XML |
|
# By dtalley11 |
|
|
|
# Imports |
|
import xml.etree.ElementTree as ET |
|
import os |
|
import sys |
|
|
|
# Window Manipulation |
|
# TODO: Maybe use APIs for this? |
|
if os.name=='nt': |
|
os.system("title "+"SBPlanets") |
|
os.system("color "+"70") |
|
os.system("mode "+"con:cols=80 lines=25") |
|
else: os.system("printf "+"'\033[8;80;25t'") |
|
|
|
# Easier to understand name for command line arguments |
|
args=sys.argv |
|
|
|
# Functions |
|
|
|
# Function to clear console |
|
def clear(): |
|
os.system('cls' if os.name=='nt' else 'clear') |
|
|
|
# Function to generate titles for each part of the program, |
|
# making it easier for users to navigate |
|
# TODO: Support custom console width |
|
def title(name): |
|
clear() |
|
if os.name=='nt': |
|
os.system("title "+name+" - SBPlanets ") |
|
print '#'*80 |
|
print "{0:{1}^80}".format(name, "=") |
|
print '#'*80 |
|
|
|
# Introduction Screen |
|
def intro(error): |
|
title("Introduction") |
|
print error |
|
print("Hello, and welcome to SBPlanets!") |
|
print |
|
print("This tool helps manage planets you have found by recording their name along with their coordinates") |
|
print |
|
print("Have you already used this program to generate a file?") |
|
choice=ynprompt() |
|
if choice==1:readxml(0,"") |
|
elif choice==0:mkaxml(0) |
|
else:intro("The response was not expected, please try again") |
|
|
|
# Yes/No prompt |
|
def ynprompt(): |
|
yes = set(['yes','y', 'ye', '']) |
|
no = set(['no','n']) |
|
choice = raw_input(('[Y,n] ')).lower() |
|
if choice in yes: return 1 |
|
elif choice in no: return 0 |
|
else: return None |
|
|
|
# Function for initially reading the XML file |
|
def readxml(error, msg): |
|
title("Load File") |
|
if error==0: print("Cool! Do you know what your file was saved as?") |
|
elif error==1: print("There was a problem loading the file provided, please try again. \n ("+msg+")\n") |
|
else: print(error) |
|
xmlfile = raw_input('(name of xml file) ') |
|
xmlload(xmlfile) |
|
|
|
def xmlload(xmlfile): |
|
# Set up xml variables |
|
try: |
|
tree = ET.parse(xmlfile) |
|
except (IOError, ET.ParseError) as e: |
|
readxml(1,str(e)) |
|
root = tree.getroot() |
|
charscreen(xmlfile,tree,root) |
|
|
|
# Function for initially creating a XML file |
|
# TODO: Use ElementTree to create the xml file |
|
def mkaxml(error): |
|
title("New File") |
|
if error==0: print("What do you wish to name the new File?") |
|
elif error==1:print("There was a problem creating a XML file with that name, please try again.") |
|
else: print(error) |
|
xmlfile = raw_input('(name of xml file) ') |
|
with open(xmlfile,"a+") as f: |
|
f.write("<planets></planets>") |
|
f.close |
|
try: |
|
tree = ET.parse(xmlfile) |
|
except IOError, e: |
|
mkaxml(1) |
|
root = tree.getroot() |
|
charscreen(xmlfile,tree,root) |
|
|
|
# The Character selection screen |
|
def charscreen(xmlfile,tree,root): |
|
title("Character Selection") |
|
charlist=[] |
|
for character in root.iter('character'): |
|
charlist.append(character.get('name')) |
|
charlist.append("Create New") |
|
charlist.append("Back") |
|
select=int(listoptions(charlist,0)) |
|
print select |
|
print len(charlist) |
|
if select==len(charlist)-2: newchar(xmlfile,tree,root) |
|
elif select==len(charlist)-1: intro(0) |
|
else: worldlist(xmlfile,tree,root,select,0) |
|
|
|
# List of planets |
|
def worldlist(xmlfile,tree,root,select,error): |
|
playerdex=select |
|
title("Planet Selection") |
|
if error==0: print("Select the planet you wish to see of coordinates") |
|
else: print(error+" Select the planet you wish to see of coordinates") |
|
planlist=[] |
|
for planet in root[select].iter('planet'): |
|
planlist.append(planet.get('name')) |
|
planlist.append("Create New") |
|
planlist.append("Back") |
|
select=listoptions(planlist,0) |
|
print select |
|
print len(planlist) |
|
if select==len(planlist)-2: newplan(xmlfile,tree,root,playerdex) |
|
elif select==len(planlist)-1: charscreen(xmlfile,tree,root) |
|
else: details(xmlfile,tree,root,select,playerdex) |
|
|
|
# Show details for selected planet |
|
def details(xmlfile,tree,root,worlddex,playerdex): |
|
world=root[playerdex][worlddex].get('name') |
|
title(world+" Planet Details") |
|
char=root[playerdex].get('name') |
|
x=root[playerdex][worlddex].get('x') |
|
y=root[playerdex][worlddex].get('y') |
|
list=["Name: "+world] |
|
list.append("X: "+x) |
|
list.append("Y: "+y) |
|
for i in list: print i |
|
print |
|
options=["Edit","Remove","back"] |
|
option=listoptions(options,0) |
|
if option==len(options)-1: worldlist(xmlfile,tree,root,playerdex,0) |
|
if option==len(options)-2: removeplan(xmlfile,tree,root,worlddex,playerdex,world,0) |
|
elif option==len(options)-3: editplan(xmlfile,tree,root,worlddex,playerdex,world,0) |
|
|
|
# Function to remove planet |
|
def removeplan(xmlfile,tree,root,worlddex,playerdex,world,error): |
|
title("Remove "+world) |
|
if error==0: print("Are you sure you want to remove "+world+"?") |
|
elif error==1: print("Invalid option, are you sure?") |
|
else: print error |
|
choice=ynprompt() |
|
if choice==1: |
|
root[playerdex].remove(root[playerdex][worlddex]) |
|
tree.write(xmlfile) |
|
worldlist(xmlfile,tree,root,worlddex,root[playerdex][worlddex]) |
|
elif choice==0:details(xmlfile,tree,root,worlddex,playerdex) |
|
else: removeplan(xmlfile,tree,root,worlddex,playerdex,1) |
|
|
|
# Function to edit planet |
|
def editplan(xmlfile,tree,root,worlddex,playerdex,world,error): |
|
title("Edit Planet") |
|
print "What do you want to change?" |
|
options=["name","x coordinate","y coordinate","back"] |
|
option=listoptions(options,0) |
|
if option==3: details(xmlfile,tree,root,worlddex,playerdex) |
|
elif option==2: |
|
title("Change Y Coordinate") |
|
print"Enter new number for Y coordinate" |
|
new=raw_input('('+root[playerdex][worlddex].get('y')+') ') |
|
if new=="": new==root[playerdex][worlddex].get('y') |
|
root[playerdex][worlddex].set("y", new) |
|
tree.write(xmlfile) |
|
editplan(xmlfile,tree,root,worlddex,playerdex,world,0) |
|
elif option==1: |
|
title("Change X Coordinate") |
|
print"Enter new number for X coordinate" |
|
new=raw_input('('+root[playerdex][worlddex].get('x')+') ') |
|
if new=="": new==root[playerdex][worlddex].get('x') |
|
root[playerdex][worlddex].set("x", new) |
|
tree.write(xmlfile) |
|
editplan(xmlfile,tree,root,worlddex,playerdex,world,0) |
|
elif option==0: |
|
title("Change Name") |
|
print"Enter new Name for "+world |
|
new=raw_input('('+root[playerdex][worlddex].get('name')+') ') |
|
if new=="": new==root[playerdex][worlddex].get('name') |
|
root[playerdex][worlddex].set("name", new) |
|
tree.write(xmlfile) |
|
editplan(xmlfile,tree,root,worlddex,playerdex,world,0) |
|
|
|
# Option Lister |
|
# Function to list options in 'oplist' and let the user pick which based on the option's cosponsoring number |
|
def listoptions(oplist,error): |
|
if error==0: print("Select One:") |
|
elif error==1: print("Sorry, that option is not valid. Please try again:") |
|
else: print(error) |
|
selection="" |
|
for i in range(len(oplist)): |
|
print i, oplist[i] |
|
selection+=str(i)+"," |
|
select=raw_input('('+selection+') ') |
|
print select |
|
try: select=int(select) |
|
except ValueError, e: |
|
listoptions(oplist,1) |
|
return select |
|
|
|
# The add new planet Screen |
|
def newplan(xmlfile,tree,root,select): |
|
title("Add Planet") |
|
print"What do you want to call this new planet?" |
|
name=raw_input('(name of new plan) ') |
|
print"what is the x value for the coordinates of this new planet?" |
|
x=raw_input('(x) ') |
|
print"what is the y value for the coordinates of this new planet?" |
|
y=raw_input('(y) ') |
|
new=ET.SubElement(root[select], "planet") |
|
new.attrib['name']=name |
|
new.attrib['x']=x |
|
new.attrib['y']=y |
|
tree.write(xmlfile) |
|
worldlist(xmlfile,tree,root,select,0) |
|
|
|
# The add new Character Screen |
|
def newchar(xmlfile,tree,root): |
|
title("Add Character") |
|
print"What do you want to call this new character?" |
|
name=raw_input('(name of new char) ') |
|
print"Creating new Element..." |
|
new=ET.SubElement(root, "character") |
|
print"Adding name attribute..." |
|
new.attrib['name']=name |
|
print"writing changes to "+xmlfile+"..." |
|
tree.write(xmlfile) |
|
charscreen(xmlfile,tree,root) |
|
|
|
# The HALP screen |
|
def help(): |
|
title("help") |
|
print"SBPlanets -- A Crappy Starbound script for keeping track of planets using XML" |
|
print |
|
print"If you run SBplanets from the command line/terminal, it does not need any command line arguments, so it's as simple as:" |
|
print"python sbplanets.py" |
|
print |
|
print"SBPlanets also supports providing the planets xml file path using command line arguments" |
|
print"python sbplanets.py planets.xml" |
|
print |
|
raw_input('Press <ENTER> to continue to the intro screen') |
|
intro("") |
|
|
|
# Assess the command line arguments and either run |
|
# the intro, the help screen or skip to the char selection |
|
# based on that assessment |
|
if len(args)==1: intro("") |
|
elif len(args)==2: |
|
if args[1] in set(['/help', '?', '-help', '--help', '-?', '/?']): help() |
|
else: xmlload(args[1]) |
|
else: help() |