TableModel
==========

* :download:`Download example <PyObjCExample-TableModel.zip>`

Shows how to fill an ``NSTableView`` using Key-Value Coding.


.. rst-class:: tabber

Sources
-------

.. rst-class:: tabbertab

TableModelAppDelegate.py
........................

.. sourcecode:: python

    import pwd
    
    from Foundation import NSMutableArray, NSObject
    
    FIELDS = "name password uid gid class change expire gecos home_dir shell".split()
    
    
    def getPasswords():
        a = NSMutableArray()
        for pw in pwd.getpwall():
            a.append(
                {
                    "name": pw.pw_name,
                    "password": pw.pw_passwd,
                    "uid": pw.pw_uid,
                    "gid": pw.pw_gid,
                    "gecos": pw.pw_gecos,
                    "home_dir": pw.pw_dir,
                    "shell": pw.pw_shell,
                }
            )
    
        return a
    
    
    class TableModelAppDelegate(NSObject):
        def passwords(self):
            if not hasattr(self, "_cachedpasswords"):
                self._cachedpasswords = getPasswords()
            return self._cachedpasswords

.. rst-class:: tabbertab

main.py
.......

.. sourcecode:: python

    #
    #  __main__.py
    #  TableModel
    #
    #  Created by Bob Ippolito on Sun Apr 04 2004.
    #  Copyright (c) 2004 Bob Ippolito. All rights reserved.
    #
    
    # import classes required to start application
    import TableModelAppDelegate  # noqa: F401
    from PyObjCTools import AppHelper
    
    # start the event loop
    AppHelper.runEventLoop(argv=[])

.. rst-class:: tabbertab

setup.py
........

.. sourcecode:: python

    """
    Script for building the example, alternative to the Xcode project.
    
    Usage:
        python3 setup.py py2app
    """
    
    from setuptools import setup
    
    setup(
        name="TableModel",
        app=["main.py"],
        data_files=["English.lproj"],
        setup_requires=["py2app", "pyobjc-framework-Cocoa"],
    )

