<commit_before>import json
import struct
import math

from django.core.management.base import BaseCommand
from django.db import transaction
from zipfile import ZipFile

from api.models import Ship, ShipModelLOD, Texture, TexturePack

class Command(BaseCommand):

    DIRECT_FIELDS = {
        'name': 'name',
        'infocard': 'infocard',
        'price': 'price',
        'hold_size': 'hold_size',
        'nanobot_limit': 'max_bots',
        'shield_battery_limit': 'max_bats', 
        'max_bank_angle': 'weapon_angle', 
        'hit_pts': 'hitpoints', 
        'strafe_force': 'strafe', 
        'strafe_power_usage': 'strafe_power', 
        'linear_drag': 'linear_drag',
        'nudge_force': 'nudge', 
        'mass': 'mass', 
    }

    TRIPLET_FIELDS = {
        'steering_torque': 'torgue',
        'angular_drag': 'drag',
        'rotation_inertia': 'inertia',
    }
    
    def add_arguments(self, parser):
        parser.add_argument(
            '--file',
            type=str,
            default=None,
            help='File to import.',
        )

    @transaction.atomic
    def handle(self, **options):
        filename = options.get('file')

        with ZipFile(filename, 'r') as zf:
            ship_data = json.loads(zf.read('data.json').decode('utf-8'))
            pack_by_pack_id = self._import_textures(zf, ship_data['texture_ids'])
            self._import_ships(zf, ship_data['ships'], pack_by_pack_id)

    @classmethod
    def _import_ships(cls, zf, data, pack_by_pack_id):
        for import_id in data:
            cls._import_ship(zf, data[import_id], import_id, pack_by_pack_id)
            data[import_id] = None


    @classmethod
    def _import_ship(cls, zf, data, import_id, pack_by_pack_id):
        full_data = {}

        for key, target in cls.DIRECT_FIELDS.items():
            full_data[target] = data[key][0] if isinstance(data[key], list) else data[key]
        
        for key, target in cls.TRIPLET_FIELDS.items():
            parts = data[key].split(',')

            full_data[target + '_x'] = math.floor(float(parts[0]))<commit_msg>Add ships flush parameter<commit_after>import json
import struct
import math

from django.core.management.base import BaseCommand
from django.db import transaction
from zipfile import ZipFile

from api.models import Ship, ShipModelLOD, Texture, TexturePack

class Command(BaseCommand):

    DIRECT_FIELDS = {
        'name': 'name',
        'infocard': 'infocard',
        'price': 'price',
        'hold_size': 'hold_size',
        'nanobot_limit': 'max_bots',
        'shield_battery_limit': 'max_bats', 
        'max_bank_angle': 'weapon_angle', 
        'hit_pts': 'hitpoints', 
        'strafe_force': 'strafe', 
        'strafe_power_usage': 'strafe_power', 
        'linear_drag': 'linear_drag',
        'nudge_force': 'nudge', 
        'mass': 'mass', 
    }

    TRIPLET_FIELDS = {
        'steering_torque': 'torgue',
        'angular_drag': 'drag',
        'rotation_inertia': 'inertia',
    }
    
    def add_arguments(self, parser):
        parser.add_argument(
            '--file',
            type=str,
            default=None,
            help='File to import.',
        )
        parser.add_argument(
            '--flush',
            action='store_true',
            default=False,
            help='delete all old ships',
        )  

    @transaction.atomic
    def handle(self, **options):
        filename = options.get('file')
        flush = options.get('flush', False)

        if flush:
            Ship.objects.all().delete()
            TexturePack.objects.all().delete()
            

        with ZipFile(filename, 'r') as zf:
            ship_data = json.loads(zf.read('data.json').decode('utf-8'))
            pack_by_pack_id = self._import_textures(zf, ship_data['texture_ids'])
            self._import_ships(zf, ship_data['ships'], pack_by_pack_id)

    @classmethod
    def _import_ships(cls, zf, data, pack_by_pack_id):
        for import_id in data:
            cls._import_ship(zf, data[import_id], import_id, pack_by_pack_id)
            data[import_id] = None


    @classmethod
    def _import_ship(cls, zf, data, import_id, pack_by_pack_id):
        full_data = {}

        for key, target in cls.DIRECT_FIELDS.items():
            full_data[target] = data[key][0] if isinstance(data[key], list) else data[key]
        
        for key, target in cls.TRIPLET_FIELDS.items():
            parts = data[key].split(',')

            full_data[target + '_x'] = math.floor(float(parts[0]))
{'fraction_non_alphanumeric': 0.12787610619469025, 'fraction_numerical': 0.001327433628318584, 'mean_word_length': 2.396694214876033, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2004589', 'n_tokens_mistral': 1481, 'n_tokens_neox': 1443, 'n_words': 289}