Baby Language Lab Scripts
A collection of data processing tools.
 All Classes Namespaces Files Functions Variables Pages
view_configs_window.py
Go to the documentation of this file.
1 from gi.repository import Gtk as gtk
2 from gi.repository import GObject as gobject
3 import subprocess
4 import glob
5 import os
6 
7 from utils.ui_utils import UIUtils
8 from utils.progress_dialog import ProgressDialog
9 from db.bll_database import BLLDatabase, DBConstants
10 from data_structs.output_config import OutputConfig
11 from parsers.stats_exporter import StatsExporter
12 from ui.stats_app.config_window import ConfigWindow
13 
15  def __init__(self):
16  self.window = gtk.Window(gtk.WindowType.TOPLEVEL)
17  self.window.set_title('Statistics Application')
18  self.window.set_border_width(10)
19  self.window.set_default_size(600, 450)
20  self.window.connect('destroy', lambda x: gtk.main_quit())
21 
22  vbox = gtk.VBox()
23 
24  treeview = self._build_treeview()
25  scrolled_win = gtk.ScrolledWindow()
26  scrolled_win.set_policy(gtk.PolicyType.AUTOMATIC, gtk.PolicyType.AUTOMATIC)
27  scrolled_win.add(treeview)
28  vbox.pack_start(scrolled_win, True, True, 0)
29 
30  button_box = gtk.HButtonBox()
31  button_box.set_layout(gtk.ButtonBoxStyle.EDGE)
32 
33  create_button = UIUtils.create_button('Create Configuration', UIUtils.BUTTON_ICONS.CREATE)
34  create_button.connect('clicked', lambda w: ConfigWindow(lambda new_config: self._add_callback(treeview.get_model(), new_config)))
35  button_box.pack_start(create_button, True, True, 0)
36 
37  edit_button = UIUtils.create_button('Edit Configuration', UIUtils.BUTTON_ICONS.EDIT)
38  edit_button.connect('clicked', lambda w: self._edit_config(treeview))
39  button_box.pack_start(edit_button, True, True, 0)
40 
41  run_button = UIUtils.create_button('Run Configuration', UIUtils.BUTTON_ICONS.RUN)
42  run_button.connect('clicked', lambda w: self._run_config(treeview))
43  button_box.pack_start(run_button, True, True, 0)
44 
45  delete_button = UIUtils.create_button('Delete', UIUtils.BUTTON_ICONS.DELETE)
46  delete_button.connect('clicked', lambda w: self._delete_config(treeview))
47  button_box.pack_start(delete_button, True, True, 0)
48 
49  exit_button = UIUtils.create_button('Exit', UIUtils.BUTTON_ICONS.EXIT)
50  exit_button.connect('clicked', lambda w: gtk.main_quit())
51  button_box.pack_start(exit_button, True, True, 0)
52 
53  vbox.pack_end(button_box, False, False, 0)
54 
55  self.window.add(vbox)
56  self.window.show_all()
57 
58  def _add_callback(self, model, new_config):
59  model.append([new_config.name,
60  new_config.desc,
61  UIUtils.get_db_timestamp_str(new_config.created) if new_config.created else '-',
62  self._get_output_names_str(new_config.outputs),
63  new_config,
64  ])
65 
66  def _edit_config(self, treeview):
67  model, it = treeview.get_selection().get_selected()
68  if it:
69  config = model.get(it, 4)[0]
70  ConfigWindow(lambda edited_config: self._edit_callback(treeview.get_model(), edited_config, it), config)
71 
72  else:
73  UIUtils.show_message_dialog('Please select a row.', gtk.MessageTYpe.WARNING)
74 
75  def _edit_callback(self, model, edited_config, row_it):
76  model.set_value(row_it, 0, edited_config.name)
77  model.set_value(row_it, 1, edited_config.desc)
78  model.set_value(row_it, 2, UIUtils.get_db_timestamp_str(edited_config.created) if edited_config.created else '-')
79  model.set_value(row_it, 3, self._get_output_names_str(edited_config.outputs))
80  model.set_value(row_it, 4, edited_config)
81 
82  def _get_output_names_str(self, outputs):
83  output_names = ''
84  i = 0
85  while i < len(outputs):
86  output_names += outputs[i].name
87  if i < len(outputs) - 1:
88  output_names += ',\n'
89  i += 1
90 
91  if not output_names:
92  output_names = '-'
93 
94  return output_names
95 
96  def _build_list_store(self):
97  list_store = gtk.ListStore(
98  gobject.TYPE_STRING, #name
99  gobject.TYPE_STRING, #description
100  gobject.TYPE_STRING, #created timestamp
101  gobject.TYPE_STRING, #output names
102  gobject.TYPE_PYOBJECT, #hidden object
103  )
104 
105  db = BLLDatabase()
106  configs = OutputConfig.db_select(db)
107  for cur_config in configs:
108  output_names = self._get_output_names_str(cur_config.outputs)
109  created_str = UIUtils.get_db_timestamp_str(cur_config.created) if cur_config.created else '-'
110 
111  list_store.append([
112  cur_config.name,
113  cur_config.desc,
114  created_str,
115  output_names,
116  cur_config,
117  ])
118 
119  db.close()
120 
121  return list_store
122 
123  def _build_treeview(self):
124  list_store = self._build_list_store()
125  treeview = gtk.TreeView(list_store)
126 
127  col_names = ['Name', 'Description', 'Created', 'Outputs']
128  for i in range(len(col_names)):
129  col = gtk.TreeViewColumn(col_names[i], gtk.CellRendererText(), text=(i))
130  col.set_resizable(True)
131  col.set_min_width( UIUtils.calc_treeview_col_min_width(col_names[i]) )
132  treeview.append_column(col)
133 
134  return treeview
135 
136  def _delete_config(self, treeview):
137  model, it = treeview.get_selection().get_selected()
138  if it:
139  if UIUtils.show_confirm_dialog('Are you sure you want to delete this configuration?'):
140  config = model.get(it, 4)[0]
141 
142  model.remove(it)
143 
144  if config.db_id != None:
145  db = BLLDatabase()
146  config.db_delete(db)
147  db.close()
148  else:
149  UIUtils.show_no_sel_dialog()
150 
151  # def _run_config(self, treeview):
152  # model, it = treeview.get_selection().get_selected()
153  # if it:
154  # config = model.get(it, 4)[0]
155  # trs_filename = UIUtils.open_file(
156  # title='Select TRS File...',
157  # filters=[UIUtils.TRS_FILE_FILTER,
158  # UIUtils.ALL_FILE_FILTER,
159  # ])
160 
161  # if trs_filename: #will be None if user clicked 'cancel' or closed the dialog window
162  # export_filename, open_now = UIUtils.save_file(
163  # title='Export to...',
164  # filters=[UIUtils.CSV_FILE_FILTER,
165  # UIUtils.ALL_FILE_FILTER,
166  # ],
167  # open_now_opt=True)
168 
169  # if export_filename:
170  # exporter = StatsExporter(config, trs_filename, export_filename)
171 
172  # progress_phases = ['Parsing TRS File...']
173  # i = 0
174  # while i < len(config.outputs):
175  # progress_phases.append('Processing output #%d' % (i + 1))
176  # i += 1
177 
178  # progress_dialog = ProgressDialog('Working...', progress_phases)
179  # progress_dialog.show()
180  # exporter.export(progress_update_fcn=progress_dialog.set_fraction,
181  # progress_next_phase_fcn=progress_dialog.next_phase)
182  # progress_dialog.ensure_finish()
183 
184  # if open_now:
185  # subprocess.Popen(['%s' % DBConstants.SETTINGS.SPREADSHEET_PATH, export_filename])
186  # else:
187  # UIUtils.show_message_dialog('Export successful.')
188 
189  # else:
190  # UIUtils.show_no_sel_dialog()
191 
192  def _run_config(self, treeview):
193  model, it = treeview.get_selection().get_selected()
194  if it:
195  config = model.get(it, 4)[0]
196  trs_folder = UIUtils.open_folder(
197  title='Select TRS Folder...'
198  )
199 
200  if trs_folder: #will be None if user clicked 'cancel' or closed the dialog window
201  export_folder = UIUtils.open_folder(
202  title='Select Output Folder...'
203  )
204  if export_folder:
205  #trs_filenames = glob.glob(trs_folder + '\\*.trs')
206  trs_filenames = self._get_trs_filenames(trs_folder)
207  export_filenames = map(lambda name: '%s\\%s-stats.csv' % (export_folder, os.path.basename(name)[:-4]), trs_filenames)
208 
209  phases = ['File %d of %d' % (i + 1, len(trs_filenames)) for i in range(len(trs_filenames))]
210  dialog = ProgressDialog('Working...', phases)
211  dialog.show()
212 
213  for j in range(len(trs_filenames)):
214  exporter = StatsExporter(config, trs_filenames[j], export_filenames[j])
215  exporter.export()
216  dialog.set_fraction(1.0)
217  if j < len(trs_filenames) - 1:
218  dialog.next_phase()
219 
220  dialog.ensure_finish()
221 
222  UIUtils.show_message_dialog('Export successful.')
223 
224  else:
225  UIUtils.show_no_sel_dialog()
226 
227  def _get_trs_filenames(self, root_dir):
228  out_filenames = []
229  in_filenames = glob.glob(root_dir + '\\*')
230 
231  for cur_name in in_filenames:
232  if os.path.isdir(cur_name):
233  out_filenames.extend(self._get_trs_filenames(cur_name))
234  elif os.path.isfile(cur_name) and cur_name.lower().endswith('.trs'):
235  out_filenames.append(cur_name)
236 
237  return out_filenames