<commit_before>#!/usr/bin/python
""" Python 2.x compliant (change <> to != for Python3)
    File organizer by type/time/name"""

import sys, string, os, time, glob
import shutil, filecmp

DEBUG_MODE = 0

def main(args):
    """Main program entry point"""

    arguments = check_args(args, len(args))

    if arguments == "INVALID":
        print(" Invalid arguments...\n  Use \'-h\' for help.")
        return

    if arguments == "HELP":
        print_help()
        return

    directory = args[3]

    if arguments == "VALID":
        filetypes = get_filetypes(args[2])
        classificate_dir(args[1], filetypes, directory)
        return


    if arguments == "GET_DATES":
        dates, mode = get_dates(args[1])
        classificate_dir(mode, filetypes, directory, dates)
        return

def change_dir(directory):
    """Changes current dir and returns it"""
    if directory <> ".":
        try:
            os.chdir(directory)
        except IOError:
            print("Cant access {0}...\n".format(directory))
    return os.getcwd()

def optimize(filetypes):
    """For current dir scan valid filetypes
        return a list of valid filetypes"""

    #Remove repeated, skip if "*"
    if filetypes == "*":
        return ['*']

    #Return elements not repeated
    result = []
    [result.append(x) for x in filetypes if x not in result]
    return result

def check_for_file(fname):
    """Checks if it's a valid file"""

    return not(os.path.islink(fname) or \
            os.path.isdir(fname) or \
            (fname == sys.argv[0]))


def get_dict_list(mode):
    """Generates a dictionary with a mode to valid keys to match"""

    letters = string.ascii_lowercase
    numbers_plus_letters = ''.join(string.digits + string.ascii_lowercase)

    mode_to_container = {"-n":letters, "-n+":numbers_plus_letters}
    return mode_to_container[mode]


def fix_repeated(file_a, file_b, directory):
    """Reacts on existing files, removing or saving with a different name """

    # If the files are the exact same remove it
    if filecmp.cmp(file_a, file_b):
        os.remove(file_a)
    else:
        # else we save rename and append -diff in route/'filename + diff'
        backup_filename = file_a + "-diff"
        fullroute = directory + backup_filename
        
        print("Saving as: {fullroute}\n".format(fullroute=fullroute))
        os.rename(file_a, backup_filename)
        shutil.move(backup_filename, fullroute)


def move_file(fname, directory):
    """Move a file into specified dir, and checks for existing files"""

    try:
        if DEBUG_MODE:
            print("Moving \'{origin}\' ----> \'{destiny}\'".format(origin=fname, destiny= directory + fname))
        shutil.move(fname, directory)

    except shutil.Error:
        fullpath = directory + fname
        print("{fullpath} already exists, skipping...\n".format(fullpath=fullpath))
        fix_repeated(fname, fullpath, directory)


def organize_by_symbols(orig_filetypes, directory):
    """Organize files starting with "(,),-,_" """

    current_dir = change_dir(directory)

    filetypes = optimize(orig_filetypes)

    files_processed = 0
    valid = "$()=-_"

    for char in valid:
        for filetype in filetypes:
            # For matched file in dir
            for fname in glob.glob(char + "*." + filetype):

                files_processed += 1

                # If folder for current letter doesn't exists
                if os.path.exists("#misc-symbols" + "/") == False :
                    os.mkdir("#misc-symbols")

                move_file(fname,"#misc-symbols" + "/")

    if current_dir <> directory:
        change_dir("..")

    return files_processed


def organize_by_name(orig_filetypes, mode, directory):
    """Organize by name mode (-n and -n+)"""

    current_dir = change_dir(directory)

    filetypes = optimize(orig_filetypes)
    files_processed = 0
    
    # For every chosen filetype
    for filetype in filetypes:
       
        # For every file on dir
        match = ''.join("[A-Za-z0-9]*" + "." + filetype)
        
        for fname in glob.iglob(match):
            files_processed += 1

            # If folder for current letter doesn't exists
            dir_letter = fname[0].upper()
            directory = dir_letter + "/"
            
            if os.path.exists(directory) == False:
                os.mkdir(dir_letter)

            move_file(fname, directory)

    if current_dir <> directory:
        change_dir("..")

    return files_processed


#def organize_by_date(mode, filetypes, directory, dates):
#    """Organize by date PENDING"""
#    return 0"""


def organize_by_filetype(orig_filetypes, directory):
    """Organize by filetype mode (-f)"""

    filetypes = optimize(orig_filetypes)

    files_processed = 0

    current_dir = change_dir(directory)

    for ftype in filetypes:
        for fname in glob.glob("*." + ftype):

            files_processed += 1
            
            #Get filetype from filename
            filetype = fname.partition(".")[2]

            # If folder for current file filetype doesn't exists
            dirname = filetype + "/" 
            if os.path.exists(dirname) == False :
                os.mkdir(filetype)
            move_file(fname, dirname)


        #for end
    # for end
    if current_dir <> directory:
        os.chdir("..")

    return files_processed


def classificate_dir(mode, filetypes, directory, dates=0):
    """Calls the corresponding function to sort files in selected mode"""

    files_classified = 0
    init_time = time.ctime()

    if mode == "-n" or mode == "-n+":
        files_classified = organize_by_name(filetypes, mode, directory)

        if mode == "-n+":
            files_classified += organize_by_symbols(filetypes, directory)

    elif mode == "-tf" or mode == "-te": # time frame and time exact date modes
        #files_classified = organize_by_date(mode, filetypes, directory, dates)
        pass

    elif mode == "-f":
        files_classified = organize_by_filetype(filetypes, directory)

    if files_classified == 0:
        print("No files were organized. Ending...")
    else:
        if(files_classified > 1):
            file_str = "files"
        else:
            file_str = "file"
        
        entries = {
                    'quantity': files_classified, 'files': file_str,
                    'start': init_time, 'end': time.ctime()
                  }
        #I really have to improve this
        message = """\n {0} {1} were organized.\n\n\
                Started at:\n\t{2}\n  \
                Ended at:\n\t{3}\n""".format(files_classified, file_str, init_time, time.ctime()) #I wish passing 'entries' would be possible :'(
        print(message)


def print_help():
    """Print help string"""

    indent = "\t  "

    #Improve this calamity xD
    help_string = """nforg.py - A file organizer in python -\n\
            Arguments: nforg.py <mode> <filetypes> <dir>\n\
            Example: nforg.py -n \"pdf,png,txt\" .\n\n\
    Organizing mode:\n\t\t-n: by name\n\t\t-n+: by name (including numbers and symbols)\
            \n\t\t-t: by time\n\t\t-f: by filetype\n\n    Using \"-t\" time mode:\n\
    \n%sLooking for jpg files with the two specified dates, moving them to\n%sfolders with that specified date.\
    \n\n\t\tnforg.py -t \"12/05/2010+5/06/2012\" \"jpg\" .\
    \n\n%sLooking for ugly .doc files in a time frame and moving into\n%sfiles with the file day/month/year.\
    \n\n\t\tnforg.py -t \"12/04/2008 8/06/2009\" \"doc\" .\n\
    """

    print(help_string % (indent, indent, indent, indent))


def check_args(argv, args):
    """Checks and validates arguments"""

    global DEBUG_MODE
    valid_args = ("-n", "-n+", "-t", "-f", "-h")

    if args == 1 or argv[1] not in valid_args:
        return "INVALID"

    #argv[1] is valid then check for debug mode
    if "d" in argv[1]:
        DEBUG_MODE = 1

    if(args < 4 or argv[1] == "-h"):
        return "HELP"

    if(argv[1] == "-t"):
        return "GET_DATES"

    return "VALID"


def get_filetypes(arg):
    """Parses filetypes from third argument"""

    if arg == "*":
        return "*"

    else:
        filetypes = arg.split(",")
        return filetypes

    return "ERROR"


def get_dates(line):
    """Gets date and mode"""

    dates = []

    if "+" in line:
        dates = line.split("+")
        mode = "-te" #Exact date mode

    elif " " in line:
        dates = line.split(" ")
        mode = "-tf" #Time frame date mode

    return (dates, mode)


if __name__ == "__main__":
    main(sys.argv)


<commit_msg>Refactored nforg.py<commit_after>#!/usr/bin/python
""" Python 2.x compliant (change <> to != for Python3)
    File organizer by type/time/name"""

import sys, string, os, time, glob
import shutil, filecmp

DEBUG_MODE = 0

def main(args):
    """Main program entry point"""

    arguments = check_args(args, len(args))

    if arguments == "INVALID":
        print(" Invalid arguments...\n  Use \'-h\' for help.")
        return

    if arguments == "HELP":
        print_help()
        return

    directory = args[3]

    if arguments == "VALID":
        filetypes = get_filetypes(args[2])
        classificate_dir(args[1], filetypes, directory)
        return


    if arguments == "GET_DATES":
        dates, mode = get_dates(args[1])
        classificate_dir(mode, filetypes, directory, dates)
        return

def change_dir(directory):
    """Changes current dir and returns it"""
    if directory <> ".":
        try:
            os.chdir(directory)
        except IOError:
            print("Cant access {0}...\n".format(directory))
    return os.getcwd()

def optimize(filetypes):
    """For current dir scan valid filetypes
        return a list of valid filetypes"""

    #Remove repeated, skip if "*"
    if filetypes == "*":
        return ['*']

    #Return elements not repeated
    result = []
    [result.append(x) for x in filetypes if x not in result]
    return result

def check_for_file(fname):
    """Checks if it's a valid file"""

    return not(os.path.islink(fname) or \
            os.path.isdir(fname) or \
            (fname == sys.argv[0]))


def fix_repeated(file_a, file_b, directory):
    """Reacts on existing files, removing or saving with a different name """

    # If the files are the exact same remove it
    if filecmp.cmp(file_a, file_b):
        os.remove(file_a)
    else:
        # else we save rename and append -diff in route/'filename + diff'
        backup_filename = file_a + "-diff"
        fullroute = directory + backup_filename
        
        print("Saving as: {fullroute}\n".format(fullroute=fullroute))
        os.rename(file_a, backup_filename)
        shutil.move(backup_filename, fullroute)


def move_file(fname, directory):
    """Move a file into specified dir, and checks for existing files"""

    try:
        if DEBUG_MODE:
            print("Moving \'{origin}\' ----> \'{destiny}\'".format(origin=fname, destiny= directory + fname))
        shutil.move(fname, directory)

    except shutil.Error:
        fullpath = directory + fname
        print("{fullpath} already exists, skipping...\n".format(fullpath=fullpath))
        fix_repeated(fname, fullpath, directory)


def organize_by_symbols(orig_filetypes, directory):
    """Organize files starting with "(,),-,_" """

    current_dir = change_dir(directory)

    filetypes = optimize(orig_filetypes)

    files_processed = 0
    valid = "$()=-_"

    for char in valid:
        for filetype in filetypes:
            # For matched file in dir
            for fname in glob.glob(char + "*." + filetype):

                files_processed += 1

                # If folder for current letter doesn't exists
                if os.path.exists("#misc-symbols" + "/") == False :
                    os.mkdir("#misc-symbols")

                move_file(fname,"#misc-symbols" + "/")

    if current_dir <> directory:
        change_dir("..")

    return files_processed


def organize_by_name(orig_filetypes, mode, directory):
    """Organize by name mode (-n and -n+)"""

    current_dir = change_dir(directory)

    filetypes = optimize(orig_filetypes)
    files_processed = 0
    
    # For every chosen filetype
    for filetype in filetypes:
       
        # For every file on dir
        match = ''.join("[A-Za-z0-9]*" + "." + filetype)
        
        for fname in glob.iglob(match):
            files_processed += 1

            # If folder for current letter doesn't exists
            dir_letter = fname[0].upper()
            directory = dir_letter + "/"
            
            if os.path.exists(directory) == False:
                os.mkdir(dir_letter)

            move_file(fname, directory)

    if current_dir <> directory:
        change_dir("..")

    return files_processed


#def organize_by_date(mode, filetypes, directory, dates):
#    """Organize by date PENDING"""
#    return 0"""


def organize_by_filetype(orig_filetypes, directory):
    """Organize by filetype mode (-f)"""

    filetypes = optimize(orig_filetypes)

    files_processed = 0

    current_dir = change_dir(directory)

    for ftype in filetypes:
        for fname in glob.glob("*." + ftype):

            files_processed += 1
            
            #Get filetype from filename
            filetype = fname.partition(".")[2]

            # If folder for current file filetype doesn't exists
            dirname = filetype + "/" 
            if os.path.exists(dirname) == False :
                os.mkdir(filetype)
            move_file(fname, dirname)


        #for end
    # for end
    if current_dir <> directory:
        os.chdir("..")

    return files_processed


def classificate_dir(mode, filetypes, directory, dates=0):
    """Calls the corresponding function to sort files in selected mode"""

    files_classified = 0
    init_time = time.ctime()

    if mode == "-n" or mode == "-n+":
        files_classified = organize_by_name(filetypes, mode, directory)

        if mode == "-n+":
            files_classified += organize_by_symbols(filetypes, directory)

    elif mode == "-tf" or mode == "-te": # time frame and time exact date modes
        #files_classified = organize_by_date(mode, filetypes, directory, dates)
        pass

    elif mode == "-f":
        files_classified = organize_by_filetype(filetypes, directory)

    if files_classified == 0:
        print("No files were organized. Ending...")
    else:
        if(files_classified > 1):
            file_str = "files"
        else:
            file_str = "file"
        
        entries = {
                    'quantity': files_classified, 'files': file_str,
                    'start': init_time, 'end': time.ctime()
                  }
        #I really have to improve this
        message = """\n {0} {1} were organized.\n\n\
                Started at:\n\t{2}\n  \
                Ended at:\n\t{3}\n""".format(files_classified, file_str, init_time, time.ctime()) #I wish passing 'entries' would be possible :'(
        print(message)


def print_help():
    """Print help string"""

    indent = "\t  "

    #Improve this calamity xD
    help_string = """nforg.py - A file organizer in python -\n\
            Arguments: nforg.py <mode> <filetypes> <dir>\n\
            Example: nforg.py -n \"pdf,png,txt\" .\n\n\
    Organizing mode:\n\t\t-n: by name\n\t\t-n+: by name (including numbers and symbols)\
            \n\t\t-t: by time\n\t\t-f: by filetype\n\n    Using \"-t\" time mode:\n\
    \n%sLooking for jpg files with the two specified dates, moving them to\n%sfolders with that specified date.\
    \n\n\t\tnforg.py -t \"12/05/2010+5/06/2012\" \"jpg\" .\
    \n\n%sLooking for ugly .doc files in a time frame and moving into\n%sfiles with the file day/month/year.\
    \n\n\t\tnforg.py -t \"12/04/2008 8/06/2009\" \"doc\" .\n\
    """

    print(help_string % (indent, indent, indent, indent))


def check_args(argv, args):
    """Checks and validates arguments"""

    global DEBUG_MODE
    valid_args = ("-n", "-n+", "-t", "-f", "-h")

    if args == 1 or argv[1] not in valid_args:
        return "INVALID"

    #argv[1] is valid then check for debug mode
    if "d" in argv[1]:
        DEBUG_MODE = 1

    if(args < 4 or argv[1] == "-h"):
        return "HELP"

    if(argv[1] == "-t"):
        return "GET_DATES"

    return "VALID"


def get_filetypes(arg):
    """Parses filetypes from third argument"""

    if arg == "*":
        return "*"

    else:
        filetypes = arg.split(",")
        return filetypes

    return "ERROR"


def get_dates(line):
    """Gets date and mode"""

    dates = []

    if "+" in line:
        dates = line.split("+")
        mode = "-te" #Exact date mode

    elif " " in line:
        dates = line.split(" ")
        mode = "-tf" #Time frame date mode

    return (dates, mode)


if __name__ == "__main__":
    main(sys.argv)



{'fraction_non_alphanumeric': 0.12929245003261966, 'fraction_numerical': 0.007828717157938437, 'mean_word_length': 2.2154843630816172, 'pattern_counts': {'":': 26, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5083085', 'n_tokens_mistral': 5453, 'n_tokens_neox': 5080, 'n_words': 1472}