Escape character in Groovy to AnsibleTower - groovy

During passcode transfer from groovy script to ansible tower plugin, is there any way to send passcode with special character with slash "<passcode>"
ansibleTower(async: false, credential: 'test_cred', extraVars: """
---
password: "U*gLO8br1dMJ\y!U"
"""
While passing a special character it is not able to read the passcode. Is there any regular expression to send this special character
Unable to request job template invocation Extra vars are bad: ["Cannot parse as JSON (error: Expecting value: line 2 column 1 (char 1)) or YAML (error: while scanning a double-quoted scalar\n in \"<unicode string>\", line 4, column 16:\n sudo_password: \"U*gLO8br1dMJ\\y!U\"\n ^\nfound unknown escape character 'y'\n in \"<unicode string>\", line 4, column 30:\n sudo_password: \"U*gLO8br1dMJ\\y!U\"\n ^)."]

Adding an extra "\" in the password will suffice to escape the password. Like "U*gLO8br1dMJ\\y!U" will suffice

Related

Why does the Syntaxerror f-string: expecting '}' happening in my code?

Here is where the error happened
print(f'Hey, {Username}, your password is {'*' * Password}. The password is **{len(Password)}** long.')
That is because you are using single quote for main string as well as '*'.
Python interprets it as -
print( f'Hey, {Username}, your password is {'# string ends here.
*' * Password}. The password is **{len(Password)}** long.')
The last bracket goes unmatched and hence the error.
A quick fix would be to use double quotes in one of the strings.
print(f'Hey, {Username}, your password is {"*" * Password}. The password is **{len(Password)}** long.')

System.JSONException: Unexpected character ('i' (code 105)): was expecting comma to separate OBJECT entries at [line:1, column:18]

#SalesforceChallenge
I'm trying to escape a string but I had no success so far.
This is the response body I'm getting:
{"text":"this \"is something\" I wrote"}
Please note that there are 2 backslashes to escape the double quotes char. (This is a sample. Actually I have a big to escape with lots of "text" elements.)
When I try to deserialize it I get the following error:
System.JSONException: Unexpected character ('i' (code 105)): was expecting comma to separate OBJECT entries at [line:1, column:18]
I've tried to escape by using:
String my = '{"text":"this \"is something\" I wrote"}';
System.debug('test 0: ' + my);
System.debug('test 1: ' + my.replace('\"', '-'));
System.debug('test 2: ' + my.replace('\\"', '-'));
System.debug('test 3: ' + my.replace('\\\"', '-'));
System.debug('test 4: ' + my.replace('\\\\"', '-'));
--- Results:
[22]|DEBUG|test 0: {"text":"this "is something" I wrote"}
[23]|DEBUG|test 1: {-text-:-this -is something- I wrote-}
[23]|DEBUG|test 1: {-text-:-this -is something- I wrote-}
[24]|DEBUG|test 2: {"text":"this "is something" I wrote"}
[25]|DEBUG|test 3: {"text":"this "is something" I wrote"}
[26]|DEBUG|test 4: {"text":"this "is something" I wrote"}
--- What I need as result:
{"text":"this -is something- I wrote"}
Please, does someone has any fix to share?
Thanks a lot.
This is the problem with your test runs in Anonymous Apex:
String my = '{"text":"this \"is something\" I wrote"}';
Because \ is an escape character, you need two backslashes in an Apex string literal to produce a backslash in the actual output:
String my = '{"text":"this \\"is something\\" I wrote"}';
Since Apex quotes strings with ', you don't have to escape the quotes for Apex; you're escaping them for the JSON parser.
The same principle applies to the strings you're trying to use to do replacements: you must escape the \ for Apex.
All that said, it's unclear why you are trying to manually alter this string. The payload
{"text":"this \"is something\" I wrote"}
is valid JSON. In general, you should not perform string replacement on inbound JSON structures in Apex unless you're attempting to compensate for a payload that contains an Apex reserved word as a key so that you can use typed deserialization.

How to make the translations work with the Python 3 "format" built-in method in Odoo?

Since Python v3, format is the primary API method to make variable substitutions and value formatting. However, Odoo is still using the Python 2 approach with the %s wildcard.
message = _('Scheduled meeting with %s') % invitee.name
# VS
message = 'Scheduled meeting with {}'.format(invitee.name) # this is not translated
I have seen some parts of the Odoo code where they have used some workaround, isolating strings.
exc = MissingError(
_("Record does not exist or has been deleted.")
+ '\n\n({} {}, {} {})'.format(_('Records:'), (self - existing).ids[:6], _('User:'), self._uid)
)
But, does anybody know if there is a more convenient way to use the format method and make it work with translations?
_ return a string, so you can call format on it directly.
_("{} Sequence").format(sec.code)
or like this:
_("{code} Sequence").format(code=invitee.code)
when you export the translation in PO file you should see this for the second example:
# for FR translation you should not translate the special format expression
msgid "{code} Sequence"
msgstr "{code} Séquence"
and this for the first example:
msgid "{} Sequence"
msgstr "{} Séquence"
If you don't see this then Odoo must be checking that _() must not be followed by . so you can work around this by doing this for example by surrounding the expression by parentheses :
# I think + "" is not needed
( _("{} Séquence") + "").format(sec.code)
Because In python "this {}".format("expression") is the same as this ("this {}").format("expression")

api response contains escape characters

I am getting value from API response as below
{
"ORG_ID":"165",
"DEPOT_NAME":"Pesto",
"DEPOT_SHORT_NAME":"PSD",
"PROD_ID":"709492",
"DESCRIPTION":"EX CL (2X14) U17\SH36\5",
"PRICE":"3708.55",
"STOCK":"2"
},
now when I am parsing it in json like json.parse(response) it cashes the app.. error is below:
undefined:11
"DESCRIPTION":"EXELON HGC 4.5MG (2X14) U17\SH36\5",
^
SyntaxError: Unexpected token S in JSON at position 296
What should I do to get rid of these escapes.
though I need the same values I don't want to change any value or remove these slashes.
You need to escape the special characters before parsing the jSON.
In this case for it to be valid it should be:
{
"ORG_ID":"165",
"DEPOT_NAME":"Pesto",
"DEPOT_SHORT_NAME":"PSD",
"PROD_ID":"709492",
"DESCRIPTION":"EX CL (2X14) U17\\SH36\\5",
"PRICE":"3708.55",
"STOCK":"2"
}

Take out string from "<string>" in Lua

I want to parse some data from a string in Lua. I have tried several combination of string.match and string.sub but no luck. Here is the detail...
str='[{id:78749,name:Hrithik Roshan,character:Vijay Deenanath Chauhan,order:0,cast_id:1,profile_path:/1uGhDRNCA9I4WvyD9TfgKYnLEhZ.jpg},{id:77234,name:Priyanka Chopra,character:Kaali Gawde,ord'
fixstr = string.gsub(str,"name:","<actors>")
fixstr = string.gsub(fixstr,"character:","<actors>")
print(fixstr)
fixstr1 = string.match( fixstr, "<actors>(.+)<actors>")
print(fixstr1)
Output:
Output of rint(fixstr)
[{id:78749,<actors>Hrithik Roshan,<actors>Vijay Deenanath Chauhan,order:0,cast_id:1,profile_path:/1uGhDRNCA9I4WvyD9TfgKYnLEhZ.jpg},{id:77234,<actors>Priyanka Chopra,<actors>Kaali Gawde,ord
Output of print(fixstr1)
Hrithik Roshan,<actors>Vijay Deenanath Chauhan,order:0,cast_id:1,profile_path:/1uGhDRNCA9I4WvyD9TfgKYnLEhZ.jpg},{id:77234,<actors>Priyanka Chopra,
What I am trying to do is get all the string between <actors>...<actors>
but it didn't worked. Can anybody help on this?
To get all the strings between <actors> and <actors>, use string.gmatch for global match:
for name in string.gmatch(fixstr, "<actors>(.-)<actors>") do
print(name)
end
Note the use of .- in which - matches zero or more occurrences, but it's non-greedy.
Output:
Hrithik Roshan,
Priyanka Chopra,
Actually, unless you need fixstr for other uses, you don't need to substitute name: and character: to <actors>, just one run of string.gmatch can do the job:
for name in string.gmatch(str, "name:(.-)character:") do
print(name)
end

Resources