papiez_ipsum/src/populate_popes

72 lines
1.7 KiB
Plaintext
Raw Normal View History

2013-11-29 19:16:10 +00:00
#!/usr/bin/env python
2013-11-26 22:32:11 +00:00
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
2013-12-23 18:53:20 +00:00
from papiezator.models import PopeImage, Pope
from papiezator.pope_utils import parse_pope, pope_or_death
2013-11-26 22:32:11 +00:00
from os import path
2013-11-29 19:16:10 +00:00
from optparse import OptionParser
import ast
2013-11-26 22:32:11 +00:00
PREFIX = "popes"
2013-11-29 19:16:10 +00:00
def init_pope(meta):
2013-12-23 18:53:20 +00:00
pope = pope_or_death(Pope, id=meta["id"])
if pope:
return pope
else:
2013-12-23 18:53:20 +00:00
pope = Pope(meta["id"], meta["name"])
2014-02-03 20:11:41 +00:00
pope.save()
return pope
def add_pope_entry(pope, path):
2013-12-23 18:53:20 +00:00
if pope_or_death(PopeImage, path=path):
print('-', end='')
2014-02-03 20:10:02 +00:00
return None
pope_image = parse_pope(path)
2013-12-23 18:53:20 +00:00
pope_image.pope = pope
print('+', end='')
return pope_image
2013-11-29 19:16:10 +00:00
def main():
existing_popes = []
2013-11-29 19:16:10 +00:00
parser = OptionParser()
options, args = parser.parse_args()
pope_lists = glob("popes/*_list")
for pl in pope_lists:
pope = pl.split('_')[0]
2013-12-23 18:53:20 +00:00
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)
2014-02-03 20:10:02 +00:00
pope_images_batch = []
with open(pl, 'rU') as file:
for line in file:
2014-02-03 20:10:02 +00:00
line = line.rstrip('\n')
line = path.join("popes", line)
2014-02-03 20:10:02 +00:00
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))
2013-11-29 19:16:10 +00:00
if __name__ == '__main__':
main()
2013-11-26 22:32:11 +00:00