2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2025-01-20 01:42:30 +00:00

[svn] switched from dodgy manually writing to logfile to using python's logging module, which seems great

This commit is contained in:
substantialnoninfringinguser 2009-05-22 06:17:24 +01:00
parent 3784eb9720
commit b774c42333
5 changed files with 37 additions and 45 deletions

View File

@ -1,4 +1,4 @@
import urllib, urlparse, string, os, datetime import urllib, urlparse, string, os, datetime, logging
import troggle.mptt as mptt import troggle.mptt as mptt
from django.forms import ModelForm from django.forms import ModelForm
from django.db import models from django.db import models
@ -14,6 +14,10 @@ getcontext().prec=2 #use 2 significant figures for decimal calculations
from models_survex import * from models_survex import *
logging.basicConfig(level=logging.DEBUG,
filename=settings.LOGFILE,
filemode='w')
#This class is for adding fields and methods which all of our models will have. #This class is for adding fields and methods which all of our models will have.
class TroggleModel(models.Model): class TroggleModel(models.Model):
new_since_parsing = models.BooleanField(default=False, editable=False) new_since_parsing = models.BooleanField(default=False, editable=False)

View File

@ -1,12 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import troggle.expo.models as models import troggle.expo.models as models
from django.conf import settings from django.conf import settings
import csv import csv, time, re, os, logging
import time
import re
import os
from troggle.save_carefully import save_carefully from troggle.save_carefully import save_carefully
##format of CAVETAB2.CSV is ##format of CAVETAB2.CSV is
@ -136,20 +131,18 @@ def html_to_wiki(text):
text2 = "" text2 = ""
return out return out
def LoadCaveTab(logfile=settings.LOGFILE): def LoadCaveTab():
cavetab = open(os.path.join(settings.EXPOWEB, "noinfo", "CAVETAB2.CSV"),'rU') cavetab = open(os.path.join(settings.EXPOWEB, "noinfo", "CAVETAB2.CSV"),'rU')
caveReader = csv.reader(cavetab) caveReader = csv.reader(cavetab)
caveReader.next() # Strip out column headers caveReader.next() # Strip out column headers
if logfile: logging.info("Beginning to import caves from "+str(cavetab)+"\n"+"-"*60+"\n")
logfile.write("Beginning to import caves from "+str(cavetab)+"\n"+"-"*60+"\n")
for katArea in ['1623', '1626']: for katArea in ['1623', '1626']:
if not models.Area.objects.filter(short_name = katArea): if not models.Area.objects.filter(short_name = katArea):
newArea = models.Area(short_name = katArea) newArea = models.Area(short_name = katArea)
newArea.save() newArea.save()
if logfile: logging.info("Added area "+str(newArea.short_name)+"\n")
logfile.write("Added area "+str(newArea.short_name)+"\n")
area1626 = models.Area.objects.filter(short_name = '1626')[0] area1626 = models.Area.objects.filter(short_name = '1626')[0]
area1623 = models.Area.objects.filter(short_name = '1623')[0] area1623 = models.Area.objects.filter(short_name = '1623')[0]
@ -190,8 +183,7 @@ def LoadCaveTab(logfile=settings.LOGFILE):
addToDefaultArgs(Notes, "notes") addToDefaultArgs(Notes, "notes")
newCave, created=save_carefully(models.Cave, lookupAttribs=args, nonLookupAttribs=defaultArgs) newCave, created=save_carefully(models.Cave, lookupAttribs=args, nonLookupAttribs=defaultArgs)
if logfile: logging.info("Added cave "+str(newCave)+"\n")
logfile.write("Added cave "+str(newCave)+"\n")
#If we created a new cave, add the area to it. This does mean that if a cave's identifying features have not changed, areas will not be updated from csv. #If we created a new cave, add the area to it. This does mean that if a cave's identifying features have not changed, areas will not be updated from csv.
if created and line[Area]: if created and line[Area]:
@ -209,14 +201,14 @@ def LoadCaveTab(logfile=settings.LOGFILE):
newCave.area.add(area1623) newCave.area.add(area1623)
newCave.save() newCave.save()
if logfile:
logfile.write("Added area "+line[Area]+" to cave "+str(newCave)+"\n") logging.info("Added area "+line[Area]+" to cave "+str(newCave)+"\n")
if created and line[UnofficialName]: if created and line[UnofficialName]:
newUnofficialName = models.OtherCaveName(cave = newCave, name = line[UnofficialName]) newUnofficialName = models.OtherCaveName(cave = newCave, name = line[UnofficialName])
newUnofficialName.save() newUnofficialName.save()
if logfile:
logfile.write("Added unofficial name "+str(newUnofficialName)+" to cave "+str(newCave)+"\n") logging.info("Added unofficial name "+str(newUnofficialName)+" to cave "+str(newCave)+"\n")
if created and line[MultipleEntrances] == '' or \ if created and line[MultipleEntrances] == '' or \
line[MultipleEntrances] == 'entrance' or \ line[MultipleEntrances] == 'entrance' or \
@ -277,8 +269,8 @@ def LoadCaveTab(logfile=settings.LOGFILE):
addToArgs(Bearings, 'bearings') addToArgs(Bearings, 'bearings')
newEntrance = models.Entrance(**args) newEntrance = models.Entrance(**args)
newEntrance.save() newEntrance.save()
if logfile:
logfile.write("Added entrance "+str(newEntrance)+"\n") logging.info("Added entrance "+str(newEntrance)+"\n")
if line[Entrances]: if line[Entrances]:
entrance_letter = line[Entrances] entrance_letter = line[Entrances]
@ -287,8 +279,8 @@ def LoadCaveTab(logfile=settings.LOGFILE):
newCaveAndEntrance = models.CaveAndEntrance(cave = newCave, entrance = newEntrance, entrance_letter = entrance_letter) newCaveAndEntrance = models.CaveAndEntrance(cave = newCave, entrance = newEntrance, entrance_letter = entrance_letter)
newCaveAndEntrance.save() newCaveAndEntrance.save()
if logfile:
logfile.write("Added CaveAndEntrance "+str(newCaveAndEntrance)+"\n") logging.info("Added CaveAndEntrance "+str(newCaveAndEntrance)+"\n")
# lookup function modelled on GetPersonExpeditionNameLookup # lookup function modelled on GetPersonExpeditionNameLookup

View File

@ -1,6 +1,4 @@
import sys import sys, os, types, logging
import os
import types
#sys.path.append('C:\\Expo\\expoweb') #sys.path.append('C:\\Expo\\expoweb')
#from troggle import * #from troggle import *
#os.environ['DJANGO_SETTINGS_MODULE']='troggle.settings' #os.environ['DJANGO_SETTINGS_MODULE']='troggle.settings'
@ -26,7 +24,7 @@ def get_or_create_placeholder(year):
placeholder_logbook_entry, newly_created = save_carefully(LogbookEntry, lookupAttribs, nonLookupAttribs) placeholder_logbook_entry, newly_created = save_carefully(LogbookEntry, lookupAttribs, nonLookupAttribs)
return placeholder_logbook_entry return placeholder_logbook_entry
def readSurveysFromCSV(logfile=None): def readSurveysFromCSV():
try: try:
surveytab = open(os.path.join(settings.SURVEYS, "Surveys.csv")) surveytab = open(os.path.join(settings.SURVEYS, "Surveys.csv"))
except IOError: except IOError:
@ -43,16 +41,16 @@ def readSurveysFromCSV(logfile=None):
print "There are no expeditions in the database. Please run the logbook parser." print "There are no expeditions in the database. Please run the logbook parser."
sys.exit() sys.exit()
if logfile:
logfile.write("Deleting all scanned images") logging.info("Deleting all scanned images")
ScannedImage.objects.all().delete() ScannedImage.objects.all().delete()
if logfile:
logfile.write("Deleting all survey objects") logging.info("Deleting all survey objects")
Survey.objects.all().delete() Survey.objects.all().delete()
if logfile:
logfile.write("Beginning to import surveys from "+str(os.path.join(settings.SURVEYS, "Surveys.csv"))+"\n"+"-"*60+"\n") logging.info("Beginning to import surveys from "+str(os.path.join(settings.SURVEYS, "Surveys.csv"))+"\n"+"-"*60+"\n")
for survey in surveyreader: for survey in surveyreader:
#I hate this, but some surveys have a letter eg 2000#34a. The next line deals with that. #I hate this, but some surveys have a letter eg 2000#34a. The next line deals with that.
@ -74,8 +72,8 @@ def readSurveysFromCSV(logfile=None):
pass pass
surveyobj.save() surveyobj.save()
if logfile:
logfile.write("added survey " + survey[header['Year']] + "#" + surveyobj.wallet_number + "\r") logging.info("added survey " + survey[header['Year']] + "#" + surveyobj.wallet_number + "\r")
def listdir(*directories): def listdir(*directories):
try: try:

View File

@ -1,4 +1,4 @@
from settings import LOGFILE import logging
def save_carefully(objectType, lookupAttribs={}, nonLookupAttribs={}): def save_carefully(objectType, lookupAttribs={}, nonLookupAttribs={}):
"""Looks up instance using lookupAttribs and carries out the following: """Looks up instance using lookupAttribs and carries out the following:
@ -18,14 +18,12 @@ def save_carefully(objectType, lookupAttribs={}, nonLookupAttribs={}):
setattr(instance, k, v) setattr(instance, k, v)
instance.save() instance.save()
if LOGFILE: if created:
if created: logging.info(unicode(instance)+u' was just added to the database for the first time. \n')
LOGFILE.write(unicode(instance)+u' was just added to the database for the first time. \n')
if not created and instance.new_since_parsing: if not created and instance.new_since_parsing:
LOGFILE.write(unicode(instance)+" has been modified using Troggle, so the current script left it as is. \n") logging.info(unicode(instance)+" has been modified using Troggle, so the current script left it as is. \n")
if not created and not instance.new_since_parsing: if not created and not instance.new_since_parsing:
LOGFILE.write(unicode(instance)+" existed in the database unchanged since last parse. It was overwritten by the current script. \n") logging.info(unicode(instance)+" existed in the database unchanged since last parse. It was overwritten by the current script. \n")
LOGFILE.flush()
return (instance, created) return (instance, created)

View File

@ -10,7 +10,7 @@
<li>{{ job }}</li> <li>{{ job }}</li>
{% endfor %} {% endfor %}
</ul> </ul>
{% if settings.LOGFILE %}See the logfile at {{settings.LOGFILE.path}} for more information.{% endif %} {% if settings.LOGFILE %}See the logfile at {{settings.LOGFILE}} for more information.{% endif %}
<a href="#" class="closeDiv">dismiss this message</a> <a href="#" class="closeDiv">dismiss this message</a>
</div> </div>
{% endif %} {% endif %}