phantompy/qasync_phantompy.py

121 lines
3.3 KiB
Python
Raw Normal View History

2022-11-15 22:49:31 +01:00
#!/usr/local/bin/python3.sh
# -*-mode: python; indent-tabs-mode: nil; py-indent-offset: 4; coding: utf-8 -*
import sys
import os
import asyncio
import time
import random
2022-11-16 19:53:54 +01:00
# let qasync figure out what Qt we are using - we dont care
from qasync import QApplication, QtWidgets, QEventLoop
2022-11-15 22:49:31 +01:00
from phantompy import Render
2022-11-16 19:53:54 +01:00
# if you want an example of looking for things in downloaded HTML:
2022-11-15 23:01:55 +01:00
# from lookupdns import LookFor as Render
2022-11-16 19:33:59 +01:00
from support_phantompy import vsetup_logging, omain_argparser
2022-11-15 22:49:31 +01:00
global LOG
import logging
import warnings
warnings.filterwarnings('ignore')
LOG = logging.getLogger()
class Widget(QtWidgets.QWidget):
def __init__(self):
QtWidgets.QWidget.__init__(self)
self._label = QtWidgets.QLabel()
box = QtWidgets.QHBoxLayout()
self.setLayout(box)
box.addWidget(self._label)
2022-11-16 19:53:54 +01:00
self.progress = QtWidgets.QProgressBar()
2022-11-15 22:49:31 +01:00
self.progress.setRange(0, 99)
box.addWidget(self.progress)
def update(self, text):
i = len(asyncio.all_tasks())
self._label.setText(str(i))
self.progress.setValue(int(text))
2022-11-16 19:33:59 +01:00
2022-11-15 22:49:31 +01:00
class ContextManager:
def __init__(self) -> None:
self._seconds = 0
async def __aenter__(self):
LOG.debug("ContextManager enter")
return self
async def __aexit__(self, *args):
LOG.debug("ContextManager exit")
async def tick(self):
await asyncio.sleep(1)
self._seconds += 1
return self._seconds
async def main(widget, app, ilen):
LOG.debug("Task started")
try:
async with ContextManager() as ctx:
for i in range(1, 120):
seconds = await ctx.tick()
if widget:
widget.update(str(i))
if len(app.ldone) == ilen:
LOG.info(f"Finished with {app.ldone}")
print('\n'.join(app.ldone))
2022-11-15 22:49:31 +01:00
app.exit()
# raise asyncio.CancelledError
return
2022-11-16 19:33:59 +01:00
LOG.debug(f"{app.ldone} {seconds}")
2022-11-15 22:49:31 +01:00
except asyncio.CancelledError as ex:
LOG.debug("Task cancelled")
2022-11-16 16:33:50 +01:00
def iMain(largs):
2022-11-16 19:33:59 +01:00
parser = omain_argparser()
oargs = parser.parse_args(largs)
2022-11-16 16:33:50 +01:00
bgui=oargs.show_gui
try:
d = int(os.environ.get('DEBUG', 0))
if d > 0:
2022-11-16 19:33:59 +01:00
oargs.log_level = 10
vsetup_logging(oargs.log_level, logfile='', stream=sys.stderr)
2022-11-16 16:33:50 +01:00
except: pass
2022-11-16 19:33:59 +01:00
2022-11-16 19:53:54 +01:00
app = QApplication([])
2022-11-15 22:49:31 +01:00
app.lstart = []
if bgui:
widget = Widget()
widget._app = app
widget.show()
else:
widget = None
2022-11-16 19:33:59 +01:00
2022-11-16 19:53:54 +01:00
loop = QEventLoop(app)
2022-11-15 22:49:31 +01:00
asyncio.set_event_loop(loop)
2022-11-16 16:33:50 +01:00
url = oargs.html_url
htmlfile = oargs.html_output
pdffile = oargs.html_output
jsfile = oargs.js_input
2022-11-15 23:01:55 +01:00
# run only starts the url loading
2022-11-16 16:33:50 +01:00
r = Render(app,
do_print=True if pdffile else False,
do_save=True if htmlfile else False)
2022-11-15 23:01:55 +01:00
uri = url.strip()
2022-11-16 16:33:50 +01:00
r.run(uri, pdffile, htmlfile, jsfile)
2022-11-15 23:01:55 +01:00
LOG.debug(f"{r.percent} {app.lstart}")
2022-11-16 19:33:59 +01:00
2022-11-15 22:49:31 +01:00
LOG.info(f"queued {len(app.lstart)} urls")
2022-11-16 19:33:59 +01:00
task = loop.create_task(main(widget, app, 1))
2022-11-15 22:49:31 +01:00
loop.run_forever()
# cancel remaining tasks and wait for them to complete
task.cancel()
tasks = asyncio.all_tasks()
loop.run_until_complete(asyncio.gather(*tasks))
if __name__ == '__main__':
2022-11-16 19:33:59 +01:00
2022-11-16 16:33:50 +01:00
iMain(sys.argv[1:])
2022-11-16 19:33:59 +01:00