forked from simone-f/qat_script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_reader.py
175 lines (153 loc) · 6.59 KB
/
config_reader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#! /usr/bin/env jython
#
# Copyright 2014 Simone F. <groppo8@gmail.com>
#
# This file is part of qat_script.
# qat_script is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
#java import
from java.util import Properties
from java.io import FileInputStream, FileOutputStream, File
from java.lang import Integer, NumberFormatException
#local import
from gui.NewZoneDialog import Zone
##### Configurations ###################################################
class ConfigLoader:
def __init__(self, app):
"""Read preferences from config.properties file
"""
self.app = app
app.SCRIPTWEBSITE = "http://wiki.openstreetmap.org/wiki/Quality_Assurance_Tools_script"
#Script and tools data versions
app.scriptVersionUrl = "http://dl.dropboxusercontent.com/u/41550819/OSM/qat_script/latest_versions.properties"
app.toolsListUrl = "http://dl.dropboxusercontent.com/u/41550819/OSM/qat_script/tools/tools_list.properties"
#Read script and tools version
self.versionsFileName = File.separator.join([app.SCRIPTDIR,
"VERSIONS.properties"])
app.SCRIPTVERSION, app.TOOLSVERSION = self.read_versions()
#Configurations in config.properties
app.configFileName = File.separator.join([app.SCRIPTDIR,
"configuration",
"config.properties"])
app.properties = Properties()
fin = FileInputStream(app.configFileName)
app.properties.load(fin)
#check for update
app.checkUpdate = app.properties.getProperty("check_for_update")
#active tools names
app.toolsStatus = {}
for key in app.properties.keys():
if key.startswith("tool.") \
and len(key.split(".")) == 2:
toolName = key.split(".")[1]
if app.properties.getProperty(key) == "on":
value = True
else:
value = False
app.toolsStatus[toolName] = value
app.toolsStatus["favourites"] = True
#layers managing
app.layersModes = ("hide_other_layers",
"remove_other_layers",
"hide_layers_with_the_same_name",
"remove_layers_with_the_same_name")
app.layersMode = app.properties.getProperty("layers_mode")
#max errors number
app.maxErrorsNumber = app.properties.getProperty("max_errors_number")
if app.maxErrorsNumber != "":
app.maxErrorsNumber = int(app.maxErrorsNumber)
# warning for tools with limited downloadable errors
if app.properties.getProperty("favourite_area.errors_warning") == "on":
app.favAreaErrorsWarning = True
else:
app.favAreaErrorsWarning = False
#tools preferences
app.toolsPrefs = {}
for key in app.properties.keys():
if key.startswith("tool") and len(key.split(".")) > 2:
toolName = key.split(".")[1]
toolPref = key.split(".")[2]
value = app.properties.getProperty(key)
if toolName not in app.toolsPrefs:
app.toolsPrefs[toolName] = {}
app.toolsPrefs[toolName][toolPref] = value
if hasattr(app, "allTools"):
#check if tools have been already instantiated
#prefs of a tool are stored as tool attribute
for tool in app.allTools:
if hasattr(tool, "prefs"):
tool.update_preferences()
#favourite zones
# status
if app.properties.getProperty("favourite_area.status") == "on":
app.favouriteZoneStatus = True
else:
app.favouriteZoneStatus = False
# name
app.favZoneName = app.properties.getProperty("favourite_area.name")
fin.close()
if app.favouriteZoneStatus:
if app.zones is None:
#it is not necessary to read again the zones when preferences change
load_zones(app)
def read_versions(self):
versionsProperties = Properties()
fin = FileInputStream(self.versionsFileName)
versionsProperties.load(fin)
scriptVersion = versionsProperties.getProperty("script")
toolsVersion = versionsProperties.getProperty("tools")
fin.close()
return scriptVersion, toolsVersion
def save_versions(self):
print "save versions"
versionsProperties = Properties()
outFile = FileOutputStream(self.versionsFileName)
versionsProperties.setProperty("script", self.app.SCRIPTVERSION)
versionsProperties.setProperty("tools", self.app.TOOLSVERSION)
versionsProperties.store(outFile, None)
outFile.close()
def load_zones(app):
"""Read favourite zones
"""
app.zones = []
favZonesDir = File(File.separator.join([app.SCRIPTDIR,
"configuration",
"favourite_zones"]))
for zoneFile in sorted(favZonesDir.listFiles()):
name = zoneFile.getName()[:-4]
fileName = zoneFile.getPath()
zoneFile = open(fileName)
fileContent = zoneFile.read()
if len(fileContent.split("|")) == 2:
geomString, country = fileContent.split("|")
country = country.upper()
else:
geomString = fileContent
country = ""
zoneFile.close()
zType = "rectangle"
try:
Integer.parseInt(geomString[0])
except NumberFormatException:
zType = "polygon"
zone = Zone(app, name, zType, geomString, country)
app.zones.append(zone)
if name == app.favZoneName:
app.favZone = zone
#Fav zone is active but its data is missing
if app.favouriteZoneStatus and app.favZone is None:
app.favouriteZoneStatus = False