import logging


def extract_info(info_txt):

	"""
	Extract the content from the info.txt file that was unzipped from the raw .gt3x file

	Example:
	{'Battery_Voltage': '3,83', 'Acceleration_Scale': '256.0', 'Download_Date': '635672143370000000', 'Unexpected_Resets': '0', 'Stop_Date': '635664672000000000', 
	'Firmware': '1.3.0', 'Acceleration_Min': '-8.0', 'Subject_Name': '90046928', 'Last_Sample_Time': '635664672000000000', 'Sample_Rate': '100', 
	'Device_Type': 'wGT3XBT', 'TimeZone': '02:00:00', 'Acceleration_Max': '8.0', 'Serial_Number': 'MOS2C02150348', 'Start_Date': '635658624000000000', 
	'Board_Revision': '3'}

	Parameters
	----------
	info_txt : string
		location of the info.txt file on disk

	Returns
	---------
	info_data : dictionary
		dictionary of key, value pairs as extracted from the info file
	"""

	# create empty dictionary
	info_data = {}

	try:
		# open the file
		with open(info_txt, 'r') as f:
			# loop trough each of the lines
			for l in f.readlines():
				# strip away the new lines
				l = l.strip('\r\n')
				# split on semicolon + space (the timezone value has semicolons in it and not using the space would cause issues splitting it)
				key, value = l.split(': ')
				# add to dictionary and replace key values with space in key with underscore
				info_data[key.replace(' ', '_')] = value
	except Exception as e:
		logging.error('Error extracting data from info.txt file: {}'.format(e))	
		exit(1)
	
	# return dictionary
	return info_data