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