forked from esitarski/CrossMgr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
2,386 additions
and
338 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from typing import List, Union | ||
|
||
UTF8_ENCODING = "utf-8" | ||
ASCII_ENCODING = 'ascii' | ||
ISO_ENCODING = 'iso-8859-1' | ||
LF = b'\n' | ||
CR = b'\r' | ||
EOL = CR | ||
CRLF = b'\r\n' | ||
|
||
def encode_to_bytes(input_array: List[Union[str, bytes]], encoding: str = ISO_ENCODING) -> List[bytes]: | ||
encoded_array = [] | ||
|
||
if isinstance(input_array, bytes): | ||
raise ValueError("Input array must be a list of strings or bytes, not an array of bytes") | ||
|
||
if isinstance(input_array, str): | ||
raise ValueError("Input array must be a list of strings or bytes, not string") | ||
|
||
for element in input_array: | ||
if isinstance(element, bytes): | ||
encoded_array.append(element) | ||
else: | ||
encoded_array.append(element.encode(encoding)) | ||
return encoded_array |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import unittest | ||
from typing import List, Union | ||
|
||
from ByteUtils import CR, LF, encode_to_bytes | ||
|
||
class TestEncodeToBytes(unittest.TestCase): | ||
def test_all_strings(self): | ||
input_array = ['hello', 'world', 'test'] | ||
expected_output = [b'hello', b'world', b'test'] | ||
self.assertEqual(encode_to_bytes(input_array), expected_output) | ||
|
||
def test_all_bytes(self): | ||
input_array = [b'hello', b'world', b'test'] | ||
expected_output = [b'hello', b'world', b'test'] | ||
self.assertEqual(encode_to_bytes(input_array), expected_output) | ||
|
||
def test_mixed_strings_and_bytes(self): | ||
input_array = ['hello', b'world', 'test'] | ||
expected_output = [b'hello', b'world', b'test'] | ||
self.assertEqual(encode_to_bytes(input_array), expected_output) | ||
|
||
def test_empty_array(self): | ||
input_array = [] | ||
expected_output = [] | ||
self.assertEqual(encode_to_bytes(input_array), expected_output) | ||
|
||
def test_custom_encoding(self): | ||
input_array = ['hello', 'world', 'test'] | ||
expected_output = [b'hello', b'world', b'test'] | ||
self.assertEqual(encode_to_bytes(input_array, encoding='utf-8'), expected_output) | ||
|
||
def test_single_string(self): | ||
input_array = ['hello'] | ||
expected_output = [b'hello'] | ||
self.assertEqual(encode_to_bytes(input_array), expected_output) | ||
|
||
def test_single_byte(self): | ||
input_array = b'\n' | ||
expected_output = [b'\n'] | ||
self.assertRaises(ValueError, encode_to_bytes, input_array, 'utf-8') | ||
|
||
def test_single_string_custom_encoding(self): | ||
input_array = '\r' | ||
self.assertRaises(ValueError, encode_to_bytes, input_array, 'utf-8') | ||
|
||
def test_convert_cr_or_lf_list(self): | ||
input_array = [CR, LF] | ||
expected_output = [b'\r', b'\n'] | ||
self.assertEqual(encode_to_bytes(input_array), expected_output) | ||
|
||
def test_convert_cr_or_lf_bytes(self): | ||
input_array = b'\r\n' | ||
self.assertRaises(ValueError, encode_to_bytes, input_array) | ||
|
||
def test_convert_cr_or_lf_string(self): | ||
input_array = '\r\n' | ||
self.assertRaises(ValueError, encode_to_bytes, input_array) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import os | ||
|
||
def DebugMode() -> bool: | ||
if os.getenv('DEBUG', 'False').lower() in ('true', '1', 't'): | ||
return True | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import os | ||
from typing import List, Callable | ||
|
||
|
||
def get_file_from_directory(file_name: str, dir: str) -> str|None: | ||
file_path = os.path.join(dir, file_name) | ||
if os.path.isfile(file_path): | ||
return file_path | ||
return None | ||
|
||
|
||
def get_project_base_path() -> str: | ||
return os.path.abspath(os.path.dirname(__file__)) | ||
|
||
|
||
def get_user_home_directory() -> str: | ||
return os.path.expanduser('~') | ||
|
||
|
||
def find_file(file_name: str, search_functions: List[Callable[[], str|None]]) -> str|None: | ||
for func in search_functions: | ||
directory = func() | ||
file_path = os.path.join(directory, file_name) | ||
if os.path.isfile(file_path): | ||
return file_path | ||
return None | ||
|
||
|
||
default_search_functions = [ | ||
os.getcwd, | ||
get_project_base_path, | ||
get_user_home_directory | ||
] | ||
|
||
def config_search(file_name: str) -> str: | ||
config_path = find_file(file_name, default_search_functions) | ||
return config_path | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.