Skip to content

Commit 88c7a91

Browse files
authored
Merge pull request #16 from Code-Partners/feat-conn-string-builders
Feat conn string builders
2 parents e68995b + e180f9b commit 88c7a91

19 files changed

+373
-17
lines changed

common/file_rotate.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ class FileRotate(Enum):
77
DAILY = 2
88
WEEKLY = 3
99
MONTHLY = 4
10+
11+
def __str__(self):
12+
return "%s" % self._name_

common/lookup_table.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ def clear(self):
105105
def get_count(self):
106106
return len(self.__items)
107107

108-
def __is_valid_integer(self, value: str) -> bool:
109-
if self.__value_is_valid(value):
108+
@classmethod
109+
def __is_valid_integer(cls, value: str) -> bool:
110+
if cls.__value_is_valid(value):
110111
try:
111112
int(value)
112113
return True
@@ -145,30 +146,31 @@ def get_size_value(self, key: str, default_value: int) -> int:
145146

146147
return result
147148

148-
def size_to_int(self, value: str, default_value: int) -> int:
149+
@classmethod
150+
def size_to_int(cls, value: str, default_value: int) -> int:
149151
if not isinstance(value, str):
150152
raise TypeError("Value must be a string")
151153
if not isinstance(default_value, int):
152154
raise TypeError("Default value must be an int")
153155

154156
result = default_value
155-
factor = self.__KB_FACTOR
157+
factor = cls.__KB_FACTOR
156158
value = value.strip()
157159

158160
if len(value) >= 2:
159161
unit = value[-2:].lower()
160162

161-
if self.__is_valid_size_unit(unit):
163+
if cls.__is_valid_size_unit(unit):
162164
value = value[:-2].strip()
163165

164166
if unit == "kb":
165-
factor = self.__KB_FACTOR
167+
factor = cls.__KB_FACTOR
166168
elif unit == "mb":
167-
factor = self.__MB_FACTOR
169+
factor = cls.__MB_FACTOR
168170
elif unit == "gb":
169-
factor = self.__GB_FACTOR
171+
factor = cls.__GB_FACTOR
170172

171-
if self.__is_valid_integer(value):
173+
if cls.__is_valid_integer(value):
172174
try:
173175
result = factor * int(value)
174176
except ValueError:

connections/builders/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from connections.builders.connections_builder import ConnectionsBuilder
1+
from connections.builders.connection_string_builder import ConnectionStringBuilder
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import collections
2+
from typing import TYPE_CHECKING, TypeVar
3+
4+
from common.file_rotate import FileRotate
5+
from common.lookup_table import LookupTable
6+
from protocols.cloud.cloud_protocol import CloudProtocol
7+
8+
if TYPE_CHECKING:
9+
from connection_string_builder import ConnectionStringBuilder
10+
11+
from connections.builders.tcp_protocol_connection_string_builder import TcpProtocolConnectionStringBuilder
12+
13+
Self = TypeVar("Self", bound="CloudProtocolConnectionStringBuilder")
14+
15+
16+
class CloudProtocolConnectionStringBuilder(TcpProtocolConnectionStringBuilder):
17+
_custom_labels: collections.OrderedDict = collections.OrderedDict()
18+
19+
def __init__(self, parent: "ConnectionStringBuilder"):
20+
super().__init__(parent)
21+
22+
def end_protocol(self) -> "ConnectionStringBuilder":
23+
self._parent.cb.add_option("customlabels", CloudProtocol.compose_custom_labels_string(self._custom_labels))
24+
return super().end_protocol()
25+
26+
def set_write_key(self, write_key: str) -> Self:
27+
self._parent.cb.add_option("writekey", write_key)
28+
return self
29+
30+
def add_custom_label(self, key: str, value: str) -> Self:
31+
self._custom_labels[key] = value
32+
return self
33+
34+
def set_region(self, region: str) -> Self:
35+
self._parent.cb.add_option("region", region)
36+
return self
37+
38+
def set_chunking_enabled(self, enabled: bool) -> Self:
39+
self._parent.cb.add_option("chunking.enabled", enabled)
40+
return self
41+
42+
def set_chunking_max_size(self, max_size: str) -> Self:
43+
size_in_bytes = LookupTable.size_to_int(max_size, 0)
44+
self._parent.cb.add_option("chunking.maxsize", int(size_in_bytes / 1024))
45+
return self
46+
47+
def set_chunking_max_age_ms(self, max_age: int) -> Self:
48+
self._parent.cb.add_option("chunking.maxagems", max_age)
49+
return self
50+
51+
def set_max_size(self, max_size: str) -> Self:
52+
size_in_bytes = LookupTable.size_to_int(max_size, 0)
53+
self._parent.cb.add_option("maxsize", int(size_in_bytes / 1024))
54+
return self
55+
56+
def set_rotate(self, rotate: FileRotate) -> Self:
57+
self._parent.cb.add_option("rotate", rotate)
58+
return self
59+
60+
def set_tls_enabled(self, tls_enabled: bool) -> Self:
61+
self._parent.cb.add_option("tls.enabled", tls_enabled)
62+
return self
63+
64+
def set_tls_certificate_location(self, tls_certificate_location: str) -> Self:
65+
self._parent.cb.add_option("tls.certificate.location", tls_certificate_location)
66+
return self
67+
68+
def set_tls_certificate_filepath(self, tls_certificate_filepath: str) -> Self:
69+
self._parent.cb.add_option("tls.certificate.filepath", tls_certificate_filepath)
70+
return self
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from connections.builders.cloud_protocol_connection_string_builder import CloudProtocolConnectionStringBuilder
2+
from connections.builders.file_protocol_connection_string_builder import FileProtocolConnectionStringBuilder
3+
from connections.builders.memory_protocol_connection_string_builder import MemoryProtocolConnectionStringBuilder
4+
from connections.builders.pipe_protocol_connection_string_builder import PipeProtocolConnectionStringBuilder
5+
from connections.builders.tcp_protocol_connection_string_builder import TcpProtocolConnectionStringBuilder
6+
from connections.builders.text_protocol_connection_string_builder import TextProtocolConnectionStringBuilder
7+
from connections.connections_builder import ConnectionsBuilder
8+
9+
10+
class ConnectionStringBuilder:
11+
_protocols = {
12+
"pipe": PipeProtocolConnectionStringBuilder,
13+
"file": FileProtocolConnectionStringBuilder,
14+
"mem": MemoryProtocolConnectionStringBuilder,
15+
"tcp": TcpProtocolConnectionStringBuilder,
16+
"text": TextProtocolConnectionStringBuilder,
17+
"cloud": CloudProtocolConnectionStringBuilder,
18+
}
19+
20+
def __init__(self):
21+
self._cb = ConnectionsBuilder()
22+
23+
@property
24+
def cb(self):
25+
return self._cb
26+
27+
def _add_protocol(self, protocol_name: str):
28+
self._cb.begin_protocol(protocol_name)
29+
return self._protocols.get(protocol_name)(self)
30+
31+
def add_pipe_protocol(self) -> PipeProtocolConnectionStringBuilder:
32+
return self._add_protocol("pipe")
33+
34+
def add_file_protocol(self) -> FileProtocolConnectionStringBuilder:
35+
return self._add_protocol("file")
36+
37+
def add_memory_protocol(self) -> MemoryProtocolConnectionStringBuilder:
38+
return self._add_protocol("mem")
39+
40+
def add_tcp_protocol(self) -> TcpProtocolConnectionStringBuilder:
41+
return self._add_protocol("tcp")
42+
43+
def add_text_protocol(self) -> TextProtocolConnectionStringBuilder:
44+
return self._add_protocol("text")
45+
46+
def add_cloud_protocol(self) -> CloudProtocolConnectionStringBuilder:
47+
return self._add_protocol("cloud")
48+
49+
def build(self) -> str:
50+
return self._cb.get_connections()
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from typing import TYPE_CHECKING, TypeVar
2+
3+
from common.file_rotate import FileRotate
4+
from common.lookup_table import LookupTable
5+
6+
if TYPE_CHECKING:
7+
from connection_string_builder import ConnectionStringBuilder
8+
9+
from connections.builders.protocol_connection_string_builder import ProtocolConnectionStringBuilder
10+
11+
Self = TypeVar("Self", bound="FileProtocolConnectionStringBuilder")
12+
13+
14+
class FileProtocolConnectionStringBuilder(ProtocolConnectionStringBuilder):
15+
16+
def __init__(self, parent: "ConnectionStringBuilder"):
17+
super().__init__(parent)
18+
19+
def set_filename(self, filename: str) -> Self:
20+
self._parent.cb.add_option("filename", filename)
21+
return self
22+
23+
def set_append(self, append: bool) -> Self:
24+
self._parent.cb.add_option("append", append)
25+
return self
26+
27+
def set_buffer(self, buffer: str) -> Self:
28+
size_in_bytes = LookupTable.size_to_int(buffer, 0)
29+
self._parent.cb.add_option("buffer", int(size_in_bytes / 1024))
30+
return self
31+
32+
def set_rotate(self, rotate: FileRotate) -> Self:
33+
self._parent.cb.add_option("rotate", rotate)
34+
return self
35+
36+
def set_max_size(self, max_size: str) -> Self:
37+
size_in_bytes = LookupTable.size_to_int(max_size, 0)
38+
self._parent.cb.add_option("maxsize", int(size_in_bytes / 1024))
39+
return self
40+
41+
def set_max_parts(self, max_parts: int) -> Self:
42+
self._parent.cb.add_option("maxparts", max_parts)
43+
return self
44+
45+
def set_key(self, key: str) -> Self:
46+
self._parent.cb.add_option("key", key)
47+
return self
48+
49+
def set_encrypt(self, encrypt: bool) -> Self:
50+
self._parent.cb.add_option("encrypt", encrypt)
51+
return self
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from typing import TYPE_CHECKING, TypeVar
2+
3+
from common.lookup_table import LookupTable
4+
5+
if TYPE_CHECKING:
6+
from connection_string_builder import ConnectionStringBuilder
7+
8+
from connections.builders.protocol_connection_string_builder import ProtocolConnectionStringBuilder
9+
10+
Self = TypeVar("Self", bound="MemoryProtocolConnectionStringBuilder")
11+
12+
13+
class MemoryProtocolConnectionStringBuilder(ProtocolConnectionStringBuilder):
14+
def __init__(self, parent: "ConnectionStringBuilder"):
15+
super().__init__(parent)
16+
17+
def set_max_size(self, max_size: str) -> Self:
18+
size_in_bytes = LookupTable.size_to_int(max_size, 0)
19+
self._parent.cb.add_option("maxsize", int(size_in_bytes / 1024))
20+
return self
21+
22+
def set_as_text(self, as_text: bool) -> Self:
23+
self._parent.cb.add_option("astext", as_text)
24+
return self
25+
26+
def set_indent(self, indent: bool) -> Self:
27+
self._parent.cb.add_option("indent", indent)
28+
return self
29+
30+
def set_pattern(self, pattern: str) -> Self:
31+
self._parent.cb.add_option("pattern", pattern)
32+
return self
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import TYPE_CHECKING, TypeVar
2+
3+
if TYPE_CHECKING:
4+
from connection_string_builder import ConnectionStringBuilder
5+
6+
from connections.builders.protocol_connection_string_builder import ProtocolConnectionStringBuilder
7+
8+
Self = TypeVar("Self", bound="PipeProtocolConnectionStringBuilder")
9+
10+
11+
class PipeProtocolConnectionStringBuilder(ProtocolConnectionStringBuilder):
12+
def __init__(self, parent: "ConnectionStringBuilder"):
13+
super().__init__(parent)
14+
15+
def set_pipe_name(self, pipe_name: str) -> Self:
16+
self._parent.cb.add_option("pipename", pipe_name)
17+
return self
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from typing import TYPE_CHECKING, TypeVar
2+
3+
if TYPE_CHECKING:
4+
from connection_string_builder import ConnectionStringBuilder
5+
6+
from common.level import Level
7+
from common.lookup_table import LookupTable
8+
9+
Self = TypeVar("Self", bound="ProtocolConnectionStringBuilder")
10+
11+
12+
class ProtocolConnectionStringBuilder:
13+
def __init__(self, parent: "ConnectionStringBuilder"):
14+
self._parent = parent
15+
16+
def end_protocol(self) -> "ConnectionStringBuilder":
17+
self._parent.cb.end_protocol()
18+
return self._parent
19+
20+
def set_level(self, level: Level) -> Self:
21+
self._parent.cb.add_option("level", level)
22+
return self
23+
24+
def set_caption(self, caption: str) -> Self:
25+
self._parent.cb.add_option("caption", caption)
26+
return self
27+
28+
def set_reconnect(self, reconnect: bool) -> Self:
29+
self._parent.cb.add_option("reconnect", reconnect)
30+
return self
31+
32+
def set_reconnect_interval(self, reconnect_interval: int) -> Self:
33+
self._parent.cb.add_option("reconnect.interval", reconnect_interval)
34+
return self
35+
36+
def set_backlog_enabled(self, backlog_enabled: bool) -> Self:
37+
self._parent.cb.add_option("backlog.enabled", backlog_enabled)
38+
return self
39+
40+
def set_backlog_queue(self, backlog_queue: str) -> Self:
41+
size_in_bytes = LookupTable.size_to_int(backlog_queue, 0)
42+
self._parent.cb.add_option("backlog.queue", int(size_in_bytes / 1024))
43+
return self
44+
45+
def set_backlog_flushon(self, backlog_flushon: Level) -> Self:
46+
self._parent.cb.add_option("backlog.flushon", backlog_flushon)
47+
return self
48+
49+
def set_backlog_keepopen(self, backlog_keepopen: bool) -> Self:
50+
self._parent.cb.add_option("backlog.keepopen", backlog_keepopen)
51+
return self
52+
53+
def set_async_enabled(self, async_enabled: bool) -> Self:
54+
self._parent.cb.add_option("async.enabled", async_enabled)
55+
return self
56+
57+
def set_async_throttle(self, async_throttle: bool) -> Self:
58+
self._parent.cb.add_option("async.throttle", async_throttle)
59+
return self
60+
61+
def set_async_queue(self, async_queue: str) -> Self:
62+
size_in_bytes = LookupTable.size_to_int(async_queue, 0)
63+
self._parent.cb.add_option("async.queue", int(size_in_bytes / 1024))
64+
return self
65+
66+
def set_async_clear_on_disconnect(self, async_clear_on_disconnect: bool) -> Self:
67+
self._parent.cb.add_option("async.clearondisconnect", async_clear_on_disconnect)
68+
return self
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from typing import TYPE_CHECKING, TypeVar
2+
3+
if TYPE_CHECKING:
4+
from connection_string_builder import ConnectionStringBuilder
5+
6+
from connections.builders.protocol_connection_string_builder import ProtocolConnectionStringBuilder
7+
8+
Self = TypeVar("Self", bound="TcpProtocolConnectionStringBuilder")
9+
10+
11+
class TcpProtocolConnectionStringBuilder(ProtocolConnectionStringBuilder):
12+
def __init__(self, parent: "ConnectionStringBuilder"):
13+
super().__init__(parent)
14+
15+
def set_host(self, host: str) -> Self:
16+
self._parent.cb.add_option("host", host)
17+
return self
18+
19+
def set_port(self, port: int) -> Self:
20+
self._parent.cb.add_option("port", port)
21+
return self
22+
23+
def set_timeout(self, timeout: int) -> Self:
24+
self._parent.cb.add_option("timeout", timeout)
25+
return self

0 commit comments

Comments
 (0)