add jsonapi generator, default output to web dir
This commit is contained in:
@@ -12,3 +12,4 @@ sys.path.insert(0, project_path)
|
||||
|
||||
db_path = join(project_path, "db", "mh4u.db")
|
||||
motion_values_path = join(project_path, "db", "motion_values.json")
|
||||
web_path = join(project_path, "web")
|
||||
|
||||
@@ -5,6 +5,7 @@ Script to generate static rewards files for all items.
|
||||
|
||||
import codecs
|
||||
import urllib
|
||||
import os.path
|
||||
|
||||
import _pathfix
|
||||
|
||||
@@ -21,11 +22,14 @@ if __name__ == '__main__':
|
||||
import os
|
||||
import os.path
|
||||
|
||||
if len(sys.argv) != 2:
|
||||
print("Usage: %s outdir" % sys.argv[0])
|
||||
if len(sys.argv) == 1:
|
||||
outdir = os.path.join(_pathfix.web_path, "rewards")
|
||||
elif len(sys.argv) == 2:
|
||||
outdir = sys.argv[1]
|
||||
else:
|
||||
print("Usage: %s [outdir]" % sys.argv[0])
|
||||
sys.exit(os.EX_USAGE)
|
||||
|
||||
outdir = sys.argv[1]
|
||||
err_out = get_utf8_writer(sys.stderr)
|
||||
|
||||
# TODO: doesn't work if script is symlinked
|
||||
|
||||
98
bin/mkjsonapi.py
Executable file
98
bin/mkjsonapi.py
Executable file
@@ -0,0 +1,98 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import os
|
||||
import json
|
||||
import sys
|
||||
import errno
|
||||
from collections import defaultdict
|
||||
|
||||
import _pathfix
|
||||
|
||||
from mhapi.db import MHDB
|
||||
from mhapi import model
|
||||
|
||||
|
||||
def mkdirs_p(path):
|
||||
try:
|
||||
os.makedirs(path)
|
||||
except OSError as e:
|
||||
if e.errno != errno.EEXIST:
|
||||
raise
|
||||
|
||||
|
||||
def write_list_file(path, model_list):
|
||||
list_path = os.path.join(path, "_list.json")
|
||||
with open(list_path, "w") as f:
|
||||
json.dump([o.as_list_data() for o in model_list], f, indent=2)
|
||||
|
||||
|
||||
def write_index_file(path, indexes):
|
||||
index_path = os.path.join(path, "_index.json")
|
||||
with open(index_path, "w") as f:
|
||||
json.dump(indexes, f, indent=2)
|
||||
|
||||
|
||||
def monster_json(db, path):
|
||||
monsters = db.get_monsters()
|
||||
mkdirs_p(path)
|
||||
write_list_file(path, monsters)
|
||||
|
||||
indexes = {}
|
||||
for m in monsters:
|
||||
monster_path = os.path.join(path, "%s.json" % m.id)
|
||||
m.update_indexes(indexes)
|
||||
data = m.as_data()
|
||||
damage = db.get_monster_damage(m.id)
|
||||
data["damage"] = damage.as_data()
|
||||
with open(monster_path, "w") as f:
|
||||
json.dump(data, f, cls=model.ModelJSONEncoder, indent=2)
|
||||
|
||||
write_index_file(path, indexes)
|
||||
|
||||
|
||||
def weapon_json(db, path):
|
||||
weapons = db.get_weapons()
|
||||
mkdirs_p(path)
|
||||
write_list_file(path, weapons)
|
||||
|
||||
indexes = {}
|
||||
for w in weapons:
|
||||
weapon_path = os.path.join(path, "%s.json" % w.id)
|
||||
w.update_indexes(indexes)
|
||||
with open(weapon_path, "w") as f:
|
||||
w.json_dump(f)
|
||||
|
||||
write_index_file(path, indexes)
|
||||
|
||||
|
||||
def items_json(db, path):
|
||||
items = db.get_items()
|
||||
mkdirs_p(path)
|
||||
write_list_file(path, items)
|
||||
|
||||
indexes = {}
|
||||
for item in items:
|
||||
item_path = os.path.join(path, "%s.json" % item.id)
|
||||
item.update_indexes(indexes)
|
||||
with open(item_path, "w") as f:
|
||||
item.json_dump(f)
|
||||
|
||||
write_index_file(path, indexes)
|
||||
|
||||
|
||||
def main():
|
||||
db = MHDB(_pathfix.db_path)
|
||||
|
||||
if len(sys.argv) > 1:
|
||||
outpath = sys.argv[1]
|
||||
else:
|
||||
outpath = os.path.join(_pathfix.web_path, "jsonapi")
|
||||
|
||||
items_json(db, os.path.join(outpath, "item"))
|
||||
weapon_json(db, os.path.join(outpath, "weapon"))
|
||||
monster_json(db, os.path.join(outpath, "monster"))
|
||||
#quest_json
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user