Skip to content

Instantly share code, notes, and snippets.

@taco-shellcode
Last active May 20, 2020 18:36
Show Gist options
  • Save taco-shellcode/d3d66ac7f4bb619fa5bd262696736477 to your computer and use it in GitHub Desktop.
Save taco-shellcode/d3d66ac7f4bb619fa5bd262696736477 to your computer and use it in GitHub Desktop.
#Imports necessary python libraries for working with XML
from xml.etree.ElementTree import XML, fromstring
from xml.etree import ElementTree
#This is the base string text of your XML files. """ """ in python denotes a multiline literal string
item_text = """<id-02227>
<avail type="string">9R</avail>
<cost type="string">15,000&#165;</cost>
<description type="formattedtext">
<p>Alchemical focus: An alchemical focus adds its Force in dice to any Alchemical skill tests.</p>
<p>Disenchanting focus: When a disenchanting focus is in contact with another artifact, the magician can add dice equal to its Force to the Disenchanting Test.</p>
</description>
<itemacc type="number">0</itemacc>
<itemammo type="number">0</itemammo>
<itemap type="number">0</itemap>
<itemapmod type="number">0</itemapmod>
<itemarmor type="number">0</itemarmor>
<itemblast type="number">0</itemblast>
<itemcapacity type="number">0</itemcapacity>
<itemdataprocessing type="number">0</itemdataprocessing>
<itemdmgmod type="number">0</itemdmgmod>
<itemessence type="number">0</itemessence>
<itemfirewall type="number">0</itemfirewall>
<itemforce type="number">3</itemforce>
<itemmaxrange type="number">0</itemmaxrange>
<itemprograms type="number">0</itemprograms>
<itemrating type="number">0</itemrating>
<itemrc type="number">0</itemrc>
<itemreach type="number">0</itemreach>
<locked type="number">1</locked>
<name type="string">Enchanting Focus, F3</name>
<totalcapacity type="number">0</totalcapacity>
<totalessence type="number">0</totalessence>
<type type="string">Foci</type>
<weight type="number">0</weight>
</id-02227>"""
#This sets the base ID number and a placeholder variable for the new ID number
id_number_base = 2241
new_id_number = ""
#Sets the max ranks and default value for the current rank
max_force_rank = 13
current_rank = 1
#loops through each rank in max force or ranks and performs the necessary rank,
while current_rank < max_force_rank:
#Generates a brand new version each time the loop runs
#Parses the string object into XML
item_xml = fromstring(item_text)
#Addresses all of the properties of this current item type - may be missing some values but they should be easy to add from my examples
item_availability = item_xml.find("avail")
item_cost = item_xml.find("cost")
item_description = item_xml.find("description")
item_accuracy = item_xml.find("itemacc")
item_ammo = item_xml.find("itemammo")
item_ap = item_xml.find("itemap")
item_apmod = item_xml.find("itemapmod")
item_armor = item_xml.find("itemarmor")
item_blast = item_xml.find("itemblast")
item_capacity = item_xml.find("itemcapacity")
item_dataprocessing = item_xml.find("itemdataprocessing")
item_dmgmod = item_xml.find("itemdmgmod")
item_essence = item_xml.find("itemessence")
item_firewall = item_xml.find("itemfirewall")
item_force = item_xml.find("itemforce")
item_maxrange = item_xml.find("itemmaxrange")
item_programs = item_xml.find("itemprograms")
item_rating = item_xml.find("itemrating")
item_rc = item_xml.find("itemrc")
item_reach = item_xml.find("itemreach")
item_locked = item_xml.find("locked")
item_name = item_xml.find("name")
item_totalcapacity = item_xml.find("totalcapacity")
item_totalessence = item_xml.find("totalessence")
item_type = item_xml.find("type")
item_weight = item_xml.find("weight")
#Calculates the new values
rank_or_force_calculation = current_rank * 3
availability_calculation = (str((current_rank * 3)) + "R")
cost_calculation = current_rank * 5000
#Sets the new values on the newly generated item
item_force.text = str(current_rank)
item_availability.text = str(availability_calculation)
item_cost.text = (str(cost_calculation) + "&#165;")
item_name.text = item_name.text.replace("F3", str(current_rank) + "F")
#Sets the new ID number
new_id_number = int(id_number_base) + 1
item_xml.tag = str("id-0" + str(new_id_number))
#incriments the id_number base to continue where it left off
id_number_base = new_id_number
#parses your XML object back into literal string format for copy paste
item_xml_string = ElementTree.tostring(item_xml).decode()
#prints the string to the screen
print(item_xml_string.replace("amp;", ""))
#incriments your current rank so that you don't get stuck in an infinite loop
current_rank = current_rank + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment