/usr/share/pyshared/SCRIBES/Utils.py is in scribes 0.4~r543-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | from re import UNICODE, compile as compile_
word_pattern = compile_("\w+|[-]", UNICODE)
def calculate_resolution_independence(window, width, height):
# Need to deprecate this function.
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):
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():
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_for_mime_type(mime):
from gtksourceview2 import language_manager_get_default
lang_manager = language_manager_get_default()
lang_ids = lang_manager.get_language_ids()
for i in lang_ids:
response()
lang = lang_manager.get_language(i)
for m in lang.get_mime_types():
response()
if m == mime: return lang
return None
def get_file_monitor(path):
from gio import File, FILE_MONITOR_NONE
return File(path).monitor_file(FILE_MONITOR_NONE, None)
def get_folder_monitor(path):
from gio import File, FILE_MONITOR_NONE
return File(path).monitor_directory(FILE_MONITOR_NONE, None)
def monitor_events(args, event_types):
return args[-1] in event_types
def get_fileinfo(path):
if not path: return None
from gio import File
return File(path).query_info("standard::*")
def get_mimetype(path):
if not path: return None
from gio import File, content_type_guess
gfile = File(path)
uri = gfile.get_uri()
if File(uri).get_uri_scheme() != "file": return content_type_guess(uri)
return gfile.query_info("standard::content-type").get_content_type()
def get_language(uri):
try:
if uri is None: return None
language = __get_language_for_mime_type(get_mimetype(uri))
except RuntimeError:
print "Caught runtime error when determining mimetype or language"
return None
return language
def create_encoding_box(combobox):
from i18n 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_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):
value = True
from gio import File
if File(uri).get_uri_scheme() == "file":
local_path = File(uri).get_path()
from os import access, W_OK, path
if path.exists(local_path):
value = access(local_path, W_OK)
else:
from Globals import home_folder
if local_path.startswith(home_folder) is False:
value = False
else:
writable_scheme = ["ssh", "sftp", "smb", "dav", "davs", "ftp"]
scheme = File(uri).get_uri_scheme()
if not scheme in writable_scheme: value = False
return value
def get_file_size(uri):
from gio import File
return File(uri).query_info("*").get_size()
def create_menuitem(string, stock_id=None):
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):
# 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):
from os import path
from Globals import data_folder
file_path = path.join(data_folder, filename)
return file_path
def create_image(file_path):
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):
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):
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):
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):
is_connected = instance.handler_is_connected
disconnect = instance.disconnect
if signal_id and is_connected(signal_id): disconnect(signal_id)
return
def __is_beside_bracket(iterator, characters):
if iterator.get_char() in characters: return True
iterator.backward_char()
if iterator.get_char() in characters: return True
return False
def __is_open_bracket(iterator, characters):
return __is_beside_bracket(iterator, characters)
def __is_close_bracket(iterator, characters):
return __is_beside_bracket(iterator, characters)
def __reposition_iterator(iterator, open_chars, close_chars):
if __is_open_bracket(iterator.copy(), open_chars):
if iterator.get_char() in open_chars: return iterator
iterator.backward_char()
else:
if not (iterator.get_char() in close_chars): return iterator
iterator.forward_char()
return iterator
def __get_open_characters():
return ("{", "(", "[", "<")
def __get_close_characters():
return ("}", ")", "]", ">")
def __get_open_character(iterator):
iterator.backward_char()
return iterator.get_char()
def __get_close_character(iterator):
return iterator.get_char()
def __get_pair_character(character):
if character == "{":
pair_character = "}"
elif character == "}":
pair_character = "{"
elif character == "(":
pair_character = ")"
elif character == ")":
pair_character = "("
elif character == "[":
pair_character = "]"
elif character == "]":
pair_character = "["
elif character == "<":
pair_character = ">"
elif character == ">":
pair_character = "<"
return pair_character
def __is_open_character(iterator):
characters = __get_open_characters()
if iterator.get_char() in characters: return True
success = iterator.backward_char()
if not success: return False
if iterator.get_char() in characters: return True
return False
def __is_close_character(iterator):
characters = __get_close_characters()
iterator.backward_char()
if iterator.get_char() in characters: return True
iterator.forward_char()
if iterator.get_char() in characters: return True
return False
def __reposition_open_iterator(iterator):
characters = __get_open_characters()
if not (iterator.get_char() in characters): return iterator
iterator.forward_char()
return iterator
def __reposition_close_iterator(iterator):
characters = __get_close_characters()
iterator.backward_char()
if iterator.get_char() in characters: return iterator
iterator.forward_char()
return iterator
def __search_for_open_character(iterator):
iterator = __reposition_close_iterator(iterator.copy())
character = __get_close_character(iterator.copy())
search_character = __get_pair_character(character)
count = 0
while True:
success = iterator.backward_char()
if not success: raise ValueError
char = iterator.get_char()
if char == character: count += 1
if char == search_character and not count: break
if char == search_character and count: count -= 1
return iterator
def __search_for_close_character(iterator):
iterator = __reposition_open_iterator(iterator.copy())
character = __get_open_character(iterator.copy())
search_character = __get_pair_character(character)
count = 0
while True:
char = iterator.get_char()
if char == character: count += 1
if char == search_character and not count: break
if char == search_character and count: count -= 1
success = iterator.forward_char()
if not success: raise ValueError
return iterator
def find_matching_bracket(iterator):
try:
if __is_open_character(iterator.copy()):
iterator = __search_for_close_character(iterator.copy())
elif __is_close_character(iterator.copy()):
iterator = __search_for_open_character(iterator.copy())
else:
iterator = None
except ValueError:
return None
return iterator
def init_gnome():
from gobject import set_application_name, set_prgname
set_prgname("Scribes")
set_application_name("Scribes")
return
def backward_to_line_begin(iterator):
if iterator.starts_line(): return iterator
while True:
iterator.backward_char()
if iterator.starts_line(): break
return iterator
def forward_to_line_end(iterator):
if iterator.ends_line(): return iterator
iterator.forward_to_line_end()
return iterator
def open_database(basepath, flag="c"):
if not basepath.endswith(".gdb"): raise Exception
from Globals import metadata_folder
from os.path import exists, join, split
database_path = join(metadata_folder, basepath.strip("/"))
folder, file_ = split(database_path)
if not (folder or file_): raise Exception
if not exists(folder):
from os import makedirs
makedirs(folder)
from shelve import open as open_
from anydbm import error
try:
database = open_(database_path, flag=flag, writeback=False)
except error:
database = open_(database_path, flag="n", writeback=False)
return database
def get_save_processor():
try:
from dbus import DBusException
from Globals import dbus_iface, session_bus, python_path
from Globals import SCRIBES_SAVE_PROCESS_DBUS_PATH
from Globals import SCRIBES_SAVE_PROCESS_DBUS_SERVICE
services = dbus_iface.ListNames()
if not (SCRIBES_SAVE_PROCESS_DBUS_SERVICE in services): return None
processor_object = session_bus.get_object(SCRIBES_SAVE_PROCESS_DBUS_SERVICE, SCRIBES_SAVE_PROCESS_DBUS_PATH)
except DBusException:
return None
return processor_object
def response():
from gtk import events_pending, main_iteration
while events_pending(): main_iteration(False)
return
def create_uri(uri, exclusive=True):
response()
from gio import File
File(uri).replace_contents("")
response()
return
def remove_uri(uri):
from gio import File
response()
File(uri).delete()
response()
return
def uri_is_folder(uri):
from gio import Error
try:
if not uri: return False
from gio import File
filetype = File(uri).query_info("*").get_file_type()
if filetype == 2: return True
except Error:
return False
return False
def set_vm_interval(response=True):
return False
try:
from psyco import bind
bind(response)
bind(open_database)
except ImportError:
pass
|