How to use get_line_text method in Gherkin-python

Best Python code snippet using gherkin-python

Run Gherkin-python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

TIDclass.py

Source: TIDclass.py Github

copy
1import sys,getopt
2from pdfminer.pdfparser import PDFParser, PDFDocument
3from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
4from pdfminer.converter import PDFPageAggregator
5from pdfminer.layout import LAParams, LTTextBox, LTTextLine, LTFigure, LTImage, LTChar
6import json
7
8
9class tid:
10	def __init__(self, filename):
11		self.filename = filename
12
13	# @staticmethod	
14
15	def get_layout_count(self,layout):
16		return len(layout)
17
18
19	def get_line_text(self,layout,line_number):
20		i=1
21		for lt_obj in layout:
22			if isinstance(lt_obj, LTTextBox) :
23				if i==line_number :
24					# print('--in function--%s' % i)
25					x=lt_obj.get_text()
26					return x.strip()
27				i=i+1
28		return ''
29
30	def getInfo(self):
31		fp = open(self.filename, 'rb')
32		parser = PDFParser(fp)
33		doc = PDFDocument()
34		parser.set_document(doc)
35		doc.set_parser(parser)
36		doc.initialize('')
37		rsrcmgr = PDFResourceManager()
38		laparams = LAParams()
39		device = PDFPageAggregator(rsrcmgr, laparams=laparams)
40		interpreter = PDFPageInterpreter(rsrcmgr, device)
41		# Process each page contained in the document.
42		images_folder=""
43		text_content = []
44		# print ("Info")
45		import re
46		rex = re.compile("^[0-9]{2}[A-Z][0-9]{2}[A-Z][0-9]$")
47
48		for page in doc.get_pages():
49			interpreter.process_page(page)
50			layout = device.get_result()
51			layout_count = self.get_layout_count(layout)
52			i=1
53
54			# print ('LayOut Count : %s' % layout_count)
55			# if layout_count =9 , means Full1Box (IN) or MTY1Box(OUT)
56			company=self.get_company(layout,layout_count)
57			lpn = self.get_license_plate_number(layout,layout_count)
58			
59
60			contain1_no = self.get_container1(layout,layout_count)
61			contain1_loc =  self.get_location1(layout,layout_count)
62			contain1_seal = self.get_seal_number1(layout,layout_count)
63
64			contain2_no = self.get_container2(layout,layout_count)
65			contain2_loc =  self.get_location2(layout,layout_count)
66			contain2_seal = self.get_seal_number2(layout,layout_count)
67
68
69			call_card = self.get_call_card(layout,layout_count)
70			time_stamp =self.get_timestamp(layout,layout_count)
71
72			data = {
73					'filename' : self.filename,
74					'type' : layout_count,
75					'company' : company ,
76					'license_no': lpn,
77					'call_card':call_card,
78					'time_stamp' : time_stamp
79					}
80
81
82
83			if layout_count == 9 :
84				emptyFixContainer=False
85				# print("Full (IN)/Empty(Out) 1 Box TID")
86				c2_text = self.get_line_text(layout,2)
87				c3_text = self.get_line_text(layout,3)
88
89				# if len(c2_text) > 1 :
90				# 	direction='in'
91				# 	# print ('Full (IN) 1 box')
92				# else:
93				# 	direction='out'
94				# 	# print ('Full (OUT) 1 box')
95				if c2_text=='-' and c3_text=='-' :
96					direction='out'
97					# print ('Full (IN) 1 box')
98				else:
99					y = c2_text.split('  ')
100					if len(y) > 1 :
101						direction='in'
102					else :
103						emptyFixContainer = True
104						direction='out'
105					# print ('Full (OUT) 1 box')
106
107				if direction=='in':
108					data[direction]={}
109					data[direction]['item1']={'container_no' : contain1_no,
110								'location' : contain1_loc,
111								'seal' : contain1_seal}
112
113				if direction=='out':
114					# either 1 or 2 Full out
115					c4_text = self.get_line_text(layout,4)
116					y= c4_text.split('  ')
117					data[direction]={}
118
119					if emptyFixContainer :
120						data[direction]['item1']={'container_no' : contain1_loc,
121									'location' : '',
122									'seal' : contain1_seal,
123									'line3':''}
124					else:
125						if '-' in c4_text :
126							data[direction]['item1']={'container_no' : contain1_no,
127									'location' : contain1_loc,
128									'seal' : contain1_seal,
129									'line3':''}
130						else:
131							data[direction]['item1']={'container_no' : contain1_no,
132									'location' : contain1_loc,
133									'seal' : contain1_seal,
134									'line3' : ''}
135							data[direction]['item2']={'container_no' : contain2_no,
136									'location' : contain2_loc,
137									'seal' : contain2_seal,
138									'line3' : ''}
139
140
141					# data[direction]['item1']={'container_no' : contain1_no,
142					# 			'location' : contain1_loc,
143					# 			'seal' : contain1_seal}
144				
145			if layout_count == 7 :
146				data['in']={}
147				data['in']['item1']={'container_no' : contain1_no,
148							'location' : contain1_loc,
149							'seal' : contain1_seal}
150
151				if self.get_line_text(layout,3) == '-' :
152					data['in']['item2']={'container_no' : contain2_no,
153					'location' : contain2_loc,
154					'seal' : contain2_seal}
155				else:
156					data['out']={}
157					data['out']['item1'] ={'container_no':contain2_no,
158						'location' : contain2_loc,
159						'seal':contain2_seal,
160						'line3':''}
161						# print('')
162
163
164
165			if layout_count == 10 :
166				data['in']={}
167				data['in']['item1']={'container_no' : contain1_no,
168							'location' : contain1_loc,
169							'seal' : contain1_seal}
170
171
172				tmp=self.get_line3text1(layout,layout_count)
173				if rex.match(tmp):
174					location=tmp
175				else:
176					location=''
177
178				data['out']={}
179				data['out']['item1']={
180							'container_no' : self.get_service1(layout,layout_count),
181							'seal' : self.get_container1_type(layout,layout_count),
182							'line3' : self.get_line3text1(layout,layout_count),
183							'location' : location}
184
185				tmp=self.get_line3text2(layout,layout_count)
186				if rex.match(tmp):
187					location=tmp
188				else:
189					location=''
190
191				data['out']['item2']={
192							'container_no' : self.get_service2(layout,layout_count),
193							'seal' : self.get_container2_type(layout,layout_count),
194							'line3' : self.get_line3text2(layout,layout_count),
195							'location' : location}
196
197			if layout_count == 11 :
198				data['out']={}
199				tmp=self.get_line3text1(layout,layout_count)
200				if rex.match(tmp):
201					location=tmp
202				else:
203					location=''
204
205				data['out']['item1']={
206				'container_no' : self.get_service1(layout,layout_count),
207				'seal' : self.get_container1_type(layout,layout_count),
208				'line3' : self.get_line3text1(layout,layout_count),
209				'location' : location
210				}
211
212				if self.get_line_text(layout,3)=='-' :
213					tmp=self.get_line3text2(layout,layout_count)
214					if rex.match(tmp):
215						location=tmp
216					else:
217						location=''
218
219					data['out']['item2']={
220								'container_no' : self.get_service2(layout,layout_count),
221								'seal' : self.get_container2_type(layout,layout_count),
222								'line3' : self.get_line3text2(layout,layout_count),
223								'location' : location}
224
225
226			if layout_count == 12 :
227				data['out']={}
228				tmp=self.get_line3text1(layout,layout_count)
229				if rex.match(tmp):
230					location=tmp
231				else:
232					location=''
233
234				data['out']['item1']={
235							'container_no' : self.get_service1(layout,layout_count),
236							'seal' : self.get_container1_type(layout,layout_count),
237							'line3' : self.get_line3text1(layout,layout_count),
238							'location' :location}
239
240				tmp=self.get_line3text2(layout,layout_count)
241				if rex.match(tmp):
242					location=tmp
243				else :
244					location=''
245
246				data['out']['item2']={
247							'container_no' : self.get_service2(layout,layout_count),
248							'seal' : self.get_container2_type(layout,layout_count),
249							'line3' : self.get_line3text2(layout,layout_count),
250							'location' :location}
251
252
253			return data
254				# For View file details
255			# if argv[1] == 'y':
256			   #  for lt_obj in layout:
257			   #      if isinstance(lt_obj, LTTextBox) :
258			   #      	line_text=lt_obj.get_text()
259
260			   #      	print('%s) %s' % (i,line_text))
261			   #      	i=i+1
262
263
264			# print('-------------End Page----------------------------------------------')
265	
266	def getRaw(self):
267		fp = open(self.filename, 'rb')
268		parser = PDFParser(fp)
269		doc = PDFDocument()
270		parser.set_document(doc)
271		doc.set_parser(parser)
272		doc.initialize('')
273		rsrcmgr = PDFResourceManager()
274		laparams = LAParams()
275		device = PDFPageAggregator(rsrcmgr, laparams=laparams)
276		interpreter = PDFPageInterpreter(rsrcmgr, device)
277		# Process each page contained in the document.
278		images_folder=""
279		text_content = []
280		# print ("Info")
281		import re
282		rex = re.compile("^[0-9]{2}[A-Z][0-9]{2}[A-Z][0-9]$")
283		i=0
284		for page in doc.get_pages():
285			interpreter.process_page(page)
286			layout = device.get_result()
287			for lt_obj in layout:
288				if isinstance(lt_obj, LTTextBox) :
289					line_text=lt_obj.get_text()
290					print('%s) %s' % (i,line_text))
291					i=i+1
292
293	# tid_type 
294	# 9 : Full(in) 1 box OR Empty(out) 1 box
295	# 7 : Full(in) 2 box
296	#12 : Empty(out) 2 box
297	def get_company(self,layout,tid_type):
298		line_text = self.get_line_text(layout,1)
299		x = line_text.split('    ')
300		return x[0].strip()
301		# if tid_type == 9 or tid_type == 7 :
302		# 	return x[0].strip()
303
304		# if tid_type == 12 :
305		# 	return x[0].strip()
306
307	def get_license_plate_number(self,layout,tid_type):
308		if tid_type == 7 or tid_type == 10:
309			line_text = self.get_line_text(layout,1)
310			x = line_text.split('    ')
311			return x[len(x)-1].strip().replace(' ','')
312
313		if tid_type == 9 :
314			if len(self.get_line_text(layout,1))>2:
315				line_text = self.get_line_text(layout,2)
316				x = line_text.split('    ')
317				return x[0].strip().replace(' ','')
318			else :
319				line_text = self.get_line_text(layout,1)
320				x = line_text.split('    ')
321				return x[len(x)-1].strip().replace(' ','')
322
323
324
325
326
327		if tid_type == 11 :
328			line_text = self.get_line_text(layout,3) #Check 
329			if line_text == '-':
330				line_text = self.get_line_text(layout,1)
331				x = line_text.split('    ')
332				return x[len(x)-1].strip().replace(' ','')
333			else:
334				line_text = self.get_line_text(layout,2)
335				return line_text.strip().replace(' ','')
336
337
338
339		if tid_type == 12 :
340			line_text = self.get_line_text(layout,2)
341			x = line_text.split('    ')
342			return x[0].strip().replace(' ','')
343
344	def get_container1(self,layout,tid_type):
345		if tid_type == 9 or tid_type == 7 :
346			line_text = self.get_line_text(layout,2).strip()
347			# print (line_text)
348			if len(line_text) == 1:
349				# return self.get_line_text(layout,4).strip(x = line_text.split('    ')).replace(' ','').replace('-','')
350				line_text = self.get_line_text(layout,4).strip()
351				x = line_text.split('    ')
352				return x[0].strip().replace(' ','')
353			else:
354				x = line_text.split('    ')
355				return x[0].strip().replace(' ','')
356
357		if tid_type == 10 :
358			line_text = self.get_line_text(layout,3).strip()
359			x = line_text.split('    ')
360			return x[0].strip().replace(' ','')
361
362
363	def get_container2(self,layout,tid_type):
364		if tid_type == 7 :
365			# print ('Line #3 %s' % self.get_line_text(layout,3).strip())
366			if self.get_line_text(layout,3).strip()=='-':
367				line_text = self.get_line_text(layout,2).strip()
368				x = line_text.split('    ')
369				return x[1].strip().replace(' ','')
370			else:
371				# print ('get Container#2')
372				line_text = self.get_line_text(layout,3).strip()
373				x = line_text.split('    ')
374				return x[0].strip().replace(' ','')
375
376
377		if tid_type == 9 :
378			line_text = self.get_line_text(layout,4).strip()
379			x = line_text.split('    ')
380			return x[len(x)-1].strip().replace(' ','')
381
382
383	def get_location1(self,layout,tid_type):
384		if tid_type == 7 : #Full(in)2 boxes
385			line_text = self.get_line_text(layout,4).strip()
386			x = line_text.split('    ')
387			return x[0].strip().replace(' ','')
388
389		if tid_type == 10 : 
390			line_text = self.get_line_text(layout,7).strip()
391			x = line_text.split('    ')
392			return x[0].strip().replace(' ','')
393
394		if tid_type == 9 :
395			line_text = self.get_line_text(layout,5).strip()
396			if len(line_text) > 1 :
397				
398				return line_text.strip().replace(' ','').replace('-','')
399			else :
400				line_text = self.get_line_text(layout,7).strip()
401				x = line_text.split('    ')
402				return x[0].strip().replace(' ','').replace('-','')
403				# return self.get_line_text(layout,7).strip().replace(' ','').replace('-','')
404			# print (line_text)
405			# if len(line_text) == 1:
406			# 	return get_line_text(layout,4).strip().replace(' ','').replace('-','')
407			# else:
408				
409			# 	return x[0].strip().replace(' ','')
410
411	def get_location2(self,layout,tid_type):
412		if tid_type == 7 : #Full(in)2 boxes
413			# print ('line #3 %s ' % self.get_line_text(layout,3).strip() )
414			if self.get_line_text(layout,3).strip()=='-' :
415				line_text = self.get_line_text(layout,4).strip()
416				x = line_text.split('    ')
417				return x[2].strip().replace(' ','')
418			else :
419				line_text = self.get_line_text(layout,5).strip()
420				# print ('line #5 %s ' % line_text )
421				x = line_text.split('    ')
422				return x[0].strip().replace(' ','')
423
424		if tid_type == 9 : #Full(out)2 boxes
425			line_text = self.get_line_text(layout,7).strip()
426			x = line_text.split('    ')
427			return x[len(x)-1].strip().replace(' ','')
428
429
430	def get_seal_number1(self,layout,tid_type):
431		if tid_type == 9 or tid_type == 7 :
432			line_text = self.get_line_text(layout,6).strip()
433			# print (line_text)
434			if len(line_text) == 1:
435				#1 Full Box IN
436				# return self.get_line_text(layout,8).strip().replace(' ','').replace('-','')
437				line_text = self.get_line_text(layout,8).strip()
438				x = line_text.split('    ')
439				return x[0].strip().replace(' ','').replace('-','')
440			else:
441				#2 Full boxes IN
442				x = line_text.split('    ')
443				return x[0].strip().replace(' ','').replace('-','')
444
445		if tid_type == 10 :
446			line_text = self.get_line_text(layout,9).strip()
447			x = line_text.split('    ')
448			return x[0].strip().replace(' ','').replace('-','')
449
450
451	def get_seal_number2(self,layout,tid_type):
452		if tid_type == 7 :
453			line_text = self.get_line_text(layout,6).strip()
454			#2 Full boxes IN
455			x = line_text.split('    ')
456			return x[len(x)-1].strip().replace(' ','').replace('-','')
457
458
459
460		if tid_type == 9 : #Full(out)2 boxes
461			line_text = self.get_line_text(layout,8).strip()
462			x = line_text.split('    ')
463			return x[len(x)-1].strip().replace(' ','')
464		
465
466	def get_call_card(self,layout,tid_type):
467		if tid_type == 9 or tid_type == 7 :
468			line_text = self.get_line_text(layout,9).strip()
469			# print (line_text)
470			if len(line_text) == 0:
471				#1 Full Box IN
472				line_text=self.get_line_text(layout,7).strip()
473				x = line_text.split('    ')
474				return x[0].strip().replace(' ','').replace('-','')
475			else:
476				#2 Full boxes IN
477				x = line_text.split('    ')
478				return x[0].strip().replace(' ','').replace('-','')
479
480		if tid_type == 10 :
481			line_text = self.get_line_text(layout,10).strip()
482			x = line_text.split('    ')
483			return x[0].strip().replace(' ','').replace('-','')
484		if tid_type == 11 :
485			line_text = self.get_line_text(layout,11).strip()
486			x = line_text.split('    ')
487			return x[0].strip().replace(' ','').replace('-','')
488		if tid_type == 12 :
489			line_text = self.get_line_text(layout,12).strip()
490			x = line_text.split('    ')
491			return x[0].strip().replace(' ','').replace('-','')
492
493	def get_timestamp(self,layout,tid_type):
494		if tid_type == 9 or tid_type == 7 :
495			line_text = self.get_line_text(layout,9).strip()
496			# print (line_text)
497			if len(line_text) == 0:
498				#1 Full Box IN
499				line_text=self.get_line_text(layout,7).strip()
500				x = line_text.split('    ')
501				# print (len(x))
502				return x[len(x)-1].strip().replace('-','')
503			else:
504				#2 Full boxes IN
505				x = line_text.split('    ')
506				# print (len(x))
507				return x[len(x)-1].strip().replace('-','')
508
509		if tid_type == 10 :
510			line_text = self.get_line_text(layout,10).strip()
511			x = line_text.split('    ')
512			return x[len(x)-1].strip().replace('-','')
513
514		if tid_type == 11 :
515			line_text = self.get_line_text(layout,11).strip()
516			x = line_text.split('    ')
517			return x[len(x)-1].strip().replace(' ','').replace('-','')
518		if tid_type == 12 :
519			line_text = self.get_line_text(layout,12).strip()
520			x = line_text.split('    ')
521			return x[len(x)-1].strip().replace(' ','').replace('-','')
522
523			
524	def get_service1(self,layout,tid_type):
525		if tid_type == 10 :
526			line_text = self.get_line_text(layout,2).strip()
527			x = line_text.split('    ')
528			return x[0].strip().replace(' ','')
529
530		if tid_type == 11 :
531			line_text = self.get_line_text(layout,3).strip()
532			if line_text=='-':
533				line_text = self.get_line_text(layout,2).strip()
534				x = line_text.split('    ')
535				return x[0].strip().replace(' ','')
536			else:
537				line_text = self.get_line_text(layout,3).strip()
538				return line_text.strip().replace(' ','')
539
540
541		if tid_type == 12 :
542			line_text = self.get_line_text(layout,3).strip()
543			x = line_text.split('    ')
544			return x[0].strip().replace(' ','')
545
546	def get_service2(self,layout,tid_type):
547
548		if tid_type == 10 or tid_type == 11 :
549			line_text = self.get_line_text(layout,2).strip()
550			x = line_text.split('    ')
551			return x[len(x)-1].strip().replace(' ','')
552
553		if tid_type == 12 :
554			line_text = self.get_line_text(layout,3).strip()
555			x = line_text.split('    ')
556			return x[len(x)-1].strip().replace(' ','')
557
558	def get_container1_type(self,layout,tid_type):
559		if tid_type == 10 :
560			line_text1 = self.get_line_text(layout,4).strip()
561			x = line_text1.split('    ')
562			line_text2 = self.get_line_text(layout,5).strip()
563			y = line_text2.split('    ')
564			return '%s  %s' % (x[0].strip().replace(' ',''),y[0].strip().replace(' ','').replace('DV',' DV '))
565
566		if tid_type == 11 :
567			line_text = self.get_line_text(layout,3).strip()
568			if line_text=='-':
569				line_text1 = self.get_line_text(layout,5).strip()
570				x = line_text1.split('    ')
571				line_text2 = self.get_line_text(layout,6).strip()
572				y = line_text2.split('    ')
573				return '%s  %s' % (x[0].strip().replace(' ',''),y[0].strip().replace(' ','').replace('DV',' DV '))
574			else:
575				line_text1 = self.get_line_text(layout,6).strip()
576				x = line_text1.split('    ')
577				line_text2 = self.get_line_text(layout,7).strip()
578				y = line_text2.split('    ')
579				return '%s  %s' % (x[0].strip().replace(' ',''),y[0].strip().replace(' ','').replace('DV',' DV '))
580
581
582		if tid_type == 12 :
583			line_text1 = self.get_line_text(layout,6).strip()
584			x = line_text1.split('    ')
585			line_text2 = self.get_line_text(layout,7).strip()
586			y = line_text2.split('    ')
587			return '%s  %s' % (x[0].strip().replace(' ',''),y[0].strip().replace(' ','').replace('DV',' DV '))
588
589
590	def get_container2_type(self,layout,tid_type):
591		if tid_type == 10 :
592			line_text1 = self.get_line_text(layout,5).strip()
593			x = line_text1.split('    ')
594			line_text2 = self.get_line_text(layout,6).strip()
595			y = line_text2.split('    ')
596			return '%s  %s' % (x[len(x)-1].strip().replace(' ',''),y[0].strip().replace(' ','').replace('DV',' DV '))
597
598		if tid_type == 11 :
599			line_text1 = self.get_line_text(layout,6).strip()
600			x = line_text1.split('    ')
601			line_text2 = self.get_line_text(layout,7).strip()
602			y = line_text2.split('    ')
603			return '%s  %s' % (x[len(x)-1].strip().replace(' ',''),y[0].strip().replace(' ','').replace('DV',' DV '))
604
605		if tid_type == 12 :
606			line_text1 = self.get_line_text(layout,7).strip()
607			x = line_text1.split('    ')
608			line_text2 = self.get_line_text(layout,8).strip()
609			y = line_text2.split('    ')
610			return '%s  %s' % (x[len(x)-1].strip().replace(' ',''),y[0].strip().replace(' ','').replace('DV',' DV '))
611
612	def get_line3text1(self,layout,tid_type):
613		if tid_type == 10 :
614			line_text1 = self.get_line_text(layout,8).strip()
615			x = line_text1.split('    ')
616			return '%s' % (x[0].strip().replace(' ',''))
617
618		if tid_type == 11 :
619			line_text1 = self.get_line_text(layout,10).strip()
620			x = line_text1.split('    ')
621			return '%s' % (x[0].strip().replace(' ',''))
622
623		if tid_type == 12 :
624			line_text1 = self.get_line_text(layout,11).strip()
625			x = line_text1.split('    ')
626			return '%s' % (x[0].strip().replace(' ',''))
627
628	def get_line3text2(self,layout,tid_type):
629		if tid_type == 10 :
630			line_text1 = self.get_line_text(layout,8).strip()
631			x = line_text1.split('    ')
632			return '%s' % (x[len(x)-1].strip().replace(' ',''))
633
634		if tid_type == 11 :
635			line_text1 = self.get_line_text(layout,10).strip()
636			x = line_text1.split('    ')
637			return '%s' % (x[len(x)-1].strip().replace(' ',''))
638
639		if tid_type == 12 :
640			line_text1 = self.get_line_text(layout,11).strip()
641			x = line_text1.split('    ')
642			return '%s' % (x[len(x)-1].strip().replace(' ',''))
643
644
645	def _parse_pages (doc, images_folder):
646		"""With an open PDFDocument object, get the pages, parse each one, and return the entire text
647		[this is a higher-order function to be passed to with_pdf()]"""
648		rsrcmgr = PDFResourceManager()
649		laparams = LAParams()
650		device = PDFPageAggregator(rsrcmgr, laparams=laparams)
651		interpreter = PDFPageInterpreter(rsrcmgr, device)
652
653		text_content = [] # a list of strings, each representing text collected from each page of the doc
654		for i, page in enumerate(doc.get_pages()):
655			interpreter.process_page(page)
656			# receive the LTPage object for this page
657			layout = device.get_result()
658			# layout is an LTPage object which may contain child objects like LTTextBox, LTFigure, LTImage, etc.
659			text_content.append(parse_lt_objs(layout.objs, (i+1), images_folder))
660
661		return text_content
662
663	def parse_lt_objs (lt_objs, page_number, images_folder, text=[]):
664		"""Iterate through the list of LT* objects and capture the text or image data contained in each"""
665		text_content = [] 
666
667		for lt_obj in lt_objs:
668			if isinstance(lt_obj, LTTextBox) or isinstance(lt_obj, LTTextLine):
669				# text
670				text_content.append(lt_obj.get_text())
671				print(lt_obj.get_text())
672			elif isinstance(lt_obj, LTImage):
673				# an image, so save it to the designated folder, and note it's place in the text 
674				saved_file = save_image(lt_obj, page_number, images_folder)
675				if saved_file:
676					# use html style <img /> tag to mark the position of the image within the text
677					text_content.append('<img src="'+os.path.join(images_folder, saved_file)+'" />')
678				else:
679					print >> sys.stderr, "Error saving image on page", page_number, lt_obj.__repr__
680			elif isinstance(lt_obj, LTFigure):
681				# LTFigure objects are containers for other LT* objects, so recurse through the children
682				text_content.append(parse_lt_objs(lt_obj.objs, page_number, images_folder, text_content))
683
684		return text_content
685		# return '\n'.join(text_content)
686
687	# if __name__ == "__main__":
688	#     sys.exit(main(sys.argv[1:]))
689
690
Full Screen

history.py

Source: history.py Github

copy
1# -*- coding: utf-8 -*-
2#*****************************************************************************
3#       Copyright (C) 2006  Jorgen Stenarson. <[email protected]>
4#
5#  Distributed under the terms of the BSD License.  The full license is in
6#  the file COPYING, distributed as part of this software.
7#*****************************************************************************
8import re, operator,string, sys,os
9
10from pyreadline.unicode_helper import ensure_unicode, ensure_str
11if "pyreadline" in sys.modules:
12    pyreadline = sys.modules["pyreadline"]
13else:
14    import pyreadline
15
16from . import lineobj
17
18class EscapeHistory(Exception):
19    pass
20
21from pyreadline.logger import log
22
23
24class LineHistory(object):
25    def __init__(self):
26        self.history = []
27        self._history_length = 100
28        self._history_cursor = 0
29        self.history_filename = os.path.expanduser('~/.history') #Cannot expand unicode strings correctly on python2.4
30        self.lastcommand = None
31        self.query = ""
32        self.last_search_for = ""
33
34    def get_current_history_length(self):
35        '''Return the number of lines currently in the history.
36        (This is different from get_history_length(), which returns 
37        the maximum number of lines that will be written to a history file.)'''
38        value = len(self.history)
39        log("get_current_history_length:%d"%value)
40        return value
41
42    def get_history_length(self):
43        '''Return the desired length of the history file. Negative values imply
44        unlimited history file size.'''
45        value = self._history_length
46        log("get_history_length:%d"%value)
47        return value
48
49    def get_history_item(self, index):
50        '''Return the current contents of history item at index (starts with index 1).'''
51        item = self.history[index - 1]
52        log("get_history_item: index:%d item:%r"%(index, item))
53        return item.get_line_text()
54
55    def set_history_length(self, value):
56        log("set_history_length: old:%d new:%d"%(self._history_length, value))
57        self._history_length = value
58
59    def get_history_cursor(self):
60        value = self._history_cursor
61        log("get_history_cursor:%d"%value)
62        return value
63
64    def set_history_cursor(self, value):
65        log("set_history_cursor: old:%d new:%d"%(self._history_cursor, value))
66        self._history_cursor = value
67        
68    history_length = property(get_history_length, set_history_length)
69    history_cursor = property(get_history_cursor, set_history_cursor)
70
71    def clear_history(self):
72        '''Clear readline history.'''
73        self.history[:] = []
74        self.history_cursor = 0
75
76    def read_history_file(self, filename=None): 
77        '''Load a readline history file.'''
78        if filename is None:
79            filename = self.history_filename
80        try:
81            for line in open(filename, 'r'):
82                self.add_history(lineobj.ReadLineTextBuffer(ensure_unicode(line.rstrip())))
83        except IOError:
84            self.history = []
85            self.history_cursor = 0
86
87    def write_history_file(self, filename = None): 
88        '''Save a readline history file.'''
89        if filename is None:
90            filename = self.history_filename
91        fp = open(filename, 'wb')
92        for line in self.history[-self.history_length:]:
93            fp.write(ensure_str(line.get_line_text()))
94            fp.write('\n'.encode('ascii'))
95        fp.close()
96
97
98    def add_history(self, line):
99        '''Append a line to the history buffer, as if it was the last line typed.'''
100        if not hasattr(line, "get_line_text"):
101            line = lineobj.ReadLineTextBuffer(line)
102        if not line.get_line_text():
103            pass
104        elif len(self.history) > 0 and self.history[-1].get_line_text() == line.get_line_text():
105            pass
106        else:
107            self.history.append(line)
108        self.history_cursor = len(self.history)
109
110    def previous_history(self, current): # (C-p)
111        '''Move back through the history list, fetching the previous command. '''
112        if self.history_cursor == len(self.history):
113            self.history.append(current.copy()) #do not use add_history since we do not want to increment cursor
114            
115        if self.history_cursor > 0:
116            self.history_cursor -= 1
117            current.set_line(self.history[self.history_cursor].get_line_text())
118            current.point = lineobj.EndOfLine
119
120    def next_history(self, current): # (C-n)
121        '''Move forward through the history list, fetching the next command. '''
122        if self.history_cursor < len(self.history) - 1:
123            self.history_cursor += 1
124            current.set_line(self.history[self.history_cursor].get_line_text())
125
126    def beginning_of_history(self): # (M-<)
127        '''Move to the first line in the history.'''
128        self.history_cursor = 0
129        if len(self.history) > 0:
130            self.l_buffer = self.history[0]
131
132    def end_of_history(self, current): # (M->)
133        '''Move to the end of the input history, i.e., the line currently
134        being entered.'''
135        self.history_cursor = len(self.history)
136        current.set_line(self.history[-1].get_line_text())
137
138    def reverse_search_history(self, searchfor, startpos=None):
139        if startpos is None:
140            startpos = self.history_cursor
141        origpos = startpos
142
143        result =  lineobj.ReadLineTextBuffer("")
144
145        for idx, line in list(enumerate(self.history))[startpos:0:-1]:
146            if searchfor in line:
147                startpos = idx
148                break
149
150        #If we get a new search without change in search term it means
151        #someone pushed ctrl-r and we should find the next match
152        if self.last_search_for == searchfor and startpos > 0:
153            startpos -= 1
154            for idx, line in list(enumerate(self.history))[startpos:0:-1]:
155                if searchfor in line:
156                    startpos = idx
157                    break
158
159        if self.history:                    
160            result = self.history[startpos].get_line_text()
161        else:
162            result = ""
163        self.history_cursor = startpos
164        self.last_search_for = searchfor
165        log("reverse_search_history: old:%d new:%d result:%r"%(origpos, self.history_cursor, result))
166        return result
167        
168    def forward_search_history(self, searchfor, startpos=None):
169        if startpos is None:
170            startpos = min(self.history_cursor, max(0, self.get_current_history_length()-1))
171        origpos = startpos
172        
173        result =  lineobj.ReadLineTextBuffer("")
174
175        for idx, line in list(enumerate(self.history))[startpos:]:
176            if searchfor in line:
177                startpos = idx
178                break
179
180        #If we get a new search without change in search term it means
181        #someone pushed ctrl-r and we should find the next match
182        if self.last_search_for == searchfor and startpos < self.get_current_history_length()-1:
183            startpos += 1
184            for idx, line in list(enumerate(self.history))[startpos:]:
185                if searchfor in line:
186                    startpos = idx
187                    break
188
189        if self.history:                    
190            result = self.history[startpos].get_line_text()
191        else:
192            result = ""
193        self.history_cursor = startpos
194        self.last_search_for = searchfor
195        return result
196
197    def _search(self, direction, partial):
198        try:
199            if (self.lastcommand != self.history_search_forward and
200                    self.lastcommand != self.history_search_backward):
201                self.query = ''.join(partial[0:partial.point].get_line_text())
202            hcstart = max(self.history_cursor,0) 
203            hc = self.history_cursor + direction
204            while (direction < 0 and hc >= 0) or (direction > 0 and hc < len(self.history)):
205                h = self.history[hc]
206                if not self.query:
207                    self.history_cursor = hc
208                    result = lineobj.ReadLineTextBuffer(h, point=len(h.get_line_text()))
209                    return result
210                elif (h.get_line_text().startswith(self.query) and (h != partial.get_line_text())):
211                    self.history_cursor = hc
212                    result = lineobj.ReadLineTextBuffer(h, point=partial.point)
213                    return result
214                hc += direction
215            else:
216                if len(self.history) == 0:
217                    pass 
218                elif hc >= len(self.history) and not self.query:
219                    self.history_cursor = len(self.history)
220                    return lineobj.ReadLineTextBuffer("", point=0)
221                elif self.history[max(min(hcstart, len(self.history) - 1), 0)]\
222                        .get_line_text().startswith(self.query) and self.query:
223                    return lineobj.ReadLineTextBuffer(self.history\
224                            [max(min(hcstart, len(self.history) - 1),0)],
225                                point = partial.point)
226                else:                
227                    return lineobj.ReadLineTextBuffer(partial, 
228                                                      point=partial.point)
229                return lineobj.ReadLineTextBuffer(self.query, 
230                                                  point=min(len(self.query),
231                                                  partial.point))
232        except IndexError:
233            raise
234
235    def history_search_forward(self, partial): # ()
236        '''Search forward through the history for the string of characters
237        between the start of the current line and the point. This is a
238        non-incremental search. By default, this command is unbound.'''
239        q= self._search(1, partial)
240        return q
241
242    def history_search_backward(self, partial): # ()
243        '''Search backward through the history for the string of characters
244        between the start of the current line and the point. This is a
245        non-incremental search. By default, this command is unbound.'''
246        
247        q= self._search(-1, partial)
248        return q
249
250if __name__=="__main__":
251    import pdb
252    q = LineHistory()
253    r = LineHistory()
254    s = LineHistory()
255    RL = lineobj.ReadLineTextBuffer
256    q.add_history(RL("aaaa"))
257    q.add_history(RL("aaba"))
258    q.add_history(RL("aaca"))
259    q.add_history(RL("akca"))
260    q.add_history(RL("bbb"))
261    q.add_history(RL("ako"))
262    r.add_history(RL("ako"))
263
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Python Tests on LambdaTest Cloud Grid

Execute automation tests with Gherkin-python on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)