'''
Utility functions.

Update time: 2016-03-24 11:11:46.
'''
import os
import re



def deu(text):
    if isinstance(text,str):
        return text.decode('utf8','replace')
    else:
        return text

def enu(text):
    if isinstance(text,unicode):
        return text.encode('utf8','replace')
    else:
        return text



#-------------------Read in text file and store data-------------------
def readFile(abpath_in,verbose=True):
    '''Read in text file and store data

    <abpath_in>: str, absolute path to input txt.
    '''

    if not os.path.exists(abpath_in):
        raise Exception("\n# <readFile>: Input file not found.")

    if verbose:
        print('\n# <readFile>: Open input file:')
        print(abpath_in)
        print('\n# <readFile>: Reading lines...')
        
    lines=[]

    with open(abpath_in, 'r') as fin:
        for line in fin:
            lines.append(deu(line))
    lines=u''.join(lines)

    if verbose:
        print('# <readFile>: Got all data.')

    return lines




def autoRename(abpath):
    '''Auto rename a file to avoid overwriting an existing file

    <abpath>: str, absolute path to a folder or a file to rename.
    
    Return <newname>: str, new file path.
    If no conflict found, return <abpath>;
    If conflict with existing file, return renamed file path,
    by appending "_(n)".
    E.g. 
        n1='~/codes/tools/send2ever.py'
        n2='~/codes/tools/send2ever_(4).py'
    will be renamed to
        n1='~/codes/tools/send2ever_(1).py'
        n2='~/codes/tools/send2ever_(5).py'
    '''

    def rename_sub(match):
        base=match.group(1)
        delim=match.group(2)
        num=int(match.group(3))
        return '%s%s(%d)' %(base,delim,num+1)

    if not os.path.exists(abpath):
        return abpath

    folder,filename=os.path.split(abpath)
    basename,ext=os.path.splitext(filename)
    # match filename
    rename_re=re.compile('''
            ^(.+?)       # File name
            ([- _])      # delimiter between file name and number
            \\((\\d+)\\) # number in ()
            $''',\
            re.X)
    if rename_re.match(basename):
        newname=rename_re.sub(rename_sub,basename)
        newname='%s%s' %(newname,ext)
    else:
        newname='%s_(1)%s' %(basename,ext)

    newname=os.path.join(folder,newname)
    return newname



#---------------Save result to file---------------
def saveFile(abpath_out,text,overwrite=True,verbose=True):

    if os.path.isfile(abpath_out):
        if overwrite:
            os.remove(abpath_out)
        else:
            abpath_out=autoRename(abpath_out)

    if verbose:
        print('\n# <saveFile>: Saving result to:')
        print(abpath_out)

    with open(abpath_out, mode='a') as fout:
        fout.write(enu(text))

    return
        

        

@@ -530,24 +530,61 @@
 t.%0A    '''%0A%0A
+    abpath_in=expandUser(abpath_in)%0A%0A
     if not o
@@ -609,16 +609,16 @@
 th_in):%0A
-
         
@@ -1238,109 +1238,8 @@
 str,
- new file path.%0A    If no conflict found, return %3Cabpath%3E;%0A    If conflict with existing file, return
  ren
@@ -1253,20 +1253,16 @@
 le path,
-%0A   
  by appe
@@ -1485,24 +1485,94 @@
 y'%0A    '''%0A%0A
+    if not os.path.exists(abpath):%0A        return expandUser(abpath)%0A%0A
     def rena
@@ -1726,66 +1726,8 @@
 1)%0A%0A
-    if not os.path.exists(abpath):%0A        return abpath%0A%0A
     
@@ -2252,24 +2252,57 @@
 er,newname)%0A
+    newname=expandUser(newname)%0A%0A
     return n
@@ -2422,16 +2422,54 @@
 True):%0A%0A
+    abpath_out=expandUser(abpath_out)%0A
     if o
@@ -2803,16 +2803,16 @@
       %0A%0A
-
         
@@ -2804,16 +2804,378 @@
      %0A%0A        %0A
+%0A#------------------Expand user home %22~%22 in file names------------------%0Adef expandUser(path,verbose=True):%0A    '''Expand user home %22~%22 in file names%0A%0A    '''%0A    if type(path) is list or type(path) is tuple:%0A        filelist=%5Bos.path.expanduser(ii) for ii in path%5D%0A        return filelist%0A%0A    elif type(path) is str:%0A        return os.path.expanduser(path)%0A%0A%0A%0A

add expandUser()