import subprocess
import signal 
import os
import confparse
import readline

homePath = os.path.expanduser("~")

def signal_handler(signal, frame):
    return True

def get_history_items():
    return [ readline.get_history_item(i)
             for i in xrange(1, readline.get_current_history_length() + 1)
             ]

class HistoryCompleter(object):
    
    def __init__(self):
        self.matches = []
        return

    def complete(self, text, state):
        response = None
        if state == 0:
            history_values = get_history_items()
            logging.debug('history: %s', history_values)
            if text:
                self.matches = sorted(h 
                                      for h in history_values 
                                      if h and h.startswith(text))
            else:
                self.matches = []
            logging.debug('matches: %s', self.matches)
        try:
            response = self.matches[state]
        except IndexError:
            response = None
        logging.debug('complete(%s, %s) => %s', 
                      repr(text), state, repr(response))
        return response

def menu(PS1, histFile):
    histFilePath = ""
    histFile = histFile.replace("~", os.path.expanduser("~"))
    
    try:
        if not os.path.exists(histFile):
            os.system("touch {}".format(histFile))
            readline.read_history_file(histFile)
            histFilePath = histFile
        else:
            readline.read_history_file(histFile)
            histFilePath = histFile
    except FileNotFoundError:
        os.system("touch ~/.dsh_history")
        readline.read_history_file(os.path.expanduser("~/.dsh_history"))
        histFilePath = os.path.expanduser("~/.dsh_history")
        
    stdinShell = input(PS1)
    stdinShell = stdinShell.replace("~", os.path.expanduser("~"))
    if stdinShell == "exit":
        readline.write_history_file(histFilePath)
        exit()
    
    if stdinShell == "clear":
        subprocess.call("clear")
        return True
        
    if stdinShell == "help":
        print("Dumb Shell: the dumbest shell you'll find on the net. Period.")
        return True
    
    commandList = stdinShell.split(' ')
    
    argsString = ""
    
    firstIteration = True
    for i in commandList:
        if len(commandList) == 1:
            argsString = "no arguments given"
            break
        if i == commandList[0]:
            pass
        else:
            if firstIteration == True:
                argsString = i
                firstIteration = False
            else:
                argsString = argsString + ", " + i
                
    if(len(commandList) == 1 and commandList[0] == ""):
        pass
    else:
        try:
            if commandList[0] == "cd":
                try:
                    os.chdir(commandList[1])
                    return True
                except IndexError:
                    os.chdir(homePath)
                    return True
            spawnedProc = subprocess.Popen(commandList, shell=False); spawnedProc.wait()
        except KeyboardInterrupt:
            spawnedProc.kill()
        except FileNotFoundError:
            print("Error --> {}: no such file or directory".format(commandList[0]))
    readline.write_history_file(histFilePath)
    
def main():
    signal.signal(signal.SIGINT, signal_handler)
    readline.parse_and_bind('tab: complete')
    readline.parse_and_bind('set editing-mode vi')
    readline.set_completer(HistoryCompleter().complete)
    while(True):
        PS1 = confparse.getPS1()
        histFile = confparse.getHistFile()
        menu(PS1, histFile)

if __name__ == "__main__":
    main()

@@ -556,65 +556,8 @@
 s()%0A
-            logging.debug('history: %25s', history_values)%0A
     
@@ -800,63 +800,8 @@
  %5B%5D%0A
-            logging.debug('matches: %25s', self.matches)%0A
     
@@ -919,168 +919,134 @@
     
-logging.debug('complete(%25s, %25s) =%3E %25s', %0A                      repr(text), state, repr(response))%0A        return response%0A%0Adef menu(PS1, histFile):
+return response%0A%0Adef histFilePath():%0A    histFile = confparse.getHistFile()%0A    # if history file is specified
 %0A    
+if 
 histFile
 Path
@@ -1045,18 +1045,26 @@
 File
-Path = %22%22%0A
+ is not None:%0A    
     
@@ -1124,22 +1124,99 @@
 ~%22))
+.replace(%22./%22, os.getcwd() + %22/%22).replace(%22%5Cn%22, %22%22)
 %0A    
-%0A
     
-try:
+print(os.path.exists(histFile))
 %0A   
@@ -1361,38 +1361,30 @@
             
-histFilePath =
+return
  histFile%0A  
@@ -1452,32 +1452,39 @@
             
+return 
 histFile
 Path = histF
@@ -1475,54 +1475,121 @@
 File
-Path = histFile%0A    except FileNotFoundError:%0A
+%0A    else:%0A        # file not specified%0A        if not os.path.exists(os.path.expanduser(%22~./dsh_history%22)):%0A    
     
@@ -1618,32 +1618,36 @@
 /.dsh_history%22)%0A
+    
         readline
@@ -1711,24 +1711,28 @@
         
+    
 histFile
 Path = o
@@ -1723,20 +1723,16 @@
 histFile
-Path
  = os.pa
@@ -1767,24 +1767,253 @@
 y%22)%0A        
+    return histFile%0A        else:%0A            readline.read_history_file(os.path.expanduser(%22~/.dsh_history%22))%0A            histFile = os.path.expanduser(%22~/.dsh_history%22)%0A            return histFile%0A    %0A%0Adef menu(PS1, histFile):
 %0A    stdinSh
@@ -2164,28 +2164,24 @@
 ile(histFile
-Path
 )%0A        ex
@@ -3540,20 +3540,16 @@
 histFile
-Path
 )%0A    %0Ad
@@ -3760,16 +3760,46 @@
 mplete)%0A
+    histFile = histFilePath()%0A
     whil
@@ -3844,51 +3844,8 @@
 1()%0A
-        histFile = confparse.getHistFile()%0A
     

correct handling of the history file, now its writing will be performed only ad the end of the execution of the shell little code refactoring