From 302f4a1a0f9ced0d8e81c5462f7feff65c5b349b Mon Sep 17 00:00:00 2001 From: substantialnoninfringinguser Date: Wed, 13 May 2009 05:13:51 +0100 Subject: [PATCH] [svn] Copied from http://cucc@cucc.survex.com/svn/trunk/expoweb/troggle/, rev. 8035 by julian @ 10/26/2008 9:46 PM --- expo/models_survex.py | 17 +++++++------ parsers/survex.py | 59 +++++++++++++++++++++++++++++++++++-------- 2 files changed, 57 insertions(+), 19 deletions(-) diff --git a/expo/models_survex.py b/expo/models_survex.py index b00cf1b..29944d8 100644 --- a/expo/models_survex.py +++ b/expo/models_survex.py @@ -1,15 +1,16 @@ from django.db import models class SurvexBlock(models.Model): - name = models.CharField(max_length=100) + name = models.CharField(max_length=100, blank=True, null=True) + parent = models.ForeignKey('SurvexBlock', blank=True, null=True) text = models.TextField() - notes_person = models.ManyToManyField('PersonTrip', related_name="notes") - pics_person = models.ManyToManyField('PersonTrip', related_name="pics") - tape_person = models.ManyToManyField('PersonTrip', related_name="tape") - insts_person = models.ManyToManyField('PersonTrip', related_name="insts") + notes_person = models.ManyToManyField('PersonTrip', related_name="notes", blank=True, null=True) + pics_person = models.ManyToManyField('PersonTrip', related_name="pics", blank=True, null=True) + tape_person = models.ManyToManyField('PersonTrip', related_name="tape", blank=True, null=True) + insts_person = models.ManyToManyField('PersonTrip', related_name="insts", blank=True, null=True) begin_file = models.CharField(max_length=200) begin_char = models.IntegerField() - end_file = models.CharField(max_length=200) - end_char = models.IntegerField() + end_file = models.CharField(max_length=200, blank=True, null=True) + end_char = models.IntegerField(blank=True, null=True) def __unicode__(self): - return unicode(name) + return unicode(self.name) diff --git a/parsers/survex.py b/parsers/survex.py index 0f75e06..f6d11e4 100644 --- a/parsers/survex.py +++ b/parsers/survex.py @@ -3,16 +3,25 @@ import expo.models as models import re import os -def readFile(filename): - for line in fileIterator(settings.SURVEX_DATA, filename): - print line - re_include_extension = re.compile(r"^\s*\*include\s+([^\s]*).svx$", re.IGNORECASE) re_include_no_extension = re.compile(r"^\s*\*include\s+([^\s]*)$", re.IGNORECASE) +re_begin = re.compile(r"^\s*\*begin\s+(.*?)\s*$", re.IGNORECASE) +re_end = re.compile(r"^\s*\*end\s+(.*?)\s*$", re.IGNORECASE) + +def save(x): #There seems to be an intermitent problem with sqlite and Vista, this should fix it + try: + x.save() + except sqlite3.OperationalError: + print "Error" + time.sleep(1) + save(x) def fileIterator(directory, filename): - f = open(os.path.join(directory, filename + ".svx"), "rb") + survex_file = os.path.join(directory, filename + ".svx") + f = open(os.path.join(settings.SURVEX_DATA, survex_file), "rb") + char = 0 for line in f.readlines(): + line = unicode(line, "latin1") include_extension = re_include_extension.match(line) include_no_extension = re_include_no_extension.match(line) def a(include): @@ -20,12 +29,40 @@ def fileIterator(directory, filename): print os.path.join(directory, *link[:-1]), link[-1] return fileIterator(os.path.join(directory, *link[:-1]), link[-1]) if include_extension: - for b in a(include_extension.groups()[0]): - yield b + for sf, c, l in a(include_extension.groups()[0]): + yield sf, c, l elif include_no_extension: - for b in a(include_no_extension.groups()[0]): - yield b + for sf, c, l in a(include_no_extension.groups()[0]): + yield sf, c, l else: - yield line + yield survex_file, char, line + char = char + len(line) -readFile("all") \ No newline at end of file +def make_model(name, parent, iter_lines, sf, c, l): + if parent: + m = models.SurvexBlock(name = name, parent = parent, begin_file = sf, begin_char = c, text = l) + else: + m = models.SurvexBlock(name = name, begin_file = sf, begin_char = c, text = l) + save(m) + for survex_file, count, line in iter_lines: + begin = re_begin.match(line.split(";")[0]) + end = re_end.match(line.split(";")[0]) + if begin: + make_model(begin.groups()[0], m, iter_lines, survex_file, count, line) + elif end: + m.text = m.text + line + m.end_file = survex_file + m.end_char = count + save(m) + assert (end.groups()[0]).lower() == (name).lower() + return None + else: + m.text = m.text + line + m.text = m.text + line + m.end_file = survex_file + m.end_char = count + save(m) + + +filename = "all" +make_model("", None, fileIterator("", filename), filename, 0, "")