def build_mapping(self, source_dir):
        """ Builds a text file for mapping from the structure of the file
            system.
            This relies on the path to each media file being of the format
            source_dir/(genre)/(show name)/S(X)/(video file).
            If it finds a a dir named 'S(X)' it adds a line to the mapping
            file.

            string -> None
            :param source_dir:
        """
        contents = os.listdir(source_dir)

        for item in contents:
            new_path = vou.get_new_path(source_dir, item)
            item_contents = os.listdir(new_path)
            for item2 in item_contents:
                self.tvsm[item2] = item

        mapping_file = "mapping.txt"
        new_file = open(mapping_file, "w")
        for key, value in self.tvsm.iteritems():
            line = key + " " + value + "\n"
            new_file.write(line)
        new_file.close()

        return mapping_file
def find_directory(media_file, dst):
    """ Recursively find the dir (dst) that the media file belongs in via
        torrent file naming conventions. i.e. TV.Show.S01E01.avi belongs in
        TV.Show/S01

        String, String -> String or None
        :param dst:
        :param media_file:
    """

    contents = os.listdir(dst)
    is_leaf = True

    # Iterate over the items in dst and create a new path with these items
    for item in contents:
        if not item.startswith('.'):
            new_path = vou.get_new_path(dst, item)

            # If the items are a dir recursively call find_directory on it
            if os.path.isdir(new_path):
                is_leaf = False
                rtn = find_directory(media_file, new_path)
                if rtn is not None:
                    return rtn

    # If the dir is a leaf dir and the both itself and its parent are substrings
    # of the media_file string then return the dir path
    if is_leaf:
        path_list = string.split(dst, '/')
        print path_list
        if (path_list[-1].lower() in media_file.lower()) and (path_list[-2].lower() in media_file.lower()):
            return dst

    return None
def move_from_orig(start_path):
    """ Moves media files from original torrent directories to a season dir and
        then deletes the original torrent dir.

        Note this relies on the media file containing a substring in its
        name of the form S(x)E(y) or s(x)e(y). Files with other naming formats
        will be left alone.

        string -> None
        :param start_path:
    """
    contents = os.listdir(start_path)
    # Find the dirs that are not season dirs.
    for item in contents:
        item_path = vou.get_new_path(start_path, item)
        if not vou.is_season_indicator(item) and os.path.isdir(item_path):
            item_contents = os.listdir(item_path)

            # Find the media files in the dirs.
            for item2 in item_contents:
                if vou.is_required_format(item2):
                    item2_path = vou.get_new_path(item_path, item2)

                    # Find the season the media file belongs to.
                    item2_list = string.split(item2, '.')
                    for i in item2_list:
                        if vou.is_season_indicator(i):
                            season = i[0:3]
                            season_path = vou.get_new_path(start_path, season)

                            # Make season dir if it doesn't exist.
                            if not os.path.isdir(season_path):
                                os.mkdir(season_path)

                            # Move media file by renaming.
                            new_item2_path = vou.get_new_path(season_path, item2)
                            os.rename(item2_path, new_item2_path)
                            break
                    break

    return None
def move_actual_media(start_path):
    """ Moves a media file directly in a title dir to a season dir.

        Note this relies on the media file containing a substring in its
        name of the form S(x)E(y) or s(x)e(y). Files with other naming formats
        will be left alone.

        string -> None
        :param start_path:
    """
    contents = os.listdir(start_path)

    for item in contents:

        # Find the media files in contents.
        if vou.is_required_format(item):
            item_path = vou.get_new_path(start_path, item)
            item_list = string.split(item, '.')

            # Determine the season this file belongs to.
            for i in item_list:
                if vou.is_season_indicator(i):
                    season = i[0:3]
                    season_path = vou.get_new_path(start_path, season)

                    # Makes season dir if it doesn't exist.
                    if not os.path.isdir(season_path):
                        os.mkdir(season_path)

                    # Move media file by renaming.
                    new_item_path = vou.get_new_path(season_path, item)
                    os.rename(item_path, new_item_path)

                    break
            break

    return None
def move_files(source_dir, video_dir, excluded_items):
    """ Iterates through the items in source, moves the media files and then
        deletes the parent folder if it has one.

        String -> None
        :param excluded_items:
        :param video_dir:
        :param source_dir:
    """
    source_dir_list = os.listdir(source_dir)

    # Iterate through items in the dir source_dir and get the item paths
    for item in source_dir_list:
        if item not in excluded_items:
            item_path = vou.get_new_path(source_dir, item)

            # If the item is a directory move any media_files in item
            if os.path.isdir(item_path):
                moved_file = True
                item_list = os.listdir(item_path)

                for item2 in item_list:
                    item2_path = vou.get_new_path(item_path, item2)
                    result = move_media_file(item2_path, video_dir)
                    moved_file = (moved_file and result)

                # If all media files are moved, delete the item. Note that
                # moved_file is updated via the 'and' operator,
                # move_media_file for more information on how result is updated.
                if moved_file:
                    shutil.rmtree(item_path)

            # If the item is not a dir attempt to move it
            else:
                move_media_file(item_path, video_dir)

    return None
def make_directory(media_file, video_dir):
    """ This function makes a directory and returns a string for its path.

        String -> String
        :param video_dir:
        :param media_file:
    """
    sys.stdout.write("Please enter the path in which you would like to place " + media_file[-1] + '\n' + video_dir)
    wanted_path = raw_input()

    input_list = string.split(wanted_path, '/')
    old_path = video_dir
    for item in input_list:
        new_path = vou.get_new_path(old_path, item)
        if not os.path.isdir(new_path):
            os.mkdir(new_path)
        old_path = new_path
    return old_path