forked from po-devs/po-server-goodies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updatesmogontiers.py
executable file
·76 lines (64 loc) · 3.03 KB
/
updatesmogontiers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
import shlex
import subprocess
import os
import re
import lxml.etree as etree
from tierutils import get_po_tiers, write_po_tiers, merge_bans
TIERS_YML_URL="https://raw.github.com/sarenji/poserver/HEAD/tiers.yml"
TIERS_COMPILER_URL="https://raw.github.com/sarenji/poserver/HEAD/tiers_compiler.rb"
TIERS_TO_UPDATE = {
'Standard OU': 'Smogon OU',
'Standard UU': 'Smogon UU',
'Standard RU': 'Smogon RU',
'Standard NU': 'Smogon NU'
}
ADD_BANLIST = {
'Standard OU': ['Standard Ubers']
}
class InvalidTierException(Exception):
pass
def shell_exec(cmd_line, shell=False):
return subprocess.Popen(shlex.split(cmd_line), stdout=subprocess.PIPE).communicate()[0]
def download_smogon_tiers(download=True):
if download:
shell_exec("mkdir -p smogon")
shell_exec("rm -f smogon/tiers.xml smogon/tiers.yml")
shell_exec("wget {0} -q -Osmogon/tiers.yml".format(TIERS_YML_URL))
shell_exec("wget {0} -q -Osmogon/tiers_compiler.rb".format(TIERS_COMPILER_URL))
os.chdir("smogon"); shell_exec("ruby tiers_compiler.rb"); os.chdir("..")
tree = etree.ElementTree()
if os.path.exists("smogon/tiers.xml"):
tree.parse("smogon/tiers.xml")
return tree
def update_tiers(smogon_tiers, po_tiers):
for smogon_tier,po_tier in TIERS_TO_UPDATE.items():
print("{0} -> {1}...".format(smogon_tier, po_tier), end=" ")
smogon_element = smogon_tiers.find(".//tier[@name='{0}']".format(smogon_tier))
if smogon_element is None:
raise InvalidTierException("Smogon tier '{0}' does not exist".format(smogon_tier))
if smogon_tier in ADD_BANLIST:
for banlistname in ADD_BANLIST[smogon_tier]:
banlist = smogon_tiers.find(".//tier[@name='{0}']".format(banlistname))
if banlist is None:
raise InvalidTierException("Ban list does not exist: {0}'".format(banlistname))
if banlist.attrib["banMode"] != smogon_element.attrib["banMode"]:
raise InvalidTierException("Ban list mode does not match for '{0}' and '{1}'".format(smogon_tier, banlistname))
for key in ["pokemons", "restrictedPokemons", "items", "moves"]:
smogon_element.attrib[key] = merge_bans(smogon_element.attrib[key], banlist.attrib[key])
po_element = po_tiers.find(".//tier[@name='{0}']".format(po_tier))
for key in ["pokemons", "restrictedPokemons", "banMode", "maxLevel", "mode", "gen", "moves", "items", "numberOfPokemons", "clauses"]:
po_element.attrib[key] = smogon_element.attrib[key]
print("")
return po_tiers
if __name__ == "__main__":
print("Downloading Smogon tiers")
smogon_tiers = download_smogon_tiers(download=True)
print("Reading PO tiers")
po_tiers = get_po_tiers()
print("Merging Smogon tiers to PO tiers...", end=" ")
po_tiers = update_tiers(smogon_tiers, po_tiers)
print("Writing PO tiers")
write_po_tiers(po_tiers)
print("Done")
print("You can now remove the temporary directory 'smogon' if you want.")