-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.pythonrc
More file actions
129 lines (96 loc) · 3.9 KB
/
Copy path.pythonrc
File metadata and controls
129 lines (96 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# -*- coding: iso-8859-15 -*-
r"""~/.pythonrc.py PYTHONRC - Really Neat way to make the python shell fun and usable!
Copy this file to $HOME/.pythonrc.py
In windows, copy it to $HOMEDRIVE:$HOMEPATH\.pythonrc.py
This is called when interactive python interpreter is instructed to:
import user
You can force this to import everytime by setting an environment variable:
export PYTHONSTARTUP="$HOME/.pythonrc.py"
It makes command history persistent, allows TAB expansion, adds time
stamp.
"""
"""
Copyright (c) 2005 Christian H�ltje
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
class pythonrc:
"""PythonRC is a class that initializes a nice environment to run in."""
histfile = None
debug = None
def init(cls):
"""Initialize a nice environment to run in."""
import os
cls.debug = os.getenv("PYTHONRC_DEBUG")
if os.getenv("NOPYTHONRC"):
# Not loading the python environment
cls.dp("Skipping loading the pythonrc environment stuff")
return
try:
import readline
except ImportError:
readline = None
cls.readline = readline
if readline:
# Set the history file
cls.histfile = os.path.join(os.getenv("HOME", "."), ".python_history")
cls.dp("Using history file '%s'" % cls.histfile)
# Read the history file
if os.path.exists(cls.histfile):
readline.read_history_file(cls.histfile)
cls.dp("Using existing history information")
else:
cls.dp("Creating new history file")
# Set the number of history items to 3000
readline.set_history_length(3000)
cls.dp("History size set to 3000")
# Put a marker for the starting time.
readline.add_history("# starting %s" % cls.strtime())
cls.dp("Marked start of history file")
# Allow Tab Completion
import rlcompleter # noqa: F401 This sets up python specific completion
readline.parse_and_bind("tab: complete")
cls.dp("Started completion")
else:
cls.dp("No readline available")
# Set the prompts
import sys
sys.ps1 = "python%s> " % ".".join(map(str, sys.version_info[:2]))
sys.ps2 = " " * (len(sys.ps1) - 2) + "> "
cls.dp("Set prompts")
# The the exit function
import atexit
atexit.register(cls.exit)
cls.dp("Registered exit function")
init = classmethod(init)
def dp(cls, *msgs):
"""Debugging Print."""
if cls.debug:
for msg in msgs:
print("PYTHONRC: %s" % msg)
dp = classmethod(dp)
def strtime(cls):
"""Returns the time in a nicely formatted string."""
import time
format = "%A %I:%M %p, %B %d, %Y %Z"
return time.strftime(format)
strtime = classmethod(strtime)
def exit(cls):
"""Method to run when everything exits."""
cls.dp("Exiting...")
if cls.readline:
cls.readline.add_history("# leaving %s" % cls.strtime())
cls.readline.write_history_file(cls.histfile)
print("Bye now. Love Ya!")
exit = classmethod(exit)
if __name__ == "__main__":
pythonrc.init()