Created
March 14, 2022 08:42
-
-
Save dragonee/185fe533253f0d05d2a1e5d3587327eb to your computer and use it in GitHub Desktop.
ConfigParser example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Tasks] | |
url = https://api.example.org | |
user = someusername | |
password = somepassword |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from configparser import ConfigParser | |
from pathlib import Path | |
class TasksConfigFile: | |
url = None | |
user = None | |
password = None | |
def __init__(self): | |
self.reader = ConfigParser() | |
self.reader.read(self.paths()) | |
try: | |
self.url = self.reader['Tasks']['url'] | |
self.user = self.reader['Tasks']['user'] | |
self.password = self.reader['Tasks']['password'] | |
except KeyError: | |
raise KeyError("Create ~/.tasks-collector.ini file with section [Tasks] containing url/user/password") | |
def paths(self): | |
return [ | |
'/etc/tasks-collector.ini', | |
Path.home() / '.tasks-collector.ini', | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment