Add some exception checking to parsers/caves.py so that missing entrance

slugs don't blow up the import. Also reduce the noise, so
you just get a warning about missing slugs printed out
This commit is contained in:
wookey 2012-09-24 23:23:38 +01:00
parent 631ac20c12
commit fb495119ab

View File

@ -10,18 +10,18 @@ def readcaves():
newArea.save()
newArea = models.Area(short_name = "1626", parent = None)
newArea.save()
print "Entrances"
print "list of <Slug> <Filename>"
print "Reading Entrances"
#print "list of <Slug> <Filename>"
for filename in os.walk(settings.ENTRANCEDESCRIPTIONS).next()[2]: #Should be a better way of getting a list of files
readentrance(filename)
print "Caves"
print "Reading Caves"
for filename in os.walk(settings.CAVEDESCRIPTIONS).next()[2]: #Should be a better way of getting a list of files
readcave(filename)
def readentrance(filename):
with open(os.path.join(settings.ENTRANCEDESCRIPTIONS, filename)) as f:
contents = f.read()
context = " in file %s" % filename
context = "in file %s" % filename
entrancecontentslist = getXML(contents, "entrance", maxItems = 1, context = context)
if len(entrancecontentslist) == 1:
entrancecontents = entrancecontentslist[0]
@ -76,7 +76,7 @@ def readentrance(filename):
e.save()
primary = True
for slug in slugs:
print slug, filename
#print slug, filename
cs = models.EntranceSlug(entrance = e,
slug = slug,
primary = primary)
@ -87,7 +87,7 @@ def readcave(filename):
with open(os.path.join(settings.CAVEDESCRIPTIONS, filename)) as f:
contents = f.read()
context = " in file %s" % filename
print "Reading file %s" % filename
#print "Reading file %s" % filename
cavecontentslist = getXML(contents, "cave", maxItems = 1, context = context)
#print cavecontentslist
if len(cavecontentslist) == 1:
@ -146,17 +146,25 @@ def readcave(filename):
c.area.add(newArea)
primary = True
for slug in slugs:
cs = models.CaveSlug(cave = c,
try:
cs = models.CaveSlug(cave = c,
slug = slug,
primary = primary)
cs.save()
cs.save()
except:
print "Can't find text (slug): %s, skipping %s" % (slug, context)
primary = False
for entrance in entrances:
slug = getXML(entrance, "entranceslug", maxItems = 1, context = context)[0]
letter = getXML(entrance, "letter", maxItems = 1, context = context)[0]
entrance = models.Entrance.objects.get(entranceslug__slug = slug)
ce = models.CaveAndEntrance(cave = c, entrance_letter = letter, entrance = entrance)
ce.save()
try:
entrance = models.Entrance.objects.get(entranceslug__slug = slug)
ce = models.CaveAndEntrance(cave = c, entrance_letter = letter, entrance = entrance)
ce.save()
except:
print "Entrance text (slug) %s missing %s" % (slug, context)
def getXML(text, itemname, minItems = 1, maxItems = None, printwarnings = True, context = ""):
items = re.findall("<%(itemname)s>(.*?)</%(itemname)s>" % {"itemname": itemname}, text, re.S)