# -*- coding: utf-8 -*-
from distutils.core import setup

packages = \
['ezprinting']

package_data = \
{'': ['*']}

install_requires = \
['google-auth>=1.5,<2.0', 'pycups>=1.9,<2.0', 'requests>=2.19,<3.0']

setup_kwargs = {
    'name': 'ezprinting',
    'version': '0.2.6',
    'description': 'Python package to easily print to a printer configured on either a CUPS server or Google Cloud Print.',
    'long_description': '# ezprinting\nPython package to easily submit print jobs to a printer configured on either a CUPS server or Google Cloud Print.\n\nGCP printing is achieved using **service account authentication** which is the best authentication method provided by Google for server-to-server interactions. This means you will need to download a **service_account.json** file from Google. \n\nWe plan to support other authentication means in the future (i.e. refresh tokens).\n\n## Installation ##\npip install ezprinting\nor\npoetry add ezpriting\n\npyCups is a dependency which needs libcups2-dev (this is the name on Ubuntu/Debian) to be installed (sudo apt install libcups2-dev).\n\n## Quick Start Guide ##\n\n**Note:** success=True/False in the examples below indicate whether or not the print job was successfully submitted to CUPS or GCP, not that it was successfully printed.\n\n### 1. Option one:\n```\nfrom ezprinting import PrintJob\n    \nwith open(\'service_account.json\', \'rb\') as f:\n    service_account = f.read()\n\nwith open(\'dummy.pdf\', \'rb\') as f:\n    content = f.read()\n\n# If we want to print to GCP...\npjob = PrintJob.new_gcp(service_account=service_account, printer_id=\'gcp-printer-id\', content=content)\nsuccess = pjob.print()\n\n# If we want to use CUPS instead...\npjob = PrintJob.new_cups(printer_name=\'cups-printer-name\', content=content)\nsuccess = pjob.print()\n\n# Use host="cups.domain.tld:631", username="lpadmin", password="123456" to specify\n# a remote cups server with authentication.\n# By default "localhost:631" is assumed with blank user/passwd\n\n\n```\n\nIf **content** is in PDF there is no need to specify content_type. Auto detection of content-type is not available yet, if you skip content_type than "application/pdf" is assumed.\n\n\n### 2. Option two:\n```\nfrom ezprinting import PrintServer, Printer, PrintJob\nimport json\n\nwith open(\'service_account.json\', \'rb\') as f:\n    service_account = f.read()\n\nwith open(\'dummy.pdf\', \'rb\') as f:\n    content = f.read()\n\n# If we want GCP...\nprint_server = PrintServer.gcp(service_account=service_account)\n\n# If we want CUPS on localhost...\nprint_server = PrintServer.cups()\n# If we want remote CUPS server...\nprint_server = PrintServer.cups(host="cups.domain.tld:631", username="lpadmin", password="123456")\n\n# the rest of the code is the same for either cups or GCP...\n\nconnection_ok, message = print_server.test_connection()\nprint("Testing connection: {} - {}".format(connection_ok, message))\n\n# Let\'s check what printers we have available\nif connection_ok:\n    printers = print_server.get_printers()\n    print(json.dumps(printers, sort_keys=True, indent=4))\n    printer = Printer(print_server, \'printer name (CUPS) or printer ID (GCP)\')\n    printer_exists = printer.check_printer_exists() \n    print("Does the printer exist on that print server? {}".format(printer_exists))\n    if printer_exists:\n        pjob = PrintJob(printer=printer, content=content)\n        success = pjob.print()\n        print(\'Print job submitted with success? {}\'.format(success))\n        if success:\n            print(\'Print job id: {}\'.format(pjob.job_id))\n```\n\n## Testing\n\nYou can easily test the functionality of this package by making use of the built in test code.\n\nTo test GCP functionality you need to add a **service_account.json** (download that from Google) to the *tests/private_data/* directory.\n\nTo test CUPS functionality you must have a valid *tests/private_data/cups.json*.\n\nTo define the documents you want to test print and the printers where those documents should be test printed you must have the files:\n* tests/private_data/printers.json\n* tests/data/print_tests.json\n\nCommented sample json files are provided (do not forget to delete the comments, JSON does not support comments).\n\n## State of this package\nThe code in this repository is being used in production and mostly works. However, it is very new and does not handle well exceptional cases.\nA large piece that is still missing is functionality on the PrintJob class to track the lifecylce of a print job and being able to figure out what went wrong when something goes wrong (e.g. paper jam, out of paper, out of ink, etc).\nYour help is welcome to fill in the gaps. And please, do file bug reports.\n\n## Main TO-DOs\n* [ ] Support GCP authentication with refresh token;\n* [ ] Add a Printer.search_by_serial_number() function to search printer name or id by serial number (GCP printer ids are somewhat volatile);\n* [ ] Develop functionality on the PrintJob class to track the state of a print job and identify causes of failure (e.g. jam, out of paper, out of ink, etc)  \n* [ ] Enable printing directly to IPP printers;\n* [ ] Add-on: mqtt monitor to send print jobs received on mqtt topics, with full QoS implementation; \n\n**Feel free to help fill-in the gaps!**\n\n## Motivation\nHaving remote web applications automatically print to local printers can be difficult. However, this is often a necessity.\n\nThe task I had at hand was to have a web-based ERP software automatically push print jobs to various printers on any organization. Before, users would click a "Print" button that would open a new tab on the user\'s browser with the PDF content displayed and again a "Print" button to actually print. While this workflow works, it adds a few more seconds and an additional click to what could be a one-click operation. Also, in some cases we wanted the printer to kick-off the action (i.e. automatically print shelf labels when a product price has changed, automatically print the picking list for a warehouse operator, etc).\n\n#### When describing the problem, we figured that:\n1. We needed to standardize "the other side". Dealing with printer models, drivers, device discovery, etc was way out of scope and we did not need to reinvent the wheel;\n2. We needed remote access to those printers with little or no configuration of the network on the remote site (even setting port forwarding can be a challenge to some customers);\n3. We needed a solution that our clients could handle themselves with little to no support.\n\n#### In the end, we decided that:\n1. Google Cloud Print is the easiest solution that any customer could handle and would work everywhere. However, not all printers support GCP;\n2. If the customer wants to suport other kinds of printers (like 80mm thermal receipt printers, Brother\'s line of QL label printers,  and even legacy equipment), he could do so by configuring everything on a dedicated CUPS (virtual) server;\n3. Going CUPS does not mean abandoning GCP. With the official [Google Cloud Print Connector](https://github.com/google/cloud-print-connector), all printers on a CUPS server will be kept sincronized with GCP.\n\n#### When testing our solution, we found out that:\n1. Setting up a CUPS servers to support any number of printers is easy, most printer manufacturers have official CUPS drivers and installing printers on CUPS is often faster than on any other solution (even Windows);\n2. Lots of clients have network-ready printers connected via USB. Having our clients move all those devices to plug directly to the network was the most difficult part;\n3. To avoid headaches, network printers should have static IP addresses which is what CUPS should point to. Do not rely on DNSSD or any other kind of discovery protocol.\n4. With a CUPS server and the [Google Cloud Print coonnector](https://github.com/google/cloud-print-connector) we had at least a few options to remotely access printers on a remote site (pure GCP, port 631 forwarding, ssh tunnel, VPN, etc) which is exactly what a web-based software hosted on the cloud needs;\n5. The missing piece of the puzzle was a python package to let us easily submit print jobs to those remote printers, and this is what this package tries to accomplish.\n\n## Other (random) notes\n* pycups <=1.9.73 has a bug that prevents CUPS from working. You will see a filter failed or some kind of "document corrupted" message;\n* The Google Cloud Connector will keep all of your CUPS printers in sync with a Google Cloud Print account. However, the\nprinter ids are not persistent, they may change if you make some changes to your CUPS printers (and do not have enough \nexperience to say when/why these ids change, but they may change). In the future we plan to support setting printer by \nserial number;\n* The service account (SA) comes with a virtual email. You must either share your printers with the email of your service \naccount or share the printers with a Google Group which contains your service account email address as a group member. \nThe Google Cloud Print Connector has a "share_scope" parameter which accepts only one email address and should be the \naddress of the Google group. However, this method did not wrk well for us so we simply share the printers with the SA email;\n* When a printer is shared with the SA email, it must be accepted first. There is an undocumented API call "/processInvite"\nwhich is the only way to accept it for a SA email. This package supports this call in the Printer.enable_printer() method. \nAlso, for GCP the function Printer.check_printer_exists() processes the invite if the first query for the printer id returns\nnothing;  \n',
    'author': 'Marcelo Bello',
    'author_email': None,
    'url': 'https://github.com/mbello/ezprinting',
    'packages': packages,
    'package_data': package_data,
    'install_requires': install_requires,
}


setup(**setup_kwargs)
