def add_abilities_specs(npc_abilties: str, patch: str): ''' Fills table with the data from cleaned npc_abilities file. Args: npc_abilties: full path to the npc_abilities.txt file. patch: version of the game from which info was taken. ''' parsed = txt2json.to_json(npc_abilties) clean = abilities.get_cleaned_abilities(parsed) schema = schemas.get_abilities_specs_columns() with open(files.CONVERTER_FILE, 'r') as fp: converter = json.load(fp) for skill, description in clean.items(): try: hero, skill_name = json2rows.parse_skill_name(skill) # if skill name cannot be parsed except ValueError: continue for row in json2rows.ability_to_row(description, schema): row['HeroID'] = converter[hero] row['name'] = skill_name row['pk'] = str(row['ID']) + '.' + str(row['lvl']) row['patch'] = patch skill = AbilitySpecsModel(row) session.add(skill) session.commit()
def test_empty_value(self): ''' Example with empty value in the input file. ''' input_file = EXAMPLES_FOLDER + 'empty_values.txt' parsed = to_json(input_file) with open(EXAMPLES_FOLDER + 'expected_empty_values.json', 'r') as fp: expected = json.load(fp) self.assertEqual(sorted(parsed), sorted(expected))
def test_float_fields(self): ''' Example with float fields. ''' input_file = EXAMPLES_FOLDER + 'float_fields.txt' parsed = to_json(input_file) with open(EXAMPLES_FOLDER + 'expected_float_fields.json', 'r') as fp: expected = json.load(fp) self.assertEqual(sorted(parsed), sorted(expected))
def test_indents(self): ''' Tests ways to separate strings, keys, values. ''' input_file = EXAMPLES_FOLDER + 'intends.txt' parsed = to_json(input_file) with open(EXAMPLES_FOLDER + 'expected_intends.json', 'r') as fp: expected = json.load(fp) self.assertEqual(sorted(expected), sorted(parsed))
def test_symbols_in_values(self): ''' Test different unusual symbols in values. ''' input_file = EXAMPLES_FOLDER + 'symbols_in_values.txt' parsed = to_json(input_file) with open(EXAMPLES_FOLDER + 'expected_symbols_in_values.json', 'r') as fp: expected = json.load(fp) self.assertEqual(sorted(parsed), sorted(expected))
def test_empty_dict(self): ''' Example with empty dictionary in the input file. ''' input_file = EXAMPLES_FOLDER + 'empty_dict.txt' parsed = to_json(input_file) with open(EXAMPLES_FOLDER + 'expected_empty_dict.json', 'r') as fp: expected = json.load(fp) # sorted removes empty dictionaries self.assertEqual(sorted(parsed), sorted(expected))
def test_comments(self): ''' Tests every case of placing comments in file. ''' input_file = EXAMPLES_FOLDER + 'comments.txt' parsed = to_json(input_filename=input_file) with open(EXAMPLES_FOLDER + 'expected_comments.json', 'r') as fp: expected = json.load(fp) # I don't now why, but sorted is required here self.assertEqual(sorted(expected), sorted(parsed))
def get_abilities_texts(dota_english: str): ''' Produces ready to use dictionaries to create AbilityTextsModel object. Yields: dict: containing all the needed keys or empty if texts can not be sorted ''' texts_file = dota_english # parse texts file and take only texts from it parsed_texts = txt2json.to_json(texts_file)['lang']['Tokens'] # group texts by ability grouped_texts = group_abilities_texts(parsed_texts) # for all keys in grouped_texts prefix = 'DOTA_Tooltip_ability_' for key in list(grouped_texts): if not key.startswith(prefix): del grouped_texts[key] continue # this is a tuple parsed_name = json2rows.parse_skill_name(key[len(prefix):]) # there are 2 independent `if` statements to call parse_skill_name() # one time and only if it is needed # if the name cann't be parsed if len(parsed_name) < 2: del grouped_texts[key] continue # if the name can be split to the hero name and skill name -- change # original uppercase name to in-game style: all lower case with under # instead of space else: grouped_texts[key]['name'] = parsed_name[1] sorted_ = sort_texts(grouped_texts[key]) id_ = get_id_from_in_game_name(parsed_name[0], parsed_name[1]) if sorted_ and id_ is not None: sorted_['ID'] = id_ yield sorted_ else: yield {}
def add_heroes(npc_heroes: str, patch: str): ''' Unite everything that is needed to add heroes to the db table. That method does: parses `raw_file` to json, extracts information from json according to heroes table schema, adds heroes to the table. Args: npc_heroes: full path to the npc_heroes.txt file. patch: version of the game from which info was taken. ''' heroes_file = npc_heroes heroes_dict = txt2json.to_json(heroes_file) rows = json2rows.heroes_to_rows(heroes_dict, schemas.get_heroes_columns()) for row in rows: row['patch'] = patch hero = HeroModel(row) session.add(hero) session.commit()