Skip to content

Commit 2f813b4

Browse files
authored
CommonServerPython lower camel case support (demisto#12064)
* Added the ability to convert a string to a camel case with the first letter not capitalised. * Added example to camelcase functions. * update release notes * revert changes
1 parent a4f3b94 commit 2f813b4

4 files changed

Lines changed: 73 additions & 12 deletions

File tree

Packs/Base/ReleaseNotes/1_10_1.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
#### Scripts
3+
##### CommonServerPython
4+
- Maintenance and stability enhancements.

Packs/Base/Scripts/CommonServerPython/CommonServerPython.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4255,7 +4255,7 @@ def to_context(self):
42554255
return ret_value
42564256

42574257

4258-
def camelize_string(src_str, delim='_'):
4258+
def camelize_string(src_str, delim='_', upper_camel=True):
42594259
"""
42604260
Transform snake_case to CamelCase
42614261
@@ -4265,11 +4265,22 @@ def camelize_string(src_str, delim='_'):
42654265
:type delim: ``str``
42664266
:param delim: indicator category.
42674267
4268+
:type upper_camel: ``bool``
4269+
:param upper_camel: When True then transforms string to camel case with the first letter capitalised
4270+
(for example: demisto_content to DemistoContent), otherwise the first letter will not be capitalised
4271+
(for example: demisto_content to demistoContent).
4272+
42684273
:return: A CammelCase string.
42694274
:rtype: ``str``
42704275
"""
4276+
if not src_str: # empty string
4277+
return ""
42714278
components = src_str.split(delim)
4272-
return ''.join(map(lambda x: x.title(), components))
4279+
camelize_without_first_char = ''.join(map(lambda x: x.title(), components[1:]))
4280+
if upper_camel:
4281+
return components[0].title() + camelize_without_first_char
4282+
else:
4283+
return components[0].lower() + camelize_without_first_char
42734284

42744285

42754286
class IndicatorsTimeline:
@@ -5157,7 +5168,7 @@ def return_warning(message, exit=False, warning='', outputs=None, ignore_auto_ex
51575168
sys.exit(0)
51585169

51595170

5160-
def camelize(src, delim=' '):
5171+
def camelize(src, delim=' ', upper_camel=True):
51615172
"""
51625173
Convert all keys of a dictionary (or list of dictionaries) to CamelCase (with capital first letter)
51635174
@@ -5167,6 +5178,11 @@ def camelize(src, delim=' '):
51675178
:type delim: ``str``
51685179
:param delim: The delimiter between two words in the key (e.g. delim=' ' for "Start Date"). Default ' '.
51695180
5181+
:type upper_camel: ``bool``
5182+
:param upper_camel: When True then transforms dictionary keys to camel case with the first letter capitalised
5183+
(for example: demisto_content to DemistoContent), otherwise the first letter will not be capitalised
5184+
(for example: demisto_content to demistoContent).
5185+
51705186
:return: The dictionary (or list of dictionaries) with the keys in CamelCase.
51715187
:rtype: ``dict`` or ``list``
51725188
"""
@@ -5175,10 +5191,14 @@ def camelize_str(src_str):
51755191
if callable(getattr(src_str, "decode", None)):
51765192
src_str = src_str.decode('utf-8')
51775193
components = src_str.split(delim)
5178-
return ''.join(map(lambda x: x.title(), components))
5194+
camelize_without_first_char = ''.join(map(lambda x: x.title(), components[1:]))
5195+
if upper_camel:
5196+
return components[0].title() + camelize_without_first_char
5197+
else:
5198+
return components[0].lower() + camelize_without_first_char
51795199

51805200
if isinstance(src, list):
5181-
return [camelize(phrase, delim) for phrase in src]
5201+
return [camelize(phrase, delim, upper_camel=upper_camel) for phrase in src]
51825202
return {camelize_str(key): value for key, value in src.items()}
51835203

51845204

@@ -5249,21 +5269,30 @@ def replace_str(src_str):
52495269
# ############################## REGEX FORMATTING end ###############################
52505270

52515271

5252-
def underscoreToCamelCase(s):
5272+
def underscoreToCamelCase(s, upper_camel=True):
52535273
"""
52545274
Convert an underscore separated string to camel case
52555275
52565276
:type s: ``str``
52575277
:param s: The string to convert (e.g. hello_world) (required)
52585278
5279+
:type upper_camel: ``bool``
5280+
:param upper_camel: When True then transforms dictionarykeys to camel case with the first letter capitalised
5281+
(for example: demisto_content to DemistoContent), otherwise the first letter will not be capitalised
5282+
(for example: demisto_content to demistoContent).
5283+
52595284
:return: The converted string (e.g. HelloWorld)
52605285
:rtype: ``str``
52615286
"""
52625287
if not isinstance(s, STRING_OBJ_TYPES):
52635288
return s
52645289

52655290
components = s.split('_')
5266-
return ''.join(x.title() for x in components)
5291+
camel_without_first_char = ''.join(x.title() for x in components[1:])
5292+
if upper_camel:
5293+
return components[0].title() + camel_without_first_char
5294+
else:
5295+
return components[0].lower() + camel_without_first_char
52675296

52685297

52695298
def camel_case_to_underscore(s):

Packs/Base/Scripts/CommonServerPython/CommonServerPython_test.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -521,12 +521,40 @@ def test_hash_djb2():
521521

522522
def test_camelize():
523523
non_camalized = [{'chookity_bop': 'asdasd'}, {'ab_c': 'd e', 'fgh_ijk': 'lm', 'nop': 'qr_st'}]
524-
expected_output = [{'ChookityBop': 'asdasd'}, {'AbC': 'd e', 'Nop': 'qr_st', 'FghIjk': 'lm'}]
525-
assert camelize(non_camalized, '_') == expected_output
524+
expected_output_upper_camel = [{'ChookityBop': 'asdasd'}, {'AbC': 'd e', 'Nop': 'qr_st', 'FghIjk': 'lm'}]
525+
expected_output_lower_camel = [{'chookityBop': 'asdasd'}, {'abC': 'd e', 'nop': 'qr_st', 'fghIjk': 'lm'}]
526+
assert camelize(non_camalized, '_') == expected_output_upper_camel
527+
assert camelize(non_camalized, '_', upper_camel=True) == expected_output_upper_camel
528+
assert camelize(non_camalized, '_', upper_camel=False) == expected_output_lower_camel
526529

527530
non_camalized2 = {'ab_c': 'd e', 'fgh_ijk': 'lm', 'nop': 'qr_st'}
528-
expected_output2 = {'AbC': 'd e', 'Nop': 'qr_st', 'FghIjk': 'lm'}
529-
assert camelize(non_camalized2, '_') == expected_output2
531+
expected_output2_upper_camel = {'AbC': 'd e', 'Nop': 'qr_st', 'FghIjk': 'lm'}
532+
expected_output2_lower_camel = {'abC': 'd e', 'nop': 'qr_st', 'fghIjk': 'lm'}
533+
assert camelize(non_camalized2, '_') == expected_output2_upper_camel
534+
assert camelize(non_camalized2, '_', upper_camel=True) == expected_output2_upper_camel
535+
assert camelize(non_camalized2, '_', upper_camel=False) == expected_output2_lower_camel
536+
537+
538+
def test_camelize_string():
539+
from CommonServerPython import camelize_string
540+
non_camalized = ['chookity_bop', 'ab_c', 'fgh_ijk', 'nop']
541+
expected_output_upper_camel = ['ChookityBop', 'AbC', 'FghIjk', 'Nop']
542+
expected_output_lower_camel = ['chookityBop', 'abC', 'fghIjk', 'nop']
543+
for i in range(len(non_camalized)):
544+
assert camelize_string(non_camalized[i], '_') == expected_output_upper_camel[i]
545+
assert camelize_string(non_camalized[i], '_', upper_camel=True) == expected_output_upper_camel[i]
546+
assert camelize_string(non_camalized[i], '_', upper_camel=False) == expected_output_lower_camel[i]
547+
548+
549+
def test_underscoreToCamelCase():
550+
from CommonServerPython import underscoreToCamelCase
551+
non_camalized = ['chookity_bop', 'ab_c', 'fgh_ijk', 'nop']
552+
expected_output_upper_camel = ['ChookityBop', 'AbC', 'FghIjk', 'Nop']
553+
expected_output_lower_camel = ['chookityBop', 'abC', 'fghIjk', 'nop']
554+
for i in range(len(non_camalized)):
555+
assert underscoreToCamelCase(non_camalized[i]) == expected_output_upper_camel[i]
556+
assert underscoreToCamelCase(non_camalized[i], upper_camel=True) == expected_output_upper_camel[i]
557+
assert underscoreToCamelCase(non_camalized[i], upper_camel=False) == expected_output_lower_camel[i]
530558

531559

532560
# Note this test will fail when run locally (in pycharm/vscode) as it assumes the machine (docker image) has UTC timezone set

Packs/Base/pack_metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Base",
33
"description": "The base pack for Cortex XSOAR.",
44
"support": "xsoar",
5-
"currentVersion": "1.10.0",
5+
"currentVersion": "1.10.1",
66
"author": "Cortex XSOAR",
77
"serverMinVersion": "6.0.0",
88
"url": "https://www.paloaltonetworks.com/cortex",

0 commit comments

Comments
 (0)