Add file PyProject.

Add a hint (Thank you Trbl).
Add connectivity handler.
Replace pdfkit by  reportlab.
This commit is contained in:
Schimon Jehudah 2024-03-20 21:46:01 +00:00
parent 64b8cc167c
commit dd559dcf82
2 changed files with 113 additions and 19 deletions

53
pyproject.toml Normal file
View file

@ -0,0 +1,53 @@
[build-system]
requires = ["setuptools>=61.2"]
build-backend = "setuptools.build_meta"
[project]
name = "Slixprint"
version = "0.1"
description = "Printer bot for XMPP"
authors = [{name = "Schimon Zachary", email = "sch@fedora.email"}]
license = {text = "MIT"}
classifiers = [
"Framework :: slixmpp",
"Intended Audience :: End Users/Desktop",
"License :: OSI Approved :: MIT License",
"Natural Language :: English",
"Programming Language :: Python",
"Topic :: Communications :: Chat",
"Topic :: Internet :: XMPP",
"Topic :: Office/Business",
"Topic :: Printing",
"Topic :: System :: Hardware :: Universal Serial Bus (USB) :: Printer",
]
keywords = [
"bot",
"chat",
"im",
"jabber",
"print",
"xmpp",
]
dependencies = [
"pdfkit",
"pycups",
"python-magic",
"requests",
"slixmpp",
"reportlab",
]
[project.urls]
Homepage = "http://slixprint.i2p/"
Repository = "https://gitgud.io/sjehuda/slixprint"
Issues = "https://gitgud.io/sjehuda/slixprint/issues"
[project.readme]
text = "Slixprint is a printer bot for XMPP."
[project.scripts]
slixprint = "slixprint.py"
[tool.setuptools]
platforms = ["any"]
[tool.setuptools.package-data]

View file

@ -5,13 +5,14 @@
# This file is part of Slixmark.
# See the file LICENSE for copying permission.
import asyncio
from getpass import getpass
from argparse import ArgumentParser
import cups
import logging
import magic
import pdfkit
# from reportlab.pdfgen import canvas
# import pdfkit
from reportlab.pdfgen import canvas
import requests
import slixmpp
import time
@ -19,7 +20,7 @@ import xml.sax.saxutils as saxutils
class Actions:
def determine_mimetype(content):
def determine_mimetype(content) -> str:
m = magic.Magic(mime=True)
mimetype = m.from_buffer(content)
return mimetype
@ -29,18 +30,44 @@ class Actions:
content = response.content
return content
# Generate document using wkhtmltopdf
# def export_to_pdf(filename):
# # text = content.decode("utf-8")
# # pdfkit.from_string(text, filename + '.pdf')
# pdfkit.from_file(filename, filename + '.pdf')
def export_to_pdf(filename):
# text = content.decode("utf-8")
# pdfkit.from_string(text, filename + '.pdf')
pdfkit.from_file(filename, filename + '.pdf')
# c = canvas.Canvas("Content.pdf")
# # Add content to the canvas according to your requirements here
# c.showPage()
# c.drawString(100, 750, "Hello World")
# Single page as high as the number of lines
# def export_to_pdf(content, filename):
# with open(filename, 'r') as f:
# lines = f.readlines()
# x = 5
# y = 800
# line_height = 18
# y_adjustment = 22
# page_length = line_height*len(lines) + y_adjustment
# c = canvas.Canvas(filename + '.pdf',pagesize=(595, page_length))
# y = page_length-line_height
# for line in lines:
# line = line.rstrip()
# c.drawString(x, y, line)
# y -= line_height
# c.save()
def export_to_pdf(content, filename):
with open(filename, 'r') as f:
lines = f.readlines()
x = 5
y = 800
c = canvas.Canvas(filename + '.pdf')
for line in lines:
line = line.rstrip()
c.drawString(x, y, line)
y -= 18
if y < 36:
c.showPage()
y = 800
c.save()
def write_document(content, filename):
with open(filename, 'wb') as f:
f.write(content)
@ -171,11 +198,26 @@ class Slixprint(slixmpp.ClientXMPP):
name='slixprint',
node=None,
jid=self.boundjid.full)
while True:
rtt = None
try:
rtt = await self['xep_0199'].ping(self.boundjid.bare,
ifrom=self.boundjid,
timeout=10)
logging.info('Success! RTT: %s', rtt)
except Exception as e:
logging.error(str(e))
self.disconnect()
await asyncio.sleep(60)
async def discovery(self, DiscoInfo):
jid = DiscoInfo['from']
await self['xep_0115'].update_caps(jid=jid)
async def on_connection_failed(self, event):
await asyncio.sleep(5)
self.reconnect(wait=5.0)
async def process_message(self, message):
"""
Process incoming message stanzas. Be aware that this also
@ -204,7 +246,7 @@ class Slixprint(slixmpp.ClientXMPP):
error = Actions.print_document(filename)
if error:
logging.error(error)
Actions.export_to_pdf(filename)
Actions.export_to_pdf(content, filename)
url_upload = await self['xep_0363'].upload_file(
filename + '.pdf')
message_body = 'Error: {text}'.format(text=error)
@ -247,9 +289,9 @@ class Slixprint(slixmpp.ClientXMPP):
var='printer')
for printer in printers:
options.addOption(printer, printers[printer]["device-uri"])
session['has_next'] = True
session['next'] = self._handle_printer
session['payload'] = form
session = {'has_next' : True,
'next' : self._handle_printer,
'payload' : form}
return session
def _handle_cancel(self, payload, session):
@ -272,7 +314,6 @@ class Slixprint(slixmpp.ClientXMPP):
session['notes'] = [['info', text_note]]
return session
if __name__ == '__main__':
# Setup the command line arguments.
parser = ArgumentParser(description=Slixprint.__doc__)
@ -315,7 +356,7 @@ if __name__ == '__main__':
xmpp.register_plugin('xep_0115') # Entity Capabilities
xmpp.register_plugin('xep_0122') # Data Forms Validation
xmpp.register_plugin('xep_0199') # XMPP Ping
# xmpp.register_plugin('xep_0363') # HTTP File Upload
xmpp.register_plugin('xep_0363') # HTTP File Upload
# Connect to the XMPP server and start processing XMPP stanzas.
xmpp.connect()