Package fedex :: Package printers :: Module unix
[hide private]
[frames] | no frames]

Source Code for Module fedex.printers.unix

 1  """ 
 2  This module provides a label printing wrapper class for Unix-based 
 3  installations. By "Unix", we mean Linux, Mac OS, BSD, and various flavors 
 4  of Unix. 
 5  """ 
 6   
 7  import binascii 
 8   
 9   
10 -class DirectDevicePrinter(object):
11 """ 12 This class pipes the label data directly through a /dev/* entry. 13 Consequently, this is very Unix/Linux specific. It *MAY* work on Mac too. 14 """ 15
16 - def __init__(self, shipment, device="/dev/ttyS0"):
17 """ 18 Instantiates from a shipment object. You may optionally specify 19 a path to a /dev/ device. Defaults to /dev/ttyS0. 20 21 @type shipment: L{FedexProcessShipmentRequest} 22 @param shipment: A Fedex ProcessShipmentRequest object to pull the 23 printed label data from. 24 """ 25 26 self.device = device 27 """@ivar: A string with the path to the device to print to.""" 28 self.shipment = shipment 29 """@ivar: A reference to the L{FedexProcessShipmentRequest} to print."""
30
31 - def print_label(self, package_num=None):
32 """ 33 Prints all of a shipment's labels, or optionally just one. 34 35 @type package_num: L{int} 36 @param package_num: 0-based index of the package to print. This is 37 only useful for shipments with more than one package. 38 """ 39 40 if package_num: 41 packages = [ 42 self.shipment.response.CompletedShipmentDetail.CompletedPackageDetails[package_num] 43 ] 44 else: 45 packages = self.shipment.response.CompletedShipmentDetail.CompletedPackageDetails 46 47 for package in packages: 48 label_binary = binascii.a2b_base64(package.Label.Parts[0].Image) 49 self._print_base64(label_binary)
50
51 - def _print_base64(self, base64_data):
52 """ 53 Pipe the binary directly to the label printer. Works under Linux 54 without requiring PySerial. This is not typically something you 55 should call directly, unless you have special needs. 56 57 @type base64_data: L{str} 58 @param base64_data: The base64 encoded string for the label to print. 59 """ 60 61 label_file = open(self.device, "w") 62 label_file.write(base64_data) 63 label_file.close()
64