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/upids.py ADDED
@@ -0,0 +1,403 @@
1
+ """
2
+ scte35/upids.py
3
+
4
+ classy Upids
5
+
6
+ """
7
+
8
+ from .bitn import BitBin, NBin
9
+ from .xml import Node
10
+
11
+ charset = "ascii" # this isn't a constant pylint.
12
+
13
+ """
14
+ set charset to None to return raw bytes
15
+ """
16
+
17
+
18
+ class Upid:
19
+ """
20
+ Upid base class handles URI UPIDS
21
+ """
22
+
23
+ def __init__(self, bitbin=None, upid_type=0, upid_length=0):
24
+ self.bitbin = bitbin
25
+ self.upid_type = upid_type
26
+ self.upid_name = upid_map[upid_type][0]
27
+ self.upid_length = upid_length
28
+ self.bit_length = upid_length << 3
29
+ self.upid_value = None
30
+
31
+ def decode(self):
32
+ """
33
+ decode Upid
34
+ """
35
+ self.upid_value = self.bitbin.as_charset(self.bit_length, charset)
36
+ return self.upid_name, self.upid_value
37
+
38
+ def encode(self, nbin, seg_upid):
39
+ """
40
+ encode Upid
41
+ """
42
+ self.upid_value = seg_upid.encode()
43
+ nbin.add_bites(self.upid_value)
44
+
45
+ def redecode(self, seg_upid):
46
+ """
47
+ redecode is for decoding complex xml upids
48
+ before encodingto another format.
49
+ """
50
+ if isinstance(seg_upid, str):
51
+ try:
52
+ self.bitbin = BitBin(bytes.fromhex(seg_upid))
53
+ except ValueError:
54
+ self.bitbin = BitBin(seg_upid.encode())
55
+ return self.decode()
56
+ else:
57
+ self.upid_value = seg_upid
58
+ return self.upid_name, self.upid_value
59
+
60
+ def _xml_format_attr(self):
61
+ """
62
+ _xml_format_attr sets segmentation_upid_format
63
+ """
64
+ if self.upid_type in [0x03, 0x07, 0x09, 0x0E]:
65
+ return "text"
66
+ return "hexbinary"
67
+
68
+ def xml(self, ns="scte35"):
69
+ """
70
+ xml return a upid xml node
71
+ """
72
+ ud_attrs = {
73
+ "segmentation_upid_type": self.upid_type,
74
+ "segmentation_upid_format": self._xml_format_attr(),
75
+ }
76
+ return Node("SegmentationUpid", attrs=ud_attrs, value=self.upid_value, ns=ns)
77
+
78
+ def _extra_xml_attrs(self, ud):
79
+ if "format_identifier" in self.upid_value:
80
+ ud["format_identifier"] = int.from_bytes(
81
+ self.upid_value["format_identifier"].encode(), byteorder="big"
82
+ )
83
+ if "private_data" in self.upid_value:
84
+ ud["private_data"] = self.upid_value["private_data"]
85
+ return ud
86
+
87
+ def complexml(self, ns="scte35"):
88
+ """
89
+ complexml return a upid xml node for complex upids
90
+ """
91
+ ud_attrs = {
92
+ "segmentation_upid_type": self.upid_type,
93
+ "segmentation_upid_format": self._xml_format_attr(),
94
+ }
95
+ if isinstance(self.upid_value, dict):
96
+ ud_attrs = self._extra_xml_attrs(ud_attrs)
97
+ nbin = NBin()
98
+ self.encode(nbin, self.upid_value)
99
+ return Node("SegmentationUpid", attrs=ud_attrs, value=nbin.bites.hex(), ns=ns)
100
+
101
+
102
+ class NoUpid(Upid):
103
+ """
104
+ NoUpid class
105
+ """
106
+
107
+ def decode(self):
108
+ """
109
+ decode for no upid
110
+ """
111
+ return self.upid_name, "No UPID"
112
+
113
+ def encode(self, nbin, seg_upid):
114
+ """
115
+ encode for no upid
116
+ """
117
+ nbin.forward(0)
118
+
119
+ def xml(self, ns="scte35"):
120
+ """
121
+ xml return a upid xml node
122
+ """
123
+ ud_attrs = {
124
+ "segmentation_upid_type": 0x0,
125
+ "segmentation_upid_format": "hexbinary",
126
+ }
127
+ return Node("SegmentationUpid", attrs=ud_attrs, value="", ns=ns)
128
+
129
+
130
+ class AirId(Upid):
131
+ """
132
+ Air Id Upid
133
+ """
134
+
135
+ def decode(self):
136
+ """
137
+ decode AirId
138
+ """
139
+ self.upid_value = self.bitbin.as_hex(self.bit_length)
140
+ return self.upid_name, self.upid_value
141
+
142
+ def encode(self, nbin, seg_upid):
143
+ """
144
+ encode AirId
145
+ """
146
+ nbin.add_hex(seg_upid, (self.upid_length << 3))
147
+
148
+
149
+ class Atsc(Upid):
150
+ """
151
+ ATSC Upid
152
+ """
153
+
154
+ def decode(self):
155
+ """
156
+ decode Atsc Upid
157
+ """
158
+ cont_size = self.bit_length - 32
159
+ self.upid_value = {
160
+ "TSID": self.bitbin.as_int(16),
161
+ "reserved": self.bitbin.as_int(2),
162
+ "end_of_day": self.bitbin.as_int(5),
163
+ "unique_for": self.bitbin.as_int(9),
164
+ "content_id": self.bitbin.as_charset(cont_size, charset),
165
+ }
166
+ return self.upid_name, self.upid_value
167
+
168
+ def encode(self, nbin, seg_upid):
169
+ """
170
+ encode Atsc
171
+ """
172
+ _, self.upid_value = self.redecode(seg_upid)
173
+ nbin.add_int(self.upid_value["TSID"], 16)
174
+ nbin.add_int(self.upid_value["reserved"], 2)
175
+ nbin.add_int(self.upid_value["end_of_day"], 5)
176
+ nbin.add_int(self.upid_value["unique_for"], 9)
177
+ nbin.add_bites(self.upid_value["content_id"].encode("utf-8"))
178
+
179
+ def xml(self, ns="scte35"):
180
+ """
181
+ xml return xml node
182
+ """
183
+ return self.complexml(ns=ns)
184
+
185
+
186
+ class Eidr(Upid):
187
+ """
188
+ Eidr Upid
189
+ """
190
+
191
+ def decode(self):
192
+ """
193
+ decode Eidr Upid
194
+ """
195
+ pre = self.bitbin.as_hex(16)
196
+ post = []
197
+ # switch to compact binary format
198
+ nibbles = 20
199
+ while nibbles:
200
+ post.append(hex(self.bitbin.as_int(4))[2:])
201
+ nibbles -= 1
202
+ self.upid_value = f"{pre}{''.join(post)}"
203
+ return self.upid_name, self.upid_value
204
+
205
+ def encode(self, nbin, seg_upid):
206
+ """
207
+ encode Eidr Upid
208
+ """
209
+ _, self.upid_value = self.redecode(seg_upid)
210
+ # switch to compact binary format
211
+ nbin.add_hex(self.upid_value[:6], 16)
212
+ substring = self.upid_value[6:]
213
+ for i in substring:
214
+ hexed = f"0x{i}"
215
+ nbin.add_hex(hexed, 4)
216
+
217
+ def xml(self, ns="scte35"):
218
+ """
219
+ xml return xml node
220
+ """
221
+ return self.complexml(ns=ns)
222
+
223
+
224
+ class Isan(Upid):
225
+ """
226
+ Isan Upid
227
+ """
228
+
229
+ def decode(self):
230
+ """
231
+ decode Isan Upid
232
+ """
233
+ self.upid_value = self.bitbin.as_hex(self.bit_length)
234
+ return self.upid_name, self.upid_value
235
+
236
+ def encode(self, nbin, seg_upid):
237
+ """
238
+ encode Isan Upid
239
+ """
240
+ self.upid_value = seg_upid
241
+ nbin.add_hex(self.upid_value, (self.upid_length << 3))
242
+
243
+
244
+ class Mid(Upid):
245
+ """
246
+ Mid Upid
247
+ """
248
+
249
+ def decode(self):
250
+ """
251
+ decode Mid Upid
252
+ """
253
+ self.upid_value = []
254
+ ulb = self.bit_length
255
+ while ulb:
256
+ upid_type = self.bitbin.as_int(8) # 1 byte
257
+ ulb -= 8
258
+ upid_length = self.bitbin.as_int(8)
259
+ ulb -= 8
260
+ upid_type_name, segmentation_upid = upid_map[upid_type][1](
261
+ self.bitbin, upid_type, upid_length
262
+ ).decode()
263
+ mid_upid = {
264
+ "upid_type": upid_type,
265
+ "upid_type_name": upid_type_name,
266
+ "upid_length": upid_length,
267
+ "segmentation_upid": segmentation_upid,
268
+ }
269
+ ulb -= upid_length << 3
270
+ self.upid_value.append(mid_upid)
271
+ return self.upid_name, self.upid_value
272
+
273
+ def encode(self, nbin, seg_upid):
274
+ """
275
+ encode Mid Upid
276
+ """
277
+ for mid_upid in seg_upid:
278
+ nbin.add_int(mid_upid["upid_type"], 8)
279
+ nbin.add_int(mid_upid["upid_length"], 8)
280
+ the_upid = upid_map[mid_upid["upid_type"]][1](
281
+ None, mid_upid["upid_type"], mid_upid["upid_length"]
282
+ )
283
+ the_upid.encode(nbin, mid_upid["segmentation_upid"])
284
+
285
+ def xml(self, ns="scte35"):
286
+ """
287
+ xml return a upid xml node
288
+
289
+ """
290
+ mid_nodes = []
291
+ for u in self.upid_value:
292
+ u_attrs = {
293
+ "segmentation_upid_type": u["upid_type"],
294
+ "name": u["upid_type_name"],
295
+ }
296
+ value = u["segmentation_upid"]
297
+ node = Node("SegmentationUpid", attrs=u_attrs, value=value, ns=ns)
298
+ mid_nodes.append(node)
299
+ return mid_nodes
300
+
301
+
302
+ class Mpu(Upid):
303
+ """
304
+ Mpu Upid
305
+ """
306
+
307
+ def _decode_adfr(self):
308
+ """
309
+ decode_adfr handles Addressabkle TV MPU Upids
310
+ """
311
+ data = bytes.fromhex(self.upid_value["private_data"][2:])
312
+ self.upid_value["version"] = data[0]
313
+ self.upid_value["channel_identifier"] = hex(
314
+ int.from_bytes(data[1:3], byteorder="big")
315
+ )
316
+ self.upid_value["date"] = int.from_bytes(data[3:7], byteorder="big")
317
+ self.upid_value["break_code"] = int.from_bytes(data[7:9], byteorder="big")
318
+ self.upid_value["duration"] = hex(int.from_bytes(data[9:11], byteorder="big"))
319
+
320
+ def decode(self):
321
+ """
322
+ decode MPU Upids
323
+ """
324
+ self.upid_value = {
325
+ "format_identifier": self.bitbin.as_charset(32),
326
+ "private_data": self.bitbin.as_hex(self.bit_length - 32),
327
+ }
328
+ if self.upid_value["format_identifier"] == "ADFR":
329
+ self._decode_adfr()
330
+ return self.upid_name, self.upid_value
331
+
332
+ def encode(self, nbin, seg_upid):
333
+ """
334
+ encode MPU Upids
335
+ """
336
+ _, self.upid_value = self.redecode(seg_upid)
337
+ bit_len = self.bit_length
338
+ nbin.add_bites(bytes(self.upid_value["format_identifier"], "utf8"))
339
+ bit_len -= 32
340
+ nbin.add_hex(self.upid_value["private_data"], bit_len)
341
+
342
+ def xml(self, ns="scte35"):
343
+ """
344
+ xml return xml node
345
+ """
346
+ return self.complexml(ns=ns)
347
+
348
+
349
+ class Umid(Upid):
350
+ """
351
+ Umid Upid
352
+ """
353
+
354
+ def decode(self):
355
+ """
356
+ decode Umid Upids
357
+ """
358
+ chunks = []
359
+ ulb = self.bit_length
360
+ while ulb > 32:
361
+ chunks.append(self.bitbin.as_hex(32)[2:])
362
+ ulb -= 32
363
+ self.upid_value = ".".join(chunks)
364
+ return self.upid_name, self.upid_value
365
+
366
+ def encode(self, nbin, seg_upid):
367
+ """
368
+ encode Umid Upid
369
+ """
370
+ _, self.upid_value = self.redecode(seg_upid)
371
+ chunks = self.upid_value.split(".")
372
+ for chunk in chunks:
373
+ nbin.add_hex(chunk, 32)
374
+
375
+ def xml(self, ns="scte35"):
376
+ """
377
+ xml return xml node
378
+ """
379
+ return self.complexml(ns=ns)
380
+
381
+
382
+ # segmentation_upid_type : [name, class, length]
383
+ upid_map = {
384
+ 0x00: ["No UPID", NoUpid, 0],
385
+ 0x01: ["Type 0x01 is deprecated. Use type 0x0C, MPU.", Upid, False],
386
+ 0x02: ["Type 0x02 is deprecated. Use type 0x03, AdID.", Upid, 8],
387
+ 0x03: ["AdID", Upid, 12],
388
+ 0x04: ["UMID", Umid, 32],
389
+ 0x05: ["Type 0x05 is deprecated. Use type 0x06, ISAN.", Isan, 8],
390
+ 0x06: ["ISAN", Isan, 12],
391
+ 0x07: ["TID", Upid, 12],
392
+ 0x08: ["AiringID", AirId, 8],
393
+ 0x09: ["ADI", Upid, False],
394
+ 0x0A: ["EIDR", Eidr, 12],
395
+ 0x0B: ["ATSC", Atsc, False],
396
+ 0x0C: ["MPU", Mpu, False],
397
+ 0x0D: ["MID", Mid, False],
398
+ 0x0E: ["ADS Info", Upid, False],
399
+ 0x0F: ["URI", Upid, False],
400
+ 0x10: ["UUID", Upid, 16],
401
+ 0x11: ["SCR", Upid, False],
402
+ 0xFD: ["Unknown", Upid, False],
403
+ }
scte35/version.py ADDED
@@ -0,0 +1,6 @@
1
+ '''
2
+ scte35.version
3
+ from the cli tool run: scte35 version
4
+ '''
5
+
6
+ version = "3.0.1"