convert to unicode_literals
[htsworkflow.git] / bcmagic / utils.py
1 from __future__ import unicode_literals
2
3 from django.conf import settings
4
5 import ftplib
6 import socket
7 from six.moves import StringIO
8
9
10 def print_zpl(zpl_text, host=None):
11     """
12     Sends zpl_text to printer
13     """
14     if not host:
15         host = settings.BCPRINTER_PRINTER1_HOST
16     ftp = ftplib.FTP(host=host, user='blank', passwd='')
17     ftp.login()
18     ftp.storlines("STOR printme.txt", StringIO.StringIO(zpl_text))
19     ftp.quit()
20
21
22 def print_zpl_socket(zpl_text, host=None, port=None):
23     """
24     Sends zpl_text to printer via a socket
25
26     if zpl_text is a list of zpl_texts, it will print each one
27     in that list.
28     """
29     
30     if not host:
31         host=settings.BCPRINTER_PRINTER1_HOST
32     if not port:
33         port=settings.BCPRINTER_PRINTER1_PORT
34
35     # Process anyway if zpl_text is a list.
36     if type(zpl_text) is list:
37         zpl_text = '\n'.join(zpl_text)
38
39     s = socket.socket()
40     # PORT 9100 is default for Zebra tabletop/desktop printers
41     # PORT 6101 is default for Zebra mobile printers
42     s.connect((host, port))
43     s.sendall(zpl_text)
44     s.close()
45
46
47 def report_error(message):
48     """
49     Return a dictionary with a command to display 'message'
50     """
51     return {'mode': 'Error', 'status': message}
52
53
54 def redirect_to_url(url):
55     """
56     Return a bcm dictionary with a command to redirect to 'url'
57     """
58     return {'mode': 'redirect', 'url': url}
59
60
61 def autofill(field, value):
62     """
63     Return a bcm dictionary with a command to automatically fill the
64     corresponding "field" with "value"
65     """
66     return {'mode': 'autofill', 'field': field, 'value': value}