]> git.phdru.name Git - dotfiles.git/blobdiff - lib/python/init.py
LCD scripts: allow to increse/decrease brightness
[dotfiles.git] / lib / python / init.py
index f828226c9576264e0fcd931fe7f5056a809982e0..4bd52581da2d5f07ee33d8914854f869ffbb078b 100644 (file)
@@ -18,70 +18,69 @@ def init():
 
     # readline/pyreadline
 
-    pyreadlinew32_startup = os.path.join(
-        sys.prefix, 'lib', 'site-packages',
-        'pyreadline', 'configuration', 'startup.py')
+    # From Bruce Edge:
+    # https://mail.python.org/pipermail/python-list/2001-March/062888.html
 
-    if os.path.exists(pyreadlinew32_startup):
-        execfile(pyreadlinew32_startup)
-
-    else:
-        # From Bruce Edge:
-        # https://mail.python.org/pipermail/python-list/2001-March/062888.html
-
-        try:
-            import rlcompleter  # noqa: need for completion
-            import readline
-            initfile = os.environ.get('INPUTRC') \
-                or os.path.expanduser('~/.inputrc')
-            readline.read_init_file(initfile)
-
-            # if 'libedit' in readline.__doc__:
-            #     readline.parse_and_bind("bind ^I rl_complete")
-            # else:
-            #     readline.parse_and_bind("tab: complete")
-
-            histfiles = ['~/.python_history']
-            # if 'VIRTUAL_ENV' in os.environ:
-            #     histfiles.append('$VIRTUAL_ENV/.python_history')
-            for histfile in histfiles:
-                try:
-                    histfile = os.path.expandvars(histfile)
-                    histfile = os.path.expanduser(histfile)
-                    readline.read_history_file(histfile)
-                except IOError:
-                    pass  # No such file
-
-            def savehist():
-                histsize = os.environ.get('HISTSIZE')
-                if histsize:
-                    try:
-                        histsize = int(histsize)
-                    except ValueError:
-                        pass
-                    else:
-                        readline.set_history_length(histsize)
-                histfile = histfiles[-1]
+    try:
+        import rlcompleter  # noqa: need for completion
+        import readline
+        initfile = os.environ.get('INPUTRC') \
+            or os.path.expanduser('~/.inputrc')
+        readline.read_init_file(initfile)
+
+        # if 'libedit' in readline.__doc__:
+        #     readline.parse_and_bind("bind ^I rl_complete")
+        # else:
+        #     readline.parse_and_bind("tab: complete")
+
+        histfiles = ['~/.python_history']
+        # if 'VIRTUAL_ENV' in os.environ:
+        #     histfiles.append('$VIRTUAL_ENV/.python_history')
+        for histfile in histfiles:
+            try:
                 histfile = os.path.expandvars(histfile)
                 histfile = os.path.expanduser(histfile)
+                readline.read_history_file(histfile)
+            except IOError:
+                pass  # No such file
+
+        def savehist():
+            histsize = os.environ.get('HISTSIZE')
+            if histsize:
                 try:
-                    readline.write_history_file(histfile)
-                except IOError:
+                    histsize = int(histsize)
+                except ValueError:
                     pass
+                else:
+                    readline.set_history_length(histsize)
+            histfile = histfiles[-1]
+            histfile = os.path.expandvars(histfile)
+            histfile = os.path.expanduser(histfile)
+            try:
+                readline.write_history_file(histfile)
+            except IOError:
+                pass
+
+        import atexit
+        atexit.register(savehist)
 
+        def unset_history():
             import atexit
-            atexit.register(savehist)
+            atexit._exithandlers = []
+        builtins.unset_history = unset_history
 
-        except (ImportError, AttributeError):
-            # no readline or atexit, or readline doesn't have
-            # {read,write}_history_file - ignore the error
-            pass
+    except (ImportError, AttributeError):
+        # no readline or atexit, or readline doesn't have
+        # {read,write}_history_file - ignore the error
+        pass
 
     # terminal
 
     term = os.environ.get('TERM', '')
-    if 'linux' in term:
-        background = 'dark'
+    for _term in ['cygwin', 'linux', 'putty']:
+        if _term in term:
+            background = 'dark'
+            break
     else:
         background = os.environ.get('BACKGROUND', 'light').lower()
 
@@ -89,7 +88,8 @@ def init():
     # https://mail.python.org/pipermail/python-list/2001-March/112696.html
 
     _term_found = False
-    for _term in ['linux', 'rxvt', 'screen', 'term', 'vt100']:
+    for _term in ['cygwin', 'linux', 'putty', 'rxvt',
+                  'screen', 'term', 'vt100']:
         if _term in term:
             _term_found = True
             break
@@ -129,10 +129,7 @@ def init():
         def myinput(prompt=None):
             save_stdout = sys.stdout
             sys.stdout = sys.__stdout__
-            try:
-                result = builtin_input(prompt)
-            except EOFError:
-                result = None
+            result = builtin_input(prompt)
             sys.stdout = save_stdout
             return result
 
@@ -309,7 +306,7 @@ def init():
             return "Usage: cat('filename')"
 
         def __call__(self, filename):
-            fp = open(filename, 'rU')
+            fp = open(filename, 'r')
             text = fp.read()
             fp.close()
             print(text)