-
Notifications
You must be signed in to change notification settings - Fork 0
/
quote.py
116 lines (85 loc) · 3.42 KB
/
quote.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python
import argparse
import random
import struct
import codecs
#import textwrap
#//**********************************************************************
#//
#// globals/constants
#//
#//**********************************************************************
PROGRAM_NAME = "quote"
VERSION = "3.3.0"
COPYRIGHT_MESSAGE = "copyright (c) 2019 (1990), Rick Gutleber ([email protected])"
lineLength = 80
itemSize = struct.calcsize( 'i' )
#//**********************************************************************
#//
#// getOffset
#//
#//**********************************************************************
def getOffset( infile, quoteChoice ):
# +1 because the first item is the count
infile.seek( ( quoteChoice + 1 ) * itemSize )
return struct.unpack( 'i', infile.read( itemSize ) )[ 0 ]
#//**********************************************************************
#//
#// main
#//
#//**********************************************************************
def main( ):
parser = argparse.ArgumentParser( prog = PROGRAM_NAME, description = PROGRAM_NAME + ' - ' +
VERSION + ' - ' + COPYRIGHT_MESSAGE, add_help = False,
formatter_class = argparse.RawTextHelpFormatter,
prefix_chars = '-' )
parser.add_argument( 'quoteFile', default = 'quote.txt' )
parser.add_argument( 'indexFile', default = 'quote.idx' )
parser.add_argument( '-q', '--quote', nargs = '?', type = int, action = 'store', default = 0 )
parser.add_argument( '-n', '--count', nargs = '?', type = int, action = 'store', default = 1 )
parser.add_argument( '-s', '--search', nargs = '?', type = str, action = 'store', default = '' )
args = parser.parse_args( )
quoteFile = args.quoteFile
indexFile = args.indexFile
quoteChoice = args.quote - 1 # first quote is #1, not #0
quotesToPrint = args.count
searchTerm = args.search
if quoteChoice != -1 and quotesToPrint > 1:
print( "-q and -n cannot be used together" )
return
offsets = [ ]
quoteChoices = [ ]
with open( indexFile, "rb" ) as infile:
quoteCount = struct.unpack( 'i', infile.read( itemSize ) )[ 0 ]
if quoteChoice == -1:
for i in range( quotesToPrint ):
quoteChoices.append( random.randint( 0, quoteCount ) )
offsets.append( getOffset( infile, quoteChoices[ -1 ] ) )
else:
if quoteChoice > quoteCount:
print( "-q value is too high. There are only %d quotes." % quoteCount )
return
quoteChoices.append( quoteChoice )
offsets.append( getOffset( infile, quoteChoices[ -1 ] ) )
print( "_" * lineLength )
print( )
# with codecs.open( quoteFile, 'rU', 'ascii', 'replace' ) as infile:
with open( quoteFile, "r" ) as infile:
for i in range( 0, quotesToPrint ):
infile.seek( offsets[ i ] )
quote = ''
for line in infile:
if line == "%\n":
break
print( line, end='' )
print( )
print( "_" * lineLength )
print( quoteChoices[ i ] + 1 )
print( )
#//**********************************************************************
#//
#// __main__
#//
#//**********************************************************************
if __name__ == '__main__':
main( )