|
| 1 | +from pathlib import Path |
| 2 | +from loguru import logger as log |
| 3 | +import streamlit as st |
| 4 | +import tempfile |
| 5 | +import subprocess |
| 6 | +import shutil |
| 7 | +import re |
| 8 | + |
| 9 | +ace_path = Path(shutil.which("ace")) |
| 10 | +ansi_escape = re.compile(r"\x1B[@-_][0-?]*[ -/]*[@-~]") |
| 11 | + |
| 12 | + |
| 13 | +def check_epub(fp): |
| 14 | + # Run the ace command with subprocess.Popen |
| 15 | + fp = Path(fp) |
| 16 | + report_dir = fp.parent / f"{fp.stem}_report" |
| 17 | + report_dir.mkdir(exist_ok=True) |
| 18 | + report_file = report_dir / "report.json" |
| 19 | + |
| 20 | + # Run ACE Check |
| 21 | + cmd = [ace_path, "-f", "-o", report_dir, fp] |
| 22 | + process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) |
| 23 | + |
| 24 | + log_output = [] |
| 25 | + for line in process.stdout: |
| 26 | + stripped_line = ansi_escape.sub("", line).strip() |
| 27 | + log.info(stripped_line) |
| 28 | + log_output.append(line) |
| 29 | + yield stripped_line |
| 30 | + |
| 31 | + process.wait() |
| 32 | + if process.returncode != 0: |
| 33 | + log_output.append(f"Ace command failed with return code {process.returncode}") |
| 34 | + yield f"Ace command failed with return code {process.returncode}" |
| 35 | + |
| 36 | + |
| 37 | +# def main(): |
| 38 | +# st.title("EPUB Accessibility Fixer") |
| 39 | +# uploaded_file = st.file_uploader("Upload an EPUB file", type=["epub"]) |
| 40 | +# |
| 41 | +# if uploaded_file is not None: |
| 42 | +# with tempfile.NamedTemporaryFile(delete=False, suffix=".epub") as tmp_file: |
| 43 | +# tmp_file.write(uploaded_file.getbuffer()) |
| 44 | +# tmp_file_path = tmp_file.name |
| 45 | +# |
| 46 | +# st.text("Checking EPUB for accessibility violations...") |
| 47 | +# |
| 48 | +# # Placeholder for log messages |
| 49 | +# log_placeholder = st.empty() |
| 50 | +# log_lines = [] |
| 51 | +# |
| 52 | +# # Define the maximum number of lines to display |
| 53 | +# max_log_lines = 6 |
| 54 | +# |
| 55 | +# for log_line in check_epub(tmp_file_path): |
| 56 | +# # Add new log line to the list |
| 57 | +# log_lines.append(log_line) |
| 58 | +# |
| 59 | +# # Keep only the last 'max_log_lines' lines |
| 60 | +# if len(log_lines) > max_log_lines: |
| 61 | +# log_lines = log_lines[-max_log_lines:] |
| 62 | +# |
| 63 | +# # Join the log lines into a single string |
| 64 | +# log_messages = "\n".join(log_lines) |
| 65 | +# |
| 66 | +# # Update the log placeholder |
| 67 | +# log_placeholder.text(log_messages) |
| 68 | +# |
| 69 | +# st.text("Accessibility check completed.") |
| 70 | +# # Placeholder for fixing process |
| 71 | +# st.text("Fixing issues... (implementation required)") |
| 72 | +# fixed_file_path = tmp_file_path.replace(".epub", "_fixed.epub") |
| 73 | +# |
| 74 | +# # Example of saving fixed file (this should be replaced with actual fixing logic) |
| 75 | +# with open(fixed_file_path, "wb") as fixed_file: |
| 76 | +# fixed_file.write(uploaded_file.getbuffer()) |
| 77 | +# |
| 78 | +# st.text("Download the fixed EPUB file:") |
| 79 | +# with open(fixed_file_path, "rb") as fixed_file: |
| 80 | +# st.download_button(label="Download Fixed EPUB", data=fixed_file, file_name="fixed.epub") |
| 81 | + |
| 82 | + |
| 83 | +def main(): |
| 84 | + st.title("EPUB Accessibility Fixer") |
| 85 | + uploaded_file = st.file_uploader("Upload an EPUB file", type=["epub"]) |
| 86 | + |
| 87 | + if uploaded_file is not None: |
| 88 | + with tempfile.NamedTemporaryFile(delete=False, suffix=".epub") as tmp_file: |
| 89 | + tmp_file.write(uploaded_file.getbuffer()) |
| 90 | + tmp_file_path = tmp_file.name |
| 91 | + |
| 92 | + st.text("Checking EPUB for accessibility violations...") |
| 93 | + |
| 94 | + # Placeholder for log messages |
| 95 | + log_placeholder = st.empty() |
| 96 | + |
| 97 | + for log_line in check_epub(tmp_file_path): |
| 98 | + log_placeholder.text(log_line, append=True) |
| 99 | + |
| 100 | + st.text("Accessibility check completed.") |
| 101 | + # Placeholder for fixing process |
| 102 | + st.text("Fixing issues... (implementation required)") |
| 103 | + fixed_file_path = tmp_file_path.replace(".epub", "_fixed.epub") |
| 104 | + |
| 105 | + # Example of saving fixed file (this should be replaced with actual fixing logic) |
| 106 | + with open(fixed_file_path, "wb") as fixed_file: |
| 107 | + fixed_file.write(uploaded_file.getbuffer()) |
| 108 | + |
| 109 | + st.text("Download the fixed EPUB file:") |
| 110 | + with open(fixed_file_path, "rb") as fixed_file: |
| 111 | + st.download_button(label="Download Fixed EPUB", data=fixed_file, file_name="fixed.epub") |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + main() |
0 commit comments