<commit_before>#
# ControlPanel.py -- AirMass chart plugin
# 
# <NAME> (<EMAIL>)
#
import os.path

from ginga.misc import Bunch
from ginga.misc import Widgets
from PyQt4 import QtGui, QtCore

import PlBase
import entity
import misc

class ControlPanel(PlBase.Plugin):

    def __init__(self, model, view, controller, logger):
        super(ControlPanel, self).__init__(model, view, controller, logger)

        self.input_dir = "inputs"
        
    def build_gui(self, container):
        vbox = Widgets.VBox()
        vbox.set_border_width(4)
        vbox.set_spacing(2)

        sw = Widgets.ScrollArea()
        sw.set_widget(vbox)

        fr = Widgets.Frame("Factors")

        captions = (('Slew weight:', 'label', 'Slew weight', 'entry'),
                    ('Delay weight:', 'label', 'Delay weight', 'entry'),
                    ('Filter weight:', 'label', 'Filter weight', 'entry'),
                    ('Rank weight:', 'label', 'Rank weight', 'entry'),
                    ('Priority weight:', 'label', 'Priority weight', 'entry'),
                    ('Build Schedule', 'button'),
                    )
        w, b = Widgets.build_info(captions, orientation='vertical')
        self.w = b

        b.slew_weight.set_text(str(self.model.w_slew))
        b.delay_weight.set_text(str(self.model.w_delay))
        b.filter_weight.set_text(str(self.model.w_filterchange))
        b.rank_weight.set_text(str(self.model.w_rank))
        b.priority_weight.set_text(str(self.model.w_priority))
        b.build_schedule.add_callback('activated', self.build_schedule_cb)

        fr.set_widget(w)
        vbox.add_widget(fr, stretch=0)

        fr = Widgets.Frame("Files")

        captions = (('Inputs:', 'label', 'Input dir', 'entry'),
                    ('Load Info', 'button'),
                    )
        w, b = Widgets.build_info(captions, orientation='vertical')
        self.w.update(b)

        b.load_info.add_callback('activated', self.initialize_model_cb)

        fr.set_widget(w)
        vbox.add_widget(fr, stretch=0)

        spacer = Widgets.Label('')
        vbox.add_widget(spacer, stretch=1)
        
        ## btns = Widgets.HBox()
        ## btns.set_spacing(3)

        ## btn = Widgets.Button("Close")
        ## #btn.add_callback('activated', lambda w: self.close())
        ## btns.add_widget(btn, stretch=0)
        ## btns.add_widget(Widgets.Label(''), stretch=1)
        ## vbox.add_widget(btns, stretch=0)

        self.sw = sw
        top_w = sw.get_widget()

        layout = QtGui.QVBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(4)
        container.setLayout(layout)

        layout.addWidget(top_w, stretch=1)

    def initialize_model_cb(self, widget):
        self.input_dir = self.w.input_dir.get_text().strip()

        try:
            # read schedule
            schedule_file = os.path.join(self.input_dir, "schedule.csv")
            if not os.path.exists(schedule_file):
                self.logger.error("File not readable: %s" % (schedule_file))
                return
            self.logger.info("reading schedule from %s" % (schedule_file))
            schedule = entity.ScheduleFile(schedule_file, self.logger)
            # Set the appropriate "schedule" attributes in the
            # QueueModel
            if 'scheduletab' not in self.view.plugins:
                self.view.load_plugin('scheduletab', 'ScheduleTab', 'ScheduleTab', 'report', 'Schedule')
            self.model.set_schedule(schedule)
            self.model.set_schedule_info(schedule.schedule_info)

            # read proposals
            proposal_file = os.path.join(self.input_dir, "programs.csv")
            if not os.path.exists(proposal_file):
                self.logger.error("File not readable: %s" % (proposal_file))
                return
            self.logger.info("reading proposals from %s" % (proposal_file))
            programs = entity.ProgramsFile(proposal_file, self.logger)
            if 'programstab' not in self.view.plugins:
                self.view.load_plugin('programstab', 'ProgramsTab', 'ProgramsTab', 'report', 'Programs')
            self.model.set_programs(programs)
            self.model.set_programs_info(programs.programs_info)

            # read observing blocks
            oblist = {}
            oblist_info = []
            for propname in programs.programs_info:
                obfile = os.path.join(self.input_dir, propname+".csv")
                if not os.path.exists(obfile):
                    self.logger.error("File not readable: %s" % (obfile))
                    continue
                self.logger.info("loading observing blocks from file %s" % obfile)
                oblist[propname] = entity.OBListFile(obfile, self.logger, propname, programs.programs_info)
                oblist_info.extend(oblist[propname].obs_info)
            self.model.set_oblist(oblist)
            self.model.set_oblist_info(oblist_info)

        except Exception as e:
            self.logger.error("Error initializing: %s" % (str(e)))

        self.logger.info("model initialized")

    def build_schedule_cb(self, widget):
        # validate and make changes to model from gui
        self.model.w_slew = float(self.w.slew_weight.get_text())
        self.model.w_delay = float(self.w.delay_weight.get_text())
        self.model.w_filterchange = float(self.w.filter_weight.get_text())
        self.model.w_rank = float(self.w.rank_weight.get_text())
        
        self.view.nongui_do(self.model.schedule_all)
        
#END<commit_msg>Fix to allow edits to be picked up without saving and reloading<commit_after>#
# ControlPanel.py -- AirMass chart plugin
# 
# <NAME> (<EMAIL>)
#
import os.path

from ginga.misc import Bunch
from ginga.misc import Widgets
from PyQt4 import QtGui, QtCore

import PlBase
import entity
import misc

class ControlPanel(PlBase.Plugin):

    def __init__(self, model, view, controller, logger):
        super(ControlPanel, self).__init__(model, view, controller, logger)

        self.input_dir = "inputs"

        self.schedule = None
        self.programs = None
        self.oblist = None
        
    def build_gui(self, container):
        vbox = Widgets.VBox()
        vbox.set_border_width(4)
        vbox.set_spacing(2)

        sw = Widgets.ScrollArea()
        sw.set_widget(vbox)

        fr = Widgets.Frame("Factors")

        captions = (('Slew weight:', 'label', 'Slew weight', 'entry'),
                    ('Delay weight:', 'label', 'Delay weight', 'entry'),
                    ('Filter weight:', 'label', 'Filter weight', 'entry'),
                    ('Rank weight:', 'label', 'Rank weight', 'entry'),
                    ('Priority weight:', 'label', 'Priority weight', 'entry'),
                    ('Build Schedule', 'button'),
                    )
        w, b = Widgets.build_info(captions, orientation='vertical')
        self.w = b

        b.slew_weight.set_text(str(self.model.w_slew))
        b.delay_weight.set_text(str(self.model.w_delay))
        b.filter_weight.set_text(str(self.model.w_filterchange))
        b.rank_weight.set_text(str(self.model.w_rank))
        b.priority_weight.set_text(str(self.model.w_priority))
        b.build_schedule.add_callback('activated', self.build_schedule_cb)

        fr.set_widget(w)
        vbox.add_widget(fr, stretch=0)

        fr = Widgets.Frame("Files")

        captions = (('Inputs:', 'label', 'Input dir', 'entry'),
                    ('Load Info', 'button'),
                    )
        w, b = Widgets.build_info(captions, orientation='vertical')
        self.w.update(b)

        b.load_info.add_callback('activated', self.initialize_model_cb)

        fr.set_widget(w)
        vbox.add_widget(fr, stretch=0)

        spacer = Widgets.Label('')
        vbox.add_widget(spacer, stretch=1)
        
        ## btns = Widgets.HBox()
        ## btns.set_spacing(3)

        ## btn = Widgets.Button("Close")
        ## #btn.add_callback('activated', lambda w: self.close())
        ## btns.add_widget(btn, stretch=0)
        ## btns.add_widget(Widgets.Label(''), stretch=1)
        ## vbox.add_widget(btns, stretch=0)

        self.sw = sw
        top_w = sw.get_widget()

        layout = QtGui.QVBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(4)
        container.setLayout(layout)

        layout.addWidget(top_w, stretch=1)

    def initialize_model_cb(self, widget):
        self.input_dir = self.w.input_dir.get_text().strip()

        try:
            # read schedule
            schedule_file = os.path.join(self.input_dir, "schedule.csv")
            if not os.path.exists(schedule_file):
                self.logger.error("File not readable: %s" % (schedule_file))
                return
            self.logger.info("reading schedule from %s" % (schedule_file))
            self.schedule = entity.ScheduleFile(schedule_file, self.logger)
            # Set the appropriate "schedule" attributes in the
            # QueueModel
            if 'scheduletab' not in self.view.plugins:
                self.view.load_plugin('scheduletab', 'ScheduleTab', 'ScheduleTab', 'report', 'Schedule')
            self.model.set_schedule(self.schedule)
            #self.model.set_schedule_info(self.schedule.schedule_info)

            # read proposals
            proposal_file = os.path.join(self.input_dir, "programs.csv")
            if not os.path.exists(proposal_file):
                self.logger.error("File not readable: %s" % (proposal_file))
                return
            self.logger.info("reading proposals from %s" % (proposal_file))
            self.programs = entity.ProgramsFile(proposal_file, self.logger)
            if 'programstab' not in self.view.plugins:
                self.view.load_plugin('programstab', 'ProgramsTab', 'ProgramsTab', 'report', 'Programs')
            self.model.set_programs(self.programs)
            #self.model.set_programs_info(self.programs.programs_info)

            # read observing blocks
            self.oblist = {}
            self.oblist_info = []
            for propname in self.programs.programs_info:
                obfile = os.path.join(self.input_dir, propname+".csv")
                if not os.path.exists(obfile):
                    self.logger.error("File not readable: %s" % (obfile))
                    continue
                self.logger.info("loading observing blocks from file %s" % obfile)
                self.oblist[propname] = entity.OBListFile(obfile, self.logger,
                                                          propname,
                                                          self.programs.programs_info)
                #self.oblist_info.extend(self.oblist[propname].obs_info)
            self.model.set_oblist(self.oblist)
            #self.model.set_oblist_info(self.oblist_info)

        except Exception as e:
            self.logger.error("Error initializing: %s" % (str(e)))

        # update the model
        #self.update_model()
        #self.logger.info("model initialized")

    def update_model(self):
        try:
            self.model.set_schedule_info(self.schedule.schedule_info)
            self.model.set_programs_info(self.programs.programs_info)

            # TODO: this maybe should be done in the Model
            #self.oblist = {}
            self.oblist_info = []
            for propname in self.programs.programs_info:
                self.oblist_info.extend(self.oblist[propname].obs_info)
            # TODO: only needed if we ADD or REMOVE programs
            #self.model.set_oblist(self.oblist)
            self.model.set_oblist_info(self.oblist_info)

        except Exception as e:
            self.logger.error("Error storing into model: %s" % (str(e)))

        self.logger.info("model initialized")

    def build_schedule_cb(self, widget):
        # validate and make changes to model from gui
        self.model.w_slew = float(self.w.slew_weight.get_text())
        self.model.w_delay = float(self.w.delay_weight.get_text())
        self.model.w_filterchange = float(self.w.filter_weight.get_text())
        self.model.w_rank = float(self.w.rank_weight.get_text())

        # update the model with any changes from GUI
        self.update_model()
        
        self.view.nongui_do(self.model.schedule_all)
        
#END
{'fraction_non_alphanumeric': 0.12095137592337041, 'fraction_numerical': 0.002597613442649566, 'mean_word_length': 2.1793548387096773, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3272034', 'n_tokens_mistral': 3802, 'n_tokens_neox': 3669, 'n_words': 689}