Skip to content

Commit 2b102f3

Browse files
author
Greg Taylor
committed
Beginnings of a label certification module called label_certification. Also added a printers module with some common patterns for printing labels. Usage of these is entirely optional.
1 parent a6412ab commit 2b102f3

File tree

6 files changed

+154
-1
lines changed

6 files changed

+154
-1
lines changed

examples/example_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from fedex.config import FedexConfig
1010

1111
CONFIG_OBJ = FedexConfig(key='ZyNQQFdcxUATOx9L',
12-
password='GtngmKzs4Dk4RYmrlAjrLykwi',
12+
password='8irpTkULT1zjVLlL8XiVczTex',
1313
account_number='510087780',
1414
meter_number='118501898',
1515
use_test_server=True)

fedex/printers/__init__.py

Whitespace-only changes.

fedex/printers/unix.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
self.device = device
15+
self.shipment = shipment
16+
17+
def print_label(self, package_num=None):
18+
"""
19+
Prints a shipment's labels, or optionally just one.
20+
21+
package_num: (int) 0-based index of the package to print. This is
22+
only useful for shipments with more than one package.
23+
"""
24+
if package_num:
25+
packages = [self.shipment.response.CompletedShipmentDetail.CompletedPackageDetails[package_num]]
26+
else:
27+
packages = self.shipment.response.CompletedShipmentDetail.CompletedPackageDetails
28+
29+
for package in packages:
30+
label_binary = binascii.a2b_base64(package.Label.Parts[0].Image)
31+
self.print_base64(label_binary)
32+
33+
def print_base64(self, base64_data):
34+
"""
35+
Pipe the binary directly to the label printer. Works under Linux
36+
without requiring PySerial.
37+
"""
38+
label_file = open(self.device, "w")
39+
label_file.write(base64_data)
40+
label_file.close()

label_certification/__init__.py

Whitespace-only changes.

label_certification/cert_config.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
This file holds configuration for your test account. Make SURE to change
3+
the values below to your account's TESTING meter number.
4+
"""
5+
import os
6+
import sys
7+
# Use the fedex directory included in the downloaded package instead of
8+
# any globally installed versions.
9+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
10+
from fedex.config import FedexConfig
11+
12+
CONFIG_OBJ = FedexConfig(key='ZyNQQFdcxUATOx9L',
13+
password='8irpTkULT1zjVLlL8XiVczTex',
14+
account_number='510087780',
15+
meter_number='118501898',
16+
use_test_server=True)
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env python
2+
"""
3+
This module prints a label used to certify International Priority shipments.
4+
"""
5+
import logging
6+
from cert_config import CONFIG_OBJ
7+
from fedex.services.ship_service import FedexProcessShipmentRequest
8+
from fedex.printers.unix import DirectDevicePrinter
9+
10+
logging.basicConfig(level=logging.INFO)
11+
12+
shipment = FedexProcessShipmentRequest(CONFIG_OBJ)
13+
shipment.RequestedShipment.DropoffType = 'REGULAR_PICKUP'
14+
shipment.RequestedShipment.ServiceType = 'INTERNATIONAL_PRIORITY'
15+
shipment.RequestedShipment.PackagingType = 'YOUR_PACKAGING'
16+
shipment.RequestedShipment.PackageDetail = 'INDIVIDUAL_PACKAGES'
17+
18+
# Shipper contact info.
19+
shipment.RequestedShipment.Shipper.Contact.PersonName = 'Gregory Taylor'
20+
shipment.RequestedShipment.Shipper.Contact.CompanyName = 'International Paper'
21+
shipment.RequestedShipment.Shipper.Contact.PhoneNumber = '8646336010'
22+
23+
# Shipper address.
24+
shipment.RequestedShipment.Shipper.Address.StreetLines = ['155 Old Greenville Hwy',
25+
'Suite 103']
26+
shipment.RequestedShipment.Shipper.Address.City = 'Clemson'
27+
shipment.RequestedShipment.Shipper.Address.StateOrProvinceCode = 'SC'
28+
shipment.RequestedShipment.Shipper.Address.PostalCode = '29631'
29+
shipment.RequestedShipment.Shipper.Address.CountryCode = 'US'
30+
shipment.RequestedShipment.Shipper.Address.Residential = False
31+
32+
# Recipient contact info.
33+
shipment.RequestedShipment.Recipient.Contact.PersonName = 'Some Guy'
34+
#shipment.RequestedShipment.Recipient.Contact.CompanyName = 'Recipient Company'
35+
shipment.RequestedShipment.Recipient.Contact.PhoneNumber = '8646336010'
36+
37+
# Recipient address
38+
shipment.RequestedShipment.Recipient.Address.StreetLines = ['77 Foo Young']
39+
shipment.RequestedShipment.Recipient.Address.City = 'Taipei'
40+
shipment.RequestedShipment.Recipient.Address.PostalCode = '115'
41+
shipment.RequestedShipment.Recipient.Address.CountryCode = 'TW'
42+
43+
shipment.RequestedShipment.ShippingChargesPayment.PaymentType = 'SENDER'
44+
45+
# Specifies the label type to be returned.
46+
# LABEL_DATA_ONLY or COMMON2D
47+
shipment.RequestedShipment.LabelSpecification.LabelFormatType = 'COMMON2D'
48+
49+
# Specifies which format the label file will be sent to you in.
50+
# DPL, EPL2, PDF, PNG, ZPLII
51+
shipment.RequestedShipment.LabelSpecification.ImageType = 'EPL2'
52+
53+
# To use doctab stocks, you must change ImageType above to one of the
54+
# label printer formats (ZPLII, EPL2, DPL).
55+
# See documentation for paper types, there quite a few.
56+
shipment.RequestedShipment.LabelSpecification.LabelStockType = 'STOCK_4X6.75_LEADING_DOC_TAB'
57+
58+
# This indicates if the top or bottom of the label comes out of the
59+
# printer first.
60+
# BOTTOM_EDGE_OF_TEXT_FIRST or TOP_EDGE_OF_TEXT_FIRST
61+
shipment.RequestedShipment.LabelSpecification.LabelPrintingOrientation = 'BOTTOM_EDGE_OF_TEXT_FIRST'
62+
63+
package1_weight = shipment.create_wsdl_object_of_type('Weight')
64+
package1_weight.Value = 10.0
65+
package1_weight.Units = "LB"
66+
67+
package1 = shipment.create_wsdl_object_of_type('RequestedPackageLineItem')
68+
package1.Weight = package1_weight
69+
70+
shipment.add_package(package1)
71+
72+
shipment.RequestedShipment.InternationalDetail.DocumentContent = "NON_DOCUMENTS"
73+
shipment.RequestedShipment.InternationalDetail.CustomsValue.Currency = "USD"
74+
shipment.RequestedShipment.InternationalDetail.CustomsValue.Amount = "10.00"
75+
76+
commod = shipment.create_wsdl_object_of_type('Commodity')
77+
commod.NumberOfPieces = "1"
78+
commod.Description = "Technical Book"
79+
commod.CountryOfManufacture = "US"
80+
commod.Weight.Units = "LB"
81+
commod.Weight.Value = "5.0"
82+
commod.Quantity = "1"
83+
commod.QuantityUnits = "EA"
84+
commod.UnitPrice.Currency = "USD"
85+
commod.UnitPrice.Amount = "10.00"
86+
shipment.RequestedShipment.InternationalDetail.Commodities.append(commod)
87+
88+
shipment.RequestedShipment.InternationalDetail.DutiesPayment.PaymentType = "SENDER"
89+
shipment.RequestedShipment.InternationalDetail.DutiesPayment.Payor.AccountNumber = CONFIG_OBJ.account_number
90+
shipment.RequestedShipment.InternationalDetail.DutiesPayment.Payor.CountryCode = "US"
91+
92+
shipment.send_request()
93+
94+
print shipment.response.CompletedShipmentDetail.CompletedPackageDetails[0].TrackingIds
95+
96+
device = DirectDevicePrinter(shipment)
97+
device.print_label()

0 commit comments

Comments
 (0)