#!/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 False pope_image = parse_pope(path) pope_image.pope = pope pope_image.save() 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) with open(pl) as file: for line in file: line = line.rstrip() line = path.join("popes", line) add_pope_entry(pope, line) if __name__ == '__main__': main()