diff --git a/parsers/caves.py b/parsers/caves.py index e0232d9..d4d53cd 100644 --- a/parsers/caves.py +++ b/parsers/caves.py @@ -10,18 +10,18 @@ def readcaves(): newArea.save() newArea = models.Area(short_name = "1626", parent = None) newArea.save() - print "Entrances" - print "list of " + print "Reading Entrances" + #print "list of " 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": itemname}, text, re.S)