from __future__ import absolute_import
"""Hook to allow user-specified customization code to run.
This code was imported from Python 2.7 for use with CASAtools...
However, some programs or sites may find it convenient to allow users
to have a standard customization file, which gets run when a program
requests it. This module implements such a mechanism. A program
that wishes to use the mechanism must execute the statement
import ctuser
The ctuser module looks for a file .casa/toolrc.py in the user's home
directory and if it can be opened, execfile()s it in its own global
namespace. Errors during this phase are not caught; that's up to the
program that imports the ctuser module, if it wishes.
The user's .casa/toolrc.py could conceivably test for sys.version if it
wishes to do different things depending on the Python version.
"""
import os
import sys
from contextlib import contextmanager
home = os.curdir
if 'HOME' in os.environ:
home = os.environ['HOME']
elif os.name == 'posix':
home = os.path.expanduser("~/")
elif os.name == 'nt':
if 'HOMEPATH' in os.environ:
if 'HOMEDRIVE' in os.environ:
home = os.environ['HOMEDRIVE'] + os.environ['HOMEPATH']
else:
home = os.environ['HOMEPATH']
def _fileno(file_or_fd):
fd = getattr(file_or_fd, 'fileno', lambda: file_or_fd)()
if not isinstance(fd, int):
raise ValueError("Expected a file (`.fileno()`) or a file descriptor")
return fd
@contextmanager
def _stdout_redirected(to=os.devnull, stdout=None):
if stdout is None:
stdout = sys.stdout
stdout_fd = _fileno(stdout)
with os.fdopen(os.dup(stdout_fd), 'wb') as copied:
stdout.flush()
try:
os.dup2(_fileno(to), stdout_fd)
except ValueError:
with open(to, 'wb') as to_file:
os.dup2(to_file.fileno(), stdout_fd)
try:
yield stdout
finally:
stdout.flush()
os.dup2(copied.fileno(), stdout_fd)
@contextmanager
def _redirect_stdout(new_target):
old_target, sys.stdout = sys.stdout, new_target
try:
yield new_target
finally:
sys.stdout = old_target
def _merged_stderr_stdout():
return _stdout_redirected(to=sys.stdout, stdout=sys.stderr)
if len(sys.argv) > 0 and sys.argv[0] == '-m':
with _stdout_redirected(to=os.devnull), _merged_stderr_stdout():
configrc = os.path.join(home, ".casa/config.py")
try:
from casatoolrc import *
except:
try:
f = open(configrc)
except IOError:
pass
else: