zebra-day 0.0.32__py3-none-any.whl → 1.0.2__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.
Files changed (190) hide show
  1. zebra_day/__init__.py +35 -0
  2. zebra_day/bin/fetch_zebra_config.py +15 -0
  3. zebra_day/bin/generate_coord_grid_zpl.py +50 -0
  4. zebra_day/bin/print_zpl_from_file.py +21 -0
  5. zebra_day/bin/probe_new_label_dimensions.py +75 -0
  6. zebra_day/bin/scan_for_networed_zebra_printers_curl.sh +2 -1
  7. zebra_day/bin/zserve.py +701 -259
  8. zebra_day/cli/__init__.py +240 -0
  9. zebra_day/cli/cognito.py +121 -0
  10. zebra_day/cli/gui.py +255 -0
  11. zebra_day/cli/printer.py +168 -0
  12. zebra_day/cli/template.py +176 -0
  13. zebra_day/cmd_mgr.py +35 -0
  14. zebra_day/etc/label_styles/blank.zpl +0 -0
  15. zebra_day/etc/label_styles/cornersStripOf4Squares_1inX1in.zpl +55 -0
  16. zebra_day/etc/label_styles/corners_1inX2in.zpl +28 -0
  17. zebra_day/etc/label_styles/corners_20cmX30cm.zpl +6 -0
  18. zebra_day/etc/label_styles/corners_smallTube.zpl +7 -0
  19. zebra_day/etc/label_styles/corners_unspecifiedDimensions.zpl +15 -0
  20. zebra_day/etc/label_styles/plate_1inX0.25inHD.zpl +9 -0
  21. zebra_day/etc/label_styles/smallTubeWdotHD_prod.zpl +8 -0
  22. zebra_day/etc/label_styles/smallTubeWdot_corners.zpl +7 -0
  23. zebra_day/etc/label_styles/smallTubeWdot_prod.zpl +8 -0
  24. zebra_day/etc/label_styles/smallTubeWdot_prodAlt1.zpl +6 -0
  25. zebra_day/etc/label_styles/smallTubeWdot_prodAlt1b.zpl +3 -0
  26. zebra_day/etc/label_styles/smallTubeWdot_prodV2.zpl +8 -0
  27. zebra_day/etc/label_styles/smallTubeWdot_reagent.zpl +29 -0
  28. zebra_day/etc/label_styles/stripOf4Squares_1inX1in.zpl +32 -0
  29. zebra_day/etc/label_styles/test_800dX800dCoordinateArray.zpl +1 -0
  30. zebra_day/etc/label_styles/tmps/tmp_zpl_templates.here +0 -0
  31. zebra_day/etc/label_styles/tube_20mmX30mmA.zpl +7 -0
  32. zebra_day/etc/label_styles/tube_2inX0.5in.zpl +15 -0
  33. zebra_day/etc/label_styles/tube_2inX0.5inHD.zpl +15 -0
  34. zebra_day/etc/label_styles/tube_2inX1inHD.zpl +22 -0
  35. zebra_day/etc/label_styles/tube_2inX1inHDv3.zpl +21 -0
  36. zebra_day/etc/old_printer_config/2026-02-01_01:50:25.022846_printer_config.json +1 -0
  37. zebra_day/etc/old_printer_config/2026-02-01_01:50:25.033657_printer_config.json +1 -0
  38. zebra_day/etc/old_printer_config/2026-02-01_01:50:25.039597_printer_config.json +3 -0
  39. zebra_day/etc/old_printer_config/2026-02-01_01:50:25.047295_printer_config.json +1 -0
  40. zebra_day/etc/old_printer_config/2026-02-01_01:50:25.055804_printer_config.json +1 -0
  41. zebra_day/etc/old_printer_config/2026-02-01_01:50:25.061337_printer_config.json +3 -0
  42. zebra_day/etc/old_printer_config/2026-02-01_01:51:24.073326_printer_config.json +1 -0
  43. zebra_day/etc/old_printer_config/2026-02-01_01:51:24.081950_printer_config.json +1 -0
  44. zebra_day/etc/old_printer_config/2026-02-01_01:51:24.088251_printer_config.json +3 -0
  45. zebra_day/etc/old_printer_config/2026-02-01_01:51:24.096501_printer_config.json +1 -0
  46. zebra_day/etc/old_printer_config/2026-02-01_01:51:24.104767_printer_config.json +1 -0
  47. zebra_day/etc/old_printer_config/2026-02-01_01:51:24.110364_printer_config.json +3 -0
  48. zebra_day/etc/old_printer_config/2026-02-01_01:51:24.118239_printer_config.json +1 -0
  49. zebra_day/etc/old_printer_config/2026-02-01_01:51:24.125950_printer_config.json +1 -0
  50. zebra_day/etc/old_printer_config/2026-02-01_01:51:24.349866_printer_config.json +1 -0
  51. zebra_day/etc/old_printer_config/2026-02-01_01:51:24.361085_printer_config.json +3 -0
  52. zebra_day/etc/old_printer_config/2026-02-01_01:51:24.558323_printer_config.json +1 -0
  53. zebra_day/etc/old_printer_config/2026-02-01_01:51:24.565756_printer_config.json +3 -0
  54. zebra_day/etc/old_printer_config/{2023-10-25_02:19:04.139607_printer_config.json → 2026-02-01_01:51:29.739070_printer_config.json} +4 -3
  55. zebra_day/etc/old_printer_config/2026-02-01_01:51:29.753796_printer_config.json +1 -0
  56. zebra_day/etc/old_printer_config/2026-02-01_01:51:29.760201_printer_config.json +3 -0
  57. zebra_day/etc/old_printer_config/2026-02-01_01:51:29.768747_printer_config.json +1 -0
  58. zebra_day/etc/old_printer_config/2026-02-01_01:51:29.775312_printer_config.json +3 -0
  59. zebra_day/etc/old_printer_config/2026-02-01_01:51:29.782533_printer_config.json +1 -0
  60. zebra_day/etc/old_printer_config/2026-02-01_01:51:29.789287_printer_config.json +1 -0
  61. zebra_day/etc/old_printer_config/2026-02-01_01:51:29.794230_printer_config.json +3 -0
  62. zebra_day/etc/old_printer_config/2026-02-01_01:51:29.800021_printer_config.json +5 -0
  63. zebra_day/etc/printer_config.json +3 -54
  64. zebra_day/etc/printer_config.template.json +3 -2
  65. zebra_day/etc/tmp_printers0.json +5 -0
  66. zebra_day/etc/tmp_printers374.json +5 -0
  67. zebra_day/etc/tmp_printers383.json +5 -0
  68. zebra_day/etc/tmp_printers450.json +5 -0
  69. zebra_day/etc/tmp_printers504.json +5 -0
  70. zebra_day/etc/tmp_printers608.json +5 -0
  71. zebra_day/etc/tmp_printers657.json +5 -0
  72. zebra_day/etc/tmp_printers838.json +5 -0
  73. zebra_day/etc/tmp_printers839.json +5 -0
  74. zebra_day/etc/tmp_printers933.json +5 -0
  75. zebra_day/etc/tmp_printers957.json +5 -0
  76. zebra_day/exceptions.py +88 -0
  77. zebra_day/files/hold +0 -0
  78. zebra_day/files/test_png_17696.png +0 -0
  79. zebra_day/files/test_png_23477.png +0 -0
  80. zebra_day/files/test_png_28157.png +0 -0
  81. zebra_day/files/test_png_35832.png +0 -0
  82. zebra_day/files/test_png_36400.png +0 -0
  83. zebra_day/files/test_png_40816.png +0 -0
  84. zebra_day/files/test_png_49564.png +0 -0
  85. zebra_day/files/test_png_53848.png +0 -0
  86. zebra_day/files/test_png_62542.png +0 -0
  87. zebra_day/files/test_png_91597.png +0 -0
  88. zebra_day/files/test_png_93633.png +0 -0
  89. zebra_day/files/tmpbjo3k7q1.png +0 -0
  90. zebra_day/files/tmpigtr4pwy.png +0 -0
  91. zebra_day/files/zpl_label_tube_2inX1in_2026-02-01_01:51:24.370964.png +0 -0
  92. zebra_day/logging_config.py +74 -0
  93. zebra_day/logs/.hold +0 -0
  94. zebra_day/logs/print_requests.log +2 -0
  95. zebra_day/paths.py +143 -0
  96. zebra_day/print_mgr.py +489 -103
  97. zebra_day/static/datschund.css +63 -43
  98. zebra_day/static/datschund.png +0 -0
  99. zebra_day/static/daylily.png +0 -0
  100. zebra_day/static/favicon.svg +20 -0
  101. zebra_day/static/general.css +99 -0
  102. zebra_day/static/js/zebra_modern.js +172 -0
  103. zebra_day/static/lsmc.css +354 -0
  104. zebra_day/static/moon.jpeg +0 -0
  105. zebra_day/static/oakland.css +0 -32
  106. zebra_day/static/popday_daylily.css +1 -1
  107. zebra_day/static/style.css +39 -0
  108. zebra_day/static/zebra_modern.css +771 -0
  109. zebra_day/templates/base.html +36 -0
  110. zebra_day/templates/bpr.html +72 -0
  111. zebra_day/templates/build_new_config.html +36 -0
  112. zebra_day/templates/build_print_request.html +32 -0
  113. zebra_day/templates/chg_ui_style.html +19 -0
  114. zebra_day/templates/edit_template.html +128 -0
  115. zebra_day/templates/edit_zpl.html +37 -0
  116. zebra_day/templates/index.html +82 -0
  117. zebra_day/templates/legacy/base.html +37 -0
  118. zebra_day/templates/legacy/bpr.html +72 -0
  119. zebra_day/templates/legacy/build_new_config.html +36 -0
  120. zebra_day/templates/legacy/build_print_request.html +32 -0
  121. zebra_day/templates/legacy/chg_ui_style.html +19 -0
  122. zebra_day/templates/legacy/edit_template.html +128 -0
  123. zebra_day/templates/legacy/edit_zpl.html +37 -0
  124. zebra_day/templates/legacy/index.html +82 -0
  125. zebra_day/templates/legacy/list_prior_configs.html +24 -0
  126. zebra_day/templates/legacy/print_result.html +30 -0
  127. zebra_day/templates/legacy/printer_details.html +25 -0
  128. zebra_day/templates/legacy/printer_status.html +70 -0
  129. zebra_day/templates/legacy/save_result.html +17 -0
  130. zebra_day/templates/legacy/send_print_request.html +34 -0
  131. zebra_day/templates/legacy/simple_print.html +94 -0
  132. zebra_day/templates/legacy/view_pstation_json.html +29 -0
  133. zebra_day/templates/list_prior_configs.html +24 -0
  134. zebra_day/templates/modern/base.html +98 -0
  135. zebra_day/templates/modern/config.html +141 -0
  136. zebra_day/templates/modern/dashboard.html +160 -0
  137. zebra_day/templates/modern/print_request.html +141 -0
  138. zebra_day/templates/modern/print_result.html +88 -0
  139. zebra_day/templates/modern/printer_detail.html +117 -0
  140. zebra_day/templates/modern/printers.html +133 -0
  141. zebra_day/templates/modern/save_result.html +46 -0
  142. zebra_day/templates/modern/template_editor.html +172 -0
  143. zebra_day/templates/modern/templates.html +122 -0
  144. zebra_day/templates/print_result.html +30 -0
  145. zebra_day/templates/printer_details.html +25 -0
  146. zebra_day/templates/printer_status.html +70 -0
  147. zebra_day/templates/save_result.html +17 -0
  148. zebra_day/templates/send_print_request.html +34 -0
  149. zebra_day/templates/simple_print.html +94 -0
  150. zebra_day/templates/view_pstation_json.html +29 -0
  151. zebra_day/web/__init__.py +9 -0
  152. zebra_day/web/app.py +171 -0
  153. zebra_day/web/auth.py +172 -0
  154. zebra_day/web/middleware.py +159 -0
  155. zebra_day/web/routers/__init__.py +2 -0
  156. zebra_day/web/routers/api.py +163 -0
  157. zebra_day/web/routers/ui.py +1051 -0
  158. zebra_day/zpl_renderer.py +273 -0
  159. zebra_day-1.0.2.dist-info/METADATA +786 -0
  160. zebra_day-1.0.2.dist-info/RECORD +179 -0
  161. {zebra_day-0.0.32.dist-info → zebra_day-1.0.2.dist-info}/WHEEL +1 -1
  162. zebra_day-1.0.2.dist-info/entry_points.txt +4 -0
  163. zebra_day/etc/.blind +0 -1
  164. zebra_day/etc/current_style.txt +0 -1
  165. zebra_day/etc/label_styles/test_2inX1in.zpl +0 -22
  166. zebra_day/etc/label_styles/tmps/labware_2inX1in.na.2023-10-24_12:49:21.045127.zpl +0 -21
  167. zebra_day/etc/label_styles/tmps/labware_2inX1in.naggg.2023-10-24_16:02:04.704814.8888.2023-10-24_16:02:16.443911.zpl +0 -21
  168. zebra_day/etc/label_styles/tmps/labware_2inX1in.naggg.2023-10-24_16:02:04.704814.zpl +0 -21
  169. zebra_day/etc/label_styles/tmps/test_2inX1in.na.2023-10-24_12:45:37.002774.zpl +0 -22
  170. zebra_day/etc/old_printer_config/2023-10-24_16:06:06.931764_printer_config.json +0 -67
  171. zebra_day/etc/printer_config.json~ +0 -67
  172. zebra_day/files/tmp_2olihg4.png +0 -0
  173. zebra_day/files/tmpveojoyvn.png +0 -0
  174. zebra_day/files/zpl_label_labware_2inX1in_2023-10-25_02:30:08.093631.png +0 -0
  175. zebra_day/files/zpl_label_test_2inX1in_2023-10-24_15:54:29.343124.png +0 -0
  176. zebra_day/files/zpl_label_test_2inX1in_2023-10-24_16:01:45.670132.png +0 -0
  177. zebra_day/static/beyonce.css +0 -227
  178. zebra_day/static/datschund_on_moon.css +0 -164
  179. zebra_day/static/medicalsci.css +0 -144
  180. zebra_day/static/moar_zebra.css +0 -133
  181. zebra_day/static/popday.css +0 -140
  182. zebra_day/static/popday_dark.css +0 -140
  183. zebra_day/static/popday_dog.css +0 -140
  184. zebra_day-0.0.32.dist-info/METADATA +0 -14
  185. zebra_day-0.0.32.dist-info/RECORD +0 -53
  186. zebra_day-0.0.32.dist-info/entry_points.txt +0 -2
  187. /zebra_day/{etc/label_styles/blank_0inX0in.zpl → bin/__init__.py} +0 -0
  188. /zebra_day/etc/label_styles/{labware_2inX1in.zpl → generic_2inX1in.zpl} +0 -0
  189. {zebra_day-0.0.32.dist-info → zebra_day-1.0.2.dist-info/licenses}/LICENSE +0 -0
  190. {zebra_day-0.0.32.dist-info → zebra_day-1.0.2.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,70 @@
1
+ {% extends "base.html" %}
2
+
3
+ {% block title %}{{ title }}{% endblock %}
4
+
5
+ {% block content %}
6
+ <h1>Printer Status Summary For {{ lab }}</h1>
7
+ <small><a href="/">BACK HOME</a></small>
8
+ <ul>
9
+ <hr>
10
+ <br>
11
+ <table border="1">
12
+ <tr>
13
+ <th>Printer Name</th>
14
+ <th>Printer IP</th>
15
+ <th>Label Styles Available</th>
16
+ <th>Status on Network</th>
17
+ </tr>
18
+ {% for printer in printers %}
19
+ <tr>
20
+ <td>
21
+ {{ printer.name }}<br>
22
+ <small>{{ printer.model }}<br>{{ printer.serial }}</small>
23
+ </td>
24
+ <td>
25
+ {% if printer.ip_address != 'dl_png' %}
26
+ <a href="http://{{ printer.ip_address }}" target="_blank">{{ printer.ip_address }}</a>
27
+ {% else %}
28
+ {{ printer.ip_address }}
29
+ {% endif %}
30
+ <br><small>{{ printer.arp_data }}</small>
31
+ </td>
32
+ <td>
33
+ {% for style in printer.label_zpl_styles %}
34
+ <small>
35
+ <a href="/edit_zpl">{{ style }}</a>
36
+ (<a href="/_print_label?lab={{ lab }}&printer={{ printer.name }}&label_zpl_style={{ style }}&uid_barcode={{ printer.ip_address }}&alt_a={{ printer.model }}&alt_b={{ printer.serial }}&alt_c={{ lab }}&alt_d={{ lab }}" target="_blank">test</a>)
37
+ </small><br>
38
+ {% endfor %}
39
+ </td>
40
+ <td>
41
+ {{ printer.status }}
42
+ <br><small><a href="/printer_details?printer_name={{ printer.name }}&lab={{ lab }}" target="_blank">...printer details</a></small>
43
+ </td>
44
+ </tr>
45
+ {% endfor %}
46
+ </table>
47
+ </ul>
48
+
49
+ <br><hr>
50
+ <h2>Build New Printers Config JSON // Scan Network</h2>
51
+ <ul>
52
+ <p>This will probe for printers, and with all discovered printers will create a new printers JSON and overwrite the current fleet data for this lab.</p>
53
+ <form id="myForm" action="/probe_zebra_printers_add_to_printers_json">
54
+ <label>Enter IP Root To Scan:</label>
55
+ <input type="text" value="{{ ip_root }}" name="ip_stub">
56
+ <br>
57
+ <label>Select (or enter new) lab:</label>
58
+ <input type="text" name="lab" list="lab_names" value="{{ lab }}">
59
+ <datalist id="lab_names">
60
+ {% for lab_name in labs %}
61
+ <option value="{{ lab_name }}">{{ lab_name }}</option>
62
+ {% endfor %}
63
+ </datalist>
64
+ <br>
65
+ <input type="submit" value="Scan Network">
66
+ </form>
67
+ <small><a href="/list_prior_printer_config_files">The current printers JSON will be backed up and can be found here.</a></small>
68
+ </ul>
69
+ {% endblock %}
70
+
@@ -0,0 +1,17 @@
1
+ {% extends "base.html" %}
2
+
3
+ {% block title %}{{ title }}{% endblock %}
4
+
5
+ {% block content %}
6
+ <h1>Template Saved</h1>
7
+ <small><a href="/">home</a></small>
8
+ <hr>
9
+
10
+ <p>Changes saved to temp file: <strong>{{ new_filename }}</strong></p>
11
+
12
+ <ul>
13
+ <li><a href="/edit_zpl">Go back to the ZPL file list</a></li>
14
+ <li><a href="/">Go home</a></li>
15
+ </ul>
16
+ {% endblock %}
17
+
@@ -0,0 +1,34 @@
1
+ {% extends "base.html" %}
2
+
3
+ {% block title %}{{ title }}{% endblock %}
4
+
5
+ {% block content %}
6
+ <h1>Send Print Request</h1>
7
+ <small><a href="/">home</a></small>
8
+ <hr>
9
+
10
+ {% for lab_name, printers in labs.items() %}
11
+ <h2>Lab: {{ lab_name }}</h2>
12
+ <table border="1" width="90%">
13
+ <tr>
14
+ <th>Printer</th>
15
+ <th>Available Label Styles</th>
16
+ </tr>
17
+ {% for printer_name, printer_info in printers.items() %}
18
+ <tr>
19
+ <td>
20
+ <strong>{{ printer_name }}</strong><br>
21
+ <small>{{ printer_info.ip_address }}</small>
22
+ </td>
23
+ <td>
24
+ {% for style in printer_info.label_zpl_styles %}
25
+ <a href="/build_print_request?lab={{ lab_name }}&printer={{ printer_name }}&printer_ip={{ printer_info.ip_address }}&label_zpl_style={{ style }}">{{ style }}</a><br>
26
+ {% endfor %}
27
+ </td>
28
+ </tr>
29
+ {% endfor %}
30
+ </table>
31
+ <br>
32
+ {% endfor %}
33
+ {% endblock %}
34
+
@@ -0,0 +1,94 @@
1
+ {% extends "base.html" %}
2
+
3
+ {% block title %}{{ title }}{% endblock %}
4
+
5
+ {% block content %}
6
+ <h1>Send Label Print Request</h1>
7
+ <ul>
8
+ <small><a href="/">home</a></small>
9
+ <hr>
10
+
11
+ <form id="myForm" action="/_print_label" method="get">
12
+ <ul>
13
+ <li>
14
+ <label>UID for Barcode:</label>
15
+ <input type="text" name="uid_barcode">
16
+ </li>
17
+ <li>
18
+ <label>ALT-A:</label>
19
+ <input type="text" name="alt_a">
20
+ </li>
21
+ <li>
22
+ <label>ALT-B:</label>
23
+ <input type="text" name="alt_b">
24
+ </li>
25
+ <li>
26
+ <label>ALT-C:</label>
27
+ <input type="text" name="alt_c">
28
+ </li>
29
+ <li>
30
+ <label>ALT-D:</label>
31
+ <input type="text" name="alt_d">
32
+ </li>
33
+ <li>
34
+ <label>ALT-E:</label>
35
+ <input type="text" name="alt_e">
36
+ </li>
37
+ <li>
38
+ <label>ALT-F:</label>
39
+ <input type="text" name="alt_f">
40
+ </li>
41
+ <li>
42
+ <label>Select Lab:</label>
43
+ <select id="labSelect" name="lab" onchange="updatePrinters()">
44
+ <option value="">Select a Lab</option>
45
+ {% for lab in labs %}
46
+ <option value="{{ lab }}">{{ lab }}</option>
47
+ {% endfor %}
48
+ </select>
49
+ </li>
50
+ <li>
51
+ <label>Select Printer:</label>
52
+ <select id="printerSelect" name="printer">
53
+ <option value="">Select a Printer</option>
54
+ </select>
55
+ </li>
56
+ <li>
57
+ <label>Label ZPL Style:</label>
58
+ <select name="label_zpl_style">
59
+ {% for template in templates %}
60
+ <option value="{{ template }}">{{ template }}</option>
61
+ {% endfor %}
62
+ </select>
63
+ </li>
64
+ </ul>
65
+ <br>
66
+ <input type="submit" value="Print Label">
67
+ </form>
68
+ </ul>
69
+ {% endblock %}
70
+
71
+ {% block extra_scripts %}
72
+ <script>
73
+ var labsAndPrinters = {{ labs_and_printers | safe }};
74
+
75
+ function updatePrinters() {
76
+ var labSelect = document.getElementById('labSelect');
77
+ var printerSelect = document.getElementById('printerSelect');
78
+ var selectedLab = labSelect.value;
79
+
80
+ // Clear current printer options
81
+ printerSelect.innerHTML = '<option value="">Select a Printer</option>';
82
+
83
+ if (selectedLab in labsAndPrinters) {
84
+ labsAndPrinters[selectedLab].forEach(function(printer) {
85
+ var option = document.createElement('option');
86
+ option.value = printer;
87
+ option.text = printer;
88
+ printerSelect.appendChild(option);
89
+ });
90
+ }
91
+ }
92
+ </script>
93
+ {% endblock %}
94
+
@@ -0,0 +1,29 @@
1
+ {% extends "base.html" %}
2
+
3
+ {% block title %}{{ title }}{% endblock %}
4
+
5
+ {% block content %}
6
+ <h1>View and Edit Printer Config JSON</h1>
7
+ <small><a href="/">home</a></small>
8
+ <hr>
9
+
10
+ {% if error_msg %}
11
+ <p style="color: red; font-weight: bold;">{{ error_msg }}</p>
12
+ {% endif %}
13
+
14
+ <form action="/save_pstation_json" method="post">
15
+ <textarea name="json_data" rows="45" cols="80">{{ config_data }}</textarea>
16
+ <br>
17
+ <input type="submit" value="Save New Printers Config JSON & Make Active">
18
+ </form>
19
+
20
+ <hr>
21
+ <ul>
22
+ <li><a href="/list_prior_printer_config_files">View backed up printers JSON config files</a></li>
23
+ <hr>
24
+ <li><a href="/reset_pstation_json">Restore Printer Settings From Default JSON (THIS WILL DELETE YOUR CURRENT FILE!)</a></li>
25
+ <hr>
26
+ <li><a href="/clear_printers_json">CLEAR contents of current printers.json file (This Cannot Be Undone)</a></li>
27
+ </ul>
28
+ {% endblock %}
29
+
@@ -0,0 +1,9 @@
1
+ """
2
+ zebra_day web application module.
3
+
4
+ FastAPI-based web interface for managing Zebra printers and label templates.
5
+ """
6
+ from zebra_day.web.app import create_app
7
+
8
+ __all__ = ["create_app"]
9
+
zebra_day/web/app.py ADDED
@@ -0,0 +1,171 @@
1
+ """
2
+ FastAPI application factory for zebra_day.
3
+
4
+ This module provides the main FastAPI application for the zebra_day web interface.
5
+ """
6
+ from __future__ import annotations
7
+
8
+ import os
9
+ import subprocess
10
+ from pathlib import Path
11
+ from typing import Literal, Optional
12
+
13
+ from fastapi import FastAPI, Request
14
+ from fastapi.staticfiles import StaticFiles
15
+ from fastapi.templating import Jinja2Templates
16
+ from importlib.resources import files
17
+
18
+ from zebra_day.logging_config import get_logger
19
+ from zebra_day import paths as xdg
20
+ from zebra_day.web.middleware import RequestLoggingMiddleware, print_rate_limiter
21
+
22
+ _log = get_logger(__name__)
23
+
24
+ # Package paths
25
+ _PKG_PATH = Path(str(files("zebra_day")))
26
+ _STATIC_PATH = _PKG_PATH / "static"
27
+ _TEMPLATES_PATH = _PKG_PATH / "templates"
28
+
29
+
30
+ def get_local_ip() -> str:
31
+ """Get the local IP address of this machine."""
32
+ ipcmd = r"""(ip addr show | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' || ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1') 2>/dev/null"""
33
+ result = subprocess.run(ipcmd, shell=True, capture_output=True, text=True)
34
+ lines = result.stdout.strip().split("\n")
35
+ return lines[0] if lines and lines[0] else "127.0.0.1"
36
+
37
+
38
+ def create_app(
39
+ *,
40
+ debug: bool = False,
41
+ css_theme: str = "lsmc.css",
42
+ auth: Optional[Literal["none", "cognito"]] = None,
43
+ ) -> FastAPI:
44
+ """
45
+ Create and configure the FastAPI application.
46
+
47
+ Args:
48
+ debug: Enable debug mode
49
+ css_theme: Default CSS theme file name
50
+ auth: Authentication mode - "none" (public) or "cognito" (AWS Cognito).
51
+ If None, reads from ZEBRA_DAY_AUTH_MODE env var (defaults to "none").
52
+
53
+ Returns:
54
+ Configured FastAPI application
55
+ """
56
+ # Get auth mode from parameter or environment variable
57
+ if auth is None:
58
+ auth = os.environ.get("ZEBRA_DAY_AUTH_MODE", "none") # type: ignore[assignment]
59
+
60
+ # Validate auth parameter
61
+ if auth not in ("none", "cognito"):
62
+ raise ValueError(f"Invalid auth mode: {auth!r}. Must be 'none' or 'cognito'.")
63
+
64
+ app = FastAPI(
65
+ title="Zebra Day",
66
+ description="Zebra printer fleet management and label printing",
67
+ version="0.5.0",
68
+ debug=debug,
69
+ )
70
+
71
+ # Add request logging middleware
72
+ app.add_middleware(RequestLoggingMiddleware)
73
+
74
+ # Configure authentication if enabled
75
+ if auth == "cognito":
76
+ from zebra_day.web.auth import CognitoAuthMiddleware, setup_cognito_auth
77
+
78
+ cognito_auth = setup_cognito_auth(app)
79
+ app.add_middleware(CognitoAuthMiddleware, cognito_auth=cognito_auth)
80
+ app.state.cognito_auth = cognito_auth
81
+ app.state.auth_mode = "cognito"
82
+ _log.info("Cognito authentication middleware enabled")
83
+ else:
84
+ app.state.auth_mode = "none"
85
+ _log.info("Authentication disabled (auth=none)")
86
+
87
+ # Store rate limiter in app state for use in endpoints
88
+ app.state.print_rate_limiter = print_rate_limiter
89
+
90
+ # Store app state
91
+ app.state.css_theme = css_theme
92
+ app.state.local_ip = get_local_ip()
93
+ app.state.pkg_path = _PKG_PATH
94
+
95
+ # Mount static files
96
+ app.mount("/static", StaticFiles(directory=str(_STATIC_PATH)), name="static")
97
+
98
+ # Also mount package directories that need to be served
99
+ files_dir = _PKG_PATH / "files"
100
+ if files_dir.exists():
101
+ app.mount("/files", StaticFiles(directory=str(files_dir)), name="files")
102
+
103
+ etc_dir = _PKG_PATH / "etc"
104
+ if etc_dir.exists():
105
+ app.mount("/etc", StaticFiles(directory=str(etc_dir)), name="etc")
106
+
107
+ # Setup Jinja2 templates
108
+ templates = Jinja2Templates(directory=str(_TEMPLATES_PATH))
109
+ app.state.templates = templates
110
+
111
+ # Register routers
112
+ from zebra_day.web.routers import ui, api
113
+
114
+ app.include_router(ui.router)
115
+ app.include_router(api.router, prefix="/api/v1", tags=["api"])
116
+
117
+ @app.on_event("startup")
118
+ async def startup_event():
119
+ """Initialize application state on startup."""
120
+ import zebra_day.print_mgr as zdpm
121
+
122
+ app.state.zp = zdpm.zpl()
123
+ _log.info(
124
+ "zebra_day web server starting at %s:8118",
125
+ app.state.local_ip,
126
+ )
127
+
128
+ @app.get("/healthz")
129
+ async def healthz():
130
+ """Health check endpoint."""
131
+ return {"status": "healthy"}
132
+
133
+ @app.get("/readyz")
134
+ async def readyz():
135
+ """Readiness check endpoint."""
136
+ # Check if printer manager is initialized
137
+ if hasattr(app.state, "zp") and app.state.zp is not None:
138
+ return {"status": "ready"}
139
+ return {"status": "not_ready"}, 503
140
+
141
+ return app
142
+
143
+
144
+ def run_server(
145
+ host: str = "0.0.0.0",
146
+ port: int = 8118,
147
+ reload: bool = False,
148
+ auth: Literal["none", "cognito"] = "none",
149
+ ):
150
+ """
151
+ Run the FastAPI server using uvicorn.
152
+
153
+ Args:
154
+ host: Host to bind to
155
+ port: Port to listen on
156
+ reload: Enable auto-reload for development
157
+ auth: Authentication mode - "none" (public) or "cognito" (AWS Cognito)
158
+ """
159
+ import uvicorn
160
+
161
+ # Store auth mode in environment for factory function
162
+ os.environ["ZEBRA_DAY_AUTH_MODE"] = auth
163
+
164
+ uvicorn.run(
165
+ "zebra_day.web.app:create_app",
166
+ host=host,
167
+ port=port,
168
+ reload=reload,
169
+ factory=True,
170
+ )
171
+
zebra_day/web/auth.py ADDED
@@ -0,0 +1,172 @@
1
+ """Authentication integration for zebra_day web server.
2
+
3
+ Provides optional Cognito authentication support via the daylily-cognito library.
4
+ """
5
+
6
+ from __future__ import annotations
7
+
8
+ import os
9
+ from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional
10
+
11
+ from fastapi import Depends, HTTPException, Request, status
12
+ from starlette.middleware.base import BaseHTTPMiddleware
13
+ from starlette.responses import Response
14
+
15
+ from zebra_day.logging_config import get_logger
16
+
17
+ if TYPE_CHECKING:
18
+ from fastapi import FastAPI
19
+
20
+ _log = get_logger(__name__)
21
+
22
+ # Endpoints that should never require authentication
23
+ PUBLIC_PATHS: List[str] = [
24
+ "/healthz",
25
+ "/readyz",
26
+ "/docs",
27
+ "/openapi.json",
28
+ "/redoc",
29
+ ]
30
+
31
+ # Try to import daylily-cognito components
32
+ _COGNITO_AVAILABLE = False
33
+ _COGNITO_IMPORT_ERROR: Optional[str] = None
34
+
35
+ try:
36
+ from daylily_cognito import CognitoAuth, CognitoConfig, create_auth_dependency
37
+
38
+ _COGNITO_AVAILABLE = True
39
+ except ImportError as e:
40
+ _COGNITO_IMPORT_ERROR = str(e)
41
+ CognitoAuth = None # type: ignore[misc, assignment]
42
+ CognitoConfig = None # type: ignore[misc, assignment]
43
+ create_auth_dependency = None # type: ignore[misc, assignment]
44
+
45
+
46
+ def is_cognito_available() -> bool:
47
+ """Check if daylily-cognito library is installed."""
48
+ return _COGNITO_AVAILABLE
49
+
50
+
51
+ def get_cognito_import_error() -> Optional[str]:
52
+ """Get the import error message if daylily-cognito is not available."""
53
+ return _COGNITO_IMPORT_ERROR
54
+
55
+
56
+ class CognitoAuthMiddleware(BaseHTTPMiddleware):
57
+ """Middleware that enforces Cognito authentication on protected endpoints.
58
+
59
+ Exempts health check endpoints and other public paths.
60
+ """
61
+
62
+ def __init__(self, app: "FastAPI", cognito_auth: Any) -> None:
63
+ super().__init__(app)
64
+ self.cognito_auth = cognito_auth
65
+ self.get_current_user = create_auth_dependency(cognito_auth, optional=False)
66
+
67
+ async def dispatch(self, request: Request, call_next: Callable) -> Response:
68
+ """Check authentication for protected endpoints."""
69
+ path = request.url.path
70
+
71
+ # Allow public endpoints without authentication
72
+ if any(path.startswith(public) for public in PUBLIC_PATHS):
73
+ return await call_next(request)
74
+
75
+ # Allow static files without authentication
76
+ if path.startswith("/static") or path.startswith("/files") or path.startswith("/etc"):
77
+ return await call_next(request)
78
+
79
+ # Check for Authorization header
80
+ auth_header = request.headers.get("Authorization")
81
+ if not auth_header:
82
+ return Response(
83
+ content='{"detail":"Authentication required"}',
84
+ status_code=status.HTTP_401_UNAUTHORIZED,
85
+ media_type="application/json",
86
+ headers={"WWW-Authenticate": "Bearer"},
87
+ )
88
+
89
+ # Validate Bearer token
90
+ if not auth_header.startswith("Bearer "):
91
+ return Response(
92
+ content='{"detail":"Invalid authorization header format"}',
93
+ status_code=status.HTTP_401_UNAUTHORIZED,
94
+ media_type="application/json",
95
+ headers={"WWW-Authenticate": "Bearer"},
96
+ )
97
+
98
+ token = auth_header[7:] # Remove "Bearer " prefix
99
+ try:
100
+ # Verify token and attach user to request state
101
+ user_claims = self.cognito_auth.verify_token(token)
102
+ request.state.user = user_claims
103
+ except HTTPException:
104
+ return Response(
105
+ content='{"detail":"Invalid or expired token"}',
106
+ status_code=status.HTTP_401_UNAUTHORIZED,
107
+ media_type="application/json",
108
+ headers={"WWW-Authenticate": "Bearer"},
109
+ )
110
+ except Exception as e:
111
+ _log.error("Authentication error: %s", str(e))
112
+ return Response(
113
+ content='{"detail":"Authentication failed"}',
114
+ status_code=status.HTTP_401_UNAUTHORIZED,
115
+ media_type="application/json",
116
+ headers={"WWW-Authenticate": "Bearer"},
117
+ )
118
+
119
+ return await call_next(request)
120
+
121
+
122
+ def setup_cognito_auth(app: "FastAPI") -> Any:
123
+ """Configure Cognito authentication for the FastAPI app.
124
+
125
+ Reads configuration from environment variables:
126
+ - COGNITO_USER_POOL_ID: Cognito User Pool ID (required)
127
+ - COGNITO_APP_CLIENT_ID or COGNITO_CLIENT_ID: App Client ID (required)
128
+ - COGNITO_REGION or AWS_REGION: AWS region (defaults to us-west-2)
129
+ - AWS_PROFILE: Optional AWS profile name
130
+
131
+ Returns:
132
+ CognitoAuth instance
133
+
134
+ Raises:
135
+ ValueError: If required environment variables are missing
136
+ ImportError: If daylily-cognito is not installed
137
+ """
138
+ if not _COGNITO_AVAILABLE:
139
+ raise ImportError(
140
+ f"daylily-cognito library is required for Cognito authentication. "
141
+ f"Install with: pip install -e '.[auth]'\n"
142
+ f"Import error: {_COGNITO_IMPORT_ERROR}"
143
+ )
144
+
145
+ # Load config from environment
146
+ try:
147
+ config = CognitoConfig.from_legacy_env()
148
+ except ValueError as e:
149
+ raise ValueError(
150
+ f"Missing required Cognito configuration. {e}\n"
151
+ "Set the following environment variables:\n"
152
+ " COGNITO_USER_POOL_ID=your-pool-id\n"
153
+ " COGNITO_APP_CLIENT_ID=your-client-id\n"
154
+ " COGNITO_REGION=us-west-2 (optional, defaults to us-west-2)"
155
+ ) from e
156
+
157
+ # Create CognitoAuth instance
158
+ cognito_auth = CognitoAuth(
159
+ region=config.region,
160
+ user_pool_id=config.user_pool_id,
161
+ app_client_id=config.app_client_id,
162
+ profile=config.aws_profile,
163
+ )
164
+
165
+ _log.info(
166
+ "Cognito authentication enabled (region=%s, pool=%s)",
167
+ config.region,
168
+ config.user_pool_id,
169
+ )
170
+
171
+ return cognito_auth
172
+