Skip to content

Commit f8e717e

Browse files
author
d.kovalenko
committed
Initial import
Direct copy from testgres 34cc7d1d with minimal changes.
1 parent 29c4371 commit f8e717e

19 files changed

+3148
-0
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.pyc
2+
*.egg
3+
*.egg-info/
4+
.eggs/
5+
.pytest_cache/
6+
.vscode/
7+
__pycache__/
8+
env/
9+
venv/

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# testgres - os_ops
2+
3+
Subsystem of testgres to work with OS.
4+
5+
## Authors
6+
7+
[Postgres Professional](https://postgrespro.ru/about)

__init__.py

Whitespace-only changes.

setup.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[metadata]
2+
description-file = README.md
3+
4+
[flake8]
5+
ignore = E501
6+
exclude = .git,__pycache__,env,venv,testgres/__init__.py

setup.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
try:
2+
from setuptools import setup
3+
except ImportError:
4+
from distutils.core import setup
5+
6+
setup(
7+
version="0.0.1",
8+
name="testgres.os_ops",
9+
packages=[
10+
"testgres.operations",
11+
],
12+
package_dir={"testgres.operations": "src"},
13+
description='Testgres subsystem to work with OS',
14+
url='https://github.com/postgrespro/testgres.os_ops',
15+
long_description_content_type='text/markdown',
16+
license='PostgreSQL',
17+
author='Postgres Professional',
18+
author_email='testgres@postgrespro.ru',
19+
keywords=['testgres'],
20+
)

src/__init__.py

Whitespace-only changes.

src/exceptions.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# coding: utf-8
2+
3+
import six
4+
5+
6+
class TestgresException(Exception):
7+
pass
8+
9+
10+
class ExecUtilException(TestgresException):
11+
def __init__(self, message=None, command=None, exit_code=0, out=None, error=None):
12+
super(ExecUtilException, self).__init__(message)
13+
14+
self.message = message
15+
self.command = command
16+
self.exit_code = exit_code
17+
self.out = out
18+
self.error = error
19+
20+
def __str__(self):
21+
msg = []
22+
23+
if self.message:
24+
msg.append(self.message)
25+
26+
if self.command:
27+
command_s = ' '.join(self.command) if isinstance(self.command, list) else self.command,
28+
msg.append(u'Command: {}'.format(command_s))
29+
30+
if self.exit_code:
31+
msg.append(u'Exit code: {}'.format(self.exit_code))
32+
33+
if self.error:
34+
msg.append(u'---- Error:\n{}'.format(self.error))
35+
36+
if self.out:
37+
msg.append(u'---- Out:\n{}'.format(self.out))
38+
39+
return self.convert_and_join(msg)
40+
41+
@staticmethod
42+
def convert_and_join(msg_list):
43+
# Convert each byte element in the list to str
44+
str_list = [six.text_type(item, 'utf-8') if isinstance(item, bytes) else six.text_type(item) for item in
45+
msg_list]
46+
47+
# Join the list into a single string with the specified delimiter
48+
return six.text_type('\n').join(str_list)
49+
50+
51+
class InvalidOperationException(TestgresException):
52+
pass

src/helpers.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import locale
2+
3+
4+
class Helpers:
5+
@staticmethod
6+
def _make_get_default_encoding_func():
7+
# locale.getencoding is added in Python 3.11
8+
if hasattr(locale, 'getencoding'):
9+
return locale.getencoding
10+
11+
# It must exist
12+
return locale.getpreferredencoding
13+
14+
# Prepared pointer on function to get a name of system codepage
15+
_get_default_encoding_func = _make_get_default_encoding_func.__func__()
16+
17+
@staticmethod
18+
def GetDefaultEncoding():
19+
#
20+
# Original idea/source was:
21+
#
22+
# def os_ops.get_default_encoding():
23+
# if not hasattr(locale, 'getencoding'):
24+
# locale.getencoding = locale.getpreferredencoding
25+
# return locale.getencoding() or 'UTF-8'
26+
#
27+
28+
assert __class__._get_default_encoding_func is not None
29+
30+
r = __class__._get_default_encoding_func()
31+
32+
if r:
33+
assert r is not None
34+
assert type(r) == str # noqa: E721
35+
assert r != ""
36+
return r
37+
38+
# Is it an unexpected situation?
39+
return 'UTF-8'
40+
41+
@staticmethod
42+
def PrepareProcessInput(input, encoding):
43+
if not input:
44+
return None
45+
46+
if type(input) == str: # noqa: E721
47+
if encoding is None:
48+
return input.encode(__class__.GetDefaultEncoding())
49+
50+
assert type(encoding) == str # noqa: E721
51+
return input.encode(encoding)
52+
53+
# It is expected!
54+
assert type(input) == bytes # noqa: E721
55+
return input

0 commit comments

Comments
 (0)