summaryrefslogtreecommitdiff
path: root/support/gtex-letter/main.py
blob: 87b754abcd3ea52f44e2119a8791e53bef26d0ed (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env python

GTEX_LETTER_VERSION = '0.30'
PRG_DIR="/opt/gnome/bin/gtex-letter-prg"
RC_FILENAME='.gtex-letterrc'


import sys
import os.path
import getopt
import ConfigParser
import string
import gettext

gettext.bindtextdomain('gtex-letter', '/usr/share/locale')
gettext.textdomain('gtex-letter')
_ = gettext.gettext

def help():
        print "usage: gtex-letter [OPTIONS] [file.tex]"
        print "       PIPE | gtex-letter [OPTIONS] [file.tex]"
	print 
        print "       -a --address     str   get address with given ID from GnomeCard-file "
	print "                                 as default address for the letter"
	print "       -s --subject     str   subject of the letter"
	print "       -o --opening     str   the letter's opening"
	print "       -f --file        str   get letter contents from file"
	print "       -c --closing     str   closing for the letter"
	print
	print "       -r --rcfile      str   use a different rc-file (.gtex-letterrc)"
	print "       -e --header      str   headerfile of the letter"
	print "          --guessformat str   the format of filename-guesses"
	print "          --guesspath   str   the path to prepend to guessed filenames"
	print "          --guilevel    str   choose the complexity of the gui used."
	print "                                 possible values are: beginner, normal"
	print "                                 and expert. While beginner-level is easy to"
	print "                                 understand, expert is very straight forward."
	print "          --nosplash          no start-screen"
	print
	print "       -n --nogui             non-interactive letter production"
	print "          --noshow            do not show the letter "
	print "                                 (only with -n/--nogui-option)" 
	print "          --quitelatex        hide latex output"
	print
	print "       -p --print             direct printing of the letter"
	print "                                 (only with -n/--nogui-option)"
	print
	print "       -h --help              print this file and exit"
	print "       -u --usage             like help"
	print "       -v --version           print version and exit"

def version():
	return "gtex-letter " + GTEX_LETTER_VERSION

FALSE=0
TRUE=-1

rc_file = RC_FILENAME
defaultaddresses = ''
closing = ''
contents= ''
contentsfile = ''
filename = ''
header = ''
opening = ''
subject = ''
guessformat = ''
guesspath = ''
quite_latex=None

nogui = FALSE
noshow = FALSE
printit = FALSE
guilevel = FALSE
splash = TRUE

opts, args = getopt.getopt(sys.argv[1:], 'a:c:e:f:hno:pr:s:uv', ['address=', 'closing=', 'file=', 'guessformat=', 'guesspath=', 'guilevel=', 'header=', 'help', 'nogui', 'noshow', 'nosplash', 'opening=', 'print', 'quitelatex', 'rcfile=', 'subject=', 'usage', 'version'])
for opt in opts:
	option = opt[0]
	value = opt[1]
	if option =='-a' or option =='--address':
		defaultaddresses = value
	elif option =='-c' or option =='--closing':
		closing = value
	elif option =='-f' or option =='--file':
		contentsfile = value
		if not os.path.isfile(contentsfile):
			print _('Error: Provided file not found.')
			sys.exit()
		file = open(contentsfile, 'r')
		contents= file.read()
		file.close()
	elif option =='--guilevel':
		guilevel = value
	elif option =='-e' or option =='--header':
		header = value
	elif option =='--guessformat':
		guessformat = value
	elif option =='--guesspath':
		guesspath = value
	elif option =='-h' or option =='--help':
		help()
		sys.exit()
	elif option =='-n' or option =='--nogui':
		nogui = TRUE
	elif option =='--noshow':
		noshow = TRUE
	elif option =='--nosplash':
		splash = FALSE
	elif option =='-o' or option =='--opening':
		opening = value
	elif option =='-p' or option =='--print':
		printit = TRUE
	elif option =='--quitelatex':
		quite_latex = TRUE
	elif option =='-r' or option =='--rcfile':
		rc_file = value
		if not os.path.isfile(rc_file):
			print _('Error: Provided rc-file not found.')
			sys.exit()
	elif option =='-s' or option =='--subject':
		subject = value
	elif option =='-u' or option =='--usage':
		help()
		sys.exit()
	elif option =='-v' or option =='--version':
		print version()
		sys.exit()

if (noshow or printit) and not nogui:
	print _("Error: You may not set the noshow or the print option without setting nogui.")
	sys.exit()




# fetch the STDIN if it is set.
if not sys.stdin.isatty(): # redirected from file or pipe
	if not contents:
		contents = sys.stdin.read()
	elif contents and not defaultaddress:
		defaultaddress = sys.stdin.read()[:-1]  # remove CR-char
	else:
		print _("Error: I do not know what to do with STDIN (pipe). You set both, -f/--file and -a/--address, in the options, so there is no place to put the pipe.")
		sys.exit()

if args:
	filename = args[0]
	






sys.argv=['']  # i get an error from module gui, if there are options left in sys.argv

import utils, addressbook_vcard, gui    # if i put this before the opts-stuff, 
import mk_letter, config		# the options are unknown...
import error




class gtex_letter:
	def __init__ (self, rc, \
			filename = '',\
			addresskeys = '',\
			closing = '',\
			contents = '',\
			header = '',\
			opening = '',\
			subject = ''):
 
		self.init_address(addresskeys, rc)
		
		self.init_header(header, rc)

		self.subject = subject
		self.opening = opening
		self.contents = contents
		self.closing = closing

		self.filename = filename

		if not self.filename:
			file = utils.guess_filename(self, rc)
			if file:
				self.filename = file

		self.path = utils.get_path_from_file(self.filename)
		self.letter_written = FALSE
			

	def init_address(self, addresskeys, rc):
		self.address = ''

		self.addresscards = []

		if addresskeys:
			list = []
			while 1:
				hit = string.find(addresskeys, ',')
				if hit == -1:
					list.append(string.strip(addresskeys))
					addresskeys=''
					break
				list.append(string.strip(addresskeys[:hit]))
				addresskeys = addresskeys[hit+1:]
			ab = addressbook_vcard.addressbook(rc.addressbooks)
			ab.read_file()
			for name in list:
				try: 
					card = ab.get_card(name)
					if card:
						self.addresscards.append(card)
					else:
						raise error.AddressCardNotFound, name
				except error.AddressCardNotFound, cardname:
					print _("The addresscard '%s' could not be found.") % cardname
					
			for card in self.addresscards:
				if self.address:
					self.address = self.address + '\n'
				self.address = self.address + utils.mk_adr(card, \
							rc.addressformats, rc.mycountry)
				
			if self.address:
				self.address_slashes = utils.add_slashes(self.address)
			
		if not self.address:
			self.address_slashes = ''
	
	def init_header(self, header, rc):
		self.header = ''
		if header:
			self.header = header  
		else:
			if rc.headerdefaults:
				self.header = rc.headerdefaults[0]
		try:
			if self.header:
				if self.header[-4:] <> '.tex': 
					self.header = self.header + '.tex' 
				self.header = utils.get_path(self.header, rc.prg_dir)
				if self.header:
					if not os.path.split(self.header)[0]: # if it is still local
						self.header = os.path.join(os.getcwd(), \
								self.header)
				else:
					raise IOError, header
		except IOError, file:
			print _("Submitted headerfile '%s' not found") % file
			

	
def main ():
	rc = config.gtex_letter_config(rc_file, nogui=nogui, \
			noshow=noshow, splash=splash, \
			printit=printit, guilevel=guilevel, \
			guessformat=guessformat, guesspath=guesspath, quite_latex=quite_latex, prg_dir=PRG_DIR )
			
	letter = gtex_letter(rc, filename, defaultaddresses, \
				closing, contents, \
				header, opening, subject)
	if rc.nogui:
		letter.letter_written = not mk_letter.write_file(letter, rc)
		if not letter.letter_written: 
			print _("Aborting.")
			sys.exit()
		mk_letter.tex_latex(letter, rc)
		if not rc.noshow:
			mk_letter.tex_xdvi(letter, rc)
		if printit:
			mk_letter.tex_print(letter, rc)
		mk_letter.rm_latex_files(letter)
	else:
		gp = gui.GUI (rc, letter)
		gp.mainloop ()

if __name__ == '__main__':
	main ()