|
import os |
from simplegeneric import generic |
from simplegeneric import generic |
from peak.util.decorators import struct |
from peak.util.decorators import struct |
try: |
try: |
'NoPostage', 'Domestic', 'International', 'Shipment', 'Postcard', |
'NoPostage', 'Domestic', 'International', 'Shipment', 'Postcard', |
'Envelope', 'Flat', 'RectangularParcel', 'NonRectangularParcel', |
'Envelope', 'Flat', 'RectangularParcel', 'NonRectangularParcel', |
'FlatRateEnvelope', 'FlatRateBox', 'ToAddress', 'ReturnAddress', |
'FlatRateEnvelope', 'FlatRateBox', 'ToAddress', 'ReturnAddress', |
'RubberStamp', 'Print', 'Verify', |
'RubberStamp', 'Print', 'Verify', 'Batch', 'iter_docinfo', |
# ...and many more symbols added dynamically! |
# ...and many more symbols added dynamically! |
] |
] |
|
|
for ob in iter_docinfo(ob): |
for ob in iter_docinfo(ob): |
add_to_package(ob, package, isdefault) |
add_to_package(ob, package, isdefault) |
|
|
|
|
class Package: |
class Package: |
"""The XML for a single package/label""" |
"""The XML for a single package/label""" |
finished = False |
finished = False |
|
|
def __init__(self, batch): |
def __init__(self, batch): |
parent = batch.etree |
parent = batch.etree |
self.element = ET.SubElement(parent, 'Package', ID=str(len(parent)+1)) |
self.element = nested_element(parent, 'Package', ID=str(len(parent)+1)) |
self.parent = parent |
self.parent = parent |
self.queue = [] |
self.queue = [] |
|
|
else: |
else: |
el = self.element.find(tag) |
el = self.element.find(tag) |
if el is None: |
if el is None: |
el = ET.SubElement(self.element, tag) |
el = nested_element(self.element, tag, 2) |
if attr: |
if attr: |
el.attrib[attr] = unicode(value) |
el.attrib[attr] = unicode(value) |
else: |
else: |
def tostring(self, *args): |
def tostring(self, *args): |
return ET.tostring(self.etree, *args) |
return ET.tostring(self.etree, *args) |
|
|
def ship(self, *packageinfo): |
def add_package(self, *packageinfo): |
"""Add `package` to batch, with error recovery""" |
"""Add `package` to batch, with error recovery""" |
etree = self.etree |
etree = self.etree |
before = etree.attrib.copy() |
before = etree.attrib.copy(), etree.text |
self.packages.append(packageinfo) |
self.packages.append(packageinfo) |
package = Package(self) |
package = Package(self) |
try: |
try: |
package.finish() |
package.finish() |
except: |
except: |
del etree[-1], self.packages[-1] |
del etree[-1], self.packages[-1] |
etree.attrib = before |
if etree: etree[-1].tail = etree.text[:-4] |
|
etree.attrib, etree.text = before |
raise |
raise |
|
|
|
def nested_element(parent, tag, indent=1, **kw): |
|
"""Like ET.SubElement, but with pretty-printing indentation""" |
|
element = ET.SubElement(parent, tag, **kw) |
|
parent.text='\n'+' '*indent |
|
element.tail = parent.text[:-4] |
|
if len(parent)>1: |
|
parent[-2].tail = parent.text |
|
return element |
|
|
|
|
|
|
|
|
self.batches = [] |
self.batches = [] |
self.rules = rules |
self.rules = rules |
|
|
def ship(self, *packageinfo): |
def add_package(self, *packageinfo): |
for batch in self.batches: |
for batch in self.batches: |
try: |
try: |
return batch.ship(*packageinfo) |
return batch.add_package(*packageinfo) |
except DocInfoConflict: |
except DocInfoConflict: |
pass |
pass |
|
|
batch = Batch(*self.rules) |
batch = Batch(*self.rules) |
batch.ship(*packageinfo) |
batch.add_package(*packageinfo) |
|
|
# only add the batch if the above operations were successful... |
# only add the batch if the above operations were successful... |
self.batches.append(batch) |
self.batches.append(batch) |
|
|
WeekendDelivery = ~NoWeekendDelivery |
WeekendDelivery = ~NoWeekendDelivery |
HolidayDelivery = ~NoHolidayDelivery |
HolidayDelivery = ~NoHolidayDelivery |
NoPostage = DocInfo('MailClass', 'NONE') |
|
|
|
|
|
|
|
'tag', """ |
'tag', """ |
ToName ToTitle ToCompany ToCity ToState ToPostalCode ToZIP4 ToCountry |
ToName ToTitle ToCompany ToCity ToState ToPostalCode ToZIP4 ToCountry |
ToCarrierRoute ToReturnCode ToEmail ToPhone EndorsementLine ReferenceID |
ToCarrierRoute ToReturnCode ToEmail ToPhone EndorsementLine ReferenceID |
ToDeliveryPoint CustomsSigner Description |
ToDeliveryPoint CustomsSigner Description MailClass |
|
|
WeightOz Width Length Depth CostCenter Value |
WeightOz Width Length Depth CostCenter Value |
""".split(), lambda tag: DocInfo(tag).clone |
""".split(), lambda tag: DocInfo(tag).clone |
) |
) |
|
|
|
NoPostage = MailClass('NONE') |
|
|
def Layout(filename): |
def Layout(filename): |
"""Return a docinfo specifying the desired layout""" |
"""Return a docinfo specifying the desired layout""" |
return DocInfo('DAZzle', os.path.abspath(filename), 'Layout') |
return DocInfo('DAZzle', os.path.abspath(filename), 'Layout') |
|
|
|
|
|
|
|
|
|
|
class Domestic: |
class Domestic: |
FirstClass = DocInfo('MailClass', 'FIRST') |
FirstClass = DocInfo('MailClass', 'FIRST') |
Priority = DocInfo('MailClass', 'PRIORITY') |
Priority = DocInfo('MailClass', 'PRIORITY') |