<commit_before>import sqlite3
from Song import Song
import os


# Class for dealing with songs in the database
# Fills Database with songs and select next song to play
class SongStore:
	def __init__(self, databasepath):
		self.databasepath = databasepath

	def initDatabase(self):
		if not self.databaseExists():
			conn = sqlite3.connect(self.databasepath)
			conn.text_factory = str
			c = conn.cursor()
			c.execute('''CREATE TABLE songs(key text PRIMARY KEY, value text, available integer)''')

			conn.commit()
			c.close()


	def deleteDatabase(self):
		import os
		try:
			os.remove(self.databasepath)
		except:
			pass

	def databaseExists(self):
		try:
			with open(self.databasepath) as f: pass
			return True
		except IOError as e:
			return False

	def addSongs(self, filenames):
		conn = sqlite3.connect(self.databasepath)
		conn.text_factory = str
		c = conn.cursor()

		for file in filenames:
			c.execute('Select count(key) from songs where key = ?', (file,))

			count = c.fetchone()[0]

			if count is 0:
				song = Song(file)
				c.execute('INSERT INTO songs VALUES (?,?,1)', (song.url, str(song.metadata)))
				conn.commit()

		c.close()

	def addSongsFromDirectory(self, dir):
		import utils
		files = utils.getMusicFromDirectory(dir)
		self.addSongs(files)


	#remove deleted files from database and add new ones. Also mark files not available due to unmounted voule, etc
	def update(self, directories):
		import utils
		for dir in directories:
			self.markDirectoryAvailability(dir)
			if os.path.exists(dir):
				filesFromDisk = set(utils.getMusicFromDirectory(dir))
				filesFromDb = set(self.getSongPathsInDirectory(dir))

				#all songs that are in the database, but not on disk
				filesToDelete = list(filesFromDb - filesFromDisk)

				self.removeSongs(filesToDelete)

				#new songs added to disk, but not in db
				filesToAdd = list(filesFromDisk - filesFromDb)

				self.addSongs(filesToAdd)<commit_msg>Updated add songs function to commit every 500 songs inserted<commit_after>import sqlite3
from Song import Song
import os


# Class for dealing with songs in the database
# Fills Database with songs and select next song to play
class SongStore:
	def __init__(self, databasepath):
		self.databasepath = databasepath

	def initDatabase(self):
		if not self.databaseExists():
			conn = sqlite3.connect(self.databasepath)
			conn.text_factory = str
			c = conn.cursor()
			c.execute('''CREATE TABLE songs(key text PRIMARY KEY, value text, available integer)''')

			conn.commit()
			c.close()


	def deleteDatabase(self):
		import os
		try:
			os.remove(self.databasepath)
		except:
			pass

	def databaseExists(self):
		try:
			with open(self.databasepath) as f: pass
			return True
		except IOError as e:
			return False

	def addSongs(self, filenames):
		conn = sqlite3.connect(self.databasepath)
		conn.text_factory = str
		c = conn.cursor()

		for i, file in enumerate(filenames):
			c.execute('Select count(key) from songs where key = ?', (file,))

			count = c.fetchone()[0]

			if count is 0:
				song = Song(file)
				c.execute('INSERT INTO songs VALUES (?,?,1)', (song.url, str(song.metadata)))
				if i % 500 == 0:
					conn.commit()

		conn.commit()
		c.close()

	def addSongsFromDirectory(self, dir):
		import utils
		files = utils.getMusicFromDirectory(dir)
		self.addSongs(files)


	#remove deleted files from database and add new ones. Also mark files not available due to unmounted voule, etc
	def update(self, directories):
		import utils
		for dir in directories:
			self.markDirectoryAvailability(dir)
			if os.path.exists(dir):
				filesFromDisk = set(utils.getMusicFromDirectory(dir))
				filesFromDb = set(self.getSongPathsInDirectory(dir))

				#all songs that are in the database, but not on disk
				filesToDelete = list(filesFromDb - filesFromDisk)

				self.removeSongs(filesToDelete)

				#new songs added to disk, but not in db
				filesToAdd = list(filesFromDisk - filesFromDb)

				self.addSongs(filesToAdd)
{'fraction_non_alphanumeric': 0.09722921914357682, 'fraction_numerical': 0.004785894206549119, 'mean_word_length': 3.866421568627451, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7075664', 'n_tokens_mistral': 1462, 'n_tokens_neox': 1211, 'n_words': 390}