<commit_before>import argparse
import os
import re
import sys

# ###### #
# Config #
# ###### #

# formats that can be downloaded from bandcamp
file_formats = ['.mp3', '.flac', '.aac', '.ogg', '.oga', '.m4a', '.CAF', '.wav', '.aiff', '.aif', '.aifc']


# ######### #
# Functions #
# ######### #


# Parses a filename from bandcamp and extracts the information about the artist, album, tracknumber
# and title
#
# @param song - filename (without the extension)
#
# @return object containing the infos
# 'artist', 'album', 'tracknumber', 'songname'
def parseInfoFromSong(song):
	# get the arist-, album- and songname
	artist, album, songname = str.split(song, ' - ')
	# seperate track number from track title
	tracknumber = songname[0:2]
	songname = songname[2:]

	# return object
	songinfo = {
		'artist': artist,
		'album': album,
		'songname': songname,
		'tracknumber': tracknumber
	}

	return songinfo

# --------------------------


# Takes the absolute path to a file and renames it according to the format given
#
# @param path   - absolute path to the songfile
# @param format - format that the file should be renamed to
# 
# @return void 
def renameSongFile(path, format):

	# split the path
	dir = os.path.dirname(path)
	filename, extension  = os.path.splitext(os.path.basename(path))

	# get the songinfo
	info = parseInfoFromSong(filename)

	# apply the songinfo to the format string
	new_songname = format

	# artist
	new_songname = str.replace(new_songname, "%a", info['artist'])
	# album
	new_songname = str.replace(new_songname, "%A", info['album'])
	# songname
	new_songname = str.replace(new_songname, "%t", info['songname'])
	# track number
	new_songname = str.replace(new_songname, "%n", info['tracknumber'])

	# rename the file
	os.rename(path, "%s/%s%s" % (dir, new_songname, extension))


# -------------------------------

# Takes the absolute path to a directory containing the song files
# If these files match one of the extensions specified in 'file_formats'
# these files are renamed
# 
# @param path - absolute path to the directory containing the files
# @param format - format that the file should be renamed to
#
# @return void
def renameDirectory(path, format):

	# list all the files in the directory
	list_of_files = os.listdir(path)

	# iterate through everything found
	for entry in list_of_files:

		# check if the extension is supported
		extension = os.path.splitext(entry)[1]
		if(str.lower(extension) in file_formats):
			# rename the file
			renameSongFile("%s/%s" % (path, entry), format)

# --------------------------------



# ################ #
# Argument Parsing #
# ################ #

formatHelp = """The Format to rename the file to. Format specifiers are: 
			
%%a - artist
%%A - album
%%t - songtitle
%%n - track number

"""

parser = argparse.ArgumentParser(description="Renames songs downloaded from bandcamp to a format you like", formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('path', help='Path to the directory containing the files or the file itself')
parser.add_argument('format', help=formatHelp)
arguments = parser.parse_args()


# ##################### #
# Argument verification #
# ##################### #

# get the absolute path
path = os.path.abspath(arguments.path)

# check if path is actually a path or a file
if(not os.path.isdir(path) and not os.path.isfile(path)):
	print "ERROR: '%s' is neither a file nor a directory" % path
	sys.exit()

# check if it is a full directory that has to be renamed
if(os.path.isdir(path)):
	renameDirectory(path, arguments.format)

# check if it's only one file
elif(os.path.isfile(path)):
	renameSongFile(path, arguments.format)<commit_msg>Added a recursive flag to recursively rename subdirectories as well. Handy if you downloaded the whole discography of an artist<commit_after>import argparse
import os
import re
import sys

# ###### #
# Config #
# ###### #

# formats that can be downloaded from bandcamp
file_formats = ['.mp3', '.flac', '.aac', '.ogg', '.oga', '.m4a', '.CAF', '.wav', '.aiff', '.aif', '.aifc']


# ######### #
# Functions #
# ######### #


# Parses a filename from bandcamp and extracts the information about the artist, album, tracknumber
# and title
#
# @param song - filename (without the extension)
#
# @return object containing the infos
# 'artist', 'album', 'tracknumber', 'songname'
def parseInfoFromSong(song):
	# get the arist-, album- and songname
	artist, album, songname = str.split(song, ' - ')
	# seperate track number from track title
	tracknumber = songname[0:2]
	songname = songname[2:]

	# return object
	songinfo = {
		'artist': artist,
		'album': album,
		'songname': songname,
		'tracknumber': tracknumber
	}

	return songinfo

# --------------------------


# Takes the absolute path to a file and renames it according to the format given
#
# @param path   - absolute path to the songfile
# @param format - format that the file should be renamed to
# 
# @return void 
def renameSongFile(path, format):

	# split the path
	dir = os.path.dirname(path)
	filename, extension  = os.path.splitext(os.path.basename(path))

	# get the songinfo
	info = parseInfoFromSong(filename)

	# apply the songinfo to the format string
	new_songname = format

	# artist
	new_songname = str.replace(new_songname, "%a", info['artist'])
	# album
	new_songname = str.replace(new_songname, "%A", info['album'])
	# songname
	new_songname = str.replace(new_songname, "%t", info['songname'])
	# track number
	new_songname = str.replace(new_songname, "%n", info['tracknumber'])

	# rename the file
	os.rename(path, "%s/%s%s" % (dir, new_songname, extension))


# -------------------------------

# Takes the absolute path to a directory containing the song files
# If these files match one of the extensions specified in 'file_formats'
# these files are renamed
# 
# @param path - absolute path to the directory containing the files
# @param format - format that the file should be renamed to
# @param recursive (optional) - if set, subdirectories are renamed as well in a recursive fashion
#
# @return void
def renameDirectory(path, format, recursive=False):


	# list all the files in the directory
	list_of_files = os.listdir(path)

	# iterate through everything found
	for item in list_of_files:

		# if it's a directory and the recursive flag was set handle it
		if(os.path.isdir(item) and recursive):
			# recursively handle that subdirectory
			renameDirectory("%s/%s" % (path, item), format, recursive=True)
		# single file
		else:
			# check if the extension is supported
			extension = os.path.splitext(item)[1]
			if(str.lower(extension) in file_formats):
				# rename the file
				renameSongFile("%s/%s" % (path, item), format)

# --------------------------------



# ################ #
# Argument Parsing #
# ################ #

formatHelp = """The Format to rename the file to. Format specifiers are: 
			
%%a - artist
%%A - album
%%t - songtitle
%%n - track number

"""

parser = argparse.ArgumentParser(description="Renames songs downloaded from bandcamp to a format you like", formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('path', help='Path to the directory containing the files or the file itself')
parser.add_argument('format', help=formatHelp)
parser.add_argument('-r', '--recursive', action='store_true', help="If set the program will go into sub directories and rename those files as well")
arguments = parser.parse_args()


# ##################### #
# Argument verification #
# ##################### #

# get the absolute path
path = os.path.abspath(arguments.path)

# check if path is actually a path or a file
if(not os.path.isdir(path) and not os.path.isfile(path)):
	print "ERROR: '%s' is neither a file nor a directory" % path
	sys.exit()

# check if it is a full directory that has to be renamed
if(os.path.isdir(path)):	
	renameDirectory(path, arguments.format, recursive=arguments.recursive)

# check if it's only one file
elif(os.path.isfile(path)):
	renameSongFile(path, arguments.format)
{'fraction_non_alphanumeric': 0.15911367050575864, 'fraction_numerical': 0.0015022533800701052, 'mean_word_length': 3.9806733167082293, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1011263', 'n_tokens_mistral': 2586, 'n_tokens_neox': 2426, 'n_words': 835}