Skip to content

Commit

Permalink
Merge 77a5709 into e00115a
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloge committed Feb 22, 2022
2 parents e00115a + 77a5709 commit a5662f8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.rst
@@ -1,11 +1,13 @@
Toolium Changelog
=================

v2.3.1
v2.4.0
------

*Release date: In development*

- Add set_file_path and set_base64_path functions to dataset module, to set base paths for FILE and BASE64 mappings

v2.3.0
------

Expand Down
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
2.3.1.dev0
2.4.0.dev0
11 changes: 8 additions & 3 deletions toolium/test/utils/test_dataset_map_param.py
Expand Up @@ -21,11 +21,10 @@
import pytest

from toolium.config_parser import ExtendedConfigParser
from toolium.utils.dataset import map_param
from toolium.utils.dataset import hide_passwords
from toolium.utils.dataset import map_param, set_base64_path, set_file_path, hide_passwords


def test_a_env_param():
def test_an_env_param():
"""
Verification of a mapped parameter as ENV
"""
Expand All @@ -42,6 +41,9 @@ def test_a_file_param():
result = map_param("[FILE:toolium/test/resources/document.txt]")
expected = "Document used to verify functionalities in MSS "
assert expected == result
set_file_path('toolium/test/resources/')
result = map_param("[FILE:document.txt]")
assert expected == result


def test_a_base64_param():
Expand All @@ -51,6 +53,9 @@ def test_a_base64_param():
result = map_param("[BASE64:toolium/test/resources/document.txt]")
expected = "RG9jdW1lbnQgdXNlZCB0byB2ZXJpZnkgZnVuY3Rpb25hbGl0aWVzIGluIE1TUyA="
assert expected == result
set_base64_path('toolium/test/resources/')
result = map_param("[BASE64:document.txt]")
assert expected == result


def test_a_lang_param():
Expand Down
38 changes: 32 additions & 6 deletions toolium/utils/dataset.py
Expand Up @@ -29,6 +29,10 @@

logger = logging.getLogger(__name__)

# Base path for BASE64 and FILE conversions
base_base64_path = ''
base_file_path = ''


def replace_param(param, language='es', infer_param_type=True):
"""
Expand Down Expand Up @@ -565,15 +569,36 @@ def get_translation_by_poeditor_reference(reference, context):
return translation


def set_base64_path(path):
"""
Set a relative path to be used as the base for the file path specified in the BASE64 mapping pattern.
:param path: relative path to be used as base for base64 mapping
"""
global base_base64_path
base_base64_path = path


def set_file_path(path):
"""
Set a relative path to be used as the base for the file path specified in the FILE mapping pattern.
:param path: relative path to be used as base for file mapping
"""
global base_file_path
base_file_path = path


def get_file(file_path):
"""
Return the content of a file given its path.
Return the content of a file given its path. If a base path was previously set by using
the set_file_path() function, the file path specified must be relative to that path.
:param file path: file path using slash as separator (e.g. "resources/files/doc.txt")
:return: string with the file content
"""
file_path_parts = file_path.split("/")
file_path = os.path.join(*file_path_parts)
file_path_parts = (base_file_path + file_path).split("/")
file_path = os.path.abspath(os.path.join(*file_path_parts))
if not os.path.exists(file_path):
raise Exception(f' ERROR - Cannot read file "{file_path}". Does not exist.')

Expand All @@ -583,13 +608,14 @@ def get_file(file_path):

def convert_file_to_base64(file_path):
"""
Return the content of a file given its path encoded in Base64.
Return the content of a file given its path encoded in Base64. If a base path was previously set by using
the set_file_path() function, the file path specified must be relative to that path.
:param file path: file path using slash as separator (e.g. "resources/files/doc.txt")
:return: string with the file content encoded in Base64
"""
file_path_parts = file_path.split("/")
file_path = os.path.join(*file_path_parts)
file_path_parts = (base_base64_path + file_path).split("/")
file_path = os.path.abspath(os.path.join(*file_path_parts))
if not os.path.exists(file_path):
raise Exception(f' ERROR - Cannot read file "{file_path}". Does not exist.')

Expand Down

0 comments on commit a5662f8

Please sign in to comment.