ShellEnv.py :  » Development » PyObjC » trunk » pyobjc » pyobjc-framework-PreferencePanes » Examples » EnvironmentPrefs » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » PyObjC 
PyObjC » trunk » pyobjc » pyobjc framework PreferencePanes » Examples » EnvironmentPrefs » ShellEnv.py
"""
EnvironmentPane - PreferencePane for changing the global user environment

This PreferencePane provides an easy to use UI for changeing
~/.MacOSX/environment.plist. This plist is loaded by the loginwindow application
when the user logs in and is used to initialize the "shell" environment.

Note that these variables are also available outside of the Terminal, but not
when the user logs in using SSH.

TODO:
- Undo
"""
import objc
from Cocoa import *
from PreferencePanes import *
from PyObjCTools import AppHelper
import os

# Uncomment this during development, you'll get exception tracebacks when
# the Python code fails.
#objc.setVerbose(1)

# Location of the environment.plist
ENVPLIST="~/.MacOSX/environment.plist"

# Template for new keys
NEWTMPL=NSLocalizedString("New_Variable_%d", "")

class EnvironmentPane (NSPreferencePane):
    """
    The 'model/controller' for the "Shell Environment" preference pane
    """
    deleteButton = objc.IBOutlet()
    mainTable = objc.IBOutlet()

    #__slots__ = (
    #    'environ',  # The actual environment, as a NSMutableDictionary
    #    'keys',     # The list of keys, in the right order for the tableView
    #    'changed',  # True if we should save before exitting
    #)

    def initWithBundle_(self, bundle):
        # Our bundle has been loaded, initialize the instance variables.
        # We don't load the environment.plist yet, do that when we're
        # actually selected. That way we can easier pick up manual changes.

        self = super(EnvironmentPane, self).initWithBundle_(bundle)
        if self is None: return None

        self.keys = ()
        self.environ = None
        self.changed = False

        return self

    def mainViewDidLoad(self):
        self.deleteButton.setEnabled_(False)

    def didSelect(self):
        # We are the selected preference pane. Load the environment.plist.

        self.environ = NSMutableDictionary.dictionaryWithContentsOfFile_(
            os.path.expanduser(ENVPLIST))
        if self.environ is None:
            self.environ = NSMutableDictionary.dictionary()
        self.keys = list(self.environ.keys())
        self.keys.sort()
        self.changed = False
        self.mainTable.reloadData()

    def shouldUnselect(self):
        # The user wants to select another preference pane. If we have
        # unsaved changes ask if they should be saved right now.

        if self.changed:
            NSBeginAlertSheet(
                NSLocalizedString("Save changes?", ""),
                NSLocalizedString("Cancel", ""),
                NSLocalizedString("Don't Save", ""),
                NSLocalizedString("Save", ""),
                self.mainView().window(),
                self,
                None,
                "sheetDidDismiss:returnCode:contextInfo:",
                0,
                NSLocalizedString("There are unsaved changed, should these be saved?", ""))
            return NSUnselectLater
        return NSUnselectNow

    @AppHelper.endSheetMethod
    def sheetDidDismiss_returnCode_contextInfo_(self, sheet, code, info):
        # Sheet handler for saving unsaved changes.

        if code == NSAlertDefaultReturn: # 'Cancel'
            self.replyToShouldUnselect_(NSUnselectCancel)
            return

        elif code == NSAlertAlternateReturn: # 'Don't Save'
            pass

        elif code == NSAlertOtherReturn: # 'Save'
            r = self.saveEnvironment()
            if not r:
                self.runAlertSheet(
                    NSLocalizedString("Cannot save changes", ""),
                    NSLocalizedString("It was not possible to save your changes", ""))
                self.replyToShouldUnselect_(NSUnselectCancel)
                return

        self.keys = ()
        self.environ = None
        self.changed = False
        self.replyToShouldUnselect_(NSUnselectNow)

    def saveEnvironment(self):
        """
        Save the data to environment.plist
        """
        fname = os.path.expanduser(ENVPLIST)
        dname = os.path.dirname(fname)
        if not os.path.isdir(dname):
            try:
                os.mkdir(dname)
            except os.error, msg:
                return False

        if not self.environ.writeToFile_atomically_(fname, True):
            return False

        return True

    @objc.IBAction
    def deleteVariable_(self, sender):
        """
        Delete the selected variable
        """
        r = self.mainTable.selectedRow()

        envname = self.keys[r]
        del self.environ[envname]
        self.keys.remove(envname)
        self.mainTable.reloadData()
        self.changed = True

    @objc.IBAction
    def newVariable_(self, sender):
        """
        Add a new variable
        """
        i = 0
        name = NEWTMPL%(i,)
        while self.environ.has_key(name):
            i += 1
            name = NEWTMPL%(i,)
        self.environ[name] = NSLocalizedString("New Value", "")
        self.keys = list(self.environ.keys())
        self.keys.sort()
        self.mainTable.reloadData()
        self.changed = True

    def numberOfRowsInTableView_(self, aView):
        """ Return the number of environment variables """
        return len(self.keys)

    def tableView_objectValueForTableColumn_row_(self, aView, aCol, rowIndex):
        """ Get the name of value of an environment variable """
        name = aCol.identifier()
        envname = self.keys[rowIndex]

        if name == "name":
            return envname
        elif name == "value":
            return self.environ[envname]

    def tableView_setObjectValue_forTableColumn_row_(self,
            aView, value, aCol,rowIndex):
        """ Change the name or value of an environment variable """
        if self.environ is None:
            aView.reloadData()
            return

        name = aCol.identifier()
        envname = self.keys[rowIndex]

        if name == "name":
            if value != envname:
                if self.environ.has_key(value):
                    self.runAlertSheet(
                        NSLocalizedString("Name exists", ""),
                        NSLocalizedString("The name %s is already used", "")%(
                            value,))
                    aView.reloadData()
                    return

                val = self.environ[envname]
                del self.environ[envname]
                self.environ[value] = val
                self.keys = list(self.environ.keys())
                self.keys.sort()
                aView.reloadData()
                self.changed = True

        elif name == "value":
            val = self.environ[envname]
            if val != value:
                self.environ[envname] = value
                self.changed = True

    def tableViewSelectionDidChange_(self, notification):
        """ The delete button should only be active if a row is selected """
        if self.mainTable.numberOfSelectedRows() == 0:
            self.deleteButton.setEnabled_(False)
        else:
            self.deleteButton.setEnabled_(True)

    def runAlertSheet(self, title, message):
        """ Run an alertsheet without callbacks """
        NSBeginAlertSheet(title,
            NSLocalizedString("OK", ""), None, None,
            self.mainView().window(),
            self,
            None,
            None,
            0,
            message)

objc.removeAutoreleasePool()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.