Merge pull request #7 from kanzure/september-cleanup

python cleanup
This commit is contained in:
Bryan Bishop 2013-09-01 19:34:50 -07:00
commit 2e6d805aea
10 changed files with 655 additions and 402 deletions

File diff suppressed because it is too large Load Diff

View File

@ -5,7 +5,7 @@ and insert all scripting commands.
"""
import crystal
from gbz80disasm import output_bank_opcodes
import gbz80disasm
rom = crystal.load_rom()
roml = [ord(x) for x in rom]
@ -78,7 +78,7 @@ class DisassembledScriptCommand():
max_byte_count = 86
# disassemble and laso get the last address
(asm, last_address, last_hl_address, last_a_address, used_3d97) = output_bank_opcodes(address, max_byte_count=max_byte_count, stop_at=command_pointers, include_last_address=False)
(asm, last_address, last_hl_address, last_a_address, used_3d97) = gbz80disasm.output_bank_opcodes(address, max_byte_count=max_byte_count, stop_at=command_pointers, include_last_address=False)
# remove indentation
asm = asm.replace("\n\t", "\n")

View File

@ -0,0 +1,13 @@
"""
Custom exceptions used throughout the project.
"""
class AddressException(Exception):
"""
There was a problem with an address. Maybe it was out of range or invalid.
"""
class TextScriptException(Exception):
"""
TextScript encountered an inconsistency or problem.
"""

View File

@ -6,6 +6,7 @@ from copy import copy, deepcopy
from ctypes import c_int8
import random
import json
from wram import *
# New versions of json don't have read anymore.

View File

@ -5,25 +5,12 @@ import sys
import png
from math import sqrt, floor, ceil
from crystal import load_rom
from pokemon_constants import pokemon_constants
from trainers import trainer_group_names
import crystal
import pokemon_constants
import trainers
if __name__ != "__main__":
rom = load_rom()
def mkdir_p(path):
"""
Make a directory at a given path.
"""
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST:
pass
else: raise
rom = crystal.load_rom()
def hex_dump(input, debug=True):
"""
@ -1122,7 +1109,7 @@ def dump_monster_pals():
pal_length = 0x4
for mon in range(251):
name = pokemon_constants[mon+1].title().replace('_','')
name = pokemon_constants.pokemon_constants[mon+1].title().replace('_','')
num = str(mon+1).zfill(3)
dir = 'gfx/pics/'+num+'/'
@ -1160,7 +1147,7 @@ def dump_trainer_pals():
pal_length = 0x4
for trainer in range(67):
name = trainer_group_names[trainer+1]['constant'].title().replace('_','')
name = trainers.trainer_group_names[trainer+1]['constant'].title().replace('_','')
num = str(trainer).zfill(3)
dir = 'gfx/trainers/'

51
pokemontools/helpers.py Normal file
View File

@ -0,0 +1,51 @@
"""
Generic functions that should be reusable anywhere in pokemontools.
"""
import os
def index(seq, f):
"""
return the index of the first item in seq
where f(item) == True.
"""
return next((i for i in xrange(len(seq)) if f(seq[i])), None)
def grouper(some_list, count=2):
"""
splits a list into sublists
given: [1, 2, 3, 4]
returns: [[1, 2], [3, 4]]
"""
return [some_list[i:i+count] for i in range(0, len(some_list), count)]
def flattener(x):
"""
flattens a list of sublists into just one list (generator)
"""
try:
it = iter(x)
except TypeError:
yield x
else:
for i in it:
for j in flattener(i):
yield j
def flatten(x):
"""
flattens a list of sublists into just one list
"""
return list(flattener(x))
def mkdir_p(path):
"""
Make a directory at a given path.
"""
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST:
pass
else:
raise exc

View File

@ -3,10 +3,7 @@
Various label/line-related functions.
"""
from pointers import (
calculate_pointer,
calculate_bank,
)
import pointers
def remove_quoted_text(line):
"""get rid of content inside quotes
@ -125,10 +122,10 @@ def line_has_comment_address(line, returnable={}, bank=None):
if offset == None and bank == None:
return False
if bank == None:
bank = calculate_bank(offset)
bank = pointers.calculate_bank(offset)
returnable["bank"] = bank
returnable["offset"] = offset
returnable["address"] = calculate_pointer(offset, bank=bank)
returnable["address"] = pointers.calculate_pointer(offset, bank=bank)
return True
def get_address_from_line_comment(line, bank=None):

View File

@ -7,7 +7,8 @@ from copy import copy, deepcopy
from pretty_map_headers import random_hash, map_name_cleaner
from ctypes import c_int8
import sys
spacing = " "
spacing = "\t"
temp_opt_table = [
[ "ADC A", 0x8f, 0 ],
@ -848,6 +849,4 @@ if __name__ == "__main__":
extract_maps.load_map_pointers()
extract_maps.read_all_map_headers()
#0x18f96 is PalletTownText1
#0x19B5D is BluesHouseText1
print output_bank_opcodes(int(sys.argv[1], 16))[0]

View File

@ -37,6 +37,11 @@ from pokemontools.labels import (
get_label_from_line,
)
from pokemontools.helpers import (
grouper,
index,
)
from pokemontools.crystal import (
rom,
load_rom,
@ -69,9 +74,7 @@ from pokemontools.crystal import (
load_asm,
asm,
is_valid_address,
index,
how_many_until,
grouper,
get_pokemon_constant_by_id,
generate_map_constant_labels,
get_map_constant_label_by_id,
@ -321,7 +324,7 @@ class TestMultiByteParam(unittest.TestCase):
}]
self.assertEqual(self.cls.to_asm(), "poop")
class TestPostParsing: #(unittest.TestCase):
class TestPostParsing(unittest.TestCase):
"""tests that must be run after parsing all maps"""
def test_signpost_counts(self):
self.assertEqual(len(map_names[1][1]["signposts"]), 0)

View File

@ -37,6 +37,11 @@ from pokemontools.labels import (
get_label_from_line,
)
from pokemontools.helpers import (
grouper,
index,
)
from pokemontools.crystal import (
rom,
load_rom,
@ -69,9 +74,7 @@ from pokemontools.crystal import (
load_asm,
asm,
is_valid_address,
index,
how_many_until,
grouper,
get_pokemon_constant_by_id,
generate_map_constant_labels,
get_map_constant_label_by_id,