#!/usr/bin/env python
import sys
import os
import functions
try:
import pygtk
pygtk.require("2.0")
except:
print("pygtk Not Availible")
sys.exit(1)
try:
import gtk
import gobject
except:
print("GTK Not Availible")
sys.exit(1)
class PrefWin(object):
def delete_event(self, widget, event):
self.window.destroy()
def __init__(self, parent):
self.parent = parent
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title("MinimPy Program Preferences")
self.window.connect("delete_event", self.delete_event)
self.window.set_border_width(10)
self.window.set_transient_for(parent.window)
self.window.set_modal(True)
table = gtk.Table(11, 2,)
self.set_tab_pos_cbo = gtk.combo_box_new_text()
self.set_tab_pos_cbo.append_text("Left")
self.set_tab_pos_cbo.append_text("Right")
self.set_tab_pos_cbo.append_text("Top")
self.set_tab_pos_cbo.append_text("Bottom")
self.set_tab_pos_cbo.set_active(parent.notebook.get_tab_pos())
label = gtk.Label("Program Tab Positions")
table.attach(label, 0, 1, 0, 1)
table.attach(self.set_tab_pos_cbo, 1, 2, 0, 1)
sep = gtk.HSeparator()
table.attach(sep, 0, 2, 1, 2)
self.load_recent_chk = gtk.CheckButton("Load Last Project on Startup")
self.load_recent_chk.set_active(self.parent.config.getboolean('project', 'load_recent'))
table.attach(self.load_recent_chk, 0, 2, 2, 3)
self.batch_allocation_chk = gtk.CheckButton("Batch Allocation (simulated allocations, only for research purposes)")
self.batch_allocation_chk.set_active(self.parent.config.getboolean('interface', 'batch_allocation'))
table.attach(self.batch_allocation_chk, 0, 2, 4, 5)
self.alt_random_chk = gtk.CheckButton("Use Operating System's Random Number Generator (Windows and Linux)")
self.alt_random_chk.set_active(self.parent.config.getboolean('operations', 'alt_random'))
table.attach(self.alt_random_chk, 0, 2, 5, 6)
self.backup_network_file_chk = gtk.CheckButton("Back up Network Sync File to this Folder -->")
self.backup_network_file_chk.set_active(self.parent.config.getboolean('operations', 'backup_network'))
table.attach(self.backup_network_file_chk, 0, 1, 6, 7)
self.backup_network_file_path_button = gtk.FileChooserButton("Select Backup Path")
self.backup_network_file_path_button.set_title('Select Path for Network Sync Backup')
self.backup_network_file_path_button.set_action(gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER)
self.backup_network_file_path_button.set_current_folder(self.parent.config.get('operations', 'backup_network_path'))
table.attach(self.backup_network_file_path_button, 1, 2, 6, 7)
self.cach_cred_chk = gtk.CheckButton("Store username and password for network sync")
self.cach_cred_chk.set_active(self.parent.config.getboolean('operations', 'cach_cred'))
table.attach(self.cach_cred_chk, 0, 1, 7, 8)
hb = gtk.HBox()
button = gtk.Button('Show Cached Data')
button.connect('clicked', self.clear_cach_button)
hb.pack_start(button, False, False)
table.attach(hb, 1, 2, 7, 8)
sep = gtk.HSeparator()
table.attach(sep, 0, 2, 8, 9)
self.cached_cred_vbox = gtk.VBox()
table.attach(self.cached_cred_vbox, 0, 2, 9, 10)
sep = gtk.HSeparator()
table.attach(sep, 0, 2, 10, 11)
btn = gtk.Button(None, gtk.STOCK_SAVE)
btn.connect("clicked", self.save_prefs)
table.attach(btn, 0, 1, 11, 12, xoptions=gtk.EXPAND)
btn = gtk.Button(None, gtk.STOCK_CLOSE)
btn.connect("clicked", self.close_window)
table.attach(btn, 1, 2, 11, 12, xoptions=gtk.EXPAND)
self.window.add(table)
self.window.show_all()
self.cached_cred_vbox.hide()
def clear_cach_button(self, button, data=None):
if button.get_label() == 'Show Cached Data':
button.set_label('Hide Cached Data')
else:
button.set_label('Show Cached Data')
self.files_treeview.destroy()
self.cached_cred_vbox.hide()
return
path_found = True
cach_empty = False
platform = sys.platform # if linux linux2 if windows win32
if platform == 'linux2':
path = os.path.expanduser('~/.subversion/auth/svn.simple')
elif platform == 'win32':
path = os.path.join(os.path.expandvars('$APPDATA'), 'Subversion\\auth\\svn.simple')
else:
path_found = False
if path_found:
if os.path.exists(path) and os.path.isdir(path):
files = os.listdir(path)
files = map(lambda x: os.path.join(path, x), files)
files = filter(os.path.isfile, files)
#files = dict([(os.path.basename(x), x) for x in files])
if len(files):
self.display_cached_data(files)
return
else:
# this probably means the cach folder is empty
cach_empty = True
else:
path_found = False
def is_cached_data_file(self, file_name):
# every other line of file must be 'K n' and 'V m' valuse.
# n and m must be the lengths of the following lines
# ther must be a line == 'K 8' followed by a line == 'username' followed by line == 'V m' followed by username of len m
# the last line mus be a 'END'
fp = open(file_name)
lines = fp.readlines()
fp.close()
lines = map(str.strip, lines)
if lines[-1] != 'END': return (False, "Last line mus be a 'END'")
lines = lines[:-1]
if not all([line.startswith('K') for line in lines[::4]]): return (False, "Every fourth line must be a 'K n'")
if not all([line.startswith('V') for line in lines[2::4]]): return (False, "Every fourth line must be a 'V m'")
for i in range(0, len(lines)-1, 2):
if lines[i].split()[1].isdigit():
length = int(lines[i].split()[1])
else:
return (False, "In 'K n' and 'V m' n and m must be integers")
if len(lines[i+1]) != length:
return (False, "n and m in 'K n' and 'V m' must be lengths of following lines")
if lines[-3] != 'username':
return (False, "There is no 'username' line")
if lines[-7] != 'svn:realmstring':
return (False, "There is no 'svn:realmstring' line")
return [True, lines[-1], lines[-5]]
def display_cached_data(self, files):
self.cached_files = []
accounts = []
for f in files:
try:
ret = self.is_cached_data_file(f)
except:
continue
if ret[0]:
self.cached_files.append(f)
account = ret[1:]
account.append(False)
accounts.append(account)
column_types = (gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_BOOLEAN)
column_names = ('Username', 'SVN Realmstring', 'Clear')
liststore = gtk.ListStore(*column_types)
treeview = gtk.TreeView(liststore)
treeview.set_grid_lines(gtk.TREE_VIEW_GRID_LINES_BOTH)
for n in range(len(column_names)):
if n < 2:
cell = gtk.CellRendererText()
tvcolumn = gtk.TreeViewColumn(column_names[n], cell, text=n)
else:
cell = gtk.CellRendererToggle()
cell.set_property('activatable', True)
cell.connect('toggled', self.cach_clear_toggled, n, liststore)
tvcolumn = gtk.TreeViewColumn(column_names[n], cell)
tvcolumn.add_attribute(cell, 'active', n)
treeview.append_column(tvcolumn)
self.files_treeview = treeview
for account in accounts:
liststore.append(account)
hb = gtk.HBox()
hb.pack_start(self.files_treeview)
self.cached_cred_vbox.pack_start(hb)
self.cached_cred_vbox.show_all()
def cach_clear_toggled(self, cell, row, col, model):
model[row][col] = not model[row][col]
def save_prefs(self, widget, data=None):
self.parent.notebook.set_tab_pos(self.set_tab_pos_cbo.get_active())
self.parent.hbuttonbox_batch_mode.set_property('visible', self.batch_allocation_chk.get_active())
self.parent.config.set('interface', 'tab_pos', self.set_tab_pos_cbo.get_active())
self.parent.config.set('interface', 'batch_allocation', self.batch_allocation_chk.get_active())
self.parent.config.set('project', 'load_recent', self.load_recent_chk.get_active())
self.parent.config.set('operations', 'alt_random', self.alt_random_chk.get_active())
self.parent.config.set('operations', 'cach_cred', self.cach_cred_chk.get_active())
self.parent.config.set('operations', 'backup_network', self.backup_network_file_chk.get_active())
self.parent.config.set('operations', 'backup_network_path', self.backup_network_file_path_button.get_filename())
with open(self.parent.config_file, 'wb') as config_file:
self.parent.config.write(config_file)
config_file.flush()
config_file.close()
self.parent.config.read(self.parent.config_file)
if not self.parent.trial_file_name:
if self.parent.config.getboolean('project', 'load_recent'):
self.parent.trial_file_name = self.parent.config.get('project', 'recent_trial')
else:
self.parent.trial_file_name = None
if self.parent.trial_file_name:
self.parent.waiting_function(self.parent.load_trial, (self.parent.trial_file_name,))
if not self.parent.trial_file_name:
functions.error_dialog(self.window, 'Critical error while loading the last trial!', 'Trial load failed!')
self.parent.set_new_trial()
else:
self.parent.set_new_trial()
if self.cached_cred_vbox.get_property('visible'):
model = self.files_treeview.get_model()
for row in range(len(model)):
if model[row][2]:
os.remove(self.cached_files[row])
self.parent.random = functions.get_app_random(self.parent.config)
self.window.destroy()
def close_window(self, widget, data=None):
self.window.destroy()