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