File: pybliographer.py

package info (click to toggle)
pybliographer 1.2.9-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,528 kB
  • ctags: 1,399
  • sloc: python: 9,788; xml: 2,573; makefile: 609; sh: 597
file content (249 lines) | stat: -rw-r--r-- 5,713 bytes parent folder | download | duplicates (2)
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
#! @python_path@
# This file is part of pybliographer
# 
# Copyright (C) 1998-2004 Frederic GOBRY
# Email : gobry@pybliographer.org
# 	   
# 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
# 
# 

version  = "@version@"
progname = "@package@"

data_pybdir = "@datapyb@"
localedir   = "@localedir@"

import sys

sys.path.insert (0, data_pybdir)
sys.path.insert (0, '.')

import locale
locale.setlocale (locale.LC_ALL, '')

import gettext
gettext.install (progname, localedir, unicode = True)

charset = locale.getlocale () [1] or 'ascii'

def print_version ():
	print (_("This is %s, version %s") % (progname, version)).encode (charset)

def copyright ():
	print 'Copyright (C) 1998-2004 Frederic GOBRY'
	print _("This is free software with ABSOLUTELY NO WARRANTY.").encode (charset)
	print _("For details, type `warranty'.").encode (charset)

def warranty ():
	print_version ()
	print 'Copyright (C) 1998-2004 Frederic GOBRY'
	
	print _("This is free software with ABSOLUTELY NO WARRANTY.").encode (charset)
	print """
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, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
"""
	
import os
import getopt, string

sys.ps1 = 'python > '

optlist, args = getopt.getopt (sys.argv [1:],
			       'qf:nvh',
			       ['quiet',
				'file=',
				'version',
				'help'])

sys.argv = sys.argv [0:1] + args
try:
	sys.argv.remove ('--')
except ValueError: pass

quiet = 0

try:             os.stat ("pybrc.py")
except os.error: sources = [ os.path.join (data_pybdir, "pybrc.py") ]
else:  	         sources = [ "pybrc.py" ]

sources.append (os.path.expanduser('~/.pybrc.py'))
load_config = 1

for opt, value in optlist:
	if opt == '-q' or opt == '--quiet':
		quiet = 1
		continue
	
	if opt == '-n':
		sources = []
		load_config = 0
		continue

	if opt == '-f' or opt == '--file':
		try:
			os.stat (value)
		except os.error:
			print (_("%s: error: can't open file `%s'") \
			      % (progname, value)).encode (charset)
			sys.exit (1)
		
		sources.append (value)
		continue

	if opt == '-v' or opt == '--version':
		print_version ()
		sys.exit (0)

	if opt == '-h' or opt == '--help':
		print_version ()
		print (_("For help, run %s and type `help' at the prompt") 
		       % progname).encode (charset)
		sys.exit (0)
		
# ---------- Initialisation

if not quiet:
	print_version ()
	copyright ()
	print _("Useful commands:\n	help     to get some help\n	quit     to quit\n").encode (charset)

# ---------- Lire les fichiers de conf

user_global = {'__builtins__' : __builtins__,
	       '__name__'     : progname,
	       }

user_local  = {}


# Fichiers standards
for filename in sources:
	try:
		os.stat (filename)
	except os.error:
		pass
	else:
		execfile (filename, user_global)

if load_config:
	from Pyblio import Config
	Config.load_user ()

# Fichiers passes en argument au programme
if len (args) > 0 :
	filename = args [0]
	try:
		os.stat (filename)
	except os.error:
		print (_("%s: error: can't open file `%s'") % (progname, filename)).encode (charset)
		sys.exit (1)
	else:
		execfile (filename, user_global)

	# Quand on a tout lu, le programme est fini !
	sys.exit (0)

import re, traceback
import code
import Pyblio.Help
import string

try:
	import readline
except ImportError:
	pass

# ---------- Initialisations du parser
simplecom = re.compile ("^\s*(!|help|exit|quit|warranty)\s+(.*)$")
emptycomm = re.compile ("^\s*(#.*)?$")

full_line = ""
finished  = 0
prompt    = ">> "
inner = 0


while not finished:
	
	try:
		line = raw_input (prompt)
	except EOFError:
		finished = 1
		continue
	except	KeyboardInterrupt:
		print
		full_line = ""
		prompt = ">> "
		inner = 0
		continue

		
	what = simplecom.match (line + " ")
	
	if not inner and emptycomm.match (line) <> None:
		continue
	
	if what <> None:
		command = what.group (1)
		if command == "help":
			Pyblio.Help.help (string.strip (what.group (2)))
			continue
		elif command == "quit" or command == "exit":
			finished = 1
			continue
		elif command == "warranty":
			warranty ()
			continue
		elif command == "!":
			os.system (what.group (2))
			continue
	else:
		full_line = full_line + "\n" + line

	try:
		user = code.compile_command (full_line);
		
		if user == None and line <> "":
			prompt = "..."
			inner = 1
		else:
			exec (user, user_global)
			full_line = ""
			prompt = ">> "
			inner = 0
			    
	except:
		etype, value, tb = sys.exc_info ()
		traceback.print_exception (etype, value, tb)

		full_line = ""
		prompt = ">> "
		inner = 0
	

# save history