Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Structs #87

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
56d5c3e
Add game_index to Nature
encukou Jun 9, 2012
198a1fa
Update struct
encukou Jun 10, 2012
bc672dd
Add character map for generation 5
encukou Jun 17, 2012
f2670d2
Enable the PokemonStringAdapter to encode values back into a blob
encukou Jun 17, 2012
ab3d405
Export pokémon structures as dictionaries
encukou Jun 24, 2012
4dcff2e
Make SaveFilePokemon mutable, import/update from dict
encukou Oct 13, 2012
93aa4ca
Add the pkm subcommand to pokedex
encukou Oct 13, 2012
b0bedfb
Allow some "enums" in the structure to have unknown int values
encukou Oct 13, 2012
8590c51
Saving string trash bytes, bug fixes
encukou Oct 13, 2012
14ea7a0
Fix frozenset proxy, implement ribbons setter
encukou Oct 13, 2012
a2a6393
Use helper function for saving
encukou Oct 13, 2012
fcd4c43
Prevent SQLA str/unicode warnings by using a Unicode YAML loader
encukou Oct 13, 2012
61b9ffd
Remove InstrumentedList, some docs, restructuring
encukou Oct 13, 2012
5250e88
Fix breakage in gen 4
encukou Oct 14, 2012
483ec7c
Fix up forms and hidden ability flag
encukou Oct 14, 2012
8483e57
Don't group ribbons into one attribute
encukou Oct 14, 2012
840c5f3
Save & load trash bytes
encukou Oct 14, 2012
857aee1
Add Python format to the pokedex pkm command
encukou Oct 14, 2012
ba5fa43
Add tests
encukou Oct 14, 2012
271f045
Fix errors found by tests
encukou Oct 14, 2012
b944d83
Test update() too
encukou Oct 14, 2012
3f51365
Make level settable, export level
encukou Oct 14, 2012
e33d25e
pokede pkm: Make the base64 encoding default more sane
encukou Oct 14, 2012
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions pokedex/data/csv/natures.csv
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
id,identifier,decreased_stat_id,increased_stat_id,hates_flavor_id,likes_flavor_id
1,hardy,2,2,1,1
2,bold,2,3,1,5
3,modest,2,4,1,2
4,calm,2,5,1,4
5,timid,2,6,1,3
6,lonely,3,2,5,1
7,docile,3,3,5,5
8,mild,3,4,5,2
9,gentle,3,5,5,4
10,hasty,3,6,5,3
11,adamant,4,2,2,1
12,impish,4,3,2,5
13,bashful,4,4,2,2
14,careful,4,5,2,4
15,rash,5,4,4,2
16,jolly,4,6,2,3
17,naughty,5,2,4,1
18,lax,5,3,4,5
19,quirky,5,5,4,4
20,naive,5,6,4,3
21,brave,6,2,3,1
22,relaxed,6,3,3,5
23,quiet,6,4,3,2
24,sassy,6,5,3,4
25,serious,6,6,3,3
id,identifier,decreased_stat_id,increased_stat_id,hates_flavor_id,likes_flavor_id,game_index
1,hardy,2,2,1,1,0
2,bold,2,3,1,5,5
3,modest,2,4,1,2,15
4,calm,2,5,1,4,20
5,timid,2,6,1,3,10
6,lonely,3,2,5,1,1
7,docile,3,3,5,5,6
8,mild,3,4,5,2,16
9,gentle,3,5,5,4,21
10,hasty,3,6,5,3,11
11,adamant,4,2,2,1,3
12,impish,4,3,2,5,8
13,bashful,4,4,2,2,18
14,careful,4,5,2,4,23
15,rash,5,4,4,2,19
16,jolly,4,6,2,3,13
17,naughty,5,2,4,1,4
18,lax,5,3,4,5,9
19,quirky,5,5,4,4,24
20,naive,5,6,4,3,14
21,brave,6,2,3,1,2
22,relaxed,6,3,3,5,7
23,quiet,6,4,3,2,17
24,sassy,6,5,3,4,22
25,serious,6,6,3,3,12
2 changes: 2 additions & 0 deletions pokedex/db/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1392,6 +1392,8 @@ class Nature(TableBase):
info=dict(description=u"ID of the Berry flavor the Pokémon hates (if likes_flavor_id is the same, the effects cancel out)"))
likes_flavor_id = Column(Integer, ForeignKey('contest_types.id'), nullable=False,
info=dict(description=u"ID of the Berry flavor the Pokémon likes (if hates_flavor_id is the same, the effects cancel out)"))
game_index = Column(Integer, unique=True, nullable=False,
info=dict(description=u"Internal game ID of the nature"))

@property
def is_neutral(self):
Expand Down
118 changes: 118 additions & 0 deletions pokedex/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
from optparse import OptionParser
import os
import sys
import textwrap
import json
import base64
import ast
import pprint

import pokedex.db
import pokedex.db.load
import pokedex.db.tables
import pokedex.lookup
import pokedex.struct
from pokedex import defaults

def main():
Expand Down Expand Up @@ -260,6 +266,117 @@ def command_lookup(*args):
print


def command_pkm(*args):
if args and args[0] == 'encode':
mode = 'encode'
elif args and args[0] == 'decode':
mode = 'decode'
else:
print textwrap.dedent(u"""
Convert binary Pokémon data (aka PKM files) to/from JSON/YAML.
usage: pokedex pkm (encode|decode) [options] <file> ...

Commands:
encode Convert a JSON or YAML representation of a
Pokémon to the binary format.
decode Convert the binary format to a JSON/YAML
representation.

Options:
--gen=NUM, -g Generation to use (4 or 5)
--format=FORMAT, -f FORMAT
Select the human-readable format to use.
FORMAT can be:
json (default): use JSON.
yaml: use YAML. Needs the PyYAML library
installed.
python: use Python literal syntax
--crypt, -c Use encrypted binary format.
--base64, -b Use Base64 encoding for the binary format.
--binary, -B Output raw binary data. This is the default,
but you need to specify -B explicitly if you're
dumping binary data to a terminal.

If no files are given, reads from standard input.
""").encode(sys.getdefaultencoding(), 'replace')
return
parser = get_parser(verbose=False)
parser.add_option('-g', '--gen', default=5, type=int)
parser.add_option('-c', '--crypt', action='store_true')
parser.add_option('-f', '--format', default='json')
parser.add_option('-b', '--base64', action='store_true', default=None)
parser.add_option('-B', '--no-base64', action='store_false', dest='base64')
options, files = parser.parse_args(list(args[1:]))

session = get_session(options)
cls = pokedex.struct.save_file_pokemon_classes[options.gen]
if options.format == 'yaml':
import yaml

# Override the default string handling function
# to always return unicode objects.
# Inspired by http://stackoverflow.com/questions/2890146
# This prevents str/unicode SQLAlchemy warnings.
def construct_yaml_str(self, node):
return self.construct_scalar(node)
class UnicodeLoader(yaml.SafeLoader):
pass
UnicodeLoader.add_constructor(u'tag:yaml.org,2002:str',
construct_yaml_str)

if options.format not in ('yaml', 'json', 'python'):
raise parser.error('Bad "format"')

if mode == 'encode' and options.base64 is None:
try:
isatty = sys.stdout.isatty
except AttributeError:
pass
else:
if isatty():
parser.error('Refusing to dump binary data to terminal. '
'Please use -B to override, or -b for base64.')

if not files:
# Use sys.stdin in place of name, handle specially later
files = [sys.stdin]

for filename in files:
if filename is sys.stdin:
content = sys.stdin.read()
else:
with open(filename) as f:
content = f.read()
if mode == 'encode':
if options.format == 'yaml':
dict_ = yaml.load(content, Loader=UnicodeLoader)
elif options.format == 'json':
dict_ = json.loads(content)
elif options.format == 'python':
dict_ = ast.literal_eval(content)
struct = cls(session=session, dict_=dict_)
if options.crypt:
data = struct.as_encrypted
else:
data = struct.as_struct
if options.base64:
print base64.b64encode(data)
else:
sys.stdout.write(data)
else:
if options.base64:
content = base64.b64decode(content)
struct = cls(
blob=content, encrypted=options.crypt, session=session)
dict_ = struct.export_dict()
if options.format == 'yaml':
print yaml.safe_dump(dict_, explicit_start=True),
elif options.format == 'json':
print json.dumps(dict_),
elif options.format == 'python':
pprint.pprint(dict_)


def command_help():
print u"""pokedex -- a command-line Pokédex interface
usage: pokedex {command} [options...]
Expand All @@ -269,6 +386,7 @@ def command_help():
Commands:
help Displays this message.
lookup [thing] Look up something in the Pokédex.
pkm Binary Pokémon format encoding/decoding. (experimental)

System commands:
load Load Pokédex data into a database from CSV files.
Expand Down
Loading