# -*- mode: python; -*- vim: set ft=python:

# Intended to be pointed by $PYTHONSTARTUP environment variable.
# Read before first prompt is displayed in interactive mode (REPL).

from __future__ import print_function


def __setup_rlcomp():
    import readline
    import rlcompleter
    readline.parse_and_bind('tab: complete')

def __setup_rlhist():
    import atexit
    import os
    import readline

    xdg_data_home = os.getenv('XDG_DATA_HOME',
            os.path.join(os.getenv('HOME'), '.local', 'share'))
    py_data_home = os.path.join(xdg_data_home, 'python')
    histfile = os.path.join(py_data_home, 'history')

    try:
        readline.read_history_file(histfile)
    except IOError:
        pass

    readline.set_history_length(1000)

    def write_history_file():
        if not os.path.isdir(py_data_home):
            os.mkdir(py_data_home)

        readline.write_history_file(histfile)

    atexit.register(write_history_file)


try:
    import readline
except ImportError:
    print("Cannot import `readline'")
else:
    __setup_rlcomp()
    __setup_rlhist()
    del readline


# datetime
import datetime
from datetime import timedelta  # noqa: F401


def now():
    return datetime.datetime.now()
