Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from collections import Mapping
from littletable import Table, DataObject
import pydash as _
import os
messages = None
logger = logutils.logger.getChild('database')
_me_player = Table('me_player')
_me_player.create_index('id', unique=True)
_battle_players = Table('battle_players')
_battle_players.create_index('id', unique=True)
_vehicles = Table('vehicles')
_vehicles.create_index('id', unique=True)
_vehicles.create_index('player_id', unique=True)
_prebattle_players = Table('prebattle_players')
_prebattle_players.create_index('id', unique=True)
_speaking_players = Table('speaking_players')
_speaking_players.create_index('id', unique=True)
_users = Table('users')
_users.create_index('id', unique=True)
_users.create_index('unique_id')
_cached_users = Table('cached_users')
_cached_users.create_index('unique_id', unique=True)
_cached_players = Table('cached_players')
_cached_players.create_index('id', unique=True)
_pairings = Table('pairings')
_pairings.create_index('player_id')
_pairings.create_index('user_unique_id')
players_filepath = os.path.join(dest_dirpath, constants.PLAYERS_CACHE_FILE)
pairings_filepath = os.path.join(dest_dirpath, constants.PAIRINGS_CACHE_FILE)
backup_filepath = os.path.join(dest_dirpath, "tessu_mod_cache.ini.old-0.6")
source_exists = os.path.isfile(source_filepath)
dest_exists = all(map(os.path.isfile, [users_filepath, players_filepath, pairings_filepath]))
if source_exists and not dest_exists:
logger.info("Migrating caches from version 0.6 to 0.7")
# Schema for new caches
users = Table()
users.create_index('unique_id', unique=True)
players = Table()
players.create_index('id', unique=True)
pairings = Table()
pairings.create_index('player_id')
pairings.create_index('user_unique_id')
# Load old 0.6.x cache file
parser = ConfigParser.ConfigParser()
with open(source_filepath, "rb") as file:
parser.readfp(file)
# Build new cache structures
users.insert_many(DataObject(unique_id=id, name=name) for name, id in parser.items("TeamSpeakUsers"))
players.insert_many(DataObject(id=int(id), name=name) for name, id in parser.items("GamePlayers"))
for user_name, player_names in parser.items("UserPlayerPairings"):
userid = _.head(users.where(name=user_name)).unique_id
for player_name in list(csv.reader([player_names]))[0]:
playerid = _.head(players.where(name=player_name)).id
pairings.insert(DataObject(player_id=int(playerid), user_unique_id=userid))
* pairings_cache.v1.json
"""
source_filepath = os.path.join(source_dirpath, "tessu_mod_cache.ini")
users_filepath = os.path.join(dest_dirpath, constants.USERS_CACHE_FILE)
players_filepath = os.path.join(dest_dirpath, constants.PLAYERS_CACHE_FILE)
pairings_filepath = os.path.join(dest_dirpath, constants.PAIRINGS_CACHE_FILE)
backup_filepath = os.path.join(dest_dirpath, "tessu_mod_cache.ini.old-0.6")
source_exists = os.path.isfile(source_filepath)
dest_exists = all(map(os.path.isfile, [users_filepath, players_filepath, pairings_filepath]))
if source_exists and not dest_exists:
logger.info("Migrating caches from version 0.6 to 0.7")
# Schema for new caches
users = Table()
users.create_index('unique_id', unique=True)
players = Table()
players.create_index('id', unique=True)
pairings = Table()
pairings.create_index('player_id')
pairings.create_index('user_unique_id')
# Load old 0.6.x cache file
parser = ConfigParser.ConfigParser()
with open(source_filepath, "rb") as file:
parser.readfp(file)
# Build new cache structures
users.insert_many(DataObject(unique_id=id, name=name) for name, id in parser.items("TeamSpeakUsers"))
players.insert_many(DataObject(id=int(id), name=name) for name, id in parser.items("GamePlayers"))
for user_name, player_names in parser.items("UserPlayerPairings"):
_battle_players.create_index('id', unique=True)
_vehicles = Table('vehicles')
_vehicles.create_index('id', unique=True)
_vehicles.create_index('player_id', unique=True)
_prebattle_players = Table('prebattle_players')
_prebattle_players.create_index('id', unique=True)
_speaking_players = Table('speaking_players')
_speaking_players.create_index('id', unique=True)
_users = Table('users')
_users.create_index('id', unique=True)
_users.create_index('unique_id')
_cached_users = Table('cached_users')
_cached_users.create_index('unique_id', unique=True)
_cached_players = Table('cached_players')
_cached_players.create_index('id', unique=True)
_pairings = Table('pairings')
_pairings.create_index('player_id')
_pairings.create_index('user_unique_id')
def import_caches(users_file, players_file, pairings_file):
"""
Loads user, player and pairing caches from files.
May raise an error, if importing fails.
:param users_file: users cache file (file object or path to file)
:param players_file: players cache file (file object or path to file)
:param pairings_file: pairings cache file (file object or path to file)
"""
global _cached_users, _cached_players, _pairings
logger.debug('Importing caches')
users = _cached_users.copy_template()
source_filepath = os.path.join(source_dirpath, "tessu_mod_cache.ini")
users_filepath = os.path.join(dest_dirpath, constants.USERS_CACHE_FILE)
players_filepath = os.path.join(dest_dirpath, constants.PLAYERS_CACHE_FILE)
pairings_filepath = os.path.join(dest_dirpath, constants.PAIRINGS_CACHE_FILE)
backup_filepath = os.path.join(dest_dirpath, "tessu_mod_cache.ini.old-0.6")
source_exists = os.path.isfile(source_filepath)
dest_exists = all(map(os.path.isfile, [users_filepath, players_filepath, pairings_filepath]))
if source_exists and not dest_exists:
logger.info("Migrating caches from version 0.6 to 0.7")
# Schema for new caches
users = Table()
users.create_index('unique_id', unique=True)
players = Table()
players.create_index('id', unique=True)
pairings = Table()
pairings.create_index('player_id')
pairings.create_index('user_unique_id')
# Load old 0.6.x cache file
parser = ConfigParser.ConfigParser()
with open(source_filepath, "rb") as file:
parser.readfp(file)
# Build new cache structures
users.insert_many(DataObject(unique_id=id, name=name) for name, id in parser.items("TeamSpeakUsers"))
players.insert_many(DataObject(id=int(id), name=name) for name, id in parser.items("GamePlayers"))
for user_name, player_names in parser.items("UserPlayerPairings"):
userid = _.head(users.where(name=user_name)).unique_id
for player_name in list(csv.reader([player_names]))[0]:
_me_player = Table('me_player')
_me_player.create_index('id', unique=True)
_battle_players = Table('battle_players')
_battle_players.create_index('id', unique=True)
_vehicles = Table('vehicles')
_vehicles.create_index('id', unique=True)
_vehicles.create_index('player_id', unique=True)
_prebattle_players = Table('prebattle_players')
_prebattle_players.create_index('id', unique=True)
_speaking_players = Table('speaking_players')
_speaking_players.create_index('id', unique=True)
_users = Table('users')
_users.create_index('id', unique=True)
_users.create_index('unique_id')
_cached_users = Table('cached_users')
_cached_users.create_index('unique_id', unique=True)
_cached_players = Table('cached_players')
_cached_players.create_index('id', unique=True)
_pairings = Table('pairings')
_pairings.create_index('player_id')
_pairings.create_index('user_unique_id')
def import_caches(users_file, players_file, pairings_file):
"""
Loads user, player and pairing caches from files.
May raise an error, if importing fails.
:param users_file: users cache file (file object or path to file)
:param players_file: players cache file (file object or path to file)
:param pairings_file: pairings cache file (file object or path to file)
import os
messages = None
logger = logutils.logger.getChild('database')
_me_player = Table('me_player')
_me_player.create_index('id', unique=True)
_battle_players = Table('battle_players')
_battle_players.create_index('id', unique=True)
_vehicles = Table('vehicles')
_vehicles.create_index('id', unique=True)
_vehicles.create_index('player_id', unique=True)
_prebattle_players = Table('prebattle_players')
_prebattle_players.create_index('id', unique=True)
_speaking_players = Table('speaking_players')
_speaking_players.create_index('id', unique=True)
_users = Table('users')
_users.create_index('id', unique=True)
_users.create_index('unique_id')
_cached_users = Table('cached_users')
_cached_users.create_index('unique_id', unique=True)
_cached_players = Table('cached_players')
_cached_players.create_index('id', unique=True)
_pairings = Table('pairings')
_pairings.create_index('player_id')
_pairings.create_index('user_unique_id')
def import_caches(users_file, players_file, pairings_file):
"""
Loads user, player and pairing caches from files.
import pydash as _
import os
messages = None
logger = logutils.logger.getChild('database')
_me_player = Table('me_player')
_me_player.create_index('id', unique=True)
_battle_players = Table('battle_players')
_battle_players.create_index('id', unique=True)
_vehicles = Table('vehicles')
_vehicles.create_index('id', unique=True)
_vehicles.create_index('player_id', unique=True)
_prebattle_players = Table('prebattle_players')
_prebattle_players.create_index('id', unique=True)
_speaking_players = Table('speaking_players')
_speaking_players.create_index('id', unique=True)
_users = Table('users')
_users.create_index('id', unique=True)
_users.create_index('unique_id')
_cached_users = Table('cached_users')
_cached_users.create_index('unique_id', unique=True)
_cached_players = Table('cached_players')
_cached_players.create_index('id', unique=True)
_pairings = Table('pairings')
_pairings.create_index('player_id')
_pairings.create_index('user_unique_id')
def import_caches(users_file, players_file, pairings_file):
"""