Skip to content

Commit 5fd1e5c

Browse files
committed
Addressed @mgermain's comments
1 parent d04ea50 commit 5fd1e5c

File tree

5 files changed

+45
-28
lines changed

5 files changed

+45
-28
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
import re
22

33

4-
class FoldedArgument(object):
4+
class FoldedArgumentTemplate(object):
55
def __init__(self):
66
self.name = ""
77
self.regex = ""
88

99
def unfold(self, match):
10-
raise NotImplementedError("Subclass must implement this method!")
10+
raise NotImplementedError("Subclass must implement method `unfold(self, match)`!")
1111

1212

13-
class EnumerationFoldedArgument(FoldedArgument):
13+
class ListFoldedArgumentTemplate(FoldedArgumentTemplate):
1414
def __init__(self):
15-
self.name = "enumeration"
15+
self.name = "list"
1616
self.regex = "\[[^]]*\]"
1717

1818
def unfold(self, match):
1919
return match[1:-1].split(' ')
2020

2121

22-
class RangeFoldedArgument(FoldedArgument):
22+
class RangeFoldedArgumentTemplate(FoldedArgumentTemplate):
2323
def __init__(self):
2424
self.name = "range"
2525
self.regex = "\[(\d+):(\d+)(?::(\d+))?\]"

smartdispatch/smartdispatch.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import smartdispatch
99
from smartdispatch import utils
10-
from smartdispatch.argument import EnumerationFoldedArgument, RangeFoldedArgument
10+
from smartdispatch.folded_argument_template import ListFoldedArgumentTemplate, RangeFoldedArgumentTemplate
1111

1212
UID_TAG = "{UID}"
1313

@@ -117,7 +117,7 @@ def get_commands_from_arguments(arguments):
117117
def unfold_arguments(arguments):
118118
''' Unfolds folded arguments into a list of unfolded arguments.
119119
120-
An argument can be folded e.g. a list of unfolded arguments separated by commas.
120+
An argument can be folded e.g. a list of unfolded arguments separated by spaces.
121121
An unfolded argument unfolds to itself.
122122
123123
Parameters
@@ -132,16 +132,17 @@ def unfold_arguments(arguments):
132132
133133
Complex arguments
134134
-----------------
135-
*enumeration*: "[item1 item2 ... itemN]"
135+
*list*: "[item1 item2 ... itemN]"
136136
*range*: "[start:end]" or "[start:end:step]"
137137
'''
138-
text = utils.escape(" ".join(arguments))
138+
text = utils.encode_escaped_characters(" ".join(arguments))
139139

140140
# Order matter, if some regex is more greedy than another, the it should go after
141-
arguments = [RangeFoldedArgument(), EnumerationFoldedArgument()]
141+
folded_argument_templates = [RangeFoldedArgumentTemplate(), ListFoldedArgumentTemplate()]
142+
folded_argument_templates_dict = {arg.name: arg for arg in folded_argument_templates}
142143

143144
# Build the master regex with all argument's regex
144-
regex = "(" + "|".join(["(?P<{0}>{1})".format(arg.name, arg.regex) for arg in arguments]) + ")"
145+
regex = "(" + "|".join(["(?P<{0}>{1})".format(arg.name, arg.regex) for arg in folded_argument_templates]) + ")"
145146

146147
pos = 0
147148
unfolded_arguments = []
@@ -150,15 +151,13 @@ def unfold_arguments(arguments):
150151
unfolded_arguments.append([text[pos:match.start()]])
151152

152153
# Unfold argument
153-
groupdict = match.groupdict()
154-
for argument in arguments:
155-
if groupdict[argument.name] is not None:
156-
unfolded_arguments.append(argument.unfold(groupdict[argument.name]))
157-
154+
folded_argument_template_name, matched_text = [(k, v) for k, v in match.groupdict().items() if v is not None][0]
155+
folded_argument_template = folded_argument_templates_dict[folded_argument_template_name]
156+
unfolded_arguments.append(folded_argument_template.unfold(matched_text))
158157
pos = match.end()
159158

160159
unfolded_arguments.append([text[pos:]]) # Add remaining unfolded arguments
161-
unfolded_arguments = [map(utils.hex2str, argvalues) for argvalues in unfolded_arguments]
160+
unfolded_arguments = [map(utils.decode_escaped_characters, argvalues) for argvalues in unfolded_arguments]
162161
return unfolded_arguments
163162

164163

smartdispatch/tests/test_argument.py renamed to smartdispatch/tests/test_folded_argument_template.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
from nose.tools import assert_equal, assert_true
22
from numpy.testing import assert_array_equal
33

4-
from smartdispatch.argument import EnumerationFoldedArgument, RangeFoldedArgument
5-
import smartdispatch
4+
from smartdispatch.folded_argument_template import ListFoldedArgumentTemplate, RangeFoldedArgumentTemplate
65

76
import re
8-
import os
9-
import unittest
107

118

12-
def test_enumeration_folded_argument():
9+
def test_list_folded_argument_template():
1310
# Test valid enumeration folded arguments
14-
arg = EnumerationFoldedArgument()
11+
arg = ListFoldedArgumentTemplate()
1512
folded_arguments = [("[]", [""]),
1613
("[1]", ["1"]),
1714
("[1 ]", ["1", ""]),
@@ -27,8 +24,8 @@ def test_enumeration_folded_argument():
2724
assert_true(re.match(arg.regex, "[1 2 3") is None)
2825

2926

30-
def test_range_folded_argument():
31-
arg = RangeFoldedArgument()
27+
def test_range_folded_argument_template():
28+
arg = RangeFoldedArgumentTemplate()
3229
folded_arguments = [("[1:4]", ["1", "2", "3"]),
3330
("[1:4:2]", ["1", "3"]),
3431
]

smartdispatch/tests/test_smartdispatch.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def test_get_commands_from_arguments():
107107
"python my_command.py 0.00000000001 -1 omicron",
108108
"python my_command.py 0.00000000001 -1 mu"])
109109

110-
# Test multiple unfolded arguments and not unfoldable brackets
110+
# Test multiple folded arguments and not unfoldable brackets
111111
args = smartdispatch.unfold_arguments(["python my_command.py [0.01 0.000001 0.00000000001] -1 \[[42 133,666]\] slow [omicron mu]"])
112112
assert_equal(smartdispatch.get_commands_from_arguments(args), ["python my_command.py 0.01 -1 [42] slow omicron",
113113
"python my_command.py 0.01 -1 [42] slow mu",
@@ -122,6 +122,27 @@ def test_get_commands_from_arguments():
122122
"python my_command.py 0.00000000001 -1 [133,666] slow omicron",
123123
"python my_command.py 0.00000000001 -1 [133,666] slow mu"])
124124

125+
# Test multiple different folded arguments
126+
args = smartdispatch.unfold_arguments(["python my_command.py [0.01 0.001] -[1:5] slow"])
127+
assert_equal(smartdispatch.get_commands_from_arguments(args), ["python my_command.py 0.01 -1 slow",
128+
"python my_command.py 0.01 -2 slow",
129+
"python my_command.py 0.01 -3 slow",
130+
"python my_command.py 0.01 -4 slow",
131+
"python my_command.py 0.001 -1 slow",
132+
"python my_command.py 0.001 -2 slow",
133+
"python my_command.py 0.001 -3 slow",
134+
"python my_command.py 0.001 -4 slow"])
135+
136+
args = smartdispatch.unfold_arguments(["python my_command.py -[1:5] slow [0.01 0.001]"])
137+
assert_equal(smartdispatch.get_commands_from_arguments(args), ["python my_command.py -1 slow 0.01",
138+
"python my_command.py -1 slow 0.001",
139+
"python my_command.py -2 slow 0.01",
140+
"python my_command.py -2 slow 0.001",
141+
"python my_command.py -3 slow 0.01",
142+
"python my_command.py -3 slow 0.001",
143+
"python my_command.py -4 slow 0.01",
144+
"python my_command.py -4 slow 0.001"])
145+
125146

126147
def test_replace_uid_tag():
127148
command = "command without uid tag"

smartdispatch/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ def slugify(value):
3535
return str(re.sub('[-\s]+', '_', value))
3636

3737

38-
def escape(text, escaping_character="\\"):
38+
def encode_escaped_characters(text, escaping_character="\\"):
3939
""" Escape the escaped character using its hex representation """
4040
def hexify(match):
4141
return "\\x{0}".format(match.group()[-1].encode("hex"))
4242

4343
return re.sub(r"\\.", hexify, text)
4444

4545

46-
def hex2str(text):
46+
def decode_escaped_characters(text):
4747
""" Convert hex representation to the character it represents """
4848
if len(text) == 0:
4949
return ''

0 commit comments

Comments
 (0)