ADCT Python
Python client API implementation for publishing ADCT data
Loading...
Searching...
No Matches
Functions
adctk_hello_world Namespace Reference

Functions

 get_png_mime ()
 
int main ()
 adctk python hello world without hard-coded publisher choices.
 

Function Documentation

◆ get_png_mime()

adctk_hello_world.get_png_mime ( )
Generates a grid png.
12def get_png_mime():
13 """Generates a grid png."""
14 png_header = b'\x89PNG\r\n\x1a\n'
15 height = 128
16 width=128
17 ihdr_data = struct.pack("!IIBBBBB", width, height, 8, 2, 0, 0, 0)
18 ihdr_chunk = b'IHDR' + ihdr_data
19 ihdr_crc = struct.pack("!I", zlib.crc32(ihdr_chunk))
20 ihdr = struct.pack("!I", len(ihdr_data)) + ihdr_chunk + ihdr_crc
21
22 raw_data = bytearray()
23 for y in range(height):
24 raw_data.append(0) # Filter type 0 for each scanline row
25 for x in range(width):
26 color = 255 if ((x // 16) + (y // 16)) % 2 == 0 else 0
27 raw_data.extend([color, color, color]) # R, G, B bytes
28 idat_data = zlib.compress(raw_data)
29 idat_chunk = b'IDAT' + idat_data
30 idat_crc = struct.pack("!I", zlib.crc32(idat_chunk))
31 idat = struct.pack("!I", len(idat_data)) + idat_chunk + idat_crc
32 iend_chunk = b'IEND'
33 iend_crc = struct.pack("!I", zlib.crc32(iend_chunk))
34 iend = struct.pack("!I", 0) + iend_chunk + iend_crc
35 x = png_header + ihdr + idat + iend
36 return f"{base64.b64encode(x).decode('utf-8')}"
37
38#*! \file adcHelloWorld.py
39# This demonstrates using the adctk.factory API to build and publish a message.
40# The message sent includes the bare minimum, plus hello world and host data
41# and a trivial mime-encoded png.
42
43#! \addtogroup examples
44# @{
45
46#!
47# \brief adctk python hello world without hard-coded publisher choices.

◆ main()

int adctk_hello_world.main ( )

adctk python hello world without hard-coded publisher choices.

48def main() -> int:
49 """!
50 @brief adctk python hello world without hard-coded publisher choices.
51 """
52
53 # create a factory
54 f = adctk.Factory()
55 avail = f.get_publisher_names()
56 print(f"available publishers are: {avail}")
57 print(f'ADC_MULTI_PUBLISHER_NAMES is: {os.getenv("ADC_MULTI_PUBLISHER_NAMES")}')
58
59 # create a message and add header
60 b = f.get_builder()
61 b.add_header_section("cxx_demo_1")
62
63 # add an application-defined payload to the message
64 app_data = f.get_builder()
65 app_data.add("hello", "world")
66 img = get_png_mime()
67 app_data.add_mime("grid1", "image/png", "base64", "test.png", img)
68 b.add_app_data_section(app_data)
69
70 # add environment chunks of interest on at least the first message in production
71 b.add_host_section(all_hs=True)
72
73 # could add lots of other sections, as needed.
74
75 print(f'adc pub version: {adctk.Publisher.API_VERSION["version"]}' )
76 print(f'adc builder version: {adctk.Builder.API_VERSION["version"]}' )
77
78 # create publishers following runtime environment variables and defaults.
79 # Do not tolerate failures in a testing environment.
80 mp = f.get_multi_publisher(strict=True, plugins=[])
81
82 # send built message b to all publishers
83 err = mp.publish(b)
84 if err:
85 print(f"got {err} publication errors." )
86
87 # do some work
88
89 # send the final status updating the header to get the needed new timestamp and uuid
90 b.add_exit_data_section(0, "all good in python", None)
91 b.add_header_section("cxx_demo_1")
92 err = mp.publish(b)
93 if err:
94 print(f"got {err} publication errors." )
95
96 # clean up all publishers
97 mp.terminate()
98
99 # the next block is skipped pending implementation of get_multifile_log_path
100 # the next block is skipped pending implementation of consolidate_multifile_logs
101 if False:
102 # may need to sleep here to give local fs a chance to catch up
103 # dir/user/[wfid.].host.Ppid.Tstarttime.pptr/application.Rrank.XXXXXX
104 # -->
105 # dir/user/consolidated.[wfid].adct-json.multi.xml
106 path = os.getenv("ADC_MULTIFILE_PLUGIN_DIRECTORY")
107 wfid = os.getenv("ADC_WFID")
108
109
110 old_paths = []
111
112 new_files = []
113 if len(old_paths):
114 for i in old_paths:
115 print(f"consolidating from: {i}")
116 if False:
117 try:
118 os.remove(i) # we could delete the merged files.
119 except FileNotFoundError:
120 pass
121 for i in new_files:
122 print(f"consolidated to: {i}" )
123 else:
124 print("no consolidation done.")
125
126 return 0
127
128#! @}
129#! @}
130
factory for adctk api objects.
Definition factory.py:34