#extends phd_site
#implements respond
#attr $Title = "init.py"
#attr $Copyright = 2003
#attr $First = "init.py.html"
#attr $Next = "pdbrc.html"
#attr $Last = "pdbrc.py.html"
#attr $alternates = (("Plain text version", "text/plain", "init.py.txt"),)
#def body_html
#raw
<pre>
<font color="#0000ff"># This is startup file for interactive python.
# It is not automatically loaded by python interpreter.
# To instruct the interpreter to load it insert the following commands
# into your .profile (use whatever syntax and initialization file
# is appropriate for your shell):
#
# PYTHONSTARTUP=$HOME/init.py # or where you have really put it
# export PYTHONSTARTUP
#
# Due to nested_scopes and pydoc.help(*args, **kwds) this file only works with
# Python 2.1 or higher.
#
# Text version <a href="init.py.txt">here</a>
#
# Generated by <a href="../vimrc.html">gvim</a> :runtime syntax/2html.vim
#
</font>
</pre>

<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffff; }
body { font-family: monospace; color: #000000; background-color: #ffffff; }
* { font-size: 1em; }
.pythonNumber { color: #a52a2a; }
.PreProc { color: #a020f0; }
.Identifier { color: #00ff00; }
.Statement { color: #000000; font-weight: bold; }
.Comment { color: #bebebe; }
.pythonExceptions { color: #8b0000; }
.pythonBuiltin { color: #0000ff; font-weight: bold; }
.Special { color: #6a5acd; }
-->
</style>
<!--

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment"># This is startup file for interactive python.</span>
<span class="Comment"># It is not automatically loaded by python interpreter.</span>
<span class="Comment"># To instruct the interpreter to load it insert the following commands</span>
<span class="Comment"># into your .profile (use whatever syntax and initialization file</span>
<span class="Comment"># is appropriate for your shell):</span>
<span class="Comment">#</span>
<span class="Comment"># PYTHONSTARTUP=$HOME/init.py  # or where you really put it</span>
<span class="Comment"># export PYTHONSTARTUP</span>


<span class="Statement">def</span> <span class="Identifier">init</span>():
    <span class="Statement">try</span>:
        <span class="PreProc">import</span> __builtin__ <span class="Statement">as</span> builtins
    <span class="Statement">except</span> <span class="pythonExceptions">ImportError</span>:
        <span class="PreProc">import</span> builtins
    <span class="PreProc">import</span> os
    <span class="PreProc">import</span> sys

    <span class="Comment"># readline/pyreadline</span>

    pyreadlinew32_startup = os.path.join(
        sys.prefix, 'lib', 'site-packages',
        'pyreadline', 'configuration', 'startup.py')

    <span class="Statement">if</span> os.path.exists(pyreadlinew32_startup):
        <span class="pythonBuiltin">execfile</span>(pyreadlinew32_startup)

    <span class="Statement">else</span>:
        <span class="Comment"># From Bruce Edge:</span>
        <span class="Comment"># <a href="https://mail.python.org/pipermail/python-list/2001-March/062888.html">https://mail.python.org/pipermail/python-list/2001-March/062888.html</a></span>

        <span class="Statement">try</span>:
            <span class="PreProc">import</span> rlcompleter  <span class="Comment"># noqa: need for completion</span>
            <span class="PreProc">import</span> readline
            initfile = os.environ.get('INPUTRC') <span class="Special">\</span>
                <span class="Statement">or</span> os.path.expanduser('~/.inputrc')
            readline.read_init_file(initfile)

            <span class="Comment"># if 'libedit' in readline.__doc__:</span>
            <span class="Comment">#     readline.parse_and_bind(&quot;bind ^I rl_complete&quot;)</span>
            <span class="Comment"># else:</span>
            <span class="Comment">#     readline.parse_and_bind(&quot;tab: complete&quot;)</span>

            histfiles = ['~/.python_history']
            <span class="Comment"># if 'VIRTUAL_ENV' in os.environ:</span>
            <span class="Comment">#     histfiles.append('$VIRTUAL_ENV/.python_history')</span>
            <span class="Statement">for</span> histfile <span class="Statement">in</span> histfiles:
                <span class="Statement">try</span>:
                    histfile = os.path.expandvars(histfile)
                    histfile = os.path.expanduser(histfile)
                    readline.read_history_file(histfile)
                <span class="Statement">except</span> <span class="pythonExceptions">IOError</span>:
                    <span class="Statement">pass</span>  <span class="Comment"># No such file</span>

            <span class="Statement">def</span> <span class="Identifier">savehist</span>():
                histsize = os.environ.get('HISTSIZE')
                <span class="Statement">if</span> histsize:
                    <span class="Statement">try</span>:
                        histsize = <span class="pythonBuiltin">int</span>(histsize)
                    <span class="Statement">except</span> <span class="pythonExceptions">ValueError</span>:
                        <span class="Statement">pass</span>
                    <span class="Statement">else</span>:
                        readline.set_history_length(histsize)
                histfile = histfiles[-<span class="pythonNumber">1</span>]
                histfile = os.path.expandvars(histfile)
                histfile = os.path.expanduser(histfile)
                <span class="Statement">try</span>:
                    readline.write_history_file(histfile)
                <span class="Statement">except</span> <span class="pythonExceptions">IOError</span>:
                    <span class="Statement">pass</span>

            <span class="PreProc">import</span> atexit
            atexit.register(savehist)

        <span class="Statement">except</span> (<span class="pythonExceptions">ImportError</span>, <span class="pythonExceptions">AttributeError</span>):
            <span class="Comment"># no readline or atexit, or readline doesn't have</span>
            <span class="Comment"># {read,write}_history_file - ignore the error</span>
            <span class="Statement">pass</span>

    <span class="Comment"># terminal</span>

    term = os.environ.get('TERM', '')
    <span class="Statement">for</span> _term <span class="Statement">in</span> ['cygwin', 'linux', 'putty']:
        <span class="Statement">if</span> _term <span class="Statement">in</span> term:
            background = 'dark'
            <span class="Statement">break</span>
    <span class="Statement">else</span>:
        background = os.environ.get('BACKGROUND', 'light').lower()

    <span class="Comment"># From Randall Hopper:</span>
    <span class="Comment"># <a href="https://mail.python.org/pipermail/python-list/2001-March/112696.html">https://mail.python.org/pipermail/python-list/2001-March/112696.html</a></span>

    _term_found = <span class="pythonBuiltin">False</span>
    <span class="Statement">for</span> _term <span class="Statement">in</span> ['cygwin', 'linux', 'putty', 'rxvt',
                  'screen', 'term', 'vt100']:
        <span class="Statement">if</span> _term <span class="Statement">in</span> term:
            _term_found = <span class="pythonBuiltin">True</span>
            <span class="Statement">break</span>

    <span class="Statement">if</span> _term_found:
        <span class="Statement">if</span> background == 'dark':
            ps1_color = '3'  <span class="Comment"># yellow</span>
            stdout_color = '7'  <span class="Comment"># bold white</span>
        <span class="Statement">else</span>:
            ps1_color = '4'  <span class="Comment"># blue</span>
            stdout_color = '0'  <span class="Comment"># bold black</span>

        sys.ps1 = '<span class="Special">\001\033</span>[3%sm<span class="Special">\002</span>&gt;&gt;&gt;<span class="Special">\001\033</span>[0m<span class="Special">\002</span> ' % ps1_color
        sys.ps2 = '<span class="Special">\001\033</span>[1;32m<span class="Special">\002</span>...<span class="Special">\001\033</span>[0m<span class="Special">\002</span> '  <span class="Comment"># bold green</span>

        <span class="Comment"># From Denis Otkidach</span>

        <span class="Statement">class</span> <span class="Identifier">ColoredFile</span>:
            <span class="Statement">def</span> <span class="Identifier">__init__</span>(self, fp, begin,
                         end='<span class="Special">\033</span>[0m'):  <span class="Comment"># reset all attributes</span>
                self.__fp = fp
                self.__begin = begin
                self.__end = end

            <span class="Statement">def</span> <span class="Identifier">write</span>(self, s):
                self.__fp.write(self.__begin+s+self.__end)

            <span class="Statement">def</span> <span class="Identifier">writelines</span>(self, lines):
                <span class="pythonBuiltin">map</span>(self.write, lines)

            <span class="Statement">def</span> <span class="Identifier">__getattr__</span>(self, attr):
                <span class="Statement">return</span> <span class="pythonBuiltin">getattr</span>(self.__fp, attr)

        sys.stdout = ColoredFile(sys.stdout, '<span class="Special">\033</span>[1;3%sm' % stdout_color)
        sys.stderr = ColoredFile(sys.stderr, '<span class="Special">\033</span>[31m')  <span class="Comment"># red</span>

        <span class="Statement">def</span> <span class="Identifier">myinput</span>(prompt=<span class="pythonBuiltin">None</span>):
            save_stdout = sys.stdout
            sys.stdout = sys.__stdout__
            result = builtin_input(prompt)
            sys.stdout = save_stdout
            <span class="Statement">return</span> result

        <span class="Statement">try</span>:
            builtins.raw_input
        <span class="Statement">except</span> <span class="pythonExceptions">AttributeError</span>:  <span class="Comment"># PY3</span>
            builtin_input = builtins.input
            builtins.input = myinput
        <span class="Statement">else</span>:
            builtin_input = builtins.raw_input
            builtins.raw_input = myinput

    <span class="Statement">try</span>:
        <span class="PreProc">import</span> locale
    <span class="Statement">except</span> <span class="pythonExceptions">ImportError</span>:
        <span class="Statement">pass</span>  <span class="Comment"># locale was not compiled</span>
    <span class="Statement">else</span>:
        <span class="Statement">try</span>:
            locale.setlocale(locale.LC_ALL, '')
        <span class="Statement">except</span> (<span class="pythonExceptions">ImportError</span>, locale.Error):
            <span class="Statement">pass</span>  <span class="Comment"># no locale support or unsupported locale</span>

    <span class="Comment"># set displayhook and excepthook</span>

    <span class="PreProc">from</span> pprint <span class="PreProc">import</span> pprint
    <span class="PreProc">from</span> traceback <span class="PreProc">import</span> format_exception, print_exc

    pager = os.environ.get(&quot;PAGER&quot;) <span class="Statement">or</span> 'more'

    <span class="Comment"># if your pager is 'less', options '-F' and '-R' must be passed to it,</span>
    <span class="Comment"># and option '-X' is very much recommended</span>
    <span class="Statement">if</span> pager == 'less':
        less = os.environ.get(&quot;LESS&quot;) <span class="Statement">or</span> ''
        <span class="Statement">for</span> opt <span class="Statement">in</span> 'X', 'R', 'F':
            <span class="Statement">if</span> opt <span class="Statement">not</span> <span class="Statement">in</span> less:
                less = opt + less
        os.environ[&quot;LESS&quot;] = less

    <span class="Statement">class</span> <span class="Identifier">BasePager</span>:
        <span class="Statement">def</span> <span class="Identifier">write</span>(self, value):
            self.stdout.write(value)

        <span class="Statement">if</span> _term_found:
            <span class="Statement">def</span> <span class="Identifier">pprint</span>(self, value):
                pprint(value,
                       stream=ColoredFile(self.stdout,
                                          '<span class="Special">\033</span>[1;3%sm' % stdout_color))

        <span class="Statement">def</span> <span class="Identifier">close</span>(self):
            self.stdout.close()

    <span class="Statement">try</span>:
        <span class="PreProc">from</span> subprocess <span class="PreProc">import</span> Popen, PIPE
    <span class="Statement">except</span> <span class="pythonExceptions">ImportError</span>:
        <span class="Statement">class</span> <span class="Identifier">Pager</span>(BasePager):
            <span class="Statement">def</span> <span class="Identifier">__init__</span>(self):
                self.stdout = os.popen(pager, 'w')
    <span class="Statement">else</span>:
        <span class="Statement">class</span> <span class="Identifier">Pager</span>(BasePager):
            <span class="Statement">def</span> <span class="Identifier">__init__</span>(self):
                self.pipe = Popen(pager, shell=<span class="pythonBuiltin">True</span>, stdin=PIPE,
                                  universal_newlines=<span class="pythonBuiltin">True</span>)
                self.stdout = self.pipe.stdin

            <span class="Statement">def</span> <span class="Identifier">close</span>(self):
                BasePager.close(self)
                self.pipe.wait()

    <span class="Statement">def</span> <span class="Identifier">displayhook</span>(value):
        <span class="Statement">if</span> value <span class="Statement">is</span> <span class="Statement">not</span> <span class="pythonBuiltin">None</span>:
            builtins._ = value
        pager = Pager()
        <span class="Statement">try</span>:
            pager.pprint(value)
        <span class="Statement">except</span>:  <span class="Comment"># noqa</span>
            <span class="Statement">if</span> _term_found:
                pager.stdout = ColoredFile(pager.stdout, '<span class="Special">\033</span>[31m')  <span class="Comment"># red</span>
            print_exc(<span class="pythonBuiltin">file</span>=pager)
        pager.close()

    sys.displayhook = displayhook

    <span class="Statement">def</span> <span class="Identifier">excepthook</span>(etype, evalue, etraceback):
        lines = format_exception(etype, evalue, etraceback)
        pager = Pager()
        <span class="Statement">if</span> _term_found:
            pager.stdout = ColoredFile(pager.stdout, '<span class="Special">\033</span>[31m')  <span class="Comment"># red</span>
        <span class="Statement">for</span> line <span class="Statement">in</span> lines:
            pager.write(line)
        pager.close()

    sys.excepthook = excepthook

    <span class="Comment"># try:</span>
    <span class="Comment">#     import cgitb</span>
    <span class="Comment"># except ImportError:</span>
    <span class="Comment">#     pass</span>
    <span class="Comment"># else:</span>
    <span class="Comment">#     # cgitb.enable() overrides sys.excepthook</span>
    <span class="Comment">#     cgitb.enable(format='text')</span>

    <span class="Comment"># From Thomas Heller:</span>
    <span class="Comment"># <a href="https://mail.python.org/pipermail/python-list/2001-April/099020.html">https://mail.python.org/pipermail/python-list/2001-April/099020.html</a></span>

    <span class="Comment"># import pdb</span>
    <span class="Comment">#</span>
    <span class="Comment"># def info(*args):</span>
    <span class="Comment">#    pdb.pm()</span>
    <span class="Comment"># sys.excepthook = info</span>

    <span class="Comment"># utilities</span>

    <span class="Comment"># From: Paul Magwene:</span>
    <span class="Comment"># <a href="https://mail.python.org/pipermail/python-list/2001-March/086191.html">https://mail.python.org/pipermail/python-list/2001-March/086191.html</a></span>
    <span class="Comment"># With a lot of my fixes:</span>

    <span class="Statement">class</span> <span class="Identifier">DirLister</span>:
        <span class="Statement">def</span> <span class="Identifier">__getitem__</span>(self, key):
            s = os.listdir(os.curdir)
            <span class="Statement">return</span> s[key]

        <span class="Statement">def</span> <span class="Identifier">__getslice__</span>(self, i, j):
            s = os.listdir(os.curdir)
            <span class="Statement">return</span> s[i:j]

        <span class="Statement">def</span> <span class="Identifier">__repr__</span>(self):
            <span class="Statement">return</span> <span class="pythonBuiltin">str</span>(os.listdir(os.curdir))

        <span class="Statement">def</span> <span class="Identifier">__call__</span>(self, path=<span class="pythonBuiltin">None</span>):
            <span class="Statement">if</span> path:
                path = os.path.expanduser(os.path.expandvars(path))
            <span class="Statement">else</span>:
                path = os.curdir
            <span class="Statement">return</span> os.listdir(path)

    <span class="Statement">class</span> <span class="Identifier">DirChanger</span>:
        <span class="Statement">def</span> <span class="Identifier">__repr__</span>(self):
            self()
            <span class="Statement">return</span> os.getcwd()

        <span class="Statement">def</span> <span class="Identifier">__call__</span>(self, path=<span class="pythonBuiltin">None</span>):
            path = os.path.expanduser(os.path.expandvars(path <span class="Statement">or</span> '~'))
            os.chdir(path)

    builtins.ls = DirLister()
    builtins.cd = DirChanger()

    <span class="Comment"># print working directory</span>

    <span class="Statement">class</span> <span class="Identifier">Pwd</span>:
        <span class="Statement">def</span> <span class="Identifier">__repr__</span>(self):
            <span class="Statement">return</span> os.getcwd()

        <span class="Statement">def</span> <span class="Identifier">__call__</span>(self):
            <span class="Statement">return</span> <span class="pythonBuiltin">repr</span>(self)

    builtins.pwd = Pwd()

    <span class="Comment"># exit REPL with 'exit', 'quit' or simple 'x'</span>

    <span class="Statement">class</span> <span class="Identifier">_Exit</span>:
        <span class="Statement">def</span> <span class="Identifier">__repr__</span>(self):
            sys.exit()

        <span class="Statement">def</span> <span class="Identifier">__call__</span>(self, msg=<span class="pythonBuiltin">None</span>):
            sys.exit(msg)

    builtins.x = _Exit()

    <span class="Comment"># print conten of a file</span>

    <span class="Statement">class</span> <span class="Identifier">_Cat</span>:
        <span class="Statement">def</span> <span class="Identifier">__repr__</span>(self):
            <span class="Statement">return</span> &quot;Usage: cat('filename')&quot;

        <span class="Statement">def</span> <span class="Identifier">__call__</span>(self, filename):
            fp = <span class="pythonBuiltin">open</span>(filename, 'rU')
            text = fp.read()
            fp.close()
            <span class="pythonBuiltin">print</span>(text)

    builtins.cat = _Cat()

    <span class="Comment"># call shell</span>

    <span class="Statement">class</span> <span class="Identifier">_Sh</span>:
        <span class="Statement">def</span> <span class="Identifier">__repr__</span>(self):
            os.system(os.environ[&quot;SHELL&quot;])
            <span class="Statement">return</span> ''

        <span class="Statement">def</span> <span class="Identifier">__call__</span>(self, cmdline):
            os.system(cmdline)

    builtins.sh = _Sh()

    <span class="Comment"># paginate a file</span>

    <span class="Statement">class</span> <span class="Identifier">_Pager</span>:
        <span class="Statement">def</span> <span class="Identifier">__repr__</span>(self):
            <span class="Statement">return</span> &quot;Usage: pager('filename')&quot;

        <span class="Statement">def</span> <span class="Identifier">__call__</span>(self, filename):
            os.system(&quot;%s '%s'&quot; % (pager, filename.replace(&quot;'&quot;, '&quot;<span class="Special">\'</span>&quot;')))

    builtins.pager = _Pager()

    <span class="Comment"># edit a file</span>

    <span class="Statement">class</span> <span class="Identifier">_Editor</span>:
        <span class="Statement">def</span> <span class="Identifier">__repr__</span>(self):
            <span class="Statement">return</span> &quot;Usage: edit('filename')&quot;

        <span class="Statement">def</span> <span class="Identifier">__call__</span>(self, filename):
            editor = os.environ.get(&quot;VISUAL&quot;) <span class="Special">\</span>
                <span class="Statement">or</span> os.environ.get(&quot;EDITOR&quot;) <span class="Statement">or</span> 'vi'
            os.system(&quot;%s '%s'&quot; % (editor, filename.replace(&quot;'&quot;, '&quot;<span class="Special">\'</span>&quot;')))

    builtins.edit = builtins.editor = _Editor()


init()
<span class="Statement">del</span> init
</pre>
<!-- vim: set foldmethod=manual : -->
#end raw
#end def
$phd_site.respond(self)
