Skip to content

Instantly share code, notes, and snippets.

@dragonee
Created March 14, 2022 08:42
Show Gist options
  • Save dragonee/185fe533253f0d05d2a1e5d3587327eb to your computer and use it in GitHub Desktop.
Save dragonee/185fe533253f0d05d2a1e5d3587327eb to your computer and use it in GitHub Desktop.
ConfigParser example
[Tasks]
url = https://api.example.org
user = someusername
password = somepassword
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