reach_commons 0.18.42__py3-none-any.whl → 0.18.43__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,30 @@ from dataclasses import dataclass, field
2
2
  from math import ceil
3
3
  from typing import Union
4
4
 
5
+ # Basic GSM-7 char set (without extension table handling).
6
+ # If you want 100% accuracy for GSM-7, we can add the extension table later.
7
+ GSM7_BASIC = set(
8
+ "@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞ"
9
+ "ÆæßÉ !\"#¤%&'()*+,-./"
10
+ "0123456789:;<=>?"
11
+ "¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ"
12
+ "¿abcdefghijklmnopqrstuvwxyzäöñüà"
13
+ )
14
+
15
+
16
+ def is_gsm7(text: str) -> bool:
17
+ return all(ch in GSM7_BASIC for ch in text)
18
+
19
+
20
+ def ucs2_units(text: str) -> int:
21
+ # UTF-16 code units: emoji counts as 2 (surrogate pair), which matches UCS-2 segment math used in practice.
22
+ return len(text.encode("utf-16-be")) // 2
23
+
24
+
25
+ def fix_surrogates(text: str) -> str:
26
+ # Turns surrogate pairs like \uD83D\uDC96 into the real character (💖)
27
+ return text.encode("utf-16", "surrogatepass").decode("utf-16")
28
+
5
29
 
6
30
  @dataclass
7
31
  class MessageSmartEncoding:
@@ -31,15 +55,25 @@ class MessageSmartEncoding:
31
55
  segments: int = field(init=False)
32
56
 
33
57
  def __post_init__(self):
34
- # Convert body to string if it's a list
35
- if isinstance(self.body, list):
36
- body_text = " ".join(str(item) for item in self.body)
37
- else:
38
- body_text = self.body
58
+ body_text = (
59
+ " ".join(map(str, self.body))
60
+ if isinstance(self.body, list)
61
+ else str(self.body)
62
+ )
63
+ self.normalized_text = fix_surrogates(self.normalize(body_text))
39
64
 
40
- self.normalized_text = self.normalize(body_text)
41
- self.length = len(self.normalized_text)
42
- self.segments = ceil(self.length / 160)
65
+ if is_gsm7(self.normalized_text):
66
+ self.encoding = "GSM-7"
67
+ text_length = len(self.normalized_text)
68
+ self.length = text_length
69
+ per_segment = 160 if text_length <= 160 else 153
70
+ self.segments = ceil(text_length / per_segment)
71
+ else:
72
+ self.encoding = "UCS-2"
73
+ text_units_count = ucs2_units(self.normalized_text)
74
+ self.length = text_units_count
75
+ per_segment = 70 if text_units_count <= 70 else 67
76
+ self.segments = ceil(text_units_count / per_segment)
43
77
 
44
78
  def normalize(self, text: str) -> str:
45
79
  for char, replacement in self.replacements.items():
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: reach_commons
3
- Version: 0.18.42
3
+ Version: 0.18.43
4
4
  Summary: Reach Commons is a versatile utility library designed to streamline and enhance development workflows within the Reach ecosystem.
5
5
  License: MIT
6
6
  Author: Engineering
@@ -27,9 +27,9 @@ reach_commons/reach_aws/s3.py,sha256=2MLlDNFx0SROJBpE_KjJefyrB7lMqTlrYuRhSZx4iKs
27
27
  reach_commons/reach_aws/sqs.py,sha256=IKKWrd-qbhMMVYUvGbaq1ouVRdx-0u-SqwYaTcp0tWY,21645
28
28
  reach_commons/reach_base_model.py,sha256=vgdGDcZr3iXMmyRhmBhOf_LKWB_6QzT3r_Yiyo6OmEk,3009
29
29
  reach_commons/redis_manager.py,sha256=yRed53ZKlbIb6rZnL53D1F_aB-xWT3nbeUP2cqYzhoc,3668
30
- reach_commons/sms_smart_encoding.py,sha256=92y0RmZ0l4ONHpC9qeO5KfViSNq64yE2rc7lhNDSZqE,1241
30
+ reach_commons/sms_smart_encoding.py,sha256=Hpc9tIgTJM0ZKdhloSjgIPQCi52gDAFEOKIoO2rtJ30,2537
31
31
  reach_commons/utils.py,sha256=dMgKIGqTgoSItuBI8oz81gKtW3qi21Jkljv9leS_V88,8475
32
32
  reach_commons/validations.py,sha256=x_lkrtlrCAJC_f7mZb19JjfKFbYlPFv-P84K_lbZyYs,1056
33
- reach_commons-0.18.42.dist-info/METADATA,sha256=GcTKPeC_hnviyGqRmrpxr2VsoTr6SBdYwpPSQsH7MdQ,1863
34
- reach_commons-0.18.42.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
35
- reach_commons-0.18.42.dist-info/RECORD,,
33
+ reach_commons-0.18.43.dist-info/METADATA,sha256=BY3z5YCwX2Koe9hUbM4vrG6I0cID8zGUz9DkPm2dNT0,1863
34
+ reach_commons-0.18.43.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
35
+ reach_commons-0.18.43.dist-info/RECORD,,