diff --git a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/__init__.py b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/alphanumeric.py b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/alphanumeric.py new file mode 100644 index 00000000000..62c85aaa148 --- /dev/null +++ b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/alphanumeric.py @@ -0,0 +1,33 @@ +import random +import string + +LOWERCASEASCII = "abcdefghijklmnopqrstuvwxyz" +UPPERCASEASCII = LOWERCASEASCII.upper() +NUMBERS = "1234567890" + +def to_alphanumeric(string): + return ''.join(c for c in string if c.isalnum()) + +def random_string(length=4, charset=LOWERCASEASCII + NUMBERS): + return ''.join(random.choice(charset) for _ in range(length)) + +def alphanumeric(length=4, lowercase=True): + charset = LOWERCASEASCII + NUMBERS + if not lowercase: + charset += UPPERCASEASCII + return random_string(length, charset) + +def alpha(length=4, lowercase=True): + charset = LOWERCASEASCII + if not lowercase: + charset += UPPERCASEASCII + return random_string(length, charset) + +def numeric(length=4): + return random_string(length, NUMBERS) + +def is_numeric(string): + return string.isnumeric() + +def is_alphanumeric(string): + return string.isalnum() diff --git a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/base64.py b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/base64.py new file mode 100644 index 00000000000..3b6822855f3 --- /dev/null +++ b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/base64.py @@ -0,0 +1,30 @@ +import base64 + +def encode(input): + if isinstance(input, str): + input = input.encode('utf-8') + encoded_bytes = base64.b64encode(input) + return encoded_bytes.decode('utf-8') + +def encode_as_bytes(input): + if isinstance(input, str): + input = input.encode('utf-8') + return base64.b64encode(input) + +def encode_as_native_bytes(input): + if isinstance(input, str): + input = input.encode('utf-8') + return input + +def decode(input): + try: + decoded_bytes = base64.b64decode(input) + return decoded_bytes.decode('utf-8') + except Exception as e: + return str(e) + +def decode_as_native_bytes(input): + try: + return base64.b64decode(input) + except Exception as e: + return str(e) diff --git a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/digest.py b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/digest.py new file mode 100644 index 00000000000..f78d0e6e6e5 --- /dev/null +++ b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/digest.py @@ -0,0 +1,43 @@ +import hashlib + +def md5(input): + if isinstance(input, str): + input = input.encode('utf-8') + md5_hash = hashlib.md5(input) + return md5_hash.digest() + +def md5_hex(input): + if isinstance(input, str): + input = input.encode('utf-8') + md5_hash = hashlib.md5(input) + return md5_hash.hexdigest() + +def sha1(input): + if isinstance(input, str): + input = input.encode('utf-8') + sha1_hash = hashlib.sha1(input) + return sha1_hash.digest() + +def sha1_hex(input): + if isinstance(input, str): + input = input.encode('utf-8') + sha1_hash = hashlib.sha1(input) + return sha1_hash.hexdigest() + +def sha256(input): + if isinstance(input, str): + input = input.encode('utf-8') + sha256_hash = hashlib.sha256(input) + return sha256_hash.digest() + +def sha384(input): + if isinstance(input, str): + input = input.encode('utf-8') + sha384_hash = hashlib.sha384(input) + return sha384_hash.digest() + +def sha512(input): + if isinstance input, str: + input = input.encode('utf-8') + sha512_hash = hashlib.sha512(input) + return sha512_hash.digest() diff --git a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/escape.py b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/escape.py new file mode 100644 index 00000000000..08d3d8c307a --- /dev/null +++ b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/escape.py @@ -0,0 +1,106 @@ +import html +import json +import xml.sax.saxutils as saxutils + +def escape_csv(input): + return '"' + input.replace('"', '""') + '"' + +def escape_javascript(input): + return json.dumps(input) + +def escape_html3(input): + return html.escape(input) + +def escape_html4(input): + return html.escape(input) + +def escape_java(input): + escaped = "" + for char in input: + if char == '\n': + escaped += '\\n' + elif char == '\r': + escaped += '\\r' + elif char == '\t': + escaped += '\\t' + elif char == '\\': + escaped += '\\\\' + elif char == '"': + escaped += '\\"' + elif ord(char) < 32 or ord(char) > 126: + escaped += "\\u{:04x}".format(ord(char)) + else: + escaped += char + return escaped + +def escape_json(input): + return json.dumps(input) + +def escape_xml(input): + return saxutils.escape(input, entities={'"': '"', "'": '''}) + +def unescape_csv(input): + if input.startswith('"') and input.endswith('"'): + input = input[1:-1] + return input.replace('""', '"') + return input + +def unescape_javascript(input): + return json.loads(input) + +def unescape_html3(input): + return html.unescape(input) + +def unescape_html4(input): + return html.unescape(input) + +def unescape_java(input): + unescaped = "" + i = 0 + while i < len(input): + char = input[i] + if char == '\\' and i + 1 < len(input): + next_char = input[i + 1] + if next_char == 'n': + unescaped += '\n' + i += 1 + elif next_char == 'r': + unescaped += '\r' + i += 1 + elif next_char == 't': + unescaped += '\t' + i += 1 + elif next_char == '\\': + unescaped += '\\' + i += 1 + elif next_char == '"': + unescaped += '"' + i += 1 + elif next_char == "'": + unescaped += "'" + i += 1 + elif next_char == 'b': + unescaped += '\b' + i += 1 + elif next_char == 'f': + unescaped += '\f' + i += 1 + elif next_char == 'u' and i + 5 < len(input): + try: + unicode_char = chr(int(input[i + 2:i + 6], 16)) + unescaped += unicode_char + i += 5 + except ValueError: + unescaped += '\\' + next_char + else: + unescaped += '\\' + next_char + else: + unescaped += char + i += 1 + return unescaped + +def unescape_json(input): + return json.loads(input) + +def unescape_xml(input): + return saxutils.unescape(input, entities={'quot': '"', 'apos': "'"}) diff --git a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/hex.py b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/hex.py new file mode 100644 index 00000000000..5e85ff2fe26 --- /dev/null +++ b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/hex.py @@ -0,0 +1,27 @@ +def encode(input): + if isinstance(input, str): + input = input.encode("utf-8") + return input.hex() + +def encode_as_bytes(input): + if isinstance(input, str): + input = input.encode("utf-8") + return bytes.fromhex(encode(input)) + +def encode_as_native_bytes(input): + if isinstance(input, str): + input = input.encode("utf-8") + return bytes.fromhex(encode(input)) + +def decode(input): + try: + decoded_bytes = bytes.fromhex(input) + return decoded_bytes.decode("utf-8") + except ValueError: + return None + +def decode_as_native_bytes(input): + try: + return bytes.fromhex(input) + except ValueError: + return None diff --git a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/jsonpath.py b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/jsonpath.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/qrcode.py b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/qrcode.py new file mode 100644 index 00000000000..d8b17e9824c --- /dev/null +++ b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/qrcode.py @@ -0,0 +1,67 @@ +import re + +class JSONPath: + def __init__(self, obj, path, callback=None): + self._obj = obj + self._path = path + self._callback = callback + self._result = [] + + def evaluate(self): + expr = self._path + json = self._obj + callback = self._callback + self._trace([expr], json, ['$', ''], callback) + return self._result + + def _get_preferred_output(self, ea): + # Implement this method to return the preferred output based on the result type + pass + + def _handle_callback(self, full_ret_obj, callback, type): + if callback: + preferred_output = self._get_preferred_output(full_ret_obj) + callback(preferred_output, type, full_ret_obj) + + def _trace(self, expr, val, path, callback): + # Implement this method to trace the JSONPath expression + pass + + def _walk(self, loc, expr, val, path, callback): + # Implement this method to walk the JSON structure + pass + + def _slice(self, loc, expr, val, path, callback): + # Implement this method to handle slicing + pass + + def _eval(self, code, v, vname, path, parent, parent_prop_name): + # Implement this method to evaluate expressions + pass + + def _walk(self, loc, expr, val, path, callback, f): + # Implement this method to walk the JSON structure with specific rules + pass + + def _slice(self, loc, expr, val, path, callback): + # Implement this method to handle slicing based on the loc expression + pass + + def _eval(self, code, v, vname, path, parent, parent_prop_name): + # Implement this method to evaluate expressions and return a result + pass + + @staticmethod + def to_path_string(path_arr): + # Implement this method to convert a path array to a path string + pass + + @staticmethod + def to_pointer(pointer): + # Implement this method to convert a JSON Path to JSON Pointer + pass + + @staticmethod + def to_path_array(expr): + # Implement this method to convert an expression to a path array + pass diff --git a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/url.py b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/url.py new file mode 100644 index 00000000000..18166b5bd37 --- /dev/null +++ b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/url.py @@ -0,0 +1,16 @@ +import urllib.parse + +def encode(input, charset): + return urllib.parse.quote(input, charset) + +def decode(input, charset): + return urllib.parse.unquote(input, charset) + +def escape(input): + return urllib.parse.quote(input) + +def escapePath(input): + return urllib.parse.quote(input, safe='/') + +def escapeForm(input): + return urllib.parse.quote_plus(input) diff --git a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/utf8.py b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/utf8.py new file mode 100644 index 00000000000..a7c281088eb --- /dev/null +++ b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/utf8.py @@ -0,0 +1,8 @@ +def encode(input): + return input.encode('utf-8') + +def decode(input): + return input.decode('utf-8') + +def bytesToString(bytes, offset, length): + return bytes[offset:offset + length].decode('utf-8') diff --git a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/uuid.py b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/uuid.py new file mode 100644 index 00000000000..736ec78f30a --- /dev/null +++ b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/uuid.py @@ -0,0 +1,11 @@ +import uuid + +def random(): + return str(uuid.uuid4()) + +def validate(input): + try: + uuid.UUID(input) + return True + except ValueError: + return False diff --git a/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/xml.py b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/xml.py new file mode 100644 index 00000000000..7e5fa042424 --- /dev/null +++ b/components/api/api-modules-python/src/main/resources/META-INF/dirigible/python-modules/dirigible/utils/xml.py @@ -0,0 +1,39 @@ +import json +import xml.etree.ElementTree as ET + +def from_json(input): + try: + data = json.loads(input) + root = ET.Element("root") + dict_to_xml(data, root) + xml_string = ET.tostring(root).decode() + return xml_string + except Exception as e: + return str(e) + +def to_json(input): + try: + root = ET.fromstring(input) + data = xml_to_dict(root) + json_string = json.dumps(data, indent=2) + return json_string + except Exception as e: + return str(e) + +def dict_to_xml(d, parent): + for key, value in d.items(): + element = ET.Element(key) + parent.append(element) + if isinstance(value, dict): + dict_to_xml(value, element) + else: + element.text = str(value) + +def xml_to_dict(element): + data = {} + for child in element: + if len(child) > 0: + data[child.tag] = xml_to_dict(child) + else: + data[child.tag] = child.text + return data