#!/usr/bin/env python import os os.environ ['PYTHONPATH'] = '/home/daz/Documents/Projects/Py/django/papiez_ipsum' os.environ['DJANGO_SETTINGS_MODULE'] = 'papiez_ipsum.settings' from glob import glob from papiezator.models import PopeImage, Pope from papiezator.pope_utils import parse_pope, pope_or_death from os import path from optparse import OptionParser import ast PREFIX = "popes" def init_pope(meta): pope = pope_or_death(Pope, id=meta["id"]) if pope: return pope else: pope = Pope(meta["id"], meta["name"]) pope.save() return pope def add_pope_entry(pope, path): if pope_or_death(PopeImage, path=path): print('-', end='') return None pope_image = parse_pope(path) pope_image.pope = pope print('+', end='') return pope_image def main(): existing_popes = [] parser = OptionParser() options, args = parser.parse_args() pope_lists = glob("popes/*_list") for pl in pope_lists: pope = pl.split('_')[0] print(pope) if os.path.exists(pope+"_meta"): with open(pope+"_meta") as file: meta = ast.literal_eval(file.read()) if meta["id"] in existing_popes: raise "jp2gmd" existing_popes.append(meta["id"]) pope = init_pope(meta) pope_images_batch = [] with open(pl, 'rU') as file: for line in file: line = line.rstrip('\n') line = path.join("popes", line) pope_images_batch.append( add_pope_entry(pope, line) ) PopeImage.objects.bulk_create((x for x in pope_images_batch if x is not None)) if __name__ == '__main__': main()