import sys
import termios, tty
import datetime


class Utils(object):
    @classmethod
    def printraw(self,strng):
        sys.stdout.write(strng)
        sys.stdout.flush()

    ### WARNING: mnustring2dir PREPENDS A SLASH: BE AWARE!
    @classmethod
    def mnustring2dir(self, mnustring):
        ss=0
        ssmax = len(mnustring)
        dirr = ''
        while ss < ssmax:
            ltr = mnustring[ss]
            dirr += ('/' + ltr)
            ss += 1
        return(dirr)

    @classmethod
    def dsnag_multi(self, datadir, key):
        fname = datadir + '/' + key
        try:
            f=open(fname, 'r')
        except:
            print('FATAL ERROR, could not open {} for read, aborting!'.format(fname))
            sys.exit(1)
        try:
            rtrn = f.readlines()
        except:
            print('Warning: Bad filename: {}'.format(fname))
            print('Perhaps you have Vim opened in the directory. Close Vim.')
            time.sleep(3)
        else:
            f.close()
            ss=0
            for line in rtrn:
                line = line.strip()
                rtrn[ss] = line
                ss += 1
            if len(rtrn) == 1:
                rtrn = rtrn[0]
        return rtrn

    @classmethod
    def dsnag(self, datadir, key):
        fname = datadir + '/' + key
        try:
            f=open(fname, 'r')
        except:
            print('FATAL ERROR, could not open {} for read, aborting!'.format(fname))
            sys.exit(1)
        try:
            rtrn = f.readline()
        except:
            print('Warning: Bad filename: {}'.format(fname))
            print('Perhaps you have Vim opened in the directory. Close Vim.')
            time.sleep(3)
        else:
            rtrn = rtrn.strip()
        f.close()
        return rtrn

    @classmethod
    def pagefeed(self):
        print('\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n')
        #print('\n\n\n\n')

    @classmethod
    def getch_unix(self):
        fileno = sys.stdin.fileno()                   # Get file descriptor number
        cur_settings = termios.tcgetattr(fileno)      # Save old terminal settings
        try:
            tty.setraw(sys.stdin.fileno())            # Put stdin in raw mode
            ltr = sys.stdin.read(1)                   # Raw (not buffered line) read one char
        finally:
            termios.tcsetattr(fileno, termios.TCSADRAIN, cur_settings) #Restore original stdin settings
        return ltr

    @classmethod
    def yyyymmddhhmmss_now(self):
        noww = datetime.datetime.now()
        s='{:04}{:02}{:02}_{:02}{:02}{:02}{:06}'
        s=s.format(
                noww.year,
                noww.month,
                noww.day,
                noww.hour,
                noww.minute,
                noww.second,
                noww.microsecond
                )
        return s
 
    @classmethod
    def is_lc_single_char(self, ltr):
        if len(ltr) == 1 and ltr.islower():
            return True
        else:
            return False



    @classmethod
    def mkdir_sure(self, path):
        try:
            os.mkdir(path)
        except OSError as exc:
            if exc.errno == errno.EEXIST and os.path.isdir(path):
                pass
            else:
                raise


