Last modified: 02.03.2009
Parcelnet parser

From time to time I needed to debug an output file which is send over to Parcelnet. a UK delivery and fulfilment provider. A little script took away some pain here:


#!/usr/bin/env python
#
#       parcelnet.py
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

path = "input.txt"

class Request:
	def __init__ (self, line):
		self.line = line
		self.Req_Rec_Record_Type = self.cutLine(2)
		self.Req_Rec_Client_ID  = self.cutLine(3)
		self.Req_Rec_Client_Child_ID = self.cutLine(3)
		self.Req_Rec_Client_Name = self.cutLine(32)
		self.Req_Rec_Client_Child_Name = self.cutLine(32)
		self.Req_Rec_Customer_Name = self.cutLine(32)
		self.Req_Rec_Customer_Address_1 = self.cutLine(32)
		self.Req_Rec_Customer_Address_2 = self.cutLine(32)
		self.Req_Rec_Customer_Address_3 = self.cutLine(32)
		self.Req_Rec_Customer_Address_4 = self.cutLine(32)
		self.Req_Rec_Customer_Address_5 = self.cutLine(32)
		self.Req_Rec_Customer_Address_6 = self.cutLine(32)
		self.Req_Rec_Customer_Outward_Postcode = self.cutLine(4)
		self.Req_Rec_Customer_Postcode_Filler = self.cutLine(1)
		self.Req_Rec_Customer_Inward_Postcode = self.cutLine(3)
		self.Req_Rec_Parcel_Weight = self.cutLine(7)
		self.Req_Rec_Parcel_Length = self.cutLine(4)
		self.Req_Rec_Parcel_Width = self.cutLine(4)
		self.Req_Rec_Parcel_Depth = self.cutLine(4)
		self.Req_Rec_Parcel_Girth = self.cutLine(4)
		self.Req_Rec_Parcel_Combined_Dimension = self.cutLine(4)
		self.Req_Rec_Parcel_Volume = self.cutLine(9)
		self.Req_Rec_Parcel_Value = self.cutLine(8)
		self.Req_Rec_Fragile_Flag = self.cutLine(1)
		self.Req_Rec_Consigned_Flag = self.cutLine(1)
		self.Req_Rec_Installation_Flag = self.cutLine(1)
		self.Req_Rec_Hanging_Garment_Flag = self.cutLine(1)
		self.Req_Rec_Theft_Risk_Flag = self.cutLine(1)
		self.Req_Rec_Multiple_Parts_Flag = self.cutLine(1)
		self.Req_Rec_Stated_Day_Flag = self.cutLine(1)
		self.Req_Rec_Stated_Time_Flag = self.cutLine(1)
		self.Req_Rec_Delivery_Service_Flag = self.cutLine(1)
		self.Req_Rec_Signature_Flag = self.cutLine(1)
		self.Req_Rec_Filler_1 = self.cutLine(100)
		self.Req_Rec_Number_of_Parts = self.cutLine(2)
		self.Req_Rec_Catalogue_Flag = self.cutLine(1)
		self.Req_Rec_Parcel_Description = self.cutLine(32)
		self.Req_Rec_Customer_Reference_1 = self.cutLine(20)
		self.Req_Rec_Customer_Reference_2 = self.cutLine(20)
		self.Req_Rec_Customer_Home_Phone_No = self.cutLine(15)
		self.Req_Rec_Customer_Work_Phone_No = self.cutLine(15)
		self.Req_Rec_Customer_Mobile_Phone_No = self.cutLine(15)
		self.Req_Rec_Customer_E_Mail_Address = self.cutLine(80)
		self.Req_Rec_Delivery_Message = self.cutLine(32)
		self.Req_Rec_System_Message = self.cutLine(32)
		self.Req_Rec_Special_Instructions_1 = self.cutLine(32)
		self.Req_Rec_Special_Instructions_2 = self.cutLine(32)
		self.Req_Rec_Required_Delivery_Date = self.cutLine(8)
		self.Req_Rec_Required_Delivery_Time = self.cutLine(4)
		self.Req_Rec_Delivery_Instruction_Flag = self.cutLine(1)
		self.Req_Rec_Number_of_Items = self.cutLine(2)
		self.Req_Rec_Parcel_Origin = self.cutLine(8)
		self.Req_Rec_Expected_Despatch_Date = self.cutLine(8)
		self.Req_Rec_Supplier_Code = self.cutLine(4)
		self.Req_Rec_Filler_2 = self.cutLine(20)

	def cutLine(self, offset):
		tmp = self.line[:offset]
		self.line = self.line[offset:]
		return tmp

###################################################################
# Enter here information (= fields) you need to know. Variable are
# called similar to spec 2007/05/21
###################################################################
	def display(self):
		print "Arg 1.:", self.Req_Rec_Customer_Reference_1,
		print "Arg 2.:", self.Req_Rec_Customer_Reference_2


def main():
	listOfLines = []

	for i in open(path, "r"):
	# This values needs to be adjusted based on the defined values
	# provided by Parcelnet
		if i.startswith("012") or i.startswith("99"):
			continue
		else:
			listOfLines.append(Request(i))

	for foo in listOfLines:
		foo.display()

	return 0

if __name__ == '__main__': main()