Skip to content

Commit

Permalink
Broncode bij het Proefpakket
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthijs van der Deijl committed Jun 7, 2011
1 parent 71f586d commit 9dcf701
Show file tree
Hide file tree
Showing 10 changed files with 3,474 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/BAG.conf
@@ -0,0 +1,15 @@
[DEFAULT]

database = BAG
host = localhost
user = postgres
password = postgres
download = c:\BAG\download\
extract = c:\BAG\extract\
logging = C:\BAG\logging\






1,011 changes: 1,011 additions & 0 deletions src/BAGextract+.py

Large diffs are not rendered by default.

853 changes: 853 additions & 0 deletions src/BAGraadpleeg.py

Large diffs are not rendered by default.

1,139 changes: 1,139 additions & 0 deletions src/libBAG.py

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions src/libBAGconfiguratie.py
@@ -0,0 +1,44 @@
#------------------------------------------------------------------------------
# Naam: libBAGconfiguratie.py
# Omschrijving: Generieke functies het lezen van BAG.conf
# Auteur: Matthijs van der Deijl
#
# Versie: 1.2
# Datum: 24 november 2009
#
# Ministerie van Volkshuisvesting, Ruimtelijke Ordening en Milieubeheer
#------------------------------------------------------------------------------
import sys, os, os.path

from ConfigParser import ConfigParser

class BAGconfiguratie:
def __init__(self):
if not os.path.exists('BAG.conf'):
print "*** FOUT *** Kan configuratiebestand 'BAG.conf' niet openen."
print ""
raw_input("Druk <enter> om af te sluiten")
sys.exit()

configdict = ConfigParser()
configdict.read('BAG.conf')
try:
self.database = configdict.defaults()['database']
self.host = configdict.defaults()['host']
self.user = configdict.defaults()['user']
self.password = configdict.defaults()['password']
self.download = configdict.defaults()['download']
self.extract = configdict.defaults()['extract']
self.logging = configdict.defaults()['logging']
except:
print "*** FOUT *** Inhoud van configuratiebestand 'BAG.conf' is niet volledig."
print ""
raw_input("Druk <enter> om af te sluiten")
sys.exit()
try:
self.bestand = configdict.defaults()['bestand']
except:
pass

# Globale variabele voor toegang tot BAG.conf
configuratie = BAGconfiguratie()
33 changes: 33 additions & 0 deletions src/libBAGextractPlusVersie.py
@@ -0,0 +1,33 @@
#------------------------------------------------------------------------------
# Naam: libBAGextractPlusVersie.py
# Omschrijving: Definitie van de versie van BAGextract+
# Auteur: Matthijs van der Deijl
#
# Versie: 1.7
# Datum: 11 maart 2011
#
# Historie: versie 1 oktober 2009
# versie 1.1 18 november 2009
# Modules samengevoegd in 1 menugestuurde applicatie
# versie 1.2 23 november 2009
# Grafische User-interface toegevoegd
# versie 1.3 8 december 2009
# Bugs gefixed
# Raadpleging via kaart toegevoegd
# Foutafhandeling verbeterd
# versie 1.4 21 januari 2010
# Unzip extractbestand met woonplaatsen verbeterd
# versie 1.5 9 september 2010
# Bug (objectType not defined) gefixt in verwerking mutatiebestanden
# versie 1.6 8 oktober 2010
# Veldlengte voor tekstwaarde van geometrie verhoogt naar 1000000
# versie 1.7 11 maart 2011
# Objecttype LPL en SPL vervangen door LIG en STA

#
# Ministerie van Volkshuisvesting, Ruimtelijke Ordening en Milieubeheer
#------------------------------------------------------------------------------

BAGextractPlusVersie = "1.7"
BAGextractPlusDatum = "maart 2011"

145 changes: 145 additions & 0 deletions src/libDatabase.py
@@ -0,0 +1,145 @@
#------------------------------------------------------------------------------
# Naam: libDatabase.py
# Omschrijving: Generieke functies voor databasegebruik binnen BAG Extract+
# Auteur: Matthijs van der Deijl
#
# Versie: 1.3
# - functie controleerTabellen toegevoegd
# - selectie van logregels gesorteerd op datum
# Datum: 9 december 2009
#
# Versie: 1.2
# Datum: 24 november 2009
#
# Ministerie van Volkshuisvesting, Ruimtelijke Ordening en Milieubeheer
#------------------------------------------------------------------------------
import psycopg2

from libBAGconfiguratie import *
from libLog import *

class Database:
def __init__(self):
self.database = configuratie.database
self.host = configuratie.host
self.user = configuratie.user
self.password = configuratie.password

try:
self.connection = psycopg2.connect("dbname='%s' user='%s' host='%s' password='%s'" %(self.database,
self.user,
self.host,
self.password));
self.cursor = self.connection.cursor()
print("Verbinding met database %s geslaagd." %(self.database))
except:
print("*** FOUT *** Verbinding met database %s niet geslaagd." %(self.database))
print("")
raw_input("Druk <enter> om af te sluiten")
sys.exit()

# Maak van de datum/tijdstip string in de BAG een datumwaarde voor in de database
def datum(self, tekst):
if tekst == '':
return "2299-12-31"
else:
return "%s%s%s%s-%s%s-%s%s" %(tekst[0],tekst[1],tekst[2],tekst[3],tekst[4],tekst[5],tekst[6],tekst[7])

# Geef de waarde in de vorm waarin het kan worden ingevoegd in de database
def string(self, tekst):
# Vervang een ' in een string door ''
# Vervang een \n door een spatie
# Vervang een \ door \\
return tekst.replace("'", "''").replace("\n", " ").replace("\\", "\\\\")

def maakObject(self, soort, naam, dropSQL, createSQL):
# Probeer eerst het oude object weg te gooien.
try:
self.connection.set_isolation_level(0)
self.cursor.execute(dropSQL)
log("%s %s verwijderd" %(soort, naam))
except:
pass

# Maak het object nieuw aan.
try:
self.connection.set_isolation_level(0)
self.cursor.execute(createSQL)
log("%s %s nieuw aangemaakt" %(soort, naam))
self.connection.commit()
return True
except (psycopg2.Error,), foutmelding:
log("*** FOUT *** Kan %s %s niet maken:\n %s" %(soort, naam, foutmelding))
return False

def maakTabel(self, naam, createSQL):
return self.maakObject("Tabel", naam, "DROP TABLE %s CASCADE" %(naam), createSQL)

def maakView(self, naam, createSQL):
return self.maakObject("View", naam, "DROP VIEW %s" %(naam), createSQL)

def maakIndex(self, naam, createSQL):
return self.maakObject("Index", naam, "DROP INDEX %s" %(naam), createSQL)

def insert(self, sql, identificatie):
try:
self.cursor.execute(sql)
except (psycopg2.IntegrityError,), foutmelding:
log("* Waarschuwing * Object %s niet opgeslagen: %s" %(identificatie, str(foutmelding)))
except (psycopg2.Error,), foutmelding:
log("*** FOUT *** Object %s niet opgeslagen: %s - %s" %(identificatie, str(foutmelding), sql))
self.connection.commit()

def execute(self, sql):
try:
self.cursor.execute(sql)
self.connection.commit()
return self.cursor.rowcount
except (psycopg2.Error,), foutmelding:
log("*** FOUT *** Kan SQL-statement '%s' niet uitvoeren:\n %s" %(sql, foutmelding))
return False

def select(self, sql):
try:
self.cursor.execute(sql)
rows = self.cursor.fetchall()
self.connection.commit()
return rows
except (psycopg2.Error,), foutmelding:
log("*** FOUT *** Kan SQL-statement '%s' niet uitvoeren:\n %s" %(sql, foutmelding))
return []

def controleerOfMaakLog(self):
try:
self.cursor.execute("SELECT * FROM BAGextractpluslog")
except:
sql = "CREATE TABLE BAGextractpluslog"
sql += "(datum DATE"
sql += ",actie VARCHAR(1000)"
sql += ",bestand VARCHAR(1000)"
sql += ",logfile VARCHAR(1000))"
self.maakTabel("BAGextractpluslog", sql)

def log(self, actie, bestand, logfile):
self.controleerOfMaakLog()
sql = "INSERT INTO BAGextractpluslog (datum, actie, bestand, logfile)"
sql += " VALUES ('now', '%s', '%s', '%s')" %(actie, self.string(bestand), self.string(logfile))
self.execute(sql)

def getLog(self):
self.controleerOfMaakLog()
self.cursor.execute("SELECT * FROM BAGextractpluslog ORDER BY datum, logfile")
rows = self.cursor.fetchall()
self.connection.commit()
return rows

def controleerTabel(self, tabel):
self.cursor.execute("SELECT tablename FROM pg_tables WHERE tablename = '%s'" %tabel)
rows = self.cursor.fetchall()
self.connection.commit()
if len(rows) == 0:
return False
return True

# Globale variabele
database = Database()
155 changes: 155 additions & 0 deletions src/libLog.py
@@ -0,0 +1,155 @@
#------------------------------------------------------------------------------
# Naam: libLog.py
# Omschrijving: Generieke functies voor logging binnen BAG Extract+
# Auteur: Matthijs van der Deijl
#
# Versie: 1.3
# - foutafhandeling verbeterd
# Datum: 16 december 2009
#
# Versie: 1.2
# Datum: 24 november 2009
#
# Ministerie van Volkshuisvesting, Ruimtelijke Ordening en Milieubeheer
#------------------------------------------------------------------------------
import time, datetime
import sys
import wx

from libBAGextractPlusVersie import *
from libBAGconfiguratie import *


class LogScherm:
def __init__(self):
self.logStack = []
self.stackGrootte = -1

def __call__(self, tekst):
self.schrijf(tekst)

def push(self, log):
self.stackGrootte += 1
self.logStack.append(" ")
self.logStack[self.stackGrootte] = log

def pop(self):
self.stackGrootte -= 1

def start(self):
if self.stackGrootte >= 0:
i = self.logStack[self.stackGrootte].GetNumberOfLines()
self.logStack[self.stackGrootte].Clear()
while i > 0:
self.logStack[self.stackGrootte].AppendText(" \n")
i -= 1
self.logStack[self.stackGrootte].Clear()

def schrijf(self, tekst):
if self.stackGrootte < 0:
print tekst
else:
self.logStack[self.stackGrootte].AppendText("\n" + tekst)
self.logStack[self.stackGrootte].Refresh()
self.logStack[self.stackGrootte].Update()

# globale variabele voor gebruik van de logging
logScherm = LogScherm()

class Log:
def __init__(self):
self.logfileNaam = ""
self.logfile = None
self.starttime = 0
self.bagextractplus = None
self.cursor = None

def __call__(self, tekst):
self.schrijf(tekst)

def start(self, applicatie, database, actie, bestand):
logScherm.start()
logScherm(actie + " " + bestand)
logScherm("")
i = 0
gevonden = False
while not gevonden:
self.logfileNaam = configuratie.logging + "BAGextract+ %s %03d.log" %(str(datetime.date.today()), i)
try:
self.logfile = open(self.logfileNaam, "r")
self.logfile.close()
except:
gevonden = True
i += 1

try:
self.logfile = open(self.logfileNaam, "w")
except Exception, foutmelding:
logScherm("Fout - kan logfile '%s' niet openen:\n %s" %(self.logfileNaam, foutmelding))
logScherm("")
self.logfile = None
self.logfileNaam = "<niet beschikbaar>"

if self.logfile:
self.logfile.write(" ____ _ ____ _____ _ _ Versie: %-13s \n" %(BAGextractPlusVersie))
self.logfile.write("| __ ) / \ / ___| | ____|_ _| |_ _ __ __ _ ___| |_ _ %-13s \n" %(BAGextractPlusDatum))
self.logfile.write("| _ \ / _ \| | _ | _| \ \/ / __| '__/ _` |/ __| __| _|+|_ \n")
self.logfile.write("| |_) / ___ \ |_| | | |___ > <| |_| | | (_| | (__| | |+++++| \n")
self.logfile.write("|____/_/ \_\VROM| |_____/_/\_\ __|_| \__,_|\___|\__| |+| \n")
self.logfile.write("\n")
self.logfile.write("Logfile: %s\n" %(self.logfileNaam))
self.logfile.write("Datum: %s\n" %(str(datetime.date.today())))
self.logfile.write("Actie: %s - %s\n" %(actie, bestand))
self.logfile.write("-------------------------------------------------------------------------------\n")
self.logfile.write("\n")
self.logfile.flush()

self.bagextractplus = applicatie
self.cursor = self.bagextractplus.GetCursor()
self.bagextractplus.SetCursor(wx.HOURGLASS_CURSOR)
for i in range(5):
self.bagextractplus.menuBalk.EnableTop(i, False)
self.bagextractplus.Refresh()
self.bagextractplus.Update()
self.bagextractplus.app.Yield(True)

self.starttime = 0
database.log(actie, bestand, self.logfileNaam)

def sluit(self):
if self.logfile:
self.logfile.close()
self.logfile = None
logScherm("Zie logfile '%s' voor het verslag" %(self.logfileNaam))
logScherm("")

self.bagextractplus.SetCursor(self.cursor)
for i in range(5):
self.bagextractplus.menuBalk.EnableTop(i, True)
self.bagextractplus.Refresh()
self.bagextractplus.Update()
self.bagextractplus.app.Yield(True)
self.bagextractplus = None

def schrijf(self, tekst):
if self.logfile:
self.logfile.write(tekst + "\n")
self.logfile.flush()
logScherm(tekst)
if self.bagextractplus:
self.bagextractplus.app.Yield(True)

def startTimer(self):
self.starttime = time.clock()

def schrijfTimer(self, tekst):
tijd = time.clock() - self.starttime
if tijd < 60 :
self.schrijf(tekst + " in %s seconden" %(tijd))
else:
self.schrijf(tekst + " in %s minuten" %(tijd/60))

# globale variabele voor gebruik van de logging
log = Log()


0 comments on commit 9dcf701

Please sign in to comment.