Salome HOME
style: black format
[tools/sat.git] / commands / template.py
index e4ddc59677672ec65b8130cd0f57eff046c6248d..ec1b090832c1691f8003425a47a9f67d810ae1cd 100644 (file)
@@ -1,5 +1,5 @@
 #!/usr/bin/env python
-#-*- coding:utf-8 -*-
+# -*- coding:utf-8 -*-
 #  Copyright (C) 2010-2013  CEA/DEN
 #
 #  This library is free software; you can redistribute it and/or
@@ -27,53 +27,82 @@ import src
 
 # Compatibility python 2/3 for input function
 # input stays input for python 3 and input = raw_input for python 2
-try: 
+try:
     input = raw_input
-except NameError: 
+except NameError:
     pass
 
 # Python 2/3 compatibility for execfile function
 try:
     execfile
 except:
+
     def execfile(somefile, global_vars, local_vars):
         with open(somefile) as f:
-            code = compile(f.read(), somefile, 'exec')
+            code = compile(f.read(), somefile, "exec")
             exec(code, global_vars, local_vars)
 
+
 parser = src.options.Options()
-parser.add_option('n', 'name', 'string', 'name',
-    _("""REQUIRED: the name of the module to create.
+parser.add_option(
+    "n",
+    "name",
+    "string",
+    "name",
+    _(
+        """REQUIRED: the name of the module to create.
 \tThe name must be a single word in upper case with only alphanumeric characters.
 \tWhen generating a c++ component the module's """
-"""name must be suffixed with 'CPP'."""))
-parser.add_option('t', 'template', 'string', 'template',
-    _('REQUIRED: the template to use.'))
-parser.add_option('', 'target', 'string', 'target',
-    _('REQUIRED: where to create the module.'))
-parser.add_option('', 'param', 'string', 'param',
-    _('''Optional: dictionary to generate the configuration for salomeTools.
+        """name must be suffixed with 'CPP'."""
+    ),
+)
+parser.add_option(
+    "t", "template", "string", "template", _("REQUIRED: the template to use.")
+)
+parser.add_option(
+    "", "target", "string", "target", _("REQUIRED: where to create the module.")
+)
+parser.add_option(
+    "",
+    "param",
+    "string",
+    "param",
+    _(
+        """Optional: dictionary to generate the configuration for salomeTools.
 \tFormat is: --param param1=value1,param2=value2... without spaces
-\tNote that when using this option you must supply all the '''
-'''values otherwise an error will be raised.'''))
-parser.add_option('', 'info', 'boolean', 'info',
-    _('Optional: Get information on the template.'), False)
+\tNote that when using this option you must supply all the """
+        """values otherwise an error will be raised."""
+    ),
+)
+parser.add_option(
+    "",
+    "info",
+    "boolean",
+    "info",
+    _("Optional: Get information on the template."),
+    False,
+)
+
 
 class TParam:
     def __init__(self, param_def, compo_name, dico=None):
         self.default = ""
         self.prompt = ""
         self.check_method = None
-        
+
         if isinstance(param_def, str):
             self.name = param_def
         elif isinstance(param_def, tuple):
             self.name = param_def[0]
             if len(param_def) > 1:
-                if dico is not None: self.default = param_def[1] % dico
-                else: self.default = param_def[1]
-            if len(param_def) > 2: self.prompt = param_def[2]
-            if len(param_def) > 3: self.check_method = param_def[3]
+                if dico is not None:
+                    self.default = param_def[1] % dico
+                else:
+                    self.default = param_def[1]
+            if len(param_def) > 2:
+                self.prompt = param_def[2]
+            if len(param_def) > 3:
+                self.check_method = param_def[3]
         else:
             raise src.SatException(_("ERROR in template parameter definition"))
 
@@ -89,11 +118,13 @@ class TParam:
             return len(val) > 0
         return len(val) > 0 and self.check_method(val)
 
+
 def get_dico_param(dico, key, default):
     if key in dico:
         return dico[key]
     return default
 
+
 class TemplateSettings:
     def __init__(self, compo_name, settings_file, target):
         self.compo_name = compo_name
@@ -107,14 +138,15 @@ class TemplateSettings:
         # check required parameters in template.info
         missing = []
         for pp in ["file_subst", "parameters"]:
-            if not (pp in ldic): missing.append("'%s'" % pp)
+            if not (pp in ldic):
+                missing.append("'%s'" % pp)
         if len(missing) > 0:
-            raise src.SatException(_(
-                "Bad format in settings file! %s not defined.") % ", ".join(
-                                                                       missing))
-        
+            raise src.SatException(
+                _("Bad format in settings file! %s not defined.") % ", ".join(missing)
+            )
+
         self.file_subst = ldic["file_subst"]
-        self.parameters = ldic['parameters']
+        self.parameters = ldic["parameters"]
         self.info = get_dico_param(ldic, "info", "").strip()
         self.pyconf = get_dico_param(ldic, "pyconf", "")
         self.post_command = get_dico_param(ldic, "post_command", "")
@@ -123,7 +155,7 @@ class TemplateSettings:
         self.delimiter_char = get_dico_param(ldic, "delimiter", ":sat:")
 
         # get the ignore filter
-        self.ignore_filters = [l.strip() for l in ldic["ignore_filters"].split(',')]
+        self.ignore_filters = [l.strip() for l in ldic["ignore_filters"].split(",")]
 
     def has_pyconf(self):
         return len(self.pyconf) > 0
@@ -144,15 +176,15 @@ class TemplateSettings:
     def check_user_values(self, values):
         if values is None:
             return
-        
+
         # create a list of all parameters (pyconf + list))
         pnames = self.get_pyconf_parameters()
         for p in self.parameters:
             tp = TParam(p, self.compo_name)
             pnames.append(tp.name)
-        
+
         # reduce the list
-        pnames = list(set(pnames)) # remove duplicates
+        pnames = list(set(pnames))  # remove duplicates
 
         known_values = ["name", "Name", "NAME", "target", self.file_subst]
         known_values.extend(values.keys())
@@ -160,10 +192,9 @@ class TemplateSettings:
         for p in pnames:
             if p not in known_values:
                 missing.append(p)
-        
+
         if len(missing) > 0:
-            raise src.SatException(_(
-                                 "Missing parameters: %s") % ", ".join(missing))
+            raise src.SatException(_("Missing parameters: %s") % ", ".join(missing))
 
     def get_parameters(self, conf_values=None):
         if self.dico is not None:
@@ -188,7 +219,7 @@ class TemplateSettings:
             tp = TParam(p, self.compo_name, dico)
             if tp.name in dico:
                 continue
-            
+
             val = ""
             while not tp.check_value(val):
                 val = input(tp.prompt)
@@ -207,6 +238,7 @@ class TemplateSettings:
         self.dico = dico
         return self.dico
 
+
 def search_template(config, template):
     # search template
     template_src_dir = ""
@@ -225,27 +257,24 @@ def search_template(config, template):
         raise src.SatException(_("Template not found: %s") % template)
 
     return template_src_dir
+
+
 ##
 # Prepares a module from a template.
-def prepare_from_template(config,
-                          name,
-                          template,
-                          target_dir,
-                          conf_values,
-                          logger):
+def prepare_from_template(config, name, template, target_dir, conf_values, logger):
     template_src_dir = search_template(config, template)
     res = 0
 
     # copy the template
     if os.path.isfile(template_src_dir):
-        logger.write("  " + _(
-                        "Extract template %s\n") % src.printcolors.printcInfo(
-                                                                   template), 4)
+        logger.write(
+            "  " + _("Extract template %s\n") % src.printcolors.printcInfo(template), 4
+        )
         src.system.archive_extract(template_src_dir, target_dir)
     else:
-        logger.write("  " + _(
-                        "Copy template %s\n") % src.printcolors.printcInfo(
-                                                                   template), 4)
+        logger.write(
+            "  " + _("Copy template %s\n") % src.printcolors.printcInfo(template), 4
+        )
         shutil.copytree(template_src_dir, target_dir)
     logger.write("\n", 5)
 
@@ -266,35 +295,38 @@ def prepare_from_template(config,
             ff = fic.replace(tsettings.file_subst, compo_name)
             if ff != fic:
                 if os.path.exists(os.path.join(root, ff)):
-                    raise src.SatException(_(
-                        "Destination file already exists: %s") % os.path.join(
-                                                                      root, ff))
+                    raise src.SatException(
+                        _("Destination file already exists: %s")
+                        % os.path.join(root, ff)
+                    )
                 logger.write("    %s -> %s\n" % (fic, ff), 5)
                 os.rename(os.path.join(root, fic), os.path.join(root, ff))
 
     # rename the directories
     logger.write("\n", 5)
-    logger.write("  " + src.printcolors.printcLabel(_("Rename directories\n")),
-                 4)
+    logger.write("  " + src.printcolors.printcLabel(_("Rename directories\n")), 4)
     for root, dirs, files in os.walk(target_dir, topdown=False):
         for rep in dirs:
             dd = rep.replace(tsettings.file_subst, compo_name)
             if dd != rep:
                 if os.path.exists(os.path.join(root, dd)):
-                    raise src.SatException(_(
-                                "Destination directory "
-                                "already exists: %s") % os.path.join(root, dd))
+                    raise src.SatException(
+                        _("Destination directory " "already exists: %s")
+                        % os.path.join(root, dd)
+                    )
                 logger.write("    %s -> %s\n" % (rep, dd), 5)
                 os.rename(os.path.join(root, rep), os.path.join(root, dd))
 
     # ask for missing parameters
     logger.write("\n", 5)
-    logger.write("  " + src.printcolors.printcLabel(
-                                        _("Make substitution in files\n")), 4)
-    logger.write("    " + _("Delimiter =") + " %s\n" % tsettings.delimiter_char,
-                 5)
-    logger.write("    " + _("Ignore Filters =") + " %s\n" % ', '.join(
-                                                   tsettings.ignore_filters), 5)
+    logger.write(
+        "  " + src.printcolors.printcLabel(_("Make substitution in files\n")), 4
+    )
+    logger.write("    " + _("Delimiter =") + " %s\n" % tsettings.delimiter_char, 5)
+    logger.write(
+        "    " + _("Ignore Filters =") + " %s\n" % ", ".join(tsettings.ignore_filters),
+        5,
+    )
     dico = tsettings.get_parameters(conf_values)
     logger.write("\n", 3)
 
@@ -312,44 +344,54 @@ def prepare_from_template(config,
                 logger.write("  - %s\n" % fpath[pathlen:], 5)
                 continue
             # read the file
-            with open(fpath, 'r') as f:
+            with open(fpath, "r") as f:
                 m = f.read()
                 # make the substitution
                 template = CompoTemplate(m)
                 d = template.safe_substitute(dico)
-                        
+
             changed = " "
             if d != m:
                 changed = "*"
-                with open(fpath, 'w') as f:
+                with open(fpath, "w") as f:
                     f.write(d)
             logger.write("  %s %s\n" % (changed, fpath[pathlen:]), 5)
 
     if not tsettings.has_pyconf:
-        logger.write(src.printcolors.printcWarning(_(
-                   "Definition for sat not found in settings file.")) + "\n", 2)
+        logger.write(
+            src.printcolors.printcWarning(
+                _("Definition for sat not found in settings file.")
+            )
+            + "\n",
+            2,
+        )
     else:
         definition = tsettings.pyconf % dico
-        pyconf_file = os.path.join(target_dir, name + '.pyconf')
-        f = open(pyconf_file, 'w')
+        pyconf_file = os.path.join(target_dir, name + ".pyconf")
+        f = open(pyconf_file, "w")
         f.write(definition)
         f.close
-        logger.write(_(
-            "Create configuration file: ") + src.printcolors.printcInfo(
-                                                         pyconf_file) + "\n", 2)
+        logger.write(
+            _("Create configuration file: ")
+            + src.printcolors.printcInfo(pyconf_file)
+            + "\n",
+            2,
+        )
 
     if len(tsettings.post_command) > 0:
         cmd = tsettings.post_command % dico
         logger.write("\n", 5, True)
-        logger.write(_(
-              "Run post command: ") + src.printcolors.printcInfo(cmd) + "\n", 3)
-        
+        logger.write(
+            _("Run post command: ") + src.printcolors.printcInfo(cmd) + "\n", 3
+        )
+
         p = subprocess.Popen(cmd, shell=True, cwd=target_dir)
         p.wait()
         res = p.returncode
 
     return res
 
+
 def get_template_info(config, template_name, logger):
     sources = search_template(config, template_name)
     src.printcolors.print_value(logger, _("Template"), sources)
@@ -368,26 +410,23 @@ def get_template_info(config, template_name, logger):
     if not os.path.exists(settings_file):
         raise src.SatException(_("Settings file not found"))
     tsettings = TemplateSettings("NAME", settings_file, "target")
-    
+
     logger.write("\n", 3)
     if len(tsettings.info) == 0:
-        logger.write(src.printcolors.printcWarning(_(
-                                       "No information for this template.")), 3)
+        logger.write(
+            src.printcolors.printcWarning(_("No information for this template.")), 3
+        )
     else:
         logger.write(tsettings.info, 3)
 
     logger.write("\n", 3)
     logger.write("= Configuration", 3)
-    src.printcolors.print_value(logger,
-                                "file substitution key",
-                                tsettings.file_subst)
-    src.printcolors.print_value(logger,
-                                "subsitution key",
-                                tsettings.delimiter_char)
+    src.printcolors.print_value(logger, "file substitution key", tsettings.file_subst)
+    src.printcolors.print_value(logger, "subsitution key", tsettings.delimiter_char)
     if len(tsettings.ignore_filters) > 0:
-        src.printcolors.print_value(logger,
-                                    "Ignore Filter",
-                                    ', '.join(tsettings.ignore_filters))
+        src.printcolors.print_value(
+            logger, "Ignore Filter", ", ".join(tsettings.ignore_filters)
+        )
 
     logger.write("\n", 3)
     logger.write("= Parameters", 3)
@@ -404,10 +443,12 @@ def get_template_info(config, template_name, logger):
     logger.write("= Verification\n", 3)
     if tsettings.file_subst not in pnames:
         logger.write(
-                     "file substitution key not defined as a "
-                     "parameter: %s" % tsettings.file_subst, 3)
+            "file substitution key not defined as a "
+            "parameter: %s" % tsettings.file_subst,
+            3,
+        )
         retcode = 1
-    
+
     reexp = tsettings.delimiter_char.replace("$", "\$") + "{(?P<name>\S[^}]*)"
     pathlen = len(tmpdir) + 1
     for root, __, files in os.walk(tmpdir):
@@ -416,15 +457,17 @@ def get_template_info(config, template_name, logger):
             if not tsettings.check_file_for_substitution(fpath[pathlen:]):
                 continue
             # read the file
-            with open(fpath, 'r') as f:
+            with open(fpath, "r") as f:
                 m = f.read()
                 zz = re.findall(reexp, m)
-                zz = list(set(zz)) # reduce
+                zz = list(set(zz))  # reduce
                 zz = filter(lambda l: l not in pnames, zz)
                 if len(zz) > 0:
-                    logger.write("Missing definition in %s: %s" % (
-                        src.printcolors.printcLabel(
-                                                fpath[pathlen:]), ", ".join(zz)), 3)
+                    logger.write(
+                        "Missing definition in %s: %s"
+                        % (src.printcolors.printcLabel(fpath[pathlen:]), ", ".join(zz)),
+                        3,
+                    )
                     retcode = 1
 
     if retcode == 0:
@@ -439,16 +482,19 @@ def get_template_info(config, template_name, logger):
 
     return retcode
 
+
 ##
 # Describes the command
 def description():
-    return _("The template command creates the sources for a SALOME "
-             "module from a template.\n\nexample\nsat template "
-             "--name my_product_name --template PythonComponent --target /tmp")
+    return _(
+        "The template command creates the sources for a SALOME "
+        "module from a template.\n\nexample\nsat template "
+        "--name my_product_name --template PythonComponent --target /tmp"
+    )
+
 
 def run(args, runner, logger):
-    '''method that is called when salomeTools is called with template parameter.
-    '''
+    """method that is called when salomeTools is called with template parameter."""
     (options, args) = parser.parse_args(args)
 
     if options.template is None:
@@ -478,9 +524,11 @@ def run(args, runner, logger):
         logger.write("\n", 1)
         return 1
 
-    if not options.name.replace('_', '').isalnum():
-        msg = _("Error: component name must contains only alphanumeric "
-                "characters and no spaces\n")
+    if not options.name.replace("_", "").isalnum():
+        msg = _(
+            "Error: component name must contains only alphanumeric "
+            "characters and no spaces\n"
+        )
         logger.write(src.printcolors.printcError(msg), 1)
         logger.write("\n", 1)
         return 1
@@ -498,35 +546,41 @@ def run(args, runner, logger):
         logger.write("\n", 1)
         return 1
 
-
-    logger.write(_('Create sources from template\n'), 1)
-    src.printcolors.print_value(logger, 'destination', target_dir, 2)
-    src.printcolors.print_value(logger, 'name', options.name, 2)
-    src.printcolors.print_value(logger, 'template', options.template, 2)
+    logger.write(_("Create sources from template\n"), 1)
+    src.printcolors.print_value(logger, "destination", target_dir, 2)
+    src.printcolors.print_value(logger, "name", options.name, 2)
+    src.printcolors.print_value(logger, "template", options.template, 2)
     logger.write("\n", 3, False)
-    
+
     conf_values = None
     if options.param is not None:
         conf_values = {}
         for elt in options.param.split(","):
-            param_def = elt.strip().split('=')
+            param_def = elt.strip().split("=")
             if len(param_def) != 2:
                 msg = _("Error: bad parameter definition")
                 logger.write(src.printcolors.printcError(msg), 1)
                 logger.write("\n", 1)
                 return 1
             conf_values[param_def[0].strip()] = param_def[1].strip()
-    
-    retcode = prepare_from_template(runner.cfg, options.name, options.template,
-        target_dir, conf_values, logger)
+
+    retcode = prepare_from_template(
+        runner.cfg, options.name, options.template, target_dir, conf_values, logger
+    )
 
     if retcode == 0:
-        logger.write(_(
-                 "The sources were created in %s") % src.printcolors.printcInfo(
-                                                                 target_dir), 3)
-        logger.write(src.printcolors.printcWarning(_("\nDo not forget to put "
-                                   "them in your version control system.")), 3)
-        
+        logger.write(
+            _("The sources were created in %s")
+            % src.printcolors.printcInfo(target_dir),
+            3,
+        )
+        logger.write(
+            src.printcolors.printcWarning(
+                _("\nDo not forget to put " "them in your version control system.")
+            ),
+            3,
+        )
+
     logger.write("\n", 3)
-    
+
     return retcode