fedex.printers.unix

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