Changeset 1d9b9cb


Ignore:
Timestamp:
Mar 14, 2012 5:27:11 PM (7 years ago)
Author:
Ted Faber <faber@…>
Branches:
gec13
Parents:
daeb329
Message:

Whoops. Forgot save. More documentation.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/policy/window.py

    re69367b r1d9b9cb  
    1717
    1818class window(gtk.Window):
    19 
     19    '''
     20    The main GUI class.  It presents the various TreeViews and menus to
     21    save/load/add, to add credentials, identities and actions and to change the
     22    policy translation variable.  It keeps its current size and location in the
     23    .abac_policy_tool.cfg file in the user's home.
     24    '''
     25
     26    # Definition of the menus
    2027    ui_def = '''
    2128    <ui>
     
    4451    </ui>
    4552    '''
     53    # Path to the configuration
    4654    cfg_path = os.path.join(os.path.expanduser('~'), '.abac_policy_tool.cfg')
    4755
    4856    @staticmethod
    4957    def wrapit(widget):
     58        '''
     59        Put widget into a ScrolledWindow with automatic scrollbars on both
     60        directions, and return the ScrolledWindow.
     61        '''
    5062        sw = gtk.ScrolledWindow()
    5163        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
     
    5466
    5567    def report_error(self, message):
     68        '''
     69        Put a MessageDialog up with the given message.  This is a member method
     70        so that it can be centered on the window.
     71        '''
    5672        md = gtk.MessageDialog(self, gtk.DIALOG_MODAL,
    5773                gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE,
     
    6177
    6278    def __init__(self, policy):
     79        '''
     80        Initialize all the GTK hooks for menus, put the various TreeViews up
     81        (connected to the policy) and read teh configuration for current
     82        position.
     83        '''
    6384        gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
    6485        self.policy = policy
     
    6990        self.pos = (0,0)
    7091        self.size = (500, 500)
     92        # These are the TreeViews that will change when policy changes
    7193        self.pages = [ ]
     94        # These are the TreeViews that change when policy translation changes
    7295        self.translated = [ ]
    7396
    7497        self.read_config()
    7598
     99        # Hook up thr TreeViews
    76100        nb = gtk.Notebook()
    77101        p = action_principal_tree(policy)
     
    105129        self.translated.append(p)
    106130
     131        # Make the Menus real
    107132        ui = gtk.UIManager()
    108133        ag = gtk.ActionGroup('action')
     
    121146            ('Translation', None, 'Translate Credentials'),
    122147            ))
     148        # load and append call the same method with different user data -
     149        # whether to clear current policy or not.
    123150        ag.add_actions((
    124151            ('FileLoad', gtk.STOCK_OPEN, None, None, None, self.load),
     
    137164        ui.add_ui_from_string(window.ui_def)
    138165
     166        # Put it all together and show it.
    139167        mb = ui.get_widget('ui/menubar')
    140168        vb = gtk.VBox()
     
    146174   
    147175    def quit(self, widget=None, data=None):
     176        '''
     177        Called from File->Quit in the menu.  Save location/size and exit
     178        '''
    148179        self.save_config()
    149180        gtk.main_quit()
    150181
    151182    def save(self, widget=None, data=None):
     183        '''
     184        Save the current state to a file.  Wrangle the FileChooserDialog to
     185        pick the file and write to it.
     186        '''
    152187        d = gtk.FileChooserDialog('Save file as', self,
    153188                gtk.FILE_CHOOSER_ACTION_SAVE, (
     
    161196        d.set_do_overwrite_confirmation(True)
    162197        d.add_filter(fil)
     198        # If the policy has been saved, start at that file.
    163199        if self.policy.filename is not None:
    164200            d.set_filename(self.policy.filename)
     201        # Display the chooser
    165202        rv = d.run()
    166203        d.hide()
     204        # Write if the user wants to do it.
    167205        if rv == gtk.RESPONSE_OK:
    168206            self.policy.write_zip(d.get_filename())
     
    170208
    171209    def new(self, widget=None, data=None):
     210        '''
     211        Called when the user asks for a blank policy.  Put up a confirmation
     212        and clear if confirmed.
     213        '''
    172214        d = gtk.MessageDialog(self, gtk.DIALOG_MODAL,
    173215                gtk.MESSAGE_ERROR, gtk.BUTTONS_OK_CANCEL,
     
    176218        if rv == gtk.RESPONSE_OK:
    177219            self.policy.clear()
     220            # After clearing, tell the TreeViews
    178221            for p in self.pages:
    179222                p.recalc()
     
    181224
    182225    def load(self, widget=None, data=None):
     226        '''
     227        Called to either load or append to the loaded policy.  data is the
     228        clearit parameter to the load call.  Other than that, just put up a
     229        requester and do the thing.
     230        '''
    183231        d = gtk.FileChooserDialog('Load file', self,
    184232                gtk.FILE_CHOOSER_ACTION_OPEN, (
     
    198246        if rv == gtk.RESPONSE_OK:
    199247            self.policy.read_zip(d.get_filename(), data)
     248            # Tell the TreeViews to update.
    200249            for p in self.pages:
    201250                p.recalc()
     
    203252
    204253    def add_principal(self, widget=None, data=None):
     254        '''
     255        Called to add a principal with a user-supplied mnemonic name to the
     256        current policy.  Do not allow duplicate mnemonic names.
     257        '''
    205258        while True:
     259            # Ask for a name
    206260            d = add_principal_dialog(self)
    207261            rv = d.run()
    208262            d.hide()
    209263            if rv == gtk.RESPONSE_OK:
     264                # Build the certificate
    210265                name = d.pname.get_text()
    211266                if name not in self.policy.principal_names():
     
    213268                        cid = Creddy.ID(name, 5 * 3600 * 24 * 365)
    214269                    except RuntimeError, e:
     270                        # Note this retries on failure
    215271                        self.report_error('Cannot create principal: %s' % e)
    216272                        continue
     273                    # Identity to policy
    217274                    self.policy.add_identity(cid)
     275                    # Update the trees
    218276                    for p in self.pages:
    219277                        p.recalc()
     278                    # clean up the dialog
    220279                    d.destroy()
    221280                    return
    222281                else:
     282                    # Duplicate, retry
    223283                    d.destroy()
    224284                    self.report_error('Cannot overwrite name: %s' % name)
    225285            else:
     286                # Cancel leave this function
    226287                d.destroy()
    227288                return
    228289
    229290    def add_cred(self, widget=None, data=None):
     291        '''
     292        Create a new credential and add it to the policy.  Most of the work is
     293        parsing the data out of teh add credential dialog which may be
     294        reporting a request for one of three kinds of credential.
     295        '''
     296        # Ask the user what they want
    230297        d = add_credential_dialog(self, self.policy)
    231298        rv = d.run()
    232299        d.hide()
    233300        if rv == gtk.RESPONSE_OK:
     301            # The name and issuer are always teh same
    234302            iname = d.issuer.get_active_text()
    235303            issuer = self.policy.issuers[self.policy.name_to_keyid(iname)]
     
    237305            cred = None
    238306            try:
     307                # Base credential
    239308                cred = Creddy.Attribute(issuer, role, 5 * 365 * 3600 * 24)
    240309            except RuntimeError, e:
     
    248317
    249318            mode = d.mechanism.get_active_text()
     319            # Principal is always in the results
    250320            p = d.subject_principal.get_text()
    251321            p = self.policy.name_to_keyid(p)
     322            # Collect the dependent components
    252323            if p is None:
    253324                self.report_error("Missing or invalid subject principal")
     
    266337                    d.destroy()
    267338                    return
    268 
     339            # Build that sucker
    269340            if mode == 'Direct assignment': cred.principal(p)
    270341            elif mode == 'Direct delegation': cred.role(p, r)
     
    272343
    273344            try:
     345                # Actual creation
    274346                cred.bake()
    275347            except:
     
    277349                d.destroy()
    278350                return
     351            # Add to policy
    279352            self.policy.add_credential(cred)
     353            # Update the TreeViews
    280354            for p in self.pages:
    281355                p.recalc()
     
    283357
    284358    def mark_action(self, widget=None, data=None):
     359        '''
     360        Mark an action. Collect the user's request and propagate it.
     361        '''
    285362        d = mark_action_dialog(self, self.policy)
    286363        rv = d.run()
     
    290367            if action is not None:
    291368                self.policy.add_action(action)
     369            # Tell the TreeViews
    292370            for p in self.pages:
    293371                p.recalc()
     
    296374
    297375    def shown(self, w):
     376        '''
     377        Handles an event where the window appears.  Move to the saved position
     378        and size.
     379        '''
    298380        self.move(*self.pos)
    299381        self.resize(*self.size)
    300382
    301383    def changed(self, w, e):
     384        '''
     385        Handles an event where the window changes (resizes or moves).  Remember
     386        the size and position.
     387        '''
    302388        self.pos = self.get_position()
    303389        self.size = self.get_size()
    304390
    305391    def translation_change(self, ra, c, user=None):
     392        '''
     393        Called from any of the menu items that request a translation change.
     394        They're radio buttons, so each one unsets the last.  Translate the
     395        radio value into the right policy string, set the translation value and
     396        update the TreeViews that depend on translation.
     397        '''
    306398        cv = c.get_current_value()
    307399        if cv == 0: self.policy.translate = 'sets'
     
    315407
    316408    def get_intpair(self, sect, opt):
     409        '''
     410        Utility to pull a pair of integers from a configuration file.  The size
     411        and position are thsi kind of data, so this is used a couple places.
     412        '''
    317413        if not self.cfg.has_section(sect):
    318414            self.cfg.add_section(sect)
     
    327423
    328424    def read_config(self):
     425        '''
     426        Get the saved size and position from the config file, if any
     427        '''
    329428        self.cfg = ConfigParser.SafeConfigParser()
    330429        self.cfg.read(window.cfg_path)
     
    335434
    336435    def save_config(self):
     436        '''
     437        Save the current postion to the default config file.
     438        '''
    337439        self.cfg.set('geom', 'pos', '%d,%d' % self.pos)
    338440        self.cfg.set('geom', 'size', '%d,%d' % self.size)
Note: See TracChangeset for help on using the changeset viewer.