Skip to content

Commit

Permalink
issue #79: added new classes NativeLanguageSupport and Globalization …
Browse files Browse the repository at this point in the history
…for globalization options. PrintJob has a new variable of this Globalization class.
  • Loading branch information
SibrenJacobs committed May 30, 2022
1 parent 4c67e12 commit d4b7b44
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 2 deletions.
1 change: 1 addition & 0 deletions cloudofficeprint/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
from .output import *
from .pdf import *
from .server import *
from .globalization import *
117 changes: 117 additions & 0 deletions cloudofficeprint/config/globalization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
from typing import Dict, List, Mapping


class NativeLanguageSupport:
"""Class for optional native language support options.
All of them are optional, which is why passing an instance of this class
in a Globalization object is also optional.
"""

def __init__(self,
sort: str = None,
comp: str = None,
numeric_characters_dec_grp: str = None,
currency: str = None,
territory: str = None,
language: str = None,
):
"""
Args:
sort (str): the native language support sort. Defaults to None.
comp (str): the native language support comp. Defaults to None.
numeric_characters_dec_grp (str): the native language support numeric characters decimal group. Defaults to None.
currency (str): the native language support currency. Defaults to None.
territory (str): the native language support territory. Defaults to None.
language (str): the native language support language. Defaults to None.
"""
self.sort: str = sort
self.comp: str = comp
self.numeric_characters_dec_grp: str = numeric_characters_dec_grp
self.currency: str = currency
self.territory: str = territory
self.language: str = language

@property
def as_dict(self) -> Dict:
"""The dict representation of these native language support options.
Returns:
Dict: the dict representation of these native language support options.
"""
result = {}

if self.sort is not None:
result["nls_sort"] = self.sort
if self.comp is not None:
result["nls_comp"] = self.comp
if self.numeric_characters_dec_grp is not None:
result["nls_numeric_characters_dec_grp"] = self.numeric_characters_dec_grp
if self.currency is not None:
result["nls_currency"] = self.currency
if self.territory is not None:
result["nls_territory"] = self.territory
if self.language is not None:
result["nls_language"] = self.language

return result


class Globalization:
"""Class for optional globalization options.
The properties of this class define all possible globalization options.
All of them are optional, which is why passing an instance of this class
in an PrintJob is also optional.
"""

def __init__(self,
date_format: str = None,
date_time_format: str = None,
timestamp_format: str = None,
timestamp_tz_format: str = None,
direction: str = None,
application_primary_language: str = None,
native_language_support: NativeLanguageSupport = None,
):
"""
Args:
date_format (str): The date format. Defaults to None.
date_time_format (str): The date time format. Defaults to None.
timestamp_format (str): The timestamp format. Defaults to None.
timestamp_tz_format (str): The timestamp tz format. Defaults to None.
direction (str): The direction. Defaults to None.
application_primary_language (str): The application primary language. Defaults to None.
native_language_support (NativeLanguageSupport): The native language support options. Defaults to None.
"""
self.date_format: str = date_format
self.date_time_format: str = date_time_format
self.timestamp_format: str = timestamp_format
self.timestamp_tz_format: str = timestamp_tz_format
self.direction: str = direction
self.application_primary_language: str = application_primary_language
self.native_language_support: NativeLanguageSupport = native_language_support

@property
def as_dict(self) -> Dict:
"""The dict representation of these globalization options.
Returns:
Dict: the dict representation of these globalization options.
"""
result = {}

if self.date_format is not None:
result["date_format"] = self.date_format
if self.date_time_format is not None:
result["date_time_format"] = self.date_time_format
if self.timestamp_format is not None:
result["timestamp_format"] = self.timestamp_format
if self.timestamp_tz_format is not None:
result["timestamp_tz_format"] = self.timestamp_tz_format
if self.direction is not None:
result["direction"] = self.direction
if self.application_primary_language is not None:
result["application_primary_language"] = self.application_primary_language
if self.native_language_support is not None:
result.update(self.native_language_support.as_dict)

return result
8 changes: 7 additions & 1 deletion cloudofficeprint/printjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import requests
import asyncio
import json
from .config import OutputConfig, Server

from .config import OutputConfig, Server, Globalization
from .exceptions import COPError
from .response import Response
from .resource import Resource
Expand Down Expand Up @@ -38,6 +39,7 @@ def __init__(self,
subtemplates: Dict[str, Resource] = {},
prepend_files: List[Resource] = [],
append_files: List[Resource] = [],
globalization: Globalization = Globalization(),
cop_verbose: bool = False):
"""
Args:
Expand All @@ -48,6 +50,7 @@ def __init__(self,
subtemplates (Dict[str, Resource], optional): Subtemplates for this print job, accessible (in docx) through `{?include subtemplate_dict_key}`. Defaults to {}.
prepend_files (List[Resource], optional): Files to prepend to the output file. Defaults to [].
append_files (List[Resource], optional): Files to append to the output file. Defaults to [].
globalization (Globalization): Globalization options to be used for this print job. Defaults to `Globalization()`.
cop_verbose (bool, optional): Whether or not verbose mode should be activated. Defaults to False.
"""

Expand All @@ -58,6 +61,7 @@ def __init__(self,
self.subtemplates: Dict[str, Resource] = subtemplates
self.prepend_files: List[Resource] = prepend_files
self.append_files: List[Resource] = append_files
self.globalization: Globalization = globalization
self.cop_verbose: bool = cop_verbose

def execute(self) -> Response:
Expand Down Expand Up @@ -210,6 +214,8 @@ def as_dict(self) -> Dict:
templates_list.append(to_add)
result["templates"] = templates_list

result["globalization"] = [self.globalization.as_dict]

# If verbose mode is activated, print the result to the terminal
if self.cop_verbose:
print('The JSON data that is sent to the Cloud Office Print server:\n')
Expand Down
36 changes: 36 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,41 @@ def test_request_option():
assert output.as_dict == output_expected


def test_globalization():
"""Test Globalization"""
globalization = cop.config.Globalization(
date_format="DD-MON-YYYY",
date_time_format="DD-MON-YYYY HH24:MI",
timestamp_format="DD-MON-YYYY",
timestamp_tz_format="DD-MON-YYYY",
direction="ltr",
application_primary_language="en",
native_language_support=cop.config.NativeLanguageSupport(
sort="BINARY",
comp="BINARY",
numeric_characters_dec_grp=".,",
currency="$",
territory="AMERICA",
language="AMERICAN",
)
)
globalization_expected = {
"date_format": "DD-MON-YYYY",
"date_time_format": "DD-MON-YYYY HH24:MI",
"timestamp_format": "DD-MON-YYYY",
"timestamp_tz_format": "DD-MON-YYYY",
"nls_sort": "BINARY",
"nls_comp": "BINARY",
"nls_numeric_characters_dec_grp": ".,",
"nls_currency": "$",
"nls_territory": "AMERICA",
"nls_language": "AMERICAN",
"direction": "ltr",
"application_primary_language": "en"
}
assert globalization.as_dict == globalization_expected


def test_route_paths():
"""Test output types of route path functions"""
assert type(server.get_version_soffice()) == str
Expand All @@ -257,6 +292,7 @@ def run():
test_commands()
test_route_paths()
test_request_option()
test_globalization()


if __name__ == '__main__':
Expand Down
37 changes: 36 additions & 1 deletion tests/test_printjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,33 @@ def test_printjob():

output_conf = cop.config.OutputConfig(filetype='pdf')

globalization = cop.config.Globalization(
date_format="DD-MON-YYYY",
date_time_format="DD-MON-YYYY HH24:MI",
timestamp_format="DD-MON-YYYY",
timestamp_tz_format="DD-MON-YYYY",
direction="ltr",
application_primary_language="en",
native_language_support=cop.config.NativeLanguageSupport(
sort="BINARY",
comp="BINARY",
numeric_characters_dec_grp=".,",
currency="$",
territory="AMERICA",
language="AMERICAN",
)
)

printjob = cop.PrintJob(
data=data,
server=server,
template=template_main,
output_config=output_conf,
subtemplates=subtemplates,
prepend_files=[prepend_file],
append_files=[append_file])
append_files=[append_file],
globalization=globalization
)
printjob_expected = {
'api_key': server.config.api_key,
'append_files': [
Expand Down Expand Up @@ -80,6 +99,22 @@ def test_printjob():
'name': 'sub2'
}
],
'globalization': [
{
"date_format": "DD-MON-YYYY",
"date_time_format": "DD-MON-YYYY HH24:MI",
"timestamp_format": "DD-MON-YYYY",
"timestamp_tz_format": "DD-MON-YYYY",
"nls_sort": "BINARY",
"nls_comp": "BINARY",
"nls_numeric_characters_dec_grp": ".,",
"nls_currency": "$",
"nls_territory": "AMERICA",
"nls_language": "AMERICAN",
"direction": "ltr",
"application_primary_language": "en"
}
],
'python_sdk_version': cop.printjob.STATIC_OPTS['python_sdk_version']
}
assert printjob.as_dict == printjob_expected
Expand Down

0 comments on commit d4b7b44

Please sign in to comment.