summaryrefslogtreecommitdiff
path: root/support/gtex-letter/config.py
blob: d5440992b160833e8cf70f3c6bd48cb40c939847 (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
FALSE=0
TRUE=-1


import ConfigParser, os, string, sys, gettext

import utils

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

from main import GTEX_LETTER_VERSION

advertisestr = "%%" + _("This file was generated by gtex-letter version") + " " + GTEX_LETTER_VERSION + ".\n"

class gtex_letter_config:
	def __init__ (self, rc_file, nogui=FALSE, noshow=FALSE, \
	              splash=TRUE, printit=FALSE, guilevel=FALSE, \
		      guessformat='', guesspath='', quite_latex=None, prg_dir=''):

		self.prg_dir = prg_dir
		self.rc_filename = rc_file
		self.init_config()
		
		self.mycountry = utils.rc_set('MAIN', "mycountry", self.rc)
		self.myeditor = utils.rc_set('MAIN', "myeditor", self.rc)
		self.mylatex = utils.rc_set('MAIN', "mylatex", self.rc)
		self.myxdvi = utils.rc_set('MAIN', "myxdvi", self.rc)
		self.mydvips = utils.rc_set('MAIN', "mydvips", self.rc)
		self.mydvipdf = utils.rc_set('MAIN', "mydvipdf", self.rc)
		self.myprintcommand = utils.rc_set('MAIN', "myprintcommand", self.rc)
		self.dateformat = utils.rc_set('MAIN', "dateformat", self.rc)

		self.lettertype = utils.rc_set('MAIN', 'lettertype', self.rc)
		self.init_advertise()

		self.filename_guess_format = guessformat
		if not self.filename_guess_format: 
			self.filename_guess_format = utils.rc_set('MAIN', 'filename_guess_format', self.rc)
		self.filename_guess_path = guesspath
		if not self.filename_guess_path: 
			self.filename_guess_path = utils.rc_set('MAIN', "filename_guess_path", self.rc)
		if self.filename_guess_path[0] == '~':
			self.filename_guess_path = os.environ['HOME'] + self.filename_guess_path[1:]


		if quite_latex==None:
			quite_latex = utils.rc_set('MAIN', "quite_latex", self.rc)
		self.init_latex_output(quite_latex)

		self.texcommands=[]
		self.init_mult_liners(self.texcommands, 'texcommands', 'COMMANDS')

		self.addressformats=[]
		self.init_mult_liners(self.addressformats, 'addressformat', 'ADDRESSFORMAT')

		self.addressbooks = []
		self.init_addressbooks('addressbook', self.addressbooks)
		
		
		self.addressbook_columns = utils.rc_set('MAIN', \
					"addressbook_columns", self.rc)
		self.addressbook_column_widths = utils.rc_set('MAIN', \
					"addressbook_column_widths", self.rc)
		
		self.headerdefaults = []
		self.init_default_texts('header_defaults', self.headerdefaults)
		self.check_headerdefaults()
						
		self.openingdefaults = []
		self.init_default_texts('opening_defaults', self.openingdefaults)
		
		self.closingdefaults = []
		self.init_default_texts('closing_defaults' ,self.closingdefaults)
		

		self.init_guilevel(guilevel)
		
		self.nogui = nogui
		self.noshow = noshow
		self.printit = printit
		self.splash = splash
		
			
	def init_config(self):
		self.rc=ConfigParser.ConfigParser()
		file = utils.get_path(self.rc_filename, os.environ['HOME'], self.prg_dir)
		if not os.path.isfile(file):
			print _("Error: Could not find rc-file:"), self.rc_filename
		self.rc.read(file)
	
	def init_addressbooks(self, rc_key, texts):
		def_str = utils.rc_set('MAIN', rc_key, self.rc)
		if def_str:
			num = 1
			while num >0:
				num = string.find(def_str, ";")
				if num >0:
					tmpstr=utils.stripmarks(string.strip(def_str[0:num]))
					texts.append (tmpstr)
					def_str= def_str[num+2:]
				else:
					tmpstr=utils.stripmarks(string.strip(def_str))
					texts.append(tmpstr)

	def init_mult_liners(self, arg, key, section):
		i=0
		while 1:
			nkey = key + "." + str(i)
			if nkey in self.rc.options(section):
				arg.append(utils.rc_set(section, nkey, self.rc))
				i = i + 1
			else:
				break
		if arg == []:
			print _("Error: Could not retrieve '%s' from rc-file.") % key
			sys.exit()

	def init_advertise(self):
		text = utils.rc_set('MAIN', 'advertise', self.rc)
		text = utils.switch_yesno(text, default = TRUE)
		if text:
			self.advertisestr = advertisestr
		else:
			self.advertisestr = ""

	def init_latex_output(self, quite_latex):
		quite_latex = utils.switch_yesno(quite_latex, default = FALSE)
		if quite_latex:
			self.latex_output = '>/dev/null'
		else:
			self.latex_output = ''

	def init_default_texts(self, rc_key, texts):
		def_str = utils.rc_set('MAIN', rc_key, self.rc)
		def stripmarks(str):
			if str[0] == "'" or str[0] == "\"":
				str = str [1:]
			if str[-1] == "'" or str[-1] == "\"":
				str = str [:-1]
			return str
		if def_str:
			num = 1
			while num >0:
				num = string.find(def_str, ";")
				if num >0:
					tmpstr=stripmarks(string.strip(def_str[0:num]))
					texts.append (tmpstr)
					def_str= def_str[num+2:]
				else:
					tmpstr=stripmarks(string.strip(def_str))
					texts.append(tmpstr)

	def check_headerdefaults(self):
		for file in self.headerdefaults:
			if file[:4] <> '.tex': 
				file = file + '.tex' 
			file = utils.get_path(file, self.prg_dir)
			if file:
				if not os.path.isfile(file):
					print _('Error: Could not find header defaultfile:'), file
					sys.exit()

	def init_guilevel(self, guilevel):
		if not guilevel:
			guilevel = utils.rc_set('MAIN', "guilevel", self.rc)
		else:
			guilevel = string.lower(guilevel)
		self.guilevel = utils.set_guilevel(guilevel)