threefive3 3.0.1__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.
scte35/__init__.py ADDED
@@ -0,0 +1,27 @@
1
+ """
2
+ scte35.__init__.py
3
+ """
4
+
5
+ from .spare import print2
6
+ from .cue import Cue
7
+ from .section import SpliceInfoSection
8
+ from .segment import Segment
9
+ from .stream import Stream
10
+ from .version import version
11
+
12
+ from .commands import (
13
+ TimeSignal,
14
+ SpliceInsert,
15
+ SpliceNull,
16
+ PrivateCommand,
17
+ BandwidthReservation,
18
+ )
19
+
20
+ from .descriptors import (
21
+ AvailDescriptor,
22
+ DVBDASDescriptor,
23
+ DtmfDescriptor,
24
+ SegmentationDescriptor,
25
+ SpliceDescriptor,
26
+ TimeDescriptor,
27
+ )
scte35/base.py ADDED
@@ -0,0 +1,189 @@
1
+ """
2
+ scte35.base contains
3
+ the class SCTE35Base.
4
+ """
5
+
6
+ import json
7
+ from .bitn import NBin
8
+ from .spare import print2
9
+ from .xml import Node
10
+
11
+
12
+ class SCTE35Base:
13
+ """
14
+ SCTE35Base is a base class for
15
+ SpliceCommand and SpliceDescriptor classes
16
+ """
17
+
18
+ ROLLOVER = 8589934591
19
+
20
+ def __repr__(self):
21
+ return str(self.__dict__)
22
+
23
+ @staticmethod
24
+ def _chk_nbin(nbin):
25
+ if not nbin:
26
+ nbin = NBin()
27
+ return nbin
28
+
29
+ def _err2(self, var_name, var_value, bit_count, var_type):
30
+ err_mesg = f"\033[1;41m !! \033[0m \033[1;41m {var_name}\033[0m is \033[1;41m {var_value} \033[0m, it should be type {var_type} of {bit_count} bit(s)\n "
31
+ print2(err_mesg)
32
+
33
+ def _bool_int(self, var_name, var_value, bit_count, var_type):
34
+ if var_type == int:
35
+ if isinstance(var_value, bool):
36
+ self._err2(var_name, var_value, bit_count, var_type)
37
+ return True
38
+
39
+ def _wrong_type(self, var_name, var_value, bit_count, var_type):
40
+ if not isinstance(var_value, var_type):
41
+ self._err2(var_name, var_value, bit_count, var_type)
42
+ return True
43
+
44
+ def _is_none(self, var_name, var_value, bit_count, var_type):
45
+ if var_value is None:
46
+ self._err2(var_name, var_value, bit_count, var_type)
47
+ return True
48
+
49
+ def _chk_var(self, var_type, nbin_method, var_name, bit_count):
50
+ """
51
+ _chk_var is used to check var values and types before encoding
52
+ """
53
+ var_value = self.__dict__[var_name]
54
+ for me in [self._is_none, self._bool_int, self._wrong_type]:
55
+ if me(var_name, var_value, bit_count, var_type):
56
+ return
57
+ nbin_method(var_value, bit_count)
58
+
59
+ @staticmethod
60
+ def as_90k(int_time):
61
+ """
62
+ ticks to 90k timestamps
63
+ """
64
+ return round((int_time / 90000.0), 6)
65
+
66
+ @staticmethod
67
+ def as_ticks(float_time):
68
+ """
69
+ 90k timestamps to ticks
70
+ """
71
+ return int(round(float_time * 90000))
72
+
73
+ @staticmethod
74
+ def as_hms(secs_of_time):
75
+ """
76
+ as_hms converts timestamp to
77
+ 00:00:00.000 format
78
+ """
79
+ hours, seconds = divmod(secs_of_time, 3600)
80
+ mins, seconds = divmod(seconds, 60)
81
+ seconds = round(seconds, 3)
82
+ output = f"{int(hours):02}:{int(mins):02}:{seconds:02}"
83
+ if len(output.split(".")[1]) < 2:
84
+ output += "0"
85
+ return output
86
+
87
+ @staticmethod
88
+ def fix_hex(hexed):
89
+ """
90
+ fix_hex adds padded zero if needed for byte conversion.
91
+ """
92
+ return (hexed.replace("0x", "0x0", 1), hexed)[len(hexed) % 2 == 0]
93
+
94
+ def get(self):
95
+ """
96
+ Returns instance as a kv_clean'ed dict
97
+ """
98
+ return self.kv_clean()
99
+
100
+ def has(self, what):
101
+ """
102
+ has runs hasattr with self and what
103
+ """
104
+ if hasattr(self, what):
105
+ return True
106
+ return False
107
+
108
+ @staticmethod
109
+ def idxsplit(gonzo, sep):
110
+ """
111
+ idxsplit is like split but you keep
112
+ the sep
113
+ example:
114
+ >>> idxsplit('123456789',4)
115
+ >>>'456789'
116
+ """
117
+ if sep in gonzo:
118
+ return gonzo[gonzo.index(sep) :]
119
+ return False
120
+
121
+ def json(self):
122
+ """
123
+ json returns self as kv_clean'ed json
124
+ """
125
+ return json.dumps(self.get(), indent=4)
126
+
127
+ def kv_clean(self):
128
+ """
129
+ kv_clean recursively removes items
130
+ from a dict if the value is None.
131
+ """
132
+
133
+ def b2l(val):
134
+ if isinstance(val, (SCTE35Base)):
135
+ val.kv_clean()
136
+ if isinstance(val, (list)):
137
+ val = [b2l(v) for v in val]
138
+ if isinstance(val, (dict)):
139
+ val = {k: b2l(v) for k, v in val.items()}
140
+ if isinstance(val, (bytes, bytearray)):
141
+ val = list(val)
142
+ return val
143
+
144
+ return {k: b2l(v) for k, v in vars(self).items() if v is not None}
145
+
146
+ def _load_str(self, gonzo):
147
+ if isinstance(gonzo, str):
148
+ gonzo = json.loads(gonzo)
149
+ return gonzo
150
+
151
+ def _chk_prevars(self, k, v, prevars):
152
+ if k in prevars:
153
+ self.__dict__[k] = v
154
+
155
+ def _vrfy_load(self, gonzo, prevars):
156
+ for k, v in gonzo.items():
157
+ self._chk_prevars(k, v, prevars)
158
+
159
+ def _load_dict(self, gonzo):
160
+ if isinstance(gonzo, dict):
161
+ prevars = vars(self)
162
+ self._vrfy_load(gonzo, prevars)
163
+
164
+ def load(self, gonzo):
165
+ """
166
+ load is used to load
167
+ data from a dict or json string.
168
+ only updates vars that exist in the obj.
169
+ """
170
+ gonzo = self._load_str(gonzo)
171
+ self._load_dict(gonzo)
172
+
173
+ def show(self):
174
+ """
175
+ show prints self as json to stderr (2)
176
+ """
177
+ print2(self.json())
178
+
179
+ def xml(self, ns="scte35"):
180
+ """
181
+ xml default xml method will return
182
+ all attributes, which is expressly allowed
183
+ for SCTE-35 xml.
184
+ """
185
+ xml_attrs = self.kv_clean()
186
+ iam = type(self)
187
+ iam = str(iam).split("'", 1)[1].split("'", 1)[0]
188
+ this = Node(iam, attrs=xml_attrs, ns=ns)
189
+ return this
scte35/bitn.py ADDED
@@ -0,0 +1,193 @@
1
+ """
2
+ The bitn.BitBin and bitn.NBin classes
3
+ """
4
+
5
+ from .spare import print2
6
+
7
+
8
+ class BitBin:
9
+ """
10
+ bitn.Bitbin takes a byte string and
11
+ converts it to a integer, a very large integer
12
+ if needed. A 1500 bit integer is no problem.
13
+ several methods are available for slicing off bits.
14
+ """
15
+
16
+ def __init__(self, bites):
17
+ # self.bites = bites
18
+ self.bitsize = self.idx = len(bites) << 3
19
+ self.bits = int.from_bytes(bites, byteorder="big")
20
+
21
+ def as_90k(self, num_bits):
22
+ """
23
+ Returns num_bits
24
+ of bits as 90k time
25
+ """
26
+ ninetyk = self.as_int(num_bits) / 90000.0
27
+ return round(ninetyk, 6)
28
+
29
+ def as_int(self, num_bits):
30
+ """
31
+ Starting at self.idx of self.bits,
32
+ slice off num_bits of bits.
33
+ """
34
+ if self.idx >= num_bits:
35
+ self.idx -= num_bits
36
+ return (self.bits >> (self.idx)) & ~(~0 << num_bits)
37
+ return False
38
+
39
+ def as_hex(self, num_bits):
40
+ """
41
+ Returns the hex value
42
+ of num_bits of bits
43
+ """
44
+ hexed = hex(self.as_int(num_bits))
45
+ return (hexed.replace("0x", "0x0", 1), hexed)[len(hexed) % 2 == 0]
46
+
47
+ def as_charset(self, num_bits, charset="ascii"):
48
+ """
49
+ Returns num_bits of bits
50
+ as bytes decoded as charset
51
+ default charset is ascii.
52
+ """
53
+ # print(charset)
54
+ gonzo = self.as_int(num_bits)
55
+ wide = num_bits >> 3
56
+ if charset is None:
57
+ return int.to_bytes(gonzo, wide, byteorder="big")
58
+ return int.to_bytes(gonzo, wide, byteorder="big").decode(
59
+ charset, errors="replace"
60
+ )
61
+
62
+ def as_bytes(self, num_bits):
63
+ """
64
+ Returns num_bits of bits
65
+ as bytes
66
+ """
67
+ gonzo = self.as_int(num_bits)
68
+ wide = num_bits >> 3
69
+ return int.to_bytes(gonzo, wide, byteorder="big")
70
+
71
+ def as_flag(self, num_bits=1):
72
+ """
73
+ Returns one bit as True or False
74
+ """
75
+ return self.as_int(num_bits) & 1 == 1
76
+
77
+ def forward(self, num_bits):
78
+ """
79
+ Advances the start point
80
+ forward by num_bits
81
+ """
82
+ self.idx -= num_bits
83
+
84
+ def negative_shift(self, num_bits):
85
+ """
86
+ negative_shift is called instead of
87
+ throwing a negative shift count error.
88
+ """
89
+ print2(f"{num_bits} bits requested, but only {self.idx} bits left.")
90
+ print2(f"\n bytes remaining: {self.as_bytes(self.idx)} ")
91
+
92
+
93
+ class NBin:
94
+ """
95
+ bitn.NBin is
96
+ the reverse BitBin.
97
+ Encodes data to integers
98
+ and then bytes
99
+ """
100
+
101
+ def __init__(self):
102
+ self.nbits = 0
103
+ self.idx = 0
104
+ self.bites = b""
105
+
106
+ def nbits2bites(self):
107
+ """
108
+ nbits2bites converts
109
+ the int self.nbits to bytes as self.bites
110
+ and sets self.nbits and self.idx to 0
111
+ """
112
+ bites_wide = self.idx >> 3
113
+ self.bites += int.to_bytes(self.nbits, bites_wide, byteorder="big")
114
+ self.nbits = 0
115
+ self.idx = 0
116
+
117
+ def add_bites(self, plus_bites):
118
+ """
119
+ add_bites appends plus_bites
120
+ to self.bites
121
+ """
122
+ if isinstance(plus_bites, int):
123
+ plus_bites = bytes.fromhex(hex(plus_bites)[2:])
124
+ self.bites += plus_bites
125
+
126
+ # if self.idx % 8 == 0:
127
+ # self.nbits2bites()
128
+
129
+ def add_int(self, int_bits, bit_len):
130
+ """
131
+ left shift nbits and append new_bits
132
+ """
133
+ self.idx += bit_len
134
+ self.nbits = (self.nbits << bit_len) | int_bits
135
+ if self.idx % 8 == 0:
136
+ self.nbits2bites()
137
+
138
+ def add_90k(self, pts, bit_len=33):
139
+ """
140
+ Converts 90k float timestamps
141
+ to an int and appends it to nbits
142
+ via self.add_int
143
+ """
144
+ ninetyk = int(pts * 90000.0)
145
+ self.add_int(ninetyk, bit_len)
146
+
147
+ def add_hex(self, hex_str, bit_len):
148
+ """
149
+ add_hex converts a
150
+ hex encoded string to an int
151
+ and appends it to self.nbits
152
+ via self.add_int
153
+ """
154
+ if isinstance(hex_str, str):
155
+ dehexed = int(hex_str, 16)
156
+ # just in case hex_str is an int....
157
+ else:
158
+ dehexed = hex_str
159
+ self.add_int(dehexed, bit_len)
160
+
161
+ def add_flag(self, flg, bit_len=1):
162
+ """
163
+ add_flag takes a boolean
164
+ value and adds it as an integer
165
+ to self.nbits via self.add_int
166
+ """
167
+ bit_len = 1
168
+ self.add_int(flg.real, bit_len)
169
+
170
+ def reserve(self, num):
171
+ """
172
+ reserve sets 'num' bits to 1
173
+ and appends them to self.nbits
174
+ via self.add_int
175
+ """
176
+ bit_len = 1
177
+ while num:
178
+ self.add_int(1, bit_len)
179
+ num -= 1
180
+
181
+ def forward(self, num):
182
+ """
183
+ Currently just an alias to reserve
184
+ """
185
+ self.reserve(num)
186
+
187
+ def zeroed(self, num):
188
+ """
189
+ zeroed sets num bits to zero
190
+ """
191
+ bit_len = 1
192
+ while num:
193
+ self.add_int(0, bit_len)