From c28f39821cc6bb3d2531a0f92bc86d582ae4e09a Mon Sep 17 00:00:00 2001
From: Andre Sailer <andre.philippe.sailer@cern.ch>
Date: Wed, 8 Jun 2022 14:24:15 +0200
Subject: [PATCH] DDSim: ExtendAction

From https://stackoverflow.com/a/41153081

Needed for python2.7 compatibility
---
 DDG4/python/DDSim/DD4hepSimulation.py    |  6 ++++--
 DDG4/python/DDSim/Helper/ConfigHelper.py | 11 +++++++++++
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/DDG4/python/DDSim/DD4hepSimulation.py b/DDG4/python/DDSim/DD4hepSimulation.py
index 03c1de2d4..12b102536 100644
--- a/DDG4/python/DDSim/DD4hepSimulation.py
+++ b/DDG4/python/DDSim/DD4hepSimulation.py
@@ -19,7 +19,7 @@ from DDSim.Helper.Filter import Filter
 from DDSim.Helper.Random import Random
 from DDSim.Helper.Action import Action
 from DDSim.Helper.OutputConfig import OutputConfig
-from DDSim.Helper.ConfigHelper import ConfigHelper
+from DDSim.Helper.ConfigHelper import ConfigHelper, ExtendAction
 from DDSim.Helper.MagneticField import MagneticField
 from DDSim.Helper.ParticleHandler import ParticleHandler
 from DDSim.Helper.Gun import Gun
@@ -131,6 +131,8 @@ class DD4hepSimulation(object):
 
     parser = argparse.ArgumentParser("Running DD4hep Simulations:",
                                      formatter_class=argparse.RawTextHelpFormatter)
+    # add myextend for python2.7
+    parser.register('action', 'myextend', ExtendAction)
 
     parser.add_argument("--steeringFile", "-S", action="store", default=self.steeringFile,
                         help="Steering file to change default behaviour")
@@ -145,7 +147,7 @@ class DD4hepSimulation(object):
     if self._argv is None:
       self._argv = list(argv) if argv else list(sys.argv)
 
-    parser.add_argument("--compactFile", nargs='+', action="extend", default=self.compactFile, type=str,
+    parser.add_argument("--compactFile", nargs='+', action="myextend", default=self.compactFile, type=str,
                         help="The compact XML file, or multiple compact files, if the last one is the closer.")
 
     parser.add_argument("--runType", action="store", choices=("batch", "vis", "run", "shell"), default=self.runType,
diff --git a/DDG4/python/DDSim/Helper/ConfigHelper.py b/DDG4/python/DDSim/Helper/ConfigHelper.py
index d52c3f033..56587271d 100644
--- a/DDG4/python/DDSim/Helper/ConfigHelper.py
+++ b/DDG4/python/DDSim/Helper/ConfigHelper.py
@@ -13,6 +13,9 @@ call for the parser object create an additional member::
 """
 
 from __future__ import absolute_import, unicode_literals
+
+import argparse
+
 import ddsix as six
 
 
@@ -129,3 +132,11 @@ class ConfigHelper(object):
                               dest="%s.%s" % (name, var),
                               **optionsDict
                               )
+
+
+class ExtendAction(argparse.Action):
+  """Class to add the extend action for argparse to python2.7"""
+  def __call__(self, parser, namespace, values, option_string=None):
+    items = getattr(namespace, self.dest) or []
+    items.extend(values)
+    setattr(namespace, self.dest, items)
-- 
GitLab