def __testLocal__():
  workingDir =os.path.dirname(os.path.abspath(__file__))
  config_loader = FileConfigLoader("../test_data/build_configuration.json")
  config = config_loader.load_config()
  executor = CommandExecutor(config["Resources"],basePath=workingDir, verbose=True)
  builder = AppConfigurator(config_loader, executor)
  builder.apply()
예제 #2
0
def save_variable(identifier, platform, name, varname):
  filepath = os.path.join(workspace_path, identifier, platform, name)
  config_loader = FileConfigLoader(filepath)
  conf = config_loader.dictionary
  res = [x for x in conf["Variables"] if x["name"] == varname][0]
  res["value"] = request.data
  config_loader.save_config(conf)
  return True
예제 #3
0
def save_variable(identifier, platform, name, varname):
    filepath = os.path.join(workspace_path, identifier, platform, name)
    config_loader = FileConfigLoader(filepath)
    conf = config_loader.dictionary
    res = [x for x in conf["Variables"] if x["name"] == varname][0]
    res["value"] = request.data
    config_loader.save_config(conf)
    return True
예제 #4
0
 def set_configuration(self, config_path, working_dir=None):
     self.config_path = config_path
     if working_dir:
         self.working_dir = working_dir
     else:
         self.working_dir = app_config.get_working_dir()
     self.loader = FileConfigLoader(self.config_path)
     self.configuration = self.loader.load_config()
     self.bindUi()
def __testLocal__():
    workingDir = os.path.dirname(os.path.abspath(__file__))
    config_loader = FileConfigLoader("../test_data/build_configuration.json")
    config = config_loader.load_config()
    executor = CommandExecutor(config["Resources"],
                               basePath=workingDir,
                               verbose=True)
    builder = AppConfigurator(config_loader, executor)
    builder.apply()
예제 #6
0
 def create_new_config(self):
   cur_dir = app_config.get_config_history()[-1]
   params = {"caption":"Choose Configuration","filter":"bc.json"}
   if cur_dir:
     params["dir"] = app_config.get_config_history()[-1]
   f = QtGui.QFileDialog.getSaveFileName(**params)
   if f[0]:
     self.config_path = f[0]
     FileConfigLoader.new(self.config_path)
     app_config.add_config_to_history(self.config_path)
     self.set_configuration(self.config_path, self.working_dir)
예제 #7
0
def save_config(identifier, platform, name):
  print "saved"
  filepath = os.path.join(workspace_path, identifier, platform, name)
  config_loader = FileConfigLoader(filepath)
  try:
    newconf = json.loads(request.data)
    config_loader.save_config(newconf)
    print 'all fine'
    return Response(response="True", status=200)
  except Exception, ex:  # @UnusedVariable
    print ex.message
    return Response(response="False", status=200)
예제 #8
0
def save_config(identifier, platform, name):
    print "saved"
    filepath = os.path.join(workspace_path, identifier, platform, name)
    config_loader = FileConfigLoader(filepath)
    try:
        newconf = json.loads(request.data)
        config_loader.save_config(newconf)
        print 'all fine'
        return Response(response="True", status=200)
    except Exception, ex:  # @UnusedVariable
        print ex.message
        return Response(response="False", status=200)
예제 #9
0
    def create_new_config(self):
        params = {"caption": "Choose Configuration", "filter": "bc.halc"}
        if len(app_config.get_config_history()) > 0:
            last_config = app_config.get_config_history()[-1]
            last_config = last_config.replace("'", '')
            params["dir"] = os.path.dirname(last_config)

        f = QtGui.QFileDialog.getSaveFileName(**params)
        if f[0]:
            self.config_path = f[0]
            FileConfigLoader.new(self.config_path)
            app_config.add_config_to_history(self.config_path)
            self.set_configuration(self.config_path, self.working_dir)
예제 #10
0
 def create_new_config(self):
     params = {"caption":"Choose Configuration","filter":"bc.halc"}
     if len(app_config.get_config_history())>0:
         last_config = app_config.get_config_history()[-1]
         last_config = last_config.replace("'", '')
         params["dir"] = os.path.dirname(last_config)
         
     f = QtGui.QFileDialog.getSaveFileName(**params)
     if f[0]:
         self.config_path = f[0]
         FileConfigLoader.new(self.config_path)
         app_config.add_config_to_history(self.config_path)
         self.set_configuration(self.config_path, self.working_dir)
 def run(self):
     is_valid, errors = self.validate_args()
     res = None
     if is_valid:
         res = self.value_substitutor.substitute(self.ConfigPath)
     else:
         raise InvalidCommandArgumentsError(str(errors))
     if not os.path.isabs(res):
         config_loader = self.executor.parent.config_loader
         dr = os.path.dirname(config_loader.config_file)
         res = os.path.join(dr, res)
     loader = FileConfigLoader(res)
     config = loader.load_config()
     builder = self.executor.parent
     bundles_filter = ConfigBuildFilter()
     builder.apply_parametrized(config, bundles_filter=bundles_filter)
예제 #12
0
def get_variable(identifier, platform, name, varname):
    filepath = os.path.join(workspace_path, identifier, platform, name)
    conf = FileConfigLoader(filepath).dictionary
    res = [x for x in conf["Variables"] if x["name"] == varname][0]
    return Response(response=json.dumps(res),
                    status=200,
                    mimetype="application/json")
예제 #13
0
 def on_show_config_clicked(self):
     if self.working_dir:
         self.config = FileConfigLoader(
             self.get_config_path()).load_config()
         self.cfg = ConfigWindow(self)
         self.cfg.set_configuration(self.get_config_path(),
                                    self.working_dir)
         self.cfg.cw.set_save_path(self.get_config_path())
         self.cfg.show()
예제 #14
0
 def set_configuration(self, config_path, working_dir=None):
     self.config_path = config_path
     if working_dir:
         self.working_dir = working_dir
     else:
         self.working_dir = app_config.get_working_dir()
     self.loader = FileConfigLoader(self.config_path)
     self.configuration = self.loader.load_config()
     self.bindUi()
예제 #15
0
 def on_platform_changed(self):
     ind = self.cmb_platforms.currentIndex()
     if ind > 0:
         self.working_dir = solution_dir.replace(
             '{PlatformType}',
             solution_for_platform[self.cmb_platforms.currentText()])
         self.config = FileConfigLoader(
             self.get_config_path()).load_config()
     else:
         self.working_dir = None
예제 #16
0
def get_config(identifier, platform, name):
    filepath = os.path.join(workspace_path, identifier, platform, name)
    conf = FileConfigLoader(filepath).dictionary
    admin = request.args.get('admin')
    result = conf['Variables']
    if bool(admin):
        result = conf
    return Response(response=json.dumps(result),
                    status=200,
                    mimetype="application/json")
예제 #17
0
    def on_configure_clicked(self):
        config_loader = FileConfigLoader(self.get_config_path())
        builder = AppConfigurator(config_loader,
                                  ZmqChainedLoger(port),
                                  verbose=self.cb_verbose.isChecked(),
                                  debug_mode=self.cb_verbose.isChecked())

        builder.set_execution_dir(self.working_dir)
        self.worker = ConfigRunnerThread(builder)
        self.set_message_receiver()
        self.worker.start()
        self.worker.finished.connect(self.on_worker_finished)
예제 #18
0
def get_resource(identifier, platform, name, resid):
    filepath = os.path.join(workspace_path, identifier, platform, name)
    conf = FileConfigLoader(filepath).dictionary
    res = [x for x in conf["Resources"] if x["rid"] == resid][0]
    file_path = os.path.join(workspace_path, identifier, platform, res['url'])
    print file_path
    if os.path.exists(file_path):
        print 'resource exists:' + file_path
        return send_file(file_path, mimetype='image/png')
    else:
        print 'non existent'
        print 'resource not available at:' + file_path
        return Response(status=404)
예제 #19
0
def job_start(identifier, platform, name):
    Workspace.set(workspace_path)
    filepath = os.path.join(workspace_path, identifier, platform, name)
    loader = FileConfigLoader(filepath)
    config = loader.dictionary
    c_loger = ConsoleLoger()
    s_loger = StringLoger()
    composite_log = CompositeLoger(*[c_loger, s_loger])
    builder = AppConfigurator(loader, composite_log)
    job_output = "No Response"
    try:
        job_output = execute_job.delay(builder, workspace_path,
                                       workspace_path).get(100)
    except OSError, e:
        job_output = e.strerror
        job_output += '\nTraceback' + job_output.traceback
예제 #20
0
def get_config_loader(args):
    if args[1] == '-from':
        ldr = None
        if args[2] == 'fs':
            ldr = FileConfigLoader(args[3])
        elif args[2] == 'svc':
            ldr = SvcConfigLoader(svcUrl, args[3])
        return ldr
    else:
        print '''
    1. automator -from svc [configuration_id] -o [destinationDir]
    2. automator -from fs [configuration_path] -o [destinationDir]
    '''
        raise Exception(
            """Console mode requires a ConfigLoader to be specified:+)
    1. automator -from svc [configuration_id] -o [destinationDir]
    2. automator -from fs [configuration_path] -o [destinationDir]
    """)
예제 #21
0
def run_config(identifier, platform, name):
    Workspace.set(workspace_path)
    filepath = os.path.join(workspace_path, identifier, platform, name)
    loader = FileConfigLoader(filepath)
    config = loader.dictionary
    c_loger = ConsoleLoger()
    s_loger = StringLoger()
    composite_log = CompositeLoger(*[c_loger, s_loger])
    builder = AppConfigurator(loader, composite_log)
    working_dir = request.args.get('wd')
    if working_dir:
        builder.set_execution_dir(working_dir)
    else:
        builder.set_execution_dir(workspace_path)
    res_msg = ''
    try:
        builder.apply()
    except Exception, ex:
        res_msg += ex.message + '\n'
예제 #22
0
def save_resource(identifier, platform, name, resid):
    filepath = os.path.join(workspace_path, identifier, platform, name)
    conf = FileConfigLoader(filepath)
    res = [x for x in conf["Resources"] if x["rid"] == resid][0]
    # data_file = request.files.get('data_file')
    files = request.files.getlist('files[]')
    file_path = os.path.join(workspace_path, identifier, platform, res['url'])
    files[0].save(file_path)
    print "File Path: %s" % file_path
    # shutil.copy(data_file, file)
    # save_file(data_file, file_name)
    # file_size = get_file_size(file_name)
    # file_url = url_for('download', file_name=file_name)
    # providing the thumbnail url is optional
    # thumbnail_url = url_for('thumbnail', file_name=file_name)
    return jsonify(name='test',
                   size=100000,
                   url='/config/%s/%s/%s' % (identifier, platform, res['url']),
                   thumbnail='/config/%s/%s/%s' %
                   (identifier, platform, res['url']))
예제 #23
0
 def on_run_click(self):
     root_url = os.path.dirname(self.config_path)
     if os.name != 'posix':
         root_url = '/' + root_url
     if self.build_output:
         self.build_output.close()
     self.build_output = ConsoleOutput()
     self.build_output.show()
     config_loader = FileConfigLoader(self.config_path)
     builder = AppConfigurator(config_loader,
                               ZmqChainedLoger(1234),
                               verbose=self.verbose)
     builder.set_execution_dir(self.working_dir)
     builder.include_bundles(self.get_included_bundles())
     self.set_message_receiver()
     if self.debug:
         builder.apply()
     else:
         self.worker = ConfigRunnerThread(builder)
         self.worker.start()
         self.worker.finished.connect(self.on_worker_finished)
예제 #24
0
 def open_clicked(self):
     fname, _ = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '~')
     config = FileConfigLoader(fname).load_config()
     self.cf = ConfigWindow(config)
     self.cf.show()
예제 #25
0
class ConfigWindow(QtGui.QMainWindow, Ui_ConfigWindow):
    def __init__(self, main_window, *args, **kwargs):
        super(ConfigWindow, self).__init__(*args, **kwargs)
        self.viewMode = 'admin'
        self.debug = False
        self.verbose = app_config.is_verbose()
        self.config_path = None
        self.working_dir = None
        self.main_window = main_window or self
        self.working_dir_choser = None
        self.messages_thread = None
        self.cw = None
        self.bundlesModel = QtGui.QStandardItemModel()
        self.set_plugins()
        self.setupUi()
        self.set_message_receiver()
        self.start_last_if_any()
    
    def set_plugins(self):
        self.plugins =[]
        for d in config.plugin_dirs:
            self.plugins.extend(get_plugins(d))

    def setupUi(self):
        super(ConfigWindow, self).setupUi(self)
        title="Configurator Version:%s"%(app_config.get_version())
        self.setWindowTitle(title)
        self.cbChooseWorkingDir.clicked.connect(self.chose_working_dir)
        self.set_menu_bar()
        self.set_recent_config_actions()
        self.tool = None
        self.workspace = app_config.get_current_workspace()
        if not self.workspace:
            self.switch_workspace()
        else:
            self.workspace.set_loger(ZmqChainedLoger(1234))
        self.viewMode = self.workspace.mode
        self.actionViewAsAdmin.setChecked(self.viewMode=='admin')
        self.actionViewAsModerator.setChecked(self.viewMode=='moderator')

    def bindUi(self):
        if self.viewMode != 'admin':
            self.tool = self.detailsContainer
        else:
            self.tool = None
        title = os.path.basename((os.path.dirname(os.path.dirname(self.config_path))))
        title +="         -- Configurator Version:%s" % (app_config.get_version())
        self.setWindowTitle(title)
        self.txtWorkingDir.setText(self.working_dir)

        if self.cw:
            self.ltv_content.removeWidget(self.cw)
            self.cw.close()
        self.cw = ConfigForm(self.loader, parent=self, details_parent = self.tool)
        #self.tool.setModel(ToolsListModel(self.plugins, False))
        self.menubar.setWindowTitle(title)
        self.build_output = None
        self.set_bundles_model()

        self.ltv_content.addWidget(self.cw)
        # if self.viewMode!='admin':
        #     self.cw.tlbx_bundles.hide()
        #     self.widget.hide()
        #     width = self.splitter_2.sizeHint().width()
        #     self.splitter_2.setSizes([width*0.3, width*0.7])
        # else:
        #     width = self.splitter_2.sizeHint().width()
        #     self.splitter_2.setSizes([width, 0])
            
    def start_last_if_any(self):
        try:
            config_history = app_config.get_config_history()
            if config_history:
                self.set_configuration(config_history[-1])
        except Exception as ex:
                print(ex)

    def set_configuration(self, config_path, working_dir=None):
        self.config_path = config_path
        if working_dir:
            self.working_dir = working_dir
        else:
            self.working_dir = app_config.get_working_dir()
        self.loader = FileConfigLoader(self.config_path)
        self.configuration = self.loader.load_config()
        self.bindUi()

    def set_bundles_model(self):
        self.bundlesModel.clear()
        work_mode = Workspace.current.mode
        bundle_filter = ConfigBuildFilter(included=Workspace.registered_bundles)
        d = self.get_mode_config_for_key(work_mode, 'bundles')
        bundle_filter.extend_from_dict(d)
        for bundle in self.configuration['Content']['OperationBundles']:
            dataItem = QtGui.QStandardItem(bundle['Name'])
            dataItem.setCheckable(True)
            check_state = bundle_filter.allowed(bundle['Name']) and QtCore.Qt.CheckState.Checked or QtCore.Qt.CheckState.Unchecked
            dataItem.setCheckState(check_state)
            self.bundlesModel.appendRow(dataItem)
    
    def get_mode_config_for_key(self, work_mode, key):
        if self.configuration.has_key('Builds'):
            if self.configuration['Builds'].has_key(work_mode):
                bc = self.configuration['Builds'][work_mode]
                if bc.has_key(key):
                    return copy.deepcopy(bc[key])
        return {}

    def get_included_bundles(self):
        i = 0
        includedBundles = []
        while self.bundlesModel.item(i):
            dataItem = self.bundlesModel.item(i)
            if dataItem.checkState():
                includedBundles.append(dataItem.text())
            i += 1
        return includedBundles
            
    def chose_working_dir(self):
        """
        Choses the current working directory for the current configuration
        """
        res = QtGui.QFileDialog.getExistingDirectory(caption="Choose working directory")
        if res:
            app_config.set_working_dir(res)
            self.working_dir = res
            self.txtWorkingDir.setText(res)


    def show_bundle_selector(self):
        self.bundleSelectorWidget = BundleSelector(self.bundlesModel)
        self.bundleSelectorWidget.show()


    def validate_configuration(self):
        validator = ConfigurationValidator(self.config_path)
        result = validator.validate(self.configuration)
        title = result.is_valid and 'Valid Configuration' or 'Invalid Configurration'
        message = 'Errors:\n'+'\n'.join(['\t'+x for x in result.errors])
        message+= '\nWarnings:\n'+'\n'.join(['\t'+x for x in result.warnings])
        message+= '\nSuggestions:\n'+'\n'.join(['\t'+x for x in result.suggestions])
        msgBox = QtGui.QMessageBox()
        msgBox.setText(title)
        msgBox.setInformativeText(message)
        msgBox.exec_()

    def open_regex_tool(self):
        self.rtool = RegexTool()
        self.rtool.show()


    def sync_workspace(self):
        self.build_output = ConsoleOutput()
        self.build_output.show()
        class thc(QtCore.QThread):
            def __init__(self, workspace):
                self.workspace = workspace
                super(thc, self).__init__()
            def run(self):
                print "running..."
                self.workspace.sync()
        self.th = thc(self.workspace)
        self.th.start()

    def switch_workspace(self, custom_title = None):
        self.workspacedialog = QtGui.QFileDialog(None, custom_title or 'Choose your Workspace!')
        self.workspacedialog.setFileMode(QtGui.QFileDialog.Directory)
        self.workspacedialog.setOption(QtGui.QFileDialog.ShowDirsOnly)
        res = self.workspacedialog.exec_()
        if res:
            dirpath = self.workspacedialog.selectedFiles()[0]
            app_config.set_current_workspace(dirpath)
        while not app_config.get_current_workspace():
            self.switch_workspace('You must choose a valid workspace in order to open the app')
        self.workspace = app_config.get_current_workspace()
        self.workspace.set_loger(ZmqChainedLoger(1234))
        self.workspacedialog.close()

    def reset_workspace(self):
        self.build_output = ConsoleOutput()
        self.build_output.show()
        class thc(QtCore.QThread):
            def __init__(self, workspace):
                self.workspace = workspace
                super(thc, self).__init__()
            def run(self):
                print "running..."
                self.workspace.reset()
        self.th = thc(self.workspace)
        self.th.start()

    def setViewMode(self, modeUsed=None):
        newViewMode = None
        if modeUsed=='moderator':
            self.actionViewAsAdmin.setChecked(not self.actionViewAsModerator.isChecked())
            newViewMode = self.actionViewAsModerator.isChecked() and 'moderator' or 'admin'
        else:
            self.actionViewAsModerator.setChecked(not self.actionViewAsAdmin.isChecked())
            newViewMode = self.actionViewAsAdmin.isChecked() and 'admin' or 'moderator'

        if newViewMode !=self.viewMode and self.config_path:
            self.viewMode = newViewMode
            self.workspace.mode = newViewMode
            self.set_configuration(self.config_path, self.working_dir)

    def set_menu_bar(self):
        def save(is_new, is_cloning_empty):
            def fn ():
                if self.cw:
                    self.cw.save_config(is_new, is_cloning_empty)
            return fn
        def viewModeSetter(mode):
            def fn ():
                return self.setViewMode(mode)
            return fn
        self.actionRun.triggered.connect(self.on_run_click)
        self.actionClose.triggered.connect(self.close)
        self.actionOpen.triggered.connect(self.open_config)
        self.actionNew.triggered.connect(self.create_new_config)
        self.actionEnable_Disable_Bundles.triggered.connect(self.show_bundle_selector)
        self.actionVallidate.triggered.connect(self.validate_configuration)
        self.actionRegex.triggered.connect(self.open_regex_tool)
        self.actionSync.triggered.connect(self.sync_workspace)
        self.actionSwitch_Workspace.triggered.connect(self.switch_workspace)
        self.actionReset.triggered.connect(self.reset_workspace)
        self.actionSave.triggered.connect(save(False, False))
        self.actionSave_As.triggered.connect(save(True, False))
        self.actionClone.triggered.connect(save(True, True))
        self.actionViewAsAdmin.triggered.connect(viewModeSetter('admin'))
        self.actionViewAsModerator.triggered.connect(viewModeSetter('moderator'))
        self.actionVerbose.triggered.connect(self.setVerbosity)
        self.actionVerbose.setChecked(app_config.is_verbose())
        self.actionDebug_2.triggered.connect(self.debugChanged)

    def debugChanged(self):
        self.debug = self.actionDebug.isChecked()

    def setVerbosity(self):
        app_config.set_verbose(self.actionVerbose.isChecked())
        self.verbose = self.actionVerbose.isChecked()

    def create_new_config(self):
        params = {"caption":"Choose Configuration","filter":"bc.halc"}
        if len(app_config.get_config_history())>0:
            last_config = app_config.get_config_history()[-1]
            last_config = last_config.replace("'", '')
            params["dir"] = os.path.dirname(last_config)
            
        f = QtGui.QFileDialog.getSaveFileName(**params)
        if f[0]:
            self.config_path = f[0]
            FileConfigLoader.new(self.config_path)
            app_config.add_config_to_history(self.config_path)
            self.set_configuration(self.config_path, self.working_dir)

    def set_recent_config_actions(self):
        history = app_config.get_config_history()
        history.reverse()
        self.historyActions = []
        for k in history:
            a = QtGui.QAction(self)
            a.triggered.connect(self.open_recent)
            a.setText(k)
            self.historyActions.append(a)
            self.menuRecent.addAction(a)

    def open_recent(self, *args, **kwargs):
        app_config.add_config_to_history(self.sender().text())
        self.set_configuration(self.sender().text(), self.working_dir)

    def open_config(self):
        cur_dir = None
        if app_config.get_config_history():
            cur_dir = app_config.get_config_history()[-1]

        params = {"caption":"Choose Configuration","filter":"Config Files(bc.json *.halc)"}
        if cur_dir:
            params["dir"] = app_config.get_config_history()[-1]
        f = QtGui.QFileDialog.getOpenFileName(**params)

        if f[0]:
            self.config_path = f[0]
            app_config.add_config_to_history(self.config_path)
            self.set_configuration(self.config_path, self.working_dir)

    def on_run_click(self):
        root_url = os.path.dirname(self.config_path)
        if os.name!='posix':
            root_url = '/'+root_url
        if self.build_output:
            self.build_output.close()
        self.build_output = ConsoleOutput()
        self.build_output.show()
        config_loader = FileConfigLoader(self.config_path)
        builder = AppConfigurator(config_loader, ZmqChainedLoger(1234), verbose=self.verbose)
        builder.set_execution_dir(self.working_dir)
        builder.include_bundles(self.get_included_bundles())
        self.set_message_receiver()
        if self.debug:
            builder.apply()
        else:
            self.worker = ConfigRunnerThread(builder)
            self.worker.start()
            self.worker.finished.connect(self.on_worker_finished)

    def on_worker_finished(self):
        self.worker.builder = None
        del self.worker

    def set_message_receiver(self):
        if not self.messages_thread:
            self.messages_thread = MessageSubsriberThread(1234)
            self.messages_thread.on_message_received.connect(self.on_message_received)
            self.messages_thread.start(QThread.TimeCriticalPriority)

    def on_message_received(self, message):
        if self.build_output:
            self.build_output.txt_output.append("%s" % message)

      
    @QtCore.Slot()
    def showOptionsMenu(self):
        self.prefs = PreferencesWidget()
        self.prefs.show()
예제 #26
0
class ConfigWindow(QtGui.QMainWindow, Ui_ConfigWindow):
    def __init__(self, main_window, *args, **kwargs):
        super(ConfigWindow, self).__init__(*args, **kwargs)
        self.viewMode = 'admin'
        self.debug = False
        self.verbose = app_config.is_verbose()
        self.config_path = None
        self.working_dir = None
        self.main_window = main_window or self
        self.working_dir_choser = None
        self.messages_thread = None
        self.cw = None
        self.bundlesModel = QtGui.QStandardItemModel()
        self.set_plugins()
        self.setupUi()
        self.set_message_receiver()
        self.start_last_if_any()

    def set_plugins(self):
        self.plugins = []
        for d in config.plugin_dirs:
            self.plugins.extend(get_plugins(d))

    def setupUi(self):
        super(ConfigWindow, self).setupUi(self)
        title = "Configurator Version:%s" % (app_config.get_version())
        self.setWindowTitle(title)
        self.cbChooseWorkingDir.clicked.connect(self.chose_working_dir)
        self.set_menu_bar()
        self.set_recent_config_actions()
        self.tool = None
        self.workspace = app_config.get_current_workspace()
        if not self.workspace:
            self.switch_workspace()
        else:
            self.workspace.set_loger(ZmqChainedLoger(1234))
        self.viewMode = self.workspace.mode
        self.actionViewAsAdmin.setChecked(self.viewMode == 'admin')
        self.actionViewAsModerator.setChecked(self.viewMode == 'moderator')

    def bindUi(self):
        if self.viewMode != 'admin':
            self.tool = self.detailsContainer
        else:
            self.tool = None
        title = os.path.basename(
            (os.path.dirname(os.path.dirname(self.config_path))))
        title += "         -- Configurator Version:%s" % (
            app_config.get_version())
        self.setWindowTitle(title)
        self.txtWorkingDir.setText(self.working_dir)

        if self.cw:
            self.ltv_content.removeWidget(self.cw)
            self.cw.close()
        self.cw = ConfigForm(self.loader,
                             parent=self,
                             details_parent=self.tool)
        #self.tool.setModel(ToolsListModel(self.plugins, False))
        self.menubar.setWindowTitle(title)
        self.build_output = None
        self.set_bundles_model()

        self.ltv_content.addWidget(self.cw)
        # if self.viewMode!='admin':
        #     self.cw.tlbx_bundles.hide()
        #     self.widget.hide()
        #     width = self.splitter_2.sizeHint().width()
        #     self.splitter_2.setSizes([width*0.3, width*0.7])
        # else:
        #     width = self.splitter_2.sizeHint().width()
        #     self.splitter_2.setSizes([width, 0])

    def start_last_if_any(self):
        try:
            config_history = app_config.get_config_history()
            if config_history:
                self.set_configuration(config_history[-1])
        except Exception as ex:
            print(ex)

    def set_configuration(self, config_path, working_dir=None):
        self.config_path = config_path
        if working_dir:
            self.working_dir = working_dir
        else:
            self.working_dir = app_config.get_working_dir()
        self.loader = FileConfigLoader(self.config_path)
        self.configuration = self.loader.load_config()
        self.bindUi()

    def set_bundles_model(self):
        self.bundlesModel.clear()
        work_mode = Workspace.current.mode
        bundle_filter = ConfigBuildFilter(
            included=Workspace.registered_bundles)
        d = self.get_mode_config_for_key(work_mode, 'bundles')
        bundle_filter.extend_from_dict(d)
        for bundle in self.configuration['Content']['OperationBundles']:
            dataItem = QtGui.QStandardItem(bundle['Name'])
            dataItem.setCheckable(True)
            check_state = bundle_filter.allowed(
                bundle['Name']
            ) and QtCore.Qt.CheckState.Checked or QtCore.Qt.CheckState.Unchecked
            dataItem.setCheckState(check_state)
            self.bundlesModel.appendRow(dataItem)

    def get_mode_config_for_key(self, work_mode, key):
        if self.configuration.has_key('Builds'):
            if self.configuration['Builds'].has_key(work_mode):
                bc = self.configuration['Builds'][work_mode]
                if bc.has_key(key):
                    return copy.deepcopy(bc[key])
        return {}

    def get_included_bundles(self):
        i = 0
        includedBundles = []
        while self.bundlesModel.item(i):
            dataItem = self.bundlesModel.item(i)
            if dataItem.checkState():
                includedBundles.append(dataItem.text())
            i += 1
        return includedBundles

    def chose_working_dir(self):
        """
        Choses the current working directory for the current configuration
        """
        res = QtGui.QFileDialog.getExistingDirectory(
            caption="Choose working directory")
        if res:
            app_config.set_working_dir(res)
            self.working_dir = res
            self.txtWorkingDir.setText(res)

    def show_bundle_selector(self):
        self.bundleSelectorWidget = BundleSelector(self.bundlesModel)
        self.bundleSelectorWidget.show()

    def validate_configuration(self):
        validator = ConfigurationValidator(self.config_path)
        result = validator.validate(self.configuration)
        title = result.is_valid and 'Valid Configuration' or 'Invalid Configurration'
        message = 'Errors:\n' + '\n'.join(['\t' + x for x in result.errors])
        message += '\nWarnings:\n' + '\n'.join(
            ['\t' + x for x in result.warnings])
        message += '\nSuggestions:\n' + '\n'.join(
            ['\t' + x for x in result.suggestions])
        msgBox = QtGui.QMessageBox()
        msgBox.setText(title)
        msgBox.setInformativeText(message)
        msgBox.exec_()

    def open_regex_tool(self):
        self.rtool = RegexTool()
        self.rtool.show()

    def sync_workspace(self):
        self.build_output = ConsoleOutput()
        self.build_output.show()

        class thc(QtCore.QThread):
            def __init__(self, workspace):
                self.workspace = workspace
                super(thc, self).__init__()

            def run(self):
                print "running..."
                self.workspace.sync()

        self.th = thc(self.workspace)
        self.th.start()

    def switch_workspace(self, custom_title=None):
        self.workspacedialog = QtGui.QFileDialog(
            None, custom_title or 'Choose your Workspace!')
        self.workspacedialog.setFileMode(QtGui.QFileDialog.Directory)
        self.workspacedialog.setOption(QtGui.QFileDialog.ShowDirsOnly)
        res = self.workspacedialog.exec_()
        if res:
            dirpath = self.workspacedialog.selectedFiles()[0]
            app_config.set_current_workspace(dirpath)
        while not app_config.get_current_workspace():
            self.switch_workspace(
                'You must choose a valid workspace in order to open the app')
        self.workspace = app_config.get_current_workspace()
        self.workspace.set_loger(ZmqChainedLoger(1234))
        self.workspacedialog.close()

    def reset_workspace(self):
        self.build_output = ConsoleOutput()
        self.build_output.show()

        class thc(QtCore.QThread):
            def __init__(self, workspace):
                self.workspace = workspace
                super(thc, self).__init__()

            def run(self):
                print "running..."
                self.workspace.reset()

        self.th = thc(self.workspace)
        self.th.start()

    def setViewMode(self, modeUsed=None):
        newViewMode = None
        if modeUsed == 'moderator':
            self.actionViewAsAdmin.setChecked(
                not self.actionViewAsModerator.isChecked())
            newViewMode = self.actionViewAsModerator.isChecked(
            ) and 'moderator' or 'admin'
        else:
            self.actionViewAsModerator.setChecked(
                not self.actionViewAsAdmin.isChecked())
            newViewMode = self.actionViewAsAdmin.isChecked(
            ) and 'admin' or 'moderator'

        if newViewMode != self.viewMode and self.config_path:
            self.viewMode = newViewMode
            self.workspace.mode = newViewMode
            self.set_configuration(self.config_path, self.working_dir)

    def set_menu_bar(self):
        def save(is_new, is_cloning_empty):
            def fn():
                if self.cw:
                    self.cw.save_config(is_new, is_cloning_empty)

            return fn

        def viewModeSetter(mode):
            def fn():
                return self.setViewMode(mode)

            return fn

        self.actionRun.triggered.connect(self.on_run_click)
        self.actionClose.triggered.connect(self.close)
        self.actionOpen.triggered.connect(self.open_config)
        self.actionNew.triggered.connect(self.create_new_config)
        self.actionEnable_Disable_Bundles.triggered.connect(
            self.show_bundle_selector)
        self.actionVallidate.triggered.connect(self.validate_configuration)
        self.actionRegex.triggered.connect(self.open_regex_tool)
        self.actionSync.triggered.connect(self.sync_workspace)
        self.actionSwitch_Workspace.triggered.connect(self.switch_workspace)
        self.actionReset.triggered.connect(self.reset_workspace)
        self.actionSave.triggered.connect(save(False, False))
        self.actionSave_As.triggered.connect(save(True, False))
        self.actionClone.triggered.connect(save(True, True))
        self.actionViewAsAdmin.triggered.connect(viewModeSetter('admin'))
        self.actionViewAsModerator.triggered.connect(
            viewModeSetter('moderator'))
        self.actionVerbose.triggered.connect(self.setVerbosity)
        self.actionVerbose.setChecked(app_config.is_verbose())
        self.actionDebug_2.triggered.connect(self.debugChanged)

    def debugChanged(self):
        self.debug = self.actionDebug.isChecked()

    def setVerbosity(self):
        app_config.set_verbose(self.actionVerbose.isChecked())
        self.verbose = self.actionVerbose.isChecked()

    def create_new_config(self):
        params = {"caption": "Choose Configuration", "filter": "bc.halc"}
        if len(app_config.get_config_history()) > 0:
            last_config = app_config.get_config_history()[-1]
            last_config = last_config.replace("'", '')
            params["dir"] = os.path.dirname(last_config)

        f = QtGui.QFileDialog.getSaveFileName(**params)
        if f[0]:
            self.config_path = f[0]
            FileConfigLoader.new(self.config_path)
            app_config.add_config_to_history(self.config_path)
            self.set_configuration(self.config_path, self.working_dir)

    def set_recent_config_actions(self):
        history = app_config.get_config_history()
        history.reverse()
        self.historyActions = []
        for k in history:
            a = QtGui.QAction(self)
            a.triggered.connect(self.open_recent)
            a.setText(k)
            self.historyActions.append(a)
            self.menuRecent.addAction(a)

    def open_recent(self, *args, **kwargs):
        app_config.add_config_to_history(self.sender().text())
        self.set_configuration(self.sender().text(), self.working_dir)

    def open_config(self):
        cur_dir = None
        if app_config.get_config_history():
            cur_dir = app_config.get_config_history()[-1]

        params = {
            "caption": "Choose Configuration",
            "filter": "Config Files(bc.json *.halc)"
        }
        if cur_dir:
            params["dir"] = app_config.get_config_history()[-1]
        f = QtGui.QFileDialog.getOpenFileName(**params)

        if f[0]:
            self.config_path = f[0]
            app_config.add_config_to_history(self.config_path)
            self.set_configuration(self.config_path, self.working_dir)

    def on_run_click(self):
        root_url = os.path.dirname(self.config_path)
        if os.name != 'posix':
            root_url = '/' + root_url
        if self.build_output:
            self.build_output.close()
        self.build_output = ConsoleOutput()
        self.build_output.show()
        config_loader = FileConfigLoader(self.config_path)
        builder = AppConfigurator(config_loader,
                                  ZmqChainedLoger(1234),
                                  verbose=self.verbose)
        builder.set_execution_dir(self.working_dir)
        builder.include_bundles(self.get_included_bundles())
        self.set_message_receiver()
        if self.debug:
            builder.apply()
        else:
            self.worker = ConfigRunnerThread(builder)
            self.worker.start()
            self.worker.finished.connect(self.on_worker_finished)

    def on_worker_finished(self):
        self.worker.builder = None
        del self.worker

    def set_message_receiver(self):
        if not self.messages_thread:
            self.messages_thread = MessageSubsriberThread(1234)
            self.messages_thread.on_message_received.connect(
                self.on_message_received)
            self.messages_thread.start(QThread.TimeCriticalPriority)

    def on_message_received(self, message):
        if self.build_output:
            self.build_output.txt_output.append("%s" % message)

    @QtCore.Slot()
    def showOptionsMenu(self):
        self.prefs = PreferencesWidget()
        self.prefs.show()