papiez_ipsum/src/populate_popes

74 lines
1.7 KiB
Python

#!/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 Pope, PopeVersion
from papiezator.pope_utils import parse_pope
from django.core.exceptions import ObjectDoesNotExist
from os import path
from optparse import OptionParser
import ast
PREFIX = "popes"
def pope_or_death(p, **kwargs):
try:
x = p.objects.get(**kwargs)
except ObjectDoesNotExist:
x = None
return x
def init_pope(meta):
pope = pope_or_death(PopeVersion, pope_id=meta["id"])
if pope:
return pope
else:
pope = PopeVersion(meta["id"], meta["name"])
pope.save()
return pope
def add_pope_entry(pope, path):
if pope_or_death(Pope, path=path):
return False
pope_image = parse_pope(path)
pope_image.pope_version = pope
pope_image.save()
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]
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()