Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use string Templating for sql scripts #433

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 23 additions & 34 deletions cadastre/cadastre_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from datetime import datetime
from distutils import dir_util
from pathlib import Path
from string import Template

from db_manager.db_plugins.plugin import BaseError
from db_manager.dlg_db_error import DlgDbError
Expand Down Expand Up @@ -86,9 +87,10 @@ def __init__(self, dialog):
self.pScriptDir = tempfile.mkdtemp('', 'cad_p_script_', tempDir)
self.edigeoPlainDir = tempfile.mkdtemp('', 'cad_edigeo_plain_', tempDir)
self.replaceDict = {
'[VERSION]': self.dialog.dataVersion,
'[ANNEE]': self.dialog.dataYear,
'[LOT]': self.dialog.edigeoLot
'VERSION': self.dialog.dataVersion,
'ANNEE': self.dialog.dataYear,
'LOT': self.dialog.edigeoLot,
'SRID': '2154', # The default
}
self.maxInsertRows = s.value("cadastre/maxInsertRows", 50000, type=int)
self.spatialiteTempStore = s.value("cadastre/spatialiteTempStore", 'MEMORY', type=str)
Expand Down Expand Up @@ -133,9 +135,9 @@ def __init__(self, dialog):
]

if self.dialog.dbType == 'postgis':
self.replaceDict['[PREFIXE]'] = '"%s".' % self.dialog.schema
self.replaceDict['PREFIXE'] = '"%s".' % self.dialog.schema
else:
self.replaceDict['[PREFIXE]'] = ''
self.replaceDict['PREFIXE'] = ''
self.go = True
self.startTime = datetime.now()
self.step = 0
Expand Down Expand Up @@ -216,7 +218,7 @@ def installCadastreStructure(self):

# Replace dictionnary
replaceDict = self.replaceDict.copy()
replaceDict['2154'] = self.targetSrid
replaceDict['SRID'] = self.targetSrid

# Suppression des éventuelles tables edigeo import
# laissées suite à bug par exemple
Expand Down Expand Up @@ -266,7 +268,7 @@ def updateCadastreStructure(self):

# Replace dictionnary
replaceDict = self.replaceDict.copy()
replaceDict['2154'] = self.targetSrid
replaceDict['SRID'] = self.targetSrid

# Run the table creation scripts
for table in newTables:
Expand Down Expand Up @@ -353,7 +355,7 @@ def importMajic(self):
# If MAJIC but no EDIGEO afterward
# run SQL script to update link between EDI/MAJ
if not self.dialog.doEdigeoImport:
replaceDict['[DEPDIR]'] = f'{self.dialog.edigeoDepartement}{self.dialog.edigeoDirection}'
replaceDict['DEPDIR'] = f'{self.dialog.edigeoDepartement}{self.dialog.edigeoDirection}'
scriptList.append(
{
'title': 'Suppression des indexes',
Expand All @@ -378,7 +380,7 @@ def importMajic(self):
)

# Ajout de la table parcelle_info
replaceDict['2154'] = self.targetSrid
replaceDict['SRID'] = self.targetSrid
scriptList.append(
{
'title': 'Ajout de la table parcelle_info',
Expand Down Expand Up @@ -675,7 +677,7 @@ def importEdigeo(self):

# Suppression et recréation des tables edigeo pour import
if self.dialog.hasData:
replaceDict['2154'] = self.targetSrid
replaceDict['SRID'] = self.targetSrid
# Drop edigeo data
self.dropEdigeoRawData()
scriptList.append(
Expand Down Expand Up @@ -716,7 +718,7 @@ def importEdigeo(self):

# Format edigeo data
replaceDict = self.replaceDict.copy()
replaceDict['[DEPDIR]'] = f'{self.dialog.edigeoDepartement}{self.dialog.edigeoDirection}'
replaceDict['DEPDIR'] = f'{self.dialog.edigeoDepartement}{self.dialog.edigeoDirection}'

scriptList = []

Expand Down Expand Up @@ -764,15 +766,15 @@ def importEdigeo(self):

# Ajout de la table parcelle_info
if (self.dialog.doMajicImport or self.dialog.hasMajicDataProp):
replaceDict['2154'] = self.targetSrid
replaceDict['SRID'] = self.targetSrid
scriptList.append(
{
'title': 'Ajout de la table parcelle_info',
'script': '%s' % os.path.join(self.pScriptDir, 'edigeo_create_table_parcelle_info_majic.sql')
}
)
else:
replaceDict['2154'] = self.targetSrid
replaceDict['SRID'] = self.targetSrid
scriptList.append(
{
'title': 'Ajout de la table parcelle_info',
Expand Down Expand Up @@ -1072,21 +1074,6 @@ def unzipFolderContent(self, path):
finally:
QApplication.restoreOverrideCursor()

def replaceParametersInString(self, string, replaceDict):
"""
Replace all occurences in string
"""

def replfunc(match):
if match.group(0) in replaceDict:
return replaceDict[match.group(0)]
else:
return None

regex = re.compile('|'.join(re.escape(x) for x in replaceDict), re.IGNORECASE)
string = regex.sub(replfunc, string)
return string

def replaceParametersInScript(self, scriptPath, replaceDict):
"""
Replace all parameters in sql scripts
Expand All @@ -1099,12 +1086,14 @@ def replaceParametersInScript(self, scriptPath, replaceDict):

try:
data = ''
with open(scriptPath, encoding='utf-8-sig') as fin:
data = fin.read() # .decode("utf-8-sig")

data = self.replaceParametersInString(data, replaceDict)
# data = data.encode('utf-8')
with open(scriptPath, 'w', encoding='utf8') as fout:
scriptPath = Path(scriptPath)
with scriptPath.open() as fin:
data = fin.read()
# Will raise a KeyError exception for
# unmatched placeholder (we *want* this, because otherwise
# we would have an invalid sql script)
data = Template(data).substitute(replaceDict)
with scriptPath.open('w') as fout:
fout.write(data)

except OSError as e:
Expand Down
Loading
Loading