Skip to content

Commit e01e5d0

Browse files
author
Amir Tocker
committed
Fix variables regex, test variables order.
1 parent 66a4222 commit e01e5d0

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

cloudinary/utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from cloudinary.compat import PY3, to_bytes, to_bytearray, to_string, string_types, urlparse
1717
from cloudinary import auth_token
1818

19+
VAR_NAME_RE = r'(\$\([a-zA-Z]\w+\))'
20+
1921
urlencode = six.moves.urllib.parse.urlencode
2022
unquote = six.moves.urllib.parse.unquote
2123

@@ -695,7 +697,7 @@ def process_layer(layer, layer_parameter):
695697
components.append(public_id)
696698

697699
if text is not None:
698-
var_pattern = r'(\$\([a-zA-Z]\w+\))'
700+
var_pattern = VAR_NAME_RE
699701
match = re.findall(var_pattern,text)
700702

701703
parts= filter(lambda p: p is not None, re.split(var_pattern,text))
@@ -728,7 +730,7 @@ def process_layer(layer, layer_parameter):
728730
"*": 'mul',
729731
"/": 'div',
730732
"+": 'add',
731-
"-": 'min'
733+
"-": 'sub'
732734
}
733735

734736
PREDEFINED_VARS = {
@@ -746,8 +748,7 @@ def process_layer(layer, layer_parameter):
746748
"width": "w"
747749
}
748750

749-
replaceRE = "(" + "|".join(PREDEFINED_VARS.keys())+ "|[=<>&|!]+)"
750-
replaceIF = "(" + '|'.join(map( lambda key: re.escape(key),IF_OPERATORS.keys()))+ ")"
751+
replaceRE = "((\\|\\||>=|<=|&&|!=|>|=|<|/|-|\\+|\\*)(?=[ _])|" + '|'.join(PREDEFINED_VARS.keys())+ ")"
751752

752753

753754
def translate_if(match):
@@ -768,7 +769,6 @@ def normalize_expression(expression):
768769
elif expression:
769770
result = str(expression)
770771
result = re.sub(replaceRE, translate_if, result)
771-
result = re.sub(replaceIF, translate_if, result)
772772
result = re.sub('[ _]+', '_', result)
773773
return result
774774
else:

tests/api_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def test11_tags_prefix(self):
187187
@unittest.skipUnless(cloudinary.config().api_secret, "requires api_key/api_secret")
188188
def test12_transformations(self):
189189
""" should allow listing transformations """
190-
transformations = api.transformations()["transformations"]
190+
transformations = api.transformations(max_results=500)["transformations"]
191191
transformation = [tr for tr in transformations if tr["name"] == "c_scale,w_100"][0]
192192

193193
self.assertIsNotNone(transformation)

tests/utils_test.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@ def test_effect(self):
271271

272272
def test_effect_with_dict(self):
273273
"""should support effect with dict"""
274-
self.__test_cloudinary_url(options={"effect": {"sepia": 10}},
275-
expected_url=DEFAULT_UPLOAD_PATH + "e_sepia:10/test")
274+
self.__test_cloudinary_url(options={"effect": {"sepia": -10}},
275+
expected_url=DEFAULT_UPLOAD_PATH + "e_sepia:-10/test")
276276

277277
def test_effect_with_array(self):
278278
"""should support effect with array"""
@@ -653,6 +653,16 @@ def test_dollar_key_should_define_a_variable(self):
653653
transformation, options = cloudinary.utils.generate_transformation_string(**options)
654654
self.assertEqual('$foo_10/if_fc_gt_2/c_scale,w_$foo_mul_200_div_fc/if_end', transformation)
655655

656+
def test_should_sort_defined_variable(self):
657+
options = { "$second": 1, "$first": 2}
658+
transformation, options = cloudinary.utils.generate_transformation_string(**options)
659+
self.assertEqual('$first_2,$second_1', transformation)
660+
661+
def test_should_place_defined_variables_before_ordered(self):
662+
options = {"variables" : [ ["$z", 5], ["$foo", "$z * 2"] ], "$second": 1, "$first": 2}
663+
transformation, options = cloudinary.utils.generate_transformation_string(**options)
664+
self.assertEqual('$first_2,$second_1,$z_5,$foo_$z_mul_2', transformation)
665+
656666
def test_should_support_text_values(self):
657667
public_id = "sample"
658668
options = {"effect":"$efname:100", "$efname":"!blur!"}

0 commit comments

Comments
 (0)