Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config parser #6

Merged
12 commits merged into from
Oct 17, 2023
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,7 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

.vscode
.DS_Store
.DS_Store

6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
},
"python.formatting.provider": "none"
}
45 changes: 45 additions & 0 deletions documentation/example.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[DEFAULT]
test=val

[Network]
interface=eth0
subnet=10.0.0.0/24
netmask=255.255.255.0
ip=10.0.0.1
dhcp-ranges=10.0.0.100-10.0.0.254
dhcp-lease=12h
no-dhcp=false
nat-requests=true
nat-interface=eth1
ftp_directory=/ftpboot
tftp_directory=/tftpboot
ftp_port=20
tftp_port=69
gateways=10.0.0.50
dns-servers=1.1.1.1
no-dns=false #

[OS]
os=debian
version=12

[UEFI]
uefi=false

[Users]
root-login=true
root-password=<encrypted string>

username=alice
password=<encrypted string>
ssh-key=<contents of id_rsa.pub>
sudoer=true

[Applications]

ssh=
sql=
mongodb=

[DNSMasq Overrides]
authoritative=false
33 changes: 33 additions & 0 deletions documentation/example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
Network:
interface: "eth0"
subnet: "10.0.0.0/24"
netmask: "255.255.255.0"
ip: "10.0.0.1"
dhcp-ranges: "10.0.0.100-10.0.0.254"
dhcp-lease: "12h"
no-dhcp: false
nat-requests: true
nat-interface: "eth1"
ftp_directory: "/ftpboot"
tftp_directory: "/tftpboot"
ftp_port: "20"
tftp_port: "69"
gateways: "10.0.0.50"
dns-servers: "1.1.1.1"
no-dns: false
OS:
os: "debian"
version: 12
UEFI:
uefi: false
Users:
root-login: true
root-password: "<encrypted string>"
username: "alice"
password: "<encrypted string>"
ssh-key: "<contents of id_rsa.pub>"
sudoer: true
Applications:
DNSMasq Overrides:
authoritative: false
56 changes: 56 additions & 0 deletions genisys/yaml-parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import yaml # Look into using ruamel.yaml for using YAML 1.2 specification
from typing import Self

class YAMLParser:
def __init__(self, fileName) -> None:
self.fileName = fileName
# end __init__

''' Returns all of the key value pairs under a specific heading as a Python dictionary '''
def getSection(self: Self, heading) -> dict:
dictionary = {}
with open(self.fileName) as file:
data = yaml.safe_load(file)

if heading in data:
sectionData = data[heading]

try:
for key, value in sectionData.items():
dictionary[key] = value
except AttributeError:
print("Heading is empty.")

else:
raise Exception('Heading not found.')

return dictionary
# end getSection

''' Returns a list object containing all section headings in the provided YAML file '''
def getAllHeadings(self: Self) -> list:
headingList = []
with open(self.fileName) as file:
data = yaml.safe_load(file)

for heading in data:
headingList.append(heading)

return headingList
# end getAllHeadings

def printDict(self: Self, dictionary) -> None:
for key in dictionary:
print(f"{key}: {dictionary[key]}")
# end printDict

# end YAMLParser

def main():
parser = YAMLParser('example.yml')
print(parser.getAllHeadings())
for eachSection in parser.getAllHeadings():
parser.printDict(parser.getSection(eachSection))

if __name__ == '__main__':
main()