File: utils.py

package info (click to toggle)
scribes 0.3.3.3-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 6,512 kB
  • ctags: 4,045
  • sloc: python: 24,125; sh: 3,358; xml: 2,224; makefile: 255
file content (535 lines) | stat: -rw-r--r-- 16,623 bytes parent folder | download
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# -*- coding: utf-8 -*-
# Copyright © 2005 Lateef Alabi-Oki
#
# This file is part of Scribes.
#
# Scribes 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.
#
# Scribes 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 Scribes; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

"""
This modules contains a library of generic auxiliary functions that can be used
accross the project. The functions are designed with reuse and modularity in
mind.

@author: Lateef Alabi-Oki
@organization: The Scribes Project
@copyright: Copyright © 2005 Lateef Alabi-Oki
@license: GNU GPLv2 or Later
@contact: mystilleef@gmail.com
"""

def calculate_resolution_independence(window, width, height):
	"""
	Calculate the resolution independent width and height of windows based on
	the host system's monitor resolution.

	@param window: Any window/dialog object.
	@type window: A gtk.Window object.

	@param width: An arbitrary ratio used to determine a resolution independent
		width.
	@type width: A Float/Integer object.

	@param height: An arbitrary ratio used to determine a resolution independent
		height.
	@type height: A Float/Integer object.

	@return: The width and height to set the window objects to.
	@rtype: A Tuple object containing the suggested width and height to set
		windows to.
	"""
	screen = window.get_screen()
	number = screen.get_number()
	rectangle = screen.get_monitor_geometry(number)
	width = int(rectangle.width/width)
	height = int(rectangle.height/height)
	return width, height

def create_button(stock_id, string):
	"""
	Create a button with a stock icon and a custom label

	@param stock_id: a GTK stock icon identifier
	@type stock_id: string
	@param string: text to be displayed on the button
	@type string: GTK Label

	@return: a stock icon and label
	@rtype: HBox object
	"""
	from gtk import HBox, Image, Label, ICON_SIZE_BUTTON, Alignment
	alignment = Alignment()
	alignment.set_property("xalign", 0.5)
	alignment.set_property("yalign", 0.5)
	hbox = HBox(False, 3)
	if stock_id:
		image = Image()
		image.set_from_stock(stock_id, ICON_SIZE_BUTTON)
		hbox.pack_start(image, False, False, 0)
	label = Label(string)
	label.set_property("use-underline", True)
	hbox.pack_start(label, False, False, 0)
	alignment.add(hbox)
	return alignment

def process_color(color):
	red = int(color[0])
	green = int(color[1])
	blue = int(color[2])
	pixel = long(color[3])
	from gtk.gdk import Color
	color = Color(red, green, blue, pixel)
	return color

def create_scrollwin():
	"""
	Create a scrollwindow object for the editor

	@return: A scroll window.
	@rtype:	A gtk.ScrolledWindow object.
	"""
	from gtk import ScrolledWindow, RESIZE_PARENT, POLICY_AUTOMATIC
	from gtk import SHADOW_IN
	scrollwin = ScrolledWindow()
	scrollwin.set_border_width(1)
	scrollwin.set_policy(POLICY_AUTOMATIC, POLICY_AUTOMATIC)
	scrollwin.set_shadow_type(SHADOW_IN)
	return scrollwin

def get_language(uri):
	"""
	Determine the source code language for a URI

	@param uri: A universal resource identifier pointing to, or representing, a
		file.
	@type uri: A String object.

	@return: An object representing a source code language.
	@rtype: A gtksourceview.SourceLanguage object.
	"""
	try:
		#print "uri", uri
		if uri is None: return None
		from gnomevfs import get_mime_type
		mimetype = get_mime_type(uri)
		#print "Mimetype", mimetype
		from gtksourceview import SourceLanguagesManager
		manager = SourceLanguagesManager()
		language = manager.get_language_from_mime_type(mimetype)
		#print "Language", language
	except RuntimeError:
		print "Caught runtime error when determining language"
		return None
	return language

def create_encoding_box(combobox):
	from internationalization import msg0157
	from gtk import Label, HBox
	label = Label(msg0157)
	label.set_use_underline(True)
	hbox = HBox(homogeneous=False, spacing=10)
	hbox.pack_start(label, False, False, 0)
	hbox.pack_start(combobox, True, True, 0)
	return hbox

def generate_encodings():
	"""
	Generate a tuple containing supported encodings.

	@return: A tuple containing supported encodings.
	@rtype: A tuple object.
	"""
	from internationalization import msg0208, msg0209, msg0210
	from internationalization import msg0211, msg0212, msg0213, msg0214, msg0215
	from internationalization import msg0216, msg0217, msg0218, msg0219, msg0220
	from internationalization import msg0221, msg0222, msg0223, msg0224, msg0225
	from internationalization import msg0226, msg0227, msg0228, msg0229, msg0230
	from internationalization import msg0231, msg0232, msg0233, msg0234, msg0235
	from internationalization import msg0236, msg0237, msg0238, msg0239, msg0240
	from internationalization import msg0241, msg0242, msg0243, msg0244, msg0245
	from internationalization import msg0246, msg0247, msg0248, msg0249, msg0250
	from internationalization import msg0251, msg0252, msg0253, msg0254, msg0255
	from internationalization import msg0256, msg0257, msg0258, msg0259, msg0260
	from internationalization import msg0261, msg0262, msg0263, msg0264, msg0265
	from internationalization import msg0266, msg0267, msg0268, msg0269, msg0270
	from internationalization import msg0271, msg0272, msg0273, msg0274, msg0275
	from internationalization import msg0276, msg0277, msg0278, msg0279, msg0280
	from internationalization import msg0281, msg0282, msg0283, msg0284, msg0285
	from internationalization import msg0286, msg0287, msg0288, msg0289, msg0290
	from internationalization import msg0291
	encodings = (
#		 Codec		Alias		Language
		("ISO-8859-1", "iso-8859-1", msg0262),
		("ISO-8859-2", "iso-8859-2", msg0263),
		("ISO-8859-3", "iso-8859-3", msg0264),
		("ISO-8859-4", "iso-8859-4", msg0265),
		("ISO-8859-5", "iso-8859-5", msg0266),
		("ISO-8859-6", "iso-8859-6", msg0267),
		("ISO-8859-7", "iso-8859-7", msg0268),
		("ISO-8859-8", "iso-8859-8", msg0269),
		("ISO-8859-9", "iso-8859-9", msg0270),
		("ISO-8859-10", "iso-8859-10", msg0271),
		("ISO-8859-13", "iso-8859-13", msg0272),
		("ISO-8859-14", "iso-8859-14", msg0273),
		("ISO-8859-15", "iso-8859-15", msg0274),
		("ISO-2022-JP", "iso2022jp", msg0255),
		("ISO-2022-jp-1", "iso2022jp-1", msg0256),
		("ISO-2022-JP-2", "iso2022jp-2", msg0257),
		("ISO-2022-JP-2004", "iso2022jp-2004", msg0258),
		("ISO-2022-JP-3", "iso2022jp-3", msg0259),
		("ISO-2022-JP-EXT", "iso2022jp-ext", msg0260),
		("ISO-2022-KR", "csiso2022kr", msg0261),
		("BIG5", "csbig5", msg0209),
		("BIG5HKSCS", "hkscs", msg0210),
		("SHIFT-JIS", "sjis", msg0285),
		("SHIFT-JIS-2004", "sjis2004", msg0286),
		("SHIFT-JISX0213", "sjisx0213", msg0287),
		("KOI8-R", "koi8_r", msg0276),
		("KOI8-U", "koi8_u", msg0277),
		("CP037", "IBM039", msg0211),
		("CP424", "IBM424", msg0212),
		("CP437", "437", msg0213),
		("CP500", "IBM500", msg0214),
		("CP737", "cp737", msg0215),
		("CP775", "IBM775", msg0216),
		("CP850", "IBM850", msg0217),
		("CP852", "IBM852", msg0218),
		("CP855", "IBM855", msg0219),
		("CP856", "cp856", msg0220),
		("CP857", "IBM857", msg0221),
		("CP860", "IBM860", msg0222),
		("CP861", "IBM861", msg0223),
		("CP862", "IBM862", msg0224),
		("CP863", "IBM863", msg0225),
		("CP864", "IBM864", msg0226),
		("CP865", "IBM865", msg0227),
		("CP866", "IBM866", msg0228),
		("CP869", "IBM869", msg0229),
		("CP874", "cp874", msg0230),
		("CP875", "cp875", msg0231),
		("CP932", "mskanji", msg0232),
		("CP949", "uhc", msg0233),
		("CP950", "ms950", msg0234),
		("CP1006", "cp1006", msg0235),
		("CP1026", "ibm1026", msg0236),
		("CP1140", "ibm1140", msg0237),
		("CP1250", "windows-1250", msg0238),
		("CP1251", "windows-1251", msg0239),
		("CP1252", "windows-1252", msg0240),
		("CP1253", "windows-1253", msg0241),
		("CP1254", "windows-1254", msg0242),
		("CP1255", "windows-1255", msg0243),
		("CP1256", "windows1256", msg0244),
		("CP1257", "windows-1257", msg0245),
		("CP1258", "windows-1258", msg0246),
		("EUC-JP", "ujis", msg0247),
		("EUC-JIS-2004", "eucjis2004", msg0248),
		("EUC-JISX0213", "eucjisx0213", msg0249),
		("EUC-KR", "ksc5601", msg0250),
		("GB2312",	"euccn", msg0251),
		("GBK", 	"ms936", msg0252),
		("GB18030", "gb18030-2000", msg0253),
		("HZ", "hzgb", msg0254),
		("JOHAB", "cp1361", msg0275),
		("MAC-CYRILLIC", "maccyrillic", msg0278),
		("MAC-GREEK", 	"macgreek", msg0279),
		("MAC-ICELAND", "maciceland", msg0280),
		("MAC-LATIN2", "maccentraleurope", msg0281),
		("MAC-ROMAN", "macroman", msg0282),
		("MAC-TURKISH", "macturkish", msg0283),
		("PTCP154", "cp154", msg0284),
		("UTF-16", "utf16", msg0288),
		("UTF-16-BE", "UTF-16BE", msg0289),
		("UTF-16-LE", "UTF-16LE", msg0290),
		("UTF-7", "U7", msg0291),
		("ASCII", "us-ascii", msg0208),
	)
	return encodings

def generate_random_number(sequence):
	from random import random
	while True:
		exit = True
		number = random()
		if sequence:
			for item in sequence:
				if number == item:
					exit = False
					break
		if exit: break
	return number

def check_uri_permission(uri):
	"""
	Check the permission flags of a URI.

	This function returns True if the URI has read/write permission flags or
	False if the URI has readonly permission.

	@param uri: An object representing, or pointing to, a document.
	@type uri: A gnomevfs.URI object.

	@return: True if you URI has read/write permissions, False otherwise
	@rtype: A Boolean object.
	"""
	value = True
	if uri.startswith("file:///"):
		from gnomevfs import get_local_path_from_uri
		local_path = get_local_path_from_uri(uri)
		from os import access, W_OK, path
		if path.exists(local_path):
			value = access(local_path, W_OK)
		else:
			from info import home_folder
			if local_path.startswith(home_folder) is False:
				value = False
	else:
		writable_scheme = ["ssh", "sftp", "smb", "dav", "davs", "ftp"]
		from gnomevfs import get_uri_scheme
		scheme = get_uri_scheme(uri)
		if not scheme in writable_scheme: value = False
	return value

def get_file_size(uri):
	"""
	Get the size of a file.

	@param uri: A URI pointing to a file.
	@type uri: A String object.

	@return: The size of a file.
	@rtype: An Integer object.
	"""
	size = 0
	from gnomevfs import get_file_info, FILE_INFO_GET_MIME_TYPE
	from gnomevfs import FILE_INFO_FORCE_SLOW_MIME_TYPE
	from gnomevfs import FILE_INFO_FOLLOW_LINKS, FILE_INFO_DEFAULT
	FILE_INFO_ACCESS_RIGHTS = 1 << 4
	try:
		fileinfo = get_file_info(uri, FILE_INFO_DEFAULT |
							FILE_INFO_GET_MIME_TYPE |
							FILE_INFO_FORCE_SLOW_MIME_TYPE |
							FILE_INFO_FOLLOW_LINKS |
							FILE_INFO_ACCESS_RIGHTS)
		if fileinfo:
			try:
				size = fileinfo.size
			except:
				pass
	except:
		pass
	return size

def create_menuitem(string, stock_id=None):
	"""
	Create a generic menu item.

	@param string: A string representing a label for the menu item.
	@type string: A String object.

	@param stock_id: An image for menu item.
	@type stock_id: A gtk.STOCK_ID object.

	@return: A menuitem with a label and stock ID.
	@rtype: A gtk.MenuItem object.
	"""
	from gtk import MenuItem, Image, HBox, Label
	hbox = HBox(spacing=7)
	hbox.set_property("border-width", 2)
	if stock_id:
		image = Image()
		image.set_property("stock", stock_id)
		hbox.pack_start(image, False, False, 0)
	label = Label(string)
	label.set_property("use-underline", True)
	hbox.pack_start(label, False, False, 0)
	menuitem = MenuItem()
	menuitem.add(hbox)
	return menuitem

def calculate_completion_window_position(editor, width, height):
	"""
	Calculate the position to place the word completion window in the text
	editor's buffer.

	@param editor: Reference to the editor object.
	@type editor: An editor object

	@param width: The width of the text editor's completion window.
	@type width: An integer object.

	@param height: The height of the text editor's completion window.
	@type height: An integer object.
	"""
	# The flag is true when the position of the word completion window needs to
	# adjusted accross the y-axis.
	editor.y_coordinate_flag = False

	# Get the cursor's coordinate and size.
	cursor_x, cursor_y = get_cursor_window_coordinates(editor)
	cursor_height = get_cursor_size(editor)[1]

	# Get the text editor's textview coordinate and size.
	window = editor.text_view.get_window(TEXT_WINDOW_TEXT)
	rectangle = editor.text_view.get_visible_rect()
	window_x, window_y = window.get_origin()
	window_width, window_height = rectangle.width, rectangle.height

	# Determine where to position the completion window.
	position_x = window_x + cursor_x
	position_y = window_y + cursor_y + cursor_height

	# If the completion window extends past the text editor's buffer,
	# reposition the completion window inside the text editor's buffer area.
	if (position_x + width) > (window_x + window_width):
		position_x = (window_x + window_width) - width
	if (position_y + height) > (window_y + window_height):
		position_y = (window_y + cursor_y) - height
		editor.y_coordinate_flag = True
	return position_x, position_y

def find_file(filename):
	"""
	Find a file in Scribes data folder.

	This function looks data files and images in Scribes data folder.

	#TODO: Use a more brute force approach.

	@param filename: The name of the file to find.
	@type filename: A String object.

	@return: The full path to the file.
	@rtype: A String object.
	"""
	from os import path
	from info import scribes_data_folder
	file_path = path.join(scribes_data_folder, filename)
	return file_path

def create_image(file_path):
	"""
	Create an image of a file.

	@param file_path: Path to the image file.
	@type file_path: A String object.
	"""
	image_file = find_file(file_path)
	from gtk import Image
	image = Image()
	image.set_from_file(image_file)
	return image

def __convert_to_string(value):
	"""
	Convert an integer to a hexidecimal string.

	@param value: An integer representing a unit of the gtk.gdk.Color
	@type value: An Integer object.

	@return: A hexidecimal string
	@rtype: A String object.
	"""
	if not value:
		return "0000"
	string = str(hex(value)).lstrip("0x")
	if len(string) < 4:
		if len(string) == 3:
			string = "0" + string
		elif len(string) == 2:
			string = "00" + string
		else:
			string = "000" + string
	return string

def convert_color_to_spec(color):
	"""
	Convert a color to a string specification.

	@param color: A color.
	@type color: A gtk.gdk.Color object.

	@return: A hexidecimal representing the gtk.gdk.color
	@rtype: A String object.
	"""
	red = __convert_to_string(color.red)
	blue = __convert_to_string(color.blue)
	green = __convert_to_string(color.green)
	string = "#" + red + green + blue
	return string

def select_row(treeview, column=0):
	"""
	Select a row in a treeview if any.

	This function disables the treeview if it has no rows. Otherwise,
	it tries to figure out any row to select.

	@param treeview: Reference to a TreeView object.
	@type self: An gtk.TreeView object.
	"""
	selection = treeview.get_selection()
	model = treeview.get_model()
	path, iterator = selection.get_selected()
	if iterator:
		path = model.get_path(iterator)
		treeview.set_cursor(path, treeview.get_column(column))
		treeview.grab_focus()
	else:
		first_iterator = model.get_iter_first()
		if first_iterator:
			path = model.get_path(first_iterator)
			treeview.set_cursor(path, treeview.get_column(column))
			treeview.grab_focus()
		else:
			treeview.set_property("sensitive", False)
	return

def disconnect_signal(signal_id, instance):
	"""
	Disconnect a signal from an object.

	@param signal_id: A numeric identifier for a signal.
	@type signal_id: A Integer object.

	@param instance: An object connected to a signal.
	@type instance: A GObject object.
	"""
	try:
		if signal_id and instance.handler_is_connected(signal_id): instance.disconnect(signal_id)
	except AttributeError:
		print "Disconnect Signal error: ", instance
	return

def init_gnome():
	"""
	Initialize the GNOME libraries.
	"""
	# Crashes the save dialog if uncommented.
	#import gnome.ui
	from info import version, name, scribes_data_path
	from gnome import PARAM_APP_DATADIR, program_init, ui
	properties = {
		PARAM_APP_DATADIR: scribes_data_path,
	}
	program = program_init(name, version, properties=properties)
	return

try:
	from psyco import bind
	bind(generate_random_number)
	bind(calculate_completion_window_position)
	bind(disconnect_signal)
except ImportError:
	pass