<commit_before># Configuration module

#
## tmp note: you should add logger module and modify this module
#

from ConfigParser import SafeConfigParser
import os


program_name = "migmig"
# config file short path (hard coded !)
cfg_short_path = "~/." + program_name + ".ini"
server_address = "localhost"
server_port = "50001"


class Configuration():

	def __init__(self, logger):
		self.logger = logger
		self.parser = SafeConfigParser()
		self.cfg_path = self.validate_path(cfg_short_path)

		if not self.cfg_path:
			# create config file and initate configurations
			self.cfg_path = os.path.expanduser(cfg_short_path)
			self.initate()

		self.parser.read(self.cfg_path)


	def initate(self):
		# Sections
		self.parser.add_section("Setting")
		self.parser.add_section("Client")

		# Options
		self.parser.set("Setting", "default_download_path", os.path.expanduser("~/Downloads/" + program_name))
		self.parser.set("Setting", "default_merge_path", os.path.expanduser("~/Downloads/" + program_name + "/merged"))
		self.parser.set("Setting", "max_connections", "6")
		self.parser.set("Setting", "number_of_tries", "3")
		self.parser.set("Setting", "verbose_level", "1")

		self.parser.set('Setting', 'server_address', server_address)
		self.parser.set('Setting', 'server_port', server_port)

		# write settings to ini file
		with open(self.cfg_path, "wb") as tmp:
				self.parser.write(tmp)



	def get(self, value):
		'''
			This method only returns values from "Client" section.
		'''
		try:
			return self.parser.get('Client', value)
		except:
			return None

	def set(self, name, value):
		'''
			Like get method, this only works for "Client" section.
		'''
		try:
			self.parser.set('Client', name, value)
			return True
		except:
			# log !
			return None

	def get_server(self):
		addr = self.parser.get('Setting','server_address')
		port = self.parser.get('Setting','server_port')
		return addr + ':' + port


	def default_download_path(self, new_path):
		new_path = self.validate_path(new_path)
		if not new_path:
			self.parser.set("Setting", "default_download_path", new_path)

	
	def validate_path(self, path):
		'''
			Check whether path exists or not
		'''
		path = os.path.expanduser(path)
		if os.path.exists(path):
			return path
		return None<commit_msg>Add constant variables. modify "write" method<commit_after># Configuration module

#
## tmp note: you should add logger module and modify this module
#

from ConfigParser import SafeConfigParser
import os


program_name = "migmig"
# config file short path (hard coded !)
cfg_short_path = "~/." + program_name + ".ini"
server_address = "localhost"
server_port = "50001"


class Configuration():

	def __init__(self, logger):
		# constant variables
		self.OK = 99
		self.BAD_URL = 98
		self.BLAHBLAH = 97


		self.logger = logger
		self.parser = SafeConfigParser()
		self.cfg_path = self.validate_path(cfg_short_path)

		if not self.cfg_path:
			# create config file and initate configurations
			self.cfg_path = os.path.expanduser(cfg_short_path)
			self.initate()

		self.parser.read(self.cfg_path)


	def initate(self):
		# Sections
		self.parser.add_section("Setting")
		self.parser.add_section("Client")

		# Options
		self.parser.set("Setting", "default_download_path", os.path.expanduser("~/Downloads/" + program_name))
		self.parser.set("Setting", "default_merge_path", os.path.expanduser("~/Downloads/" + program_name + "/merged"))
		self.parser.set("Setting", "max_connections", "6")
		self.parser.set("Setting", "number_of_tries", "3")
		self.parser.set("Setting", "verbose_level", "1")

		self.parser.set('Setting', 'server_address', server_address)
		self.parser.set('Setting', 'server_port', server_port)

		self.parser.set('Client', 'identifier', 'None')
		self.parser.set('Client', 'URL', 'None')
		self.parser.set('Client', 'client_id', 'None')

		# write settings to ini file
		self.write()



	def get(self, value):
		'''
			This method only returns values from "Client" section.
		'''
		try:
			return self.parser.get('Client', value)
		except:
			return None

	def set(self, **kwargs):
		'''
			Like get method, this only works for "Client" section.
		'''
		try:
			for key, value in kwargs.items():
				self.parser.set('Client', key, value)

			self.write()
			return True
		except:
			# log the python error. how?
			return False

	def write(self):
		'''
			Write (synchronize) the parser object to .ini file !
		'''
		# TO-DO : ...
		with open(self.cfg_path, "wb") as tmp:
				self.parser.write(tmp)

	def get_server(self):
		addr = self.parser.get('Setting','server_address')
		port = self.parser.get('Setting','server_port')
		return "http://" + addr + ':' + port
		


	def default_download_path(self, new_path):
		new_path = self.validate_path(new_path)
		if not new_path:
			self.parser.set("Setting", "default_download_path", new_path)

	
	def validate_path(self, path):
		'''
			Check whether path exists or not
		'''
		path = os.path.expanduser(path)
		if os.path.exists(path):
			return path
		return None
{'fraction_non_alphanumeric': 0.14177316293929712, 'fraction_numerical': 0.00439297124600639, 'mean_word_length': 4.169246646026831, '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': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1645557', 'n_tokens_mistral': 1867, 'n_tokens_neox': 1642, 'n_words': 399}