# list of available codes
__code_range__ = [1, 4] + list(range(30, 38)) + list(range(40, 48)) + list(range(90, 98)) + list(range(100, 108))
-# print a text with colors
def printc(txt, code=''):
- # no code means 'auto mode'
+ '''print a text with colors
+ :param txt str: The text to be printed.
+ :param code str: The color to use.
+ :return: The colored text.
+ :rtype: str
+ '''
+ # no code means 'auto mode' (works only for OK, KO, NO and ERR*)
if code == '':
striptxt = txt.strip().upper()
if striptxt == "OK":
return __colormap__[code] + txt + '\033[0m'
def printcInfo(txt):
+ '''print a text info color
+ :param txt str: The text to be printed.
+ :return: The colored text.
+ :rtype: str
+ '''
return printc(txt, COLOR_INFO)
def printcError(txt):
+ '''print a text error color
+ :param txt str: The text to be printed.
+ :return: The colored text.
+ :rtype: str
+ '''
return printc(txt, COLOR_ERROR)
def printcWarning(txt):
+ '''print a text warning color
+ :param txt str: The text to be printed.
+ :return: The colored text.
+ :rtype: str
+ '''
return printc(txt, COLOR_WARNING)
def printcHeader(txt):
+ '''print a text header color
+ :param txt str: The text to be printed.
+ :return: The colored text.
+ :rtype: str
+ '''
return printc(txt, COLOR_HEADER)
def printcLabel(txt):
+ '''print a text label color
+ :param txt str: The text to be printed.
+ :return: The colored text.
+ :rtype: str
+ '''
return printc(txt, COLOR_LABEL)
def printcSuccess(txt):
+ '''print a text success color
+ :param txt str: The text to be printed.
+ :return: The colored text.
+ :rtype: str
+ '''
return printc(txt, COLOR_SUCCESS)
def printcHighlight(txt):
+ '''print a text highlight color
+ :param txt str: The text to be printed.
+ :return: The colored text.
+ :rtype: str
+ '''
return printc(txt, COLOR_HIGLIGHT)
def cleancolor(message):
+ '''remove color from a colored text.
+ :param message str: The text to be cleaned.
+ :return: The cleaned text.
+ :rtype: str
+ '''
message = message.replace('\033[0m', '')
for i in __code_range__:
message = message.replace('\033[%dm' % i, '')
return message
-# shortcut method to print a label and a value with the info color
def print_value(logger, label, value, level=1, suffix=""):
+ '''shortcut method to print a label and a value with the info color
+ :param logger class logger: the logger instance.
+ :param label int: the label to print.
+ :param value str: the value to print.
+ :param level int: the level of verboseness.
+ :param suffix str: the suffix to add at the end.
+ '''
if logger is None:
print(" %s = %s %s" % (label, printcInfo(str(value)), suffix))
else:
logger.write(" %s = %s %s\n" % (label, printcInfo(str(value)), suffix), level)
def print_color_range(start, end):
+ '''print possible range values for colors
+ :param start int: The smaller value.
+ :param end int: The bigger value.
+ '''
for k in range(start, end+1):
print("\033[%dm%3d\033[0m" % (k, k),)
print
# This method prints the color map
def print_color_map():
+ '''This method prints the color map
+ '''
print("colormap:")
print("{")
for k in sorted(__colormap__.keys()):
\tWARNING the included files are not copied.
\tIf a name is given the new config file takes the given name."""))
-'''
-class MergeHandler:
- def __init__(self):
- pass
-
- def __call__(self, map1, map2, key):
- if '__overwrite__' in map2 and key in map2.__overwrite__:
- return "overwrite"
- else:
- return common.config_pyconf.overwriteMergeResolve(map1, map2, key)
-'''
-
class ConfigOpener:
+ ''' Class that helps to find an application pyconf in all the possible directories (pathList)
+ '''
def __init__(self, pathList):
+ '''Initialization
+ :param pathList list: The list of paths where to serach a pyconf.
+ '''
self.pathList = pathList
def __call__(self, name):
raise IOError(_("Configuration file '%s' not found") % name)
def getPath( self, name ):
+ '''The method that returns the entire path of the pyconf searched
+ :param name str: The name of the searched pyconf.
+ '''
for path in self.pathList:
if os.path.exists(os.path.join(path, name)):
return path
def getConfig(self, application=None, options=None, command=None, dataDir=None):
'''get the config from all the configuration files.
:param application str: The application for which salomeTools is called.
- :param options TODO
+ :param options calss Options: The general salomeToosl options (--overwrite or -l5, for example)
:param command str: The command that is called.
:param dataDir str: The repository that contain external data for salomeTools.
:return: The final config.
cfg.VARS = common.config_pyconf.Mapping(cfg)
for variable in var:
cfg.VARS[variable] = var[variable]
-
+
+ # apply overwrite from command line if needed
for rule in self.get_command_line_overrides(options, ["VARS"]):
exec('cfg.' + rule) # this cannot be factorized because of the exec
except common.config_pyconf.ConfigError as e:
raise common.SatException(_("Error in configuration file: salomeTools.pyconf\n %(error)s") % \
{'error': str(e) })
-
+
merger.merge(cfg, internal_cfg)
+ # apply overwrite from command line if needed
for rule in self.get_command_line_overrides(options, ["INTERNAL"]):
exec('cfg.' + rule) # this cannot be factorized because of the exec
merger.merge(cfg, site_cfg)
+ # apply overwrite from command line if needed
for rule in self.get_command_line_overrides(options, ["SITE"]):
exec('cfg.' + rule) # this cannot be factorized because of the exec
merger.merge(cfg, application_cfg)
+ # apply overwrite from command line if needed
for rule in self.get_command_line_overrides(options, ["APPLICATION"]):
exec('cfg.' + rule) # this cannot be factorized because of the exec
# The directory containing the softwares definition
softsDir = os.path.join(cfg.VARS.dataDir, 'software_pyconf')
- # Loop on all files that are in softsDir directory and read its config
+ # Loop on all files that are in softsDir directory and read their config
for fName in os.listdir(softsDir):
if fName.endswith(".pyconf"):
common.config_pyconf.streamOpener = ConfigOpener([softsDir])
merger.merge(cfg, soft_cfg)
+ # apply overwrite from command line if needed
for rule in self.get_command_line_overrides(options, ["SOFTWARE"]):
exec('cfg.' + rule) # this cannot be factorized because of the exec
user_cfg = common.config_pyconf.Config(open(user_cfg_file))
merger.merge(cfg, user_cfg)
+ # apply overwrite from command line if needed
for rule in self.get_command_line_overrides(options, ["USER"]):
exec('cfg.' + rule) # this cannot be factorize because of the exec
def setUserConfigFile(self, config):
'''Set the user config file name and path.
If necessary, build it from another one or create it from scratch.
+ :param config class 'common.config_pyconf.Config': The global config (containing all pyconf).
'''
if not config:
raise common.SatException(_("Error in setUserConfigFile: config is None"))
sat_version = config.INTERNAL.sat_version
+ # get the expected name and path of the file
self.config_file_name = 'salomeTools-%s.pyconf'%sat_version
self.user_config_file_path = os.path.join(config.VARS.personalDir, self.config_file_name)
if not os.path.isfile(self.user_config_file_path):
'''Get a pyconf file younger than the given sat version in the given directory
The file basename can be one of salometools-<younger version>.pyconf or salomeTools.pyconf
Returns the file path or None if no file has been found.
+ :param userDir str: the directory that contain the user pyconf to find. (~/.salomeTools)
+ :param sat_version str: the version of salomeTools.
+ :return: the path to the already existing user pyconf.
+ :rtype: str
'''
file_path = None
# Get a younger pyconf version
return file_path
def createConfigFile(self, config):
+ '''This method is called when there are no user config file. It build it from scratch.
+ :param config class 'common.config_pyconf.Config': The global config.
+ :return: the config corresponding to the file created.
+ :rtype: config class 'common.config_pyconf.Config'
+ '''
cfg_name = self.getUserConfigFile()
def getUserConfigFile(self):
'''Get the user config file
+ :return: path to the user config file.
+ :rtype: str
'''
if not self.user_config_file_path:
raise common.SatException(_("Error in getUserConfigFile: missing user config file path"))
:param show_label boolean: if True, do a basic display. (useful for bash completion)
:param level int: The number of spaces to add before display.
:param show_full_path :
- :return: The final config.
- :rtype: class 'common.config_pyconf.Config'
'''
# display all the path or not
sys.stdout.write("%s\n" % val)
def description():
+ '''method that is called when salomeTools is called with --help option.
+ :return: The text to display for the config command description.
+ :rtype: str
+ '''
return _("The config command allows manipulation and operation on config files.")
def run(args, runner):
+ '''method that is called when salomeTools is called with config parameter.
+ '''
+ # Parse the options
(options, args) = parser.parse_args(args)
# case : print a value of the config