Change in osmo-gsm-tester[master]: config.py: Allow escaping commas in parametrized scenario names

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

pespin gerrit-no-reply at lists.osmocom.org
Wed Apr 1 18:05:37 UTC 2020


pespin has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-gsm-tester/+/17703 )


Change subject: config.py: Allow escaping commas in parametrized scenario names
......................................................................

config.py: Allow escaping commas in parametrized scenario names

comma character ',' is used in the command line to split between
parameters, which means a parameter value itself couldn't contain it.
This commit allows passing a comma inside a character value by escaping
it with '\,'.

Change-Id: Ic0bd9a029137a59e8c4a32b807eba7a64fcfa51f
---
M src/osmo_gsm_tester/config.py
1 file changed, 45 insertions(+), 2 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-gsm-tester refs/changes/03/17703/1

diff --git a/src/osmo_gsm_tester/config.py b/src/osmo_gsm_tester/config.py
index 5016018..9333601 100644
--- a/src/osmo_gsm_tester/config.py
+++ b/src/osmo_gsm_tester/config.py
@@ -201,6 +201,49 @@
         self.path = path
         self.param_list = param_list
 
+    @classmethod
+    def count_cont_char_backward(cls, str, before_pos, c):
+        n = 0
+        i = before_pos - 1
+        while i >= 0:
+            if str[i] != c:
+                break
+            n += 1
+            i -= 1
+        return n
+
+    @classmethod
+    def split_scenario_parameters(cls, str):
+        cur_pos = 0
+        param_li = []
+        saved = ''
+        # Split into a list, but we want to escape '\,' to avoid splitting parameters containing commas.
+        while True:
+            prev_pos = cur_pos
+            cur_pos = str.find(',', prev_pos)
+            if cur_pos == -1:
+                param_li.append(str[prev_pos:])
+                break
+            if cur_pos == 0:
+                param_li.append('')
+            elif cur_pos != 0 and str[cur_pos - 1] == '\\' and cls.count_cont_char_backward(str, cur_pos, '\\') % 2 == 1:
+                saved += str[prev_pos:cur_pos - 1] + ','
+            else:
+                param_li.append(saved + str[prev_pos:cur_pos])
+                saved = ''
+            cur_pos += 1
+        i = 0
+        # Also escape '\\' -> '\'
+        while i < len(param_li):
+            param_li[i] = param_li[i].replace('\\\\', '\\')
+            i += 1
+        return param_li
+
+    @classmethod
+    def from_param_list_str(cls, name, path, param_list_str):
+        param_list = cls.split_scenario_parameters(param_list_str)
+        return cls(name, path, param_list)
+
     def read_from_file(self, validation_schema):
         with open(self.path, 'r') as f:
             config_str = f.read()
@@ -228,6 +271,7 @@
     if not is_parametrized_file:
         if not os.path.isfile(path):
             raise RuntimeError('No such scenario file: %r' % path)
+        sc = Scenario(name, path)
     else: # parametrized scenario:
         # Allow first matching complete matching names (eg: scenario at param1,param2.conf),
         # this allows setting specific content in different files for specific values.
@@ -240,8 +284,7 @@
         # At this point, we have existing file path. Let's now scrap the parameter(s):
         # get param1,param2 str from scenario at param1,param2.conf
         param_list_str = name.split('@', 1)[1][:-len('.conf')]
-        param_list = param_list_str.split(',')
-    sc = Scenario(name, path, param_list)
+        sc = Scenario.from_param_list_str(name, path, param_list_str)
     sc.read_from_file(validation_schema)
     return sc
 

-- 
To view, visit https://gerrit.osmocom.org/c/osmo-gsm-tester/+/17703
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-gsm-tester
Gerrit-Branch: master
Gerrit-Change-Id: Ic0bd9a029137a59e8c4a32b807eba7a64fcfa51f
Gerrit-Change-Number: 17703
Gerrit-PatchSet: 1
Gerrit-Owner: pespin <pespin at sysmocom.de>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20200401/5f6bf370/attachment.htm>


More information about the gerrit-log mailing list