<reponame>trufi-association/trufi-monitor-backend<filename>trufimonitor/configParser.py<gh_stars>0
"""
It converts Strings in the format

# to deactivate commands just comment them out by putting a # to the beginning of the line
# optional commands can be deactivated by putting a # to the lines' beginning and activated by removing # from the beginning
# replace 'example.com' with the hostname or ip address of the server the configuration structure 'servers/trufiwebsite' is for
host=example.com
# replace '22' with the port number the SSH server running on the server listens to and which the firewall allows to pass
port=22
# replace 'example' with the name of the UNIX server on the server to remote log in as
user=example
# optional but required when 'password' has been NOT set. Location to the ssh private key
private_key=./sshkey
# optional but required when 'private_key' has been set. Location to the ssh public key
public_key=./sshkey.pub
# optional but required when 'private_key' has been NOT set. The password to log into the server
password=<PASSWORD>(<PASSWORD>§\"

# if both 'private_key' and 'password' are provided then the key authentication will be tried first.
# It's a general advise not to use password authentication and to deactivate it on the server explicitly.
# If you really can't use key authentication then please use password authentication under the following conditions:
# - contains at least 20 characters
# - contains lower- and uppercase letters, symbols, underscores, minus, special characters like & % ( ) | < > / [ ] =
# Pro Tipp: Use characters from other alphabetic systems like the Japanese or Arabic one as long as you stick to the UTF8 codepage´


to a python dictionary

{
"host": "example.com",
"port": "22",
"user": "example",
"private_key": "./sshkey",
"public_key": "./sshkey.pub",
"password": "<PASSWORD>§\""
}
"""
class ConfigParser():
	def __init__(self, filepath, debug=False):
		self.filepath = filepath
		self.debug = debug
		self.config = {}
		
		sfile = open(filepath, "r")
		filebuffer = sfile.read()
		sfile.close()
		
		for line in filebuffer.split("\n"):
			if line.strip() == "":
				continue
			
			# 1. strip out the comment string starting with #
			commentStarts = line.find("#")
			if commentStarts == -1: commentStarts = len(line)-1
			commandString = line[0:commentStarts+1].strip()
			if commandString == "#": continue # means this is a comment line
			#print(commandString)
			
			# 2. parse the command
			commandString = commandString.split("=")
			
			# 3. map them to a dict (key-value pair consisting of all strings)
			self.config[commandString[0].strip()] = commandString[1].strip()

"""
It converts Strings in the format

# to deactivate commands just comment them out by putting a # to the beginning of the line
# optional commands can be deactivated by putting a # to the lines' beginning and activated by removing # from the beginning
# replace 'example.com' with the hostname or ip address of the server the configuration structure 'servers/trufiwebsite' is for
host=example.com
# replace '22' with the port number the SSH server running on the server listens to and which the firewall allows to pass
port=22
# replace 'example' with the name of the UNIX server on the server to remote log in as
user=example
# optional but required when 'password' has been NOT set. Location to the ssh private key
private_key=./sshkey
# optional but required when 'private_key' has been set. Location to the ssh public key
public_key=./sshkey.pub
# optional but required when 'private_key' has been NOT set. The password to log into the server
password=<PASSWORD>(<PASSWORD>§\"

# if both 'private_key' and 'password' are provided then the key authentication will be tried first.
# It's a general advise not to use password authentication and to deactivate it on the server explicitly.
# If you really can't use key authentication then please use password authentication under the following conditions:
# - contains at least 20 characters
# - contains lower- and uppercase letters, symbols, underscores, minus, special characters like & % ( ) | < > / [ ] =
# Pro Tipp: Use characters from other alphabetic systems like the Japanese or Arabic one as long as you stick to the UTF8 codepage´


to a python dictionary

{
"host": "example.com",
"port": "22",
"user": "example",
"private_key": "./sshkey",
"public_key": "./sshkey.pub",
"password": "<PASSWORD>§\""
}
"""
class ConfigParser():
	def __init__(self, filepath, debug=False):
		self.filepath = filepath
		self.debug = debug
		self.config = {}
		
		sfile = open(filepath, "r")
		filebuffer = sfile.read()
		sfile.close()
		
		for line in filebuffer.split("\n"):
			if line.strip() == "":
				continue
			
			# 1. strip out the comment string starting with #
			commentStarts = line.find("#")
			if commentStarts == -1: commentStarts = len(line)-1
			commandString = line[0:commentStarts+1].strip()
			if commandString == "#": continue # means this is a comment line
			#print(commandString)
			
			# 2. parse the command
			commandString = commandString.split("=")
			
			# 3. map them to a dict (key-value pair consisting of all strings)
			self.config[commandString[0].strip()] = commandString[1].strip()
It converts Strings in the format  # to deactivate commands just comment them out by putting a # to the beginning of the line # optional commands can be deactivated by putting a # to the lines' beginning and activated by removing # from the beginning # replace 'example.com' with the hostname or ip address of the server the configuration structure 'servers/trufiwebsite' is for host=example.com # replace '22' with the port number the SSH server running on the server listens to and which the firewall allows to pass port=22 # replace 'example' with the name of the UNIX server on the server to remote log in as user=example # optional but required when 'password' has been NOT set. Location to the ssh private key private_key=./sshkey # optional but required when 'private_key' has been set. Location to the ssh public key public_key=./sshkey.pub # optional but required when 'private_key' has been NOT set. The password to log into the server password=<PASSWORD>(<PASSWORD>§\"  # if both 'private_key' and 'password' are provided then the key authentication will be tried first. # It's a general advise not to use password authentication and to deactivate it on the server explicitly. # If you really can't use key authentication then please use password authentication under the following conditions: # - contains at least 20 characters # - contains lower- and uppercase letters, symbols, underscores, minus, special characters like & % ( ) | < > / [ ] = # Pro Tipp: Use characters from other alphabetic systems like the Japanese or Arabic one as long as you stick to the UTF8 codepage´   to a python dictionary  { "host": "example.com", "port": "22", "user": "example", "private_key": "./sshkey", "public_key": "./sshkey.pub", "password": "<PASSWORD>§\"" } # 1. strip out the comment string starting with # # means this is a comment line #print(commandString) # 2. parse the command # 3. map them to a dict (key-value pair consisting of all strings)