# Path: borme/models.py
# class Config(m.Model):
#     last_modified = m.DateTimeField()
#     version = m.CharField(max_length=50)
# 
# Path: libreborme/utils.py
# def get_git_revision_short_hash():
#     os.chdir(os.path.dirname(libreborme.__file__))
#     try:
#         version = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).strip()
#         if isinstance(version, bytes):
#             version = version.decode('unicode_escape')
#     except subprocess.CalledProcessError:
#         logger.warning("Couldn't guess git revision")
#         version = 'Unknown'
#     return version


from django.core.management.base import BaseCommand
from django.utils import timezone
from borme.models import Config
from libreborme.utils import get_git_revision_short_hash
import logging
import time
import borme.parser.importer


# from borme.parser.postgres import psql_update_documents



class Command(BaseCommand):
    help = 'Import BORME JSON file(s)'

    def add_arguments(self, parser):
        parser.add_argument('files', metavar='FILE', nargs='+', type=str)

    def handle(self, *args, **options):
        self.set_verbosity(int(options['verbosity']))
        start_time = time.time()

        for filename in options["files"]:
            print(filename)
            borme.parser.importer.from_json_file(filename)

        config = Config.objects.first()
        if config:
            config.last_modified = timezone.now()
        else:

# Path: borme/models.py
# class Config(m.Model):
#     last_modified = m.DateTimeField()
#     version = m.CharField(max_length=50)
# 
# Path: libreborme/utils.py
# def get_git_revision_short_hash():
#     os.chdir(os.path.dirname(libreborme.__file__))
#     try:
#         version = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).strip()
#         if isinstance(version, bytes):
#             version = version.decode('unicode_escape')
#     except subprocess.CalledProcessError:
#         logger.warning("Couldn't guess git revision")
#         version = 'Unknown'
#     return version


# Path: borme/management/commands/importbormejson.py
from django.core.management.base import BaseCommand
from django.utils import timezone
from borme.models import Config
from libreborme.utils import get_git_revision_short_hash
import logging
import time
import borme.parser.importer



# from borme.parser.postgres import psql_update_documents



class Command(BaseCommand):
    help = 'Import BORME JSON file(s)'

    def add_arguments(self, parser):
        parser.add_argument('files', metavar='FILE', nargs='+', type=str)

    def handle(self, *args, **options):
        self.set_verbosity(int(options['verbosity']))
        start_time = time.time()

        for filename in options["files"]:
            print(filename)
            borme.parser.importer.from_json_file(filename)

        config = Config.objects.first()
        if config:
            config.last_modified = timezone.now()
        else:

            config = Config(last_modified=timezone.now())