create file
This commit is contained in:
parent
7b1d14325d
commit
5715ab0179
52
edit_qgis.py
Normal file
52
edit_qgis.py
Normal file
@ -0,0 +1,52 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: UTF-8 -*-
|
||||
|
||||
from fnmatch import filter
|
||||
from io import BytesIO
|
||||
from os import (walk, path, rename)
|
||||
from zipfile import ZipFile, ZIP_DEFLATED
|
||||
|
||||
|
||||
def main(in_dir, dic_values, backup_suffix=None,encoding='UTF-8'):
|
||||
for (dirpath, dirnames, filenames) in walk(in_dir):
|
||||
qgzs = filter(filenames, '*.qgz')
|
||||
for qgz in qgzs:
|
||||
qgz_path = path.join(dirpath, qgz)
|
||||
mem_qgz = BytesIO()
|
||||
with ZipFile(qgz_path, 'r') as in_qgz, ZipFile(mem_qgz, 'w', compression=ZIP_DEFLATED) as tmp_qgz:
|
||||
for f in in_qgz.infolist():
|
||||
data = in_qgz.read(f.filename).decode(encoding,errors='ignore')
|
||||
if f.filename.endswith('.qgs'):
|
||||
for old, new in dic_values.items():
|
||||
data = data.replace(old, new)
|
||||
# data = [data.replace('{}'.format(old), '{}'.format(new)) for old, new in dic_values.items()][0]
|
||||
tmp_qgz.writestr(f.filename, data)
|
||||
|
||||
if backup_suffix is not None:
|
||||
backup_suffix = backup_suffix if backup_suffix.startswith('.') else '.' + backup_suffix
|
||||
try:
|
||||
rename(qgz_path, qgz_path + backup_suffix)
|
||||
except FileExistsError as err:
|
||||
print('Unable to backup file, skipping {} ({})'.format(qgz_path, err))
|
||||
continue
|
||||
|
||||
with open(qgz_path, 'wb') as out_qgz:
|
||||
out_qgz.write(mem_qgz.getvalue())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
backup_suffix = '.orig' # Don't overwrite orig .qgz (just in case...), append ".orig"
|
||||
in_dir = r'/home/colas/Documents/tmp/TRAVAUX'
|
||||
|
||||
encodage = 'UTF-8'
|
||||
dic_values = {
|
||||
'192.168.0.189': '91.134.194.221',
|
||||
'authcfg=1739lo5': 'authcfg=ceniser',
|
||||
'bd_cen38': 'bd-cen-38',
|
||||
# 'MS Shell Dlg 2':'Tahoma',
|
||||
# 'https://datacarto.datara.gouv.fr':' https://datacarto.open-datara.fr',
|
||||
# 'Dingbats': '',
|
||||
}
|
||||
|
||||
main(in_dir, dic_values, backup_suffix,encoding=encodage)
|
||||
Loading…
x
Reference in New Issue
Block a user