Skip to content

Commit

Permalink
Fix variables regex, test variables order.
Browse files Browse the repository at this point in the history
  • Loading branch information
Amir Tocker committed Mar 16, 2017
1 parent 66a4222 commit e01e5d0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
10 changes: 5 additions & 5 deletions cloudinary/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from cloudinary.compat import PY3, to_bytes, to_bytearray, to_string, string_types, urlparse
from cloudinary import auth_token

VAR_NAME_RE = r'(\$\([a-zA-Z]\w+\))'

urlencode = six.moves.urllib.parse.urlencode
unquote = six.moves.urllib.parse.unquote

Expand Down Expand Up @@ -695,7 +697,7 @@ def process_layer(layer, layer_parameter):
components.append(public_id)

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

parts= filter(lambda p: p is not None, re.split(var_pattern,text))
Expand Down Expand Up @@ -728,7 +730,7 @@ def process_layer(layer, layer_parameter):
"*": 'mul',
"/": 'div',
"+": 'add',
"-": 'min'
"-": 'sub'
}

PREDEFINED_VARS = {
Expand All @@ -746,8 +748,7 @@ def process_layer(layer, layer_parameter):
"width": "w"
}

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


def translate_if(match):
Expand All @@ -768,7 +769,6 @@ def normalize_expression(expression):
elif expression:
result = str(expression)
result = re.sub(replaceRE, translate_if, result)
result = re.sub(replaceIF, translate_if, result)
result = re.sub('[ _]+', '_', result)
return result
else:
Expand Down
2 changes: 1 addition & 1 deletion tests/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def test11_tags_prefix(self):
@unittest.skipUnless(cloudinary.config().api_secret, "requires api_key/api_secret")
def test12_transformations(self):
""" should allow listing transformations """
transformations = api.transformations()["transformations"]
transformations = api.transformations(max_results=500)["transformations"]
transformation = [tr for tr in transformations if tr["name"] == "c_scale,w_100"][0]

self.assertIsNotNone(transformation)
Expand Down
14 changes: 12 additions & 2 deletions tests/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ def test_effect(self):

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

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

def test_should_sort_defined_variable(self):
options = { "$second": 1, "$first": 2}
transformation, options = cloudinary.utils.generate_transformation_string(**options)
self.assertEqual('$first_2,$second_1', transformation)

def test_should_place_defined_variables_before_ordered(self):
options = {"variables" : [ ["$z", 5], ["$foo", "$z * 2"] ], "$second": 1, "$first": 2}
transformation, options = cloudinary.utils.generate_transformation_string(**options)
self.assertEqual('$first_2,$second_1,$z_5,$foo_$z_mul_2', transformation)

def test_should_support_text_values(self):
public_id = "sample"
options = {"effect":"$efname:100", "$efname":"!blur!"}
Expand Down

0 comments on commit e01e5d0

Please sign in to comment.