def add_spell_list(curs, curs_list, struct): if not struct['type'] == 'spell_list': raise Exception("This should only be run on spell list files") if struct['class'] in ("Sorcerer/wizard", "Sorcerer/Wizard"): struct['class'] = "Sorcerer" add_spell_list(curs, curs_list, struct) struct['class'] = "Wizard" add_spell_list(curs, curs_list, struct) return struct = fix_spell_list(struct) level = struct['level'] class_name = cap_words(struct['class']) for sp in struct['spells']: name = cap_words(sp['name'].strip()) find_section(curs, name=name, type='spell') spell = curs.fetchone() use_curs = curs if not spell: for c in curs_list: find_section(c, name=name, type='spell') spell = c.fetchone() use_curs = c if spell: break if not spell: raise Exception("Cannot find spell %s" % name) do_add_spell_list(use_curs, spell, sp, class_name, level)
def generate_url(curs, parent_id, name, create_index=True, parent_url=None): if not create_index: return if name is None: return name = name.replace(':', '') name = name.replace('&', 'and') name = name.replace('?', '') name = name.replace("'", "") if parent_id == None: return "pfsrd://%s" % name if parent_url == None: p_id = parent_id while parent_url is None: find_section(curs, section_id=p_id) section = curs.fetchone() parent_url = section['url'] p_id = section['parent_id'] if name.find(', ') > -1: parent_chunks = parent_url.split('/') name_chunks = name.split(', ') merged = [] while len(name_chunks) > 0: nc = name_chunks.pop() if nc == parent_chunks[-1]: parent_chunks.pop() merged.insert(0, nc) parent_chunks.extend(merged) result = '/'.join(parent_chunks) if result == parent_url: name_chunks = name.split(', ') parent_chunks.extend(merged) result = '/'.join(parent_chunks) return result return parent_url + "/" + name
def add_spell_list(curs, struct): if not struct['type'] == 'spell_list': raise Exception("This should only be run on spell list files") if struct['class'] in ("Sorcerer/wizard", "Sorcerer/Wizard"): struct['class'] = "Sorcerer" add_spell_list(curs, struct) struct['class'] = "Wizard" add_spell_list(curs, struct) return struct = fix_spell_list(struct) level = struct['level'] class_name = cap_words(struct['class']) for sp in struct['spells']: name = cap_words(sp['name'].strip()) find_section(curs, name=name, type='spell') spell = curs.fetchone() if not spell: raise Exception("Cannot find spell %s" % name) fetch_spell_lists(curs, spell['section_id'], class_name=class_name) if not curs.fetchone(): magic_type = find_magic_type(class_name.lower()) insert_spell_list(curs, spell['section_id'], level, class_name, magic_type) fix_spell_level_text(curs, spell['section_id']) if sp.has_key('description'): update_section(curs, spell['section_id'], description=sp['description'])
def process_structure_node(curs, curs_list, filename, parent, struct): section = None if struct.has_key('file'): print struct['file'] jsonfile = os.path.dirname(filename) + "/" + struct['file'] fp = open(jsonfile, 'r') data = json.load(fp) fp.close() section_id = insert_section(curs, curs_list, parent['section_id'], data) fetch_section(curs, section_id) section = curs.fetchone() else: find_section(curs, name=struct['name'], parent_id=parent['section_id']) section = curs.fetchone() if not section: section_id = append_child_section( curs, parent['section_id'], 'list', None, struct['name'], None, 'PFSRD', None, None, None, None, generate_url(curs, parent['section_id'], struct['name'], create_index=struct.get('create_index', True), parent_url=parent['url']), False) fetch_section(curs, section_id) section = curs.fetchone() for child in struct.get('children', []): process_structure_node(curs, curs_list, filename, section, child)
def process_update(conn, curs, update): find_section(curs, **{"url": update['url']}) section = curs.fetchone() if not section: raise Exception("Can't find section with name: %s" % update['url']) process_section(conn, curs, section, update) if section['type'] == 'item' or update.get('type') == 'item': process_item(conn, curs, section, update)
def load_additional_index_entries(db, conn, filename, struct): curs = conn.cursor() try: find_section(curs, name=struct['name']) parent = curs.fetchone() if struct.has_key('file'): print struct['file'] for child in struct['children']: pass #process_structure_node(curs, filename, parent, child) conn.commit() finally: curs.close()
def dump_type(output, conn, book, section_type): curs = conn.cursor() makedirs(output, book, section_type) try: if section_type == "animal_companion": find_section(curs, type=section_type, subtype="base") else: find_section(curs, type=section_type) for section in curs.fetchall(): section = dump_section(conn, section) write_type_section(output, book, section) finally: curs.close()
def generate_url(curs, parent_id, name, parent_url=None): if name is None: return if parent_id == None: return "pfsrd://name" if parent_url == None: p_id = parent_id while parent_url is None: find_section(curs, section_id=p_id) section = curs.fetchone() parent_url = section['url'] p_id = section['parent_id'] return parent_url + "/" + name
def load_rule_structure_document(db, conn, conn_list, filename, struct): curs, curs_list = get_cursors(conn, conn_list) try: find_section(curs, name=struct['name']) parent = curs.fetchone() if struct.has_key('file'): print struct['file'] for child in struct['children']: process_structure_node(curs, curs_list, filename, parent, child) conn_commit(conn, conn_list) finally: curs_close(curs, curs_list) print_struct(struct)
def dump_type(output, conn, book, section_type): curs = conn.cursor() makedirs(output, book, section_type) try: if section_type == 'animal_companion': find_section(curs, type=section_type, subtype='base') else: find_section(curs, type=section_type) for section in curs.fetchall(): section = dump_section(conn, section) write_type_section(output, book, section) finally: curs.close()
def fetch_parent(curs, parent_name): if not parent_name: return fetch_top(curs) else: find_section(curs, name=parent_name, type='list') parent = curs.fetchone() if parent: return parent else: top = fetch_top(curs) section_id = append_child_section(curs, top['section_id'], 'list', None, parent_name, None, 'PFSRD', None, None) fetch_section(curs, section_id) return curs.fetchone()
def fetch_parent(curs, parent_name, source_name): if not parent_name: fetch_top(curs) return curs.fetchone() else: find_section(curs, name=parent_name, type='list') parent = curs.fetchone() if parent: return parent else: fetch_top(curs) top = curs.fetchone() section_id = append_child_section(curs, top['section_id'], 'list', None, parent_name, None, source_name, None, None, None, None, generate_url(curs, top['section_id'], parent_name), False) fetch_section(curs, section_id) return curs.fetchone()
def update_sections(curs, section_cache, table_data, table, item): section = get_section(curs, section_cache, table_data, table, item['Name']) if section: section['changes'].append(item) alternate_urls = table_data.get('alternate_urls', {}) for url in alternate_urls.get(item['Name'], []): section = section_cache.get(url) if not section: find_section(curs, **{"url": url}) alt = curs.fetchone() section_cache[alt['url']] = {"section": alt, "changes":[]} section = section_cache[alt['url']] if not section: sys.stderr.write("%s : not found\n" % url) return section['changes'].append(item)
def insert_mythic_spell_records(curs, curs_list, section_id, mythic_spell): name = cap_words(mythic_spell['spell_source'].strip()) find_section(curs, name=name, type='spell') spell = curs.fetchone() use_curs = curs if not spell: for c in curs_list: find_section(c, name=name, type='spell') spell = c.fetchone() use_curs = c if spell: mythic_spell['spell_source'] = spell['name'] break if not spell: raise Exception("Cannot find spell %s" % name) insert_mythic_spell_detail(curs, **mythic_spell)
def find_spell_in_books(spell_name, curs, curs_list): name = cap_words(spell_name.strip()) find_section(curs, name=name, type='spell') spell = curs.fetchone() use_curs = curs if spell: return spell, use_curs else: for c in curs_list: find_section(c, name=name, type='spell') spell = c.fetchone() use_curs = c if spell: return spell, use_curs if not spell: raise Exception("Cannot find spell %s" % name)
def update_sections(curs, section_cache, table_data, table, item): section = get_section(curs, section_cache, table_data, table, item['Name']) if section: section['changes'].append(item) alternate_urls = table_data.get('alternate_urls', {}) for url in alternate_urls.get(item['Name'], []): section = section_cache.get(url) if not section: find_section(curs, **{"url": url}) alt = curs.fetchone() section_cache[alt['url']] = {"section": alt, "changes": []} section = section_cache[alt['url']] if not section: sys.stderr.write("%s : not found\n" % url) return section['changes'].append(item)
def dump_table(output_dir, db, book): extensions = load_extension_file(output_dir, book) conn = get_db_connection(db, source=book) curs = conn.cursor() section_cache = {} for table_data in extensions['tables']: url = table_data['url'] find_section(curs, **{"url": url}) table = curs.fetchone() parser = get_parser(table_data['parser']) items = parser(table, table_data) for item in items: update_sections(curs, section_cache, table_data, table, item) urls = section_cache.keys() urls.sort() output = produce_output([section_cache[url] for url in urls]) write_output(output_dir, book, output)
def dump_section(conn, section): fetch_subrecords(conn, section) curs = conn.cursor() try: find_section(curs, parent_id=section['section_id']) sections = [] for ss in curs.fetchall(): ss = dump_section(conn, ss) sections.append(ss) if len(sections) > 0: section['sections'] = sections del section['lft'] del section['rgt'] del section['parent_id'] del section['create_index'] finally: curs.close() return dict((k, v) for k, v in section.iteritems() if v)
def process_structure_node(curs, filename, parent, struct): section = None if struct.has_key('file'): jsonfile = os.path.dirname(filename) + "/" + struct['file'] fp = open(jsonfile, 'r') data = json.load(fp) fp.close() section_id = insert_section(curs, parent['section_id'], data) fetch_section(curs, section_id) section = curs.fetchone() else: find_section(curs, name=struct['name'], parent_id=parent['section_id']) section = curs.fetchone() if not section: section_id = append_child_section(curs, parent['section_id'], 'list', None, struct['name'], None, 'PFSRD', None, None) fetch_section(curs, section_id) section = curs.fetchone() for child in struct.get('children', []): process_structure_node(curs, filename, section, child)
def output_creature(conn, creature_name): curs = conn.cursor() find_section(curs, name=creature_name) creatures = curs.fetchall() assert len(creatures) == 1 creature = creatures[0] assert creature['type'] == 'creature' #pprint.pprint(creature) fetch_creature_detail(curs, creature['section_id']) creature_details = curs.fetchone() #pprint.pprint(creature_details) fetch_creature_spells(curs, creature['section_id']) creature_spells = curs.fetchall() #pprint.pprint(creature_spells) data = {} # META data['source'] = gen_source(creature) data['url'] = gen_url(creature) data['name'] = gen_name(creature) data['description'] = gen_description(creature) # BASE data['cr'] = gen_cr(creature_details) data['xp'] = gen_xp(creature_details) data['sex'] = gen_sex(creature_details) data['super_race'] = gen_super_race(creature_details) data['level'] = gen_level(creature_details) data['alignment'] = gen_alignment(creature_details) data['size'] = gen_size(creature_details) data['hit_dice'] = gen_hit_dice(creature_details) data['creature_type'] = gen_creature_type(creature_details) data['creature_subtype'] = gen_creature_subtype(creature_details) data['init'] = gen_init(creature_details) data['senses'] = gen_senses(creature_details) data['aura'] = gen_aura(creature_details) # DEFENSE data['ac'] = gen_ac(creature_details) data['hp'] = gen_hp(creature_details) data['saves'] = {} data['saves']['fortitude'] = gen_fortitude(creature_details) data['saves']['reflex'] = gen_reflex(creature_details) data['saves']['will'] = gen_will(creature_details) data['defensive_abilities'] = gen_defensive_abilities(creature_details) data['dr'] = gen_dr(creature_details) data['immune'] = gen_immune(creature_details) data['weaknesses'] = gen_weaknesses(creature_details) data['sr'] = gen_sr(creature_details) data['natural_armor'] = gen_natural_armor(creature_details) data['resist'] = gen_resist(creature_details) # OFFENSE data['speed'] = gen_speed(creature_details) data['melee'] = gen_melee(creature_details) data['ranged'] = gen_ranged(creature_details) data['breath_weapon'] = gen_breath_weapon(creature_details) data['space'] = gen_space(creature_details) data['reach'] = gen_reach(creature_details) data['special_attacks'] = gen_special_attacks(creature_details) data['spells'] = gen_spells(creature_spells) # STATISTICS data['abilities'] = {} data['abilities']['strength'] = gen_strength(creature_details) data['abilities']['dexterity'] = gen_dexterity(creature_details) data['abilities']['constitution'] = gen_constitution(creature_details) data['abilities']['intelligence'] = gen_intelligence(creature_details) data['abilities']['wisdom'] = gen_wisdom(creature_details) data['abilities']['charisma'] = gen_charisma(creature_details) data['base_attack'] = gen_base_attack(creature_details) data['cmb'] = gen_cmb(creature_details) data['cmd'] = gen_cmd(creature_details) data['feats'] = gen_feats(creature_details) data['skills'] = gen_skills(creature_details) data['racial_modifiers'] = gen_racial_modifiers(creature_details) data['languages'] = gen_languages(creature_details) data['special_qualities'] = gen_special_qualities(creature_details) data['gear'] = gen_gear(creature_details) # ECOLOGY data['environment'] = gen_environment(creature_details) data['organization'] = gen_organization(creature_details) data['treasure'] = gen_treasure(creature_details) # SPECIAL ABILITIES # TODO # TEXT # TODO del creature['create_index'] del creature['lft'] del creature['parent_id'] del creature['rgt'] del creature['section_id'] del creature['abbrev'] # is null for all the creatures we have in the database del creature['alt'] # is null for all the creatures we have in the database del creature['body'] # only has value for seven creatures; what should we do with it? # sqlite> select name, body from sections where type = 'creature' and body is not null; # Swarm, Bat|<StatBlockSection Special Features [(u'Wounding (Ex)', u'Any living creature damaged by a bat swarm continues to bleed, losing 1 hit point per round thereafter. Multiple wounds do not result in cumulative bleeding loss. The bleeding can be stopped by a DC 10 Heal check or the application of a cure spell or some other healing magic.')]> # Bat, Mobat|<p>XP 800 </p> # Blindheim|<p>Init +2; <b>Senses</b> darkvision 60 ft., low-light vision; <b>Perception</b> +9</p> # Daemon, Piscodaemon|<p><span class="char-style-override-30">XP</span> 9,600</p> # Daemon, Purrodaemon|<p><span class="char-style-override-30">XP</span> 153,600</p> # Fetchling|<p>Fetchling rogue 1</p> # Werebear (Hybrid Form)|<p>Human natural werebear ranger 4</p> del creature['image'] # is null for all the creatures we have in the database del creature['subtype'] # is null or familiar del creature['type'] assert creature == {} del creature_details['creature_details_id'] del creature_details['section_id'] assert creature_details == {} for spell in creature_spells: del spell['creature_spells_id'] del spell['section_id'] assert spell == {} schema = json.load(open(os.path.join(CONF_DIR, 'creature_level1.json'), 'r')) jsonschema.validate(data, schema) pprint.pprint(data)