215 lines
6.9 KiB
Python
215 lines
6.9 KiB
Python
"""
|
|
Settings is a collection of utilities to display, read & write the settings and position widgets.
|
|
|
|
"""
|
|
|
|
from __future__ import absolute_import
|
|
#Init has to be imported first because it has code to workaround the python bug where relative imports don't work if the module is imported as a main module.
|
|
import __init__
|
|
|
|
import ConfigParser
|
|
import os
|
|
|
|
def loadGlobalConfig(filename):
|
|
"Read a configuration file as global config"
|
|
global globalConfigParser
|
|
globalConfigParser = ConfigParser.ConfigParser()
|
|
globalConfigParser.read(filename)
|
|
|
|
def getDefaultConfigPath():
|
|
return os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "../current_config.ini"))
|
|
|
|
def safeConfigName(name):
|
|
return name.replace("=", "").replace(":", "").replace(" ", "_").replace("(", "").replace(")", "")
|
|
|
|
def getReadRepository(repository):
|
|
"Read the configuration for this 'repository'"
|
|
|
|
#Check if we have a configuration file loaded, else load the default.
|
|
if not globals().has_key('globalConfigParser'):
|
|
loadGlobalConfig(getDefaultConfigPath())
|
|
|
|
#print('getReadRepository:', repository.name)
|
|
for p in repository.preferences:
|
|
try:
|
|
p.setValueToString(globalConfigParser.get(repository.name, safeConfigName(p.name)))
|
|
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
|
|
#Value not in configuration, add it.
|
|
try:
|
|
globalConfigParser.add_section(repository.name)
|
|
except:
|
|
pass
|
|
globalConfigParser.set(repository.name, safeConfigName(p.name), str(p.value))
|
|
globalConfigParser.write(open(getDefaultConfigPath(), 'w'))
|
|
#print "============" + str(p) + "|" + p.name + "|" + str(p.value) + "|" + str(type(p.value))
|
|
return repository
|
|
|
|
def printProgress(layerIndex, procedureName):
|
|
print("Progress: ", procedureName, layerIndex)
|
|
def printProgressByNumber(layerIndex, numberOfLayers, procedureName):
|
|
print("Progress: ", procedureName, layerIndex, numberOfLayers)
|
|
|
|
def getAlterationFileLines(fileName):
|
|
'Get the alteration file line and the text lines from the fileName in the alterations directories.'
|
|
print ('getAlterationFileLines:', fileName)
|
|
return []
|
|
def getAlterationLines(fileName):
|
|
print ('getAlterationLines:', fileName)
|
|
return []
|
|
|
|
####################################
|
|
## Configuration settings classes ##
|
|
####################################
|
|
|
|
class GeneralSetting:
|
|
"Just a basic setting subclass"
|
|
def getFromValue( self, name, repository, value ):
|
|
#print('GeneralSetting:', name, repository, value )
|
|
self.name = name
|
|
self.value = value
|
|
repository.preferences.append(self)
|
|
return self
|
|
|
|
class StringSetting(GeneralSetting):
|
|
"A class to display, read & write a string."
|
|
def setValueToString(self, value):
|
|
self.value = value
|
|
|
|
class BooleanSetting( GeneralSetting ):
|
|
"A class to display, read & write a boolean."
|
|
def setValueToString(self, value):
|
|
self.value = value == "True"
|
|
|
|
class LatentStringVar:
|
|
"This is actually used as 'group' object for Radio buttons. (Did I mention the code is a mess?)"
|
|
"This class doesn't have a name, and isn't really used for anything. It doesn't even know which repository it belongs to"
|
|
|
|
class Radio( BooleanSetting ):
|
|
"A class to display, read & write a boolean with associated radio button."
|
|
def getFromRadio( self, latentStringVar, name, repository, value ):
|
|
"Initialize."
|
|
#print('Radio->getFromRadio:', latentStringVar, name, repository, value )
|
|
self.name = name
|
|
self.value = value
|
|
repository.preferences.append(self)
|
|
return self
|
|
|
|
class RadioCapitalized( Radio ):
|
|
"A class to display, read & write a boolean with associated radio button."
|
|
|
|
class RadioCapitalizedButton( Radio ):
|
|
"A class to display, read & write a boolean with associated radio button. With an added configuration dialog button"
|
|
"Only used for the extra export options, which we are not using, so ignore the path for now"
|
|
def getFromPath( self, latentStringVar, name, path, repository, value ):
|
|
"Initialize."
|
|
#print('RadioCapitalizedButton->getFromPath:', latentStringVar, name, path, repository, value )
|
|
self.name = name
|
|
self.value = value
|
|
repository.preferences.append(self)
|
|
return self
|
|
|
|
class FileNameInput(StringSetting ):
|
|
"A class to display, read & write a fileName."
|
|
def getFromFileName( self, fileTypes, name, repository, value ):
|
|
#print('FileNameInput:getFromFileName:', self, fileTypes, name, repository, value )
|
|
self.name = name
|
|
self.value = value
|
|
return self
|
|
|
|
class HelpPage:
|
|
"A class to open a help page."
|
|
def getOpenFromAbsolute( self, hypertextAddress ):
|
|
return self
|
|
|
|
class MenuButtonDisplay:
|
|
"A class to add a combo box selection."
|
|
def getFromName( self, name, repository ):
|
|
#print('MenuButtonDisplay->getFromName:', name, repository )
|
|
self.name = name
|
|
self.value = "ERROR"
|
|
self.radioList = []
|
|
repository.preferences.append(self)
|
|
return self
|
|
|
|
def addRadio(self, radio, default):
|
|
if default:
|
|
self.value = radio.name
|
|
self.radioList.append(radio)
|
|
|
|
def setValueToString(self, value):
|
|
valueFound = False
|
|
for radio in self.radioList:
|
|
if radio.name == value:
|
|
valueFound = True;
|
|
if valueFound:
|
|
self.value = value
|
|
for radio in self.radioList:
|
|
radio.value = (radio.name == value)
|
|
|
|
class MenuRadio( BooleanSetting ):
|
|
"A class to display, read & write a boolean with associated combo box selection."
|
|
def getFromMenuButtonDisplay( self, menuButtonDisplay, name, repository, value ):
|
|
"Initialize."
|
|
#print('MenuRadio->getFromMenuButtonDisplay:', menuButtonDisplay, name, repository, value )
|
|
self.name = name
|
|
self.value = value
|
|
menuButtonDisplay.addRadio(self, value)
|
|
return self
|
|
|
|
class LabelDisplay:
|
|
"A class to add a label."
|
|
def getFromName( self, name, repository ):
|
|
"Initialize."
|
|
return self
|
|
|
|
class FloatSetting(GeneralSetting):
|
|
"A class to display, read & write a float."
|
|
def setValueToString(self, value):
|
|
self.value = float(value)
|
|
|
|
class FloatSpin( FloatSetting ):
|
|
"A class to display, read & write an float in a spin box."
|
|
def getFromValue(self, from_, name, repository, to, value):
|
|
"Initialize."
|
|
self.name = name
|
|
self.value = value
|
|
repository.preferences.append(self)
|
|
return self
|
|
|
|
class LabelSeparator:
|
|
"A class to add a label and menu separator."
|
|
def getFromRepository( self, repository ):
|
|
"Initialize."
|
|
return self
|
|
|
|
class IntSpin(FloatSpin):
|
|
"A class to display, read & write an int in a spin box."
|
|
def getSingleIncrementFromValue( self, from_, name, repository, to, value ):
|
|
"Initialize."
|
|
self.name = name
|
|
self.value = value
|
|
repository.preferences.append(self)
|
|
return self
|
|
|
|
def setValueToString(self, value):
|
|
self.value = int(value)
|
|
|
|
##########################
|
|
# Helper classes
|
|
##########################
|
|
|
|
class LayerCount:
|
|
'A class to handle the layerIndex.'
|
|
def __init__(self):
|
|
'Initialize.'
|
|
self.layerIndex = -1
|
|
|
|
def __repr__(self):
|
|
'Get the string representation of this LayerCount.'
|
|
return str(self.layerIndex)
|
|
|
|
def printProgressIncrement(self, procedureName):
|
|
'Print progress then increment layerIndex.'
|
|
self.layerIndex += 1
|
|
printProgress(self.layerIndex, procedureName)
|
|
|