git-interface: Factor out configuration file parsing

Add a new module that automatically locates the configuration file and
provides methods to obtain the values of configuration options.

Use the new module instead of ConfigParser everywhere.

Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org>
This commit is contained in:
Lukas Fleischer 2016-08-03 20:21:40 +02:00
parent 2915abb9d3
commit 2f5f5583be
5 changed files with 32 additions and 18 deletions

27
git-interface/config.py Normal file
View file

@ -0,0 +1,27 @@
import configparser
import os
_parser = None
def _get_parser():
global _parser
if not _parser:
_parser = configparser.RawConfigParser()
path = os.path.dirname(os.path.realpath(__file__)) + "/../conf/config"
_parser.read(path)
return _parser
def get(section, option):
return _get_parser().get(section, option)
def getboolean(section, option):
return _get_parser().getboolean(section, option)
def getint(section, option):
return _get_parser().getint(section, option)

View file

@ -1,15 +1,12 @@
import configparser
import mysql.connector import mysql.connector
import os
import config
class Connection: class Connection:
_conn = None _conn = None
def __init__(self): def __init__(self):
config = configparser.RawConfigParser()
config.read(os.path.dirname(os.path.realpath(__file__)) + "/../conf/config")
aur_db_host = config.get('database', 'host') aur_db_host = config.get('database', 'host')
aur_db_name = config.get('database', 'name') aur_db_name = config.get('database', 'name')
aur_db_user = config.get('database', 'user') aur_db_user = config.get('database', 'user')

View file

@ -1,11 +1,10 @@
#!/usr/bin/python3 #!/usr/bin/python3
import configparser
import shlex import shlex
import os
import re import re
import sys import sys
import config
import db import db
@ -24,9 +23,6 @@ def format_command(env_vars, command, ssh_opts, ssh_key):
return msg return msg
config = configparser.RawConfigParser()
config.read(os.path.dirname(os.path.realpath(__file__)) + "/../conf/config")
valid_keytypes = config.get('auth', 'valid-keytypes').split() valid_keytypes = config.get('auth', 'valid-keytypes').split()
username_regex = config.get('auth', 'username-regex') username_regex = config.get('auth', 'username-regex')
git_serve_cmd = config.get('auth', 'git-serve-cmd') git_serve_cmd = config.get('auth', 'git-serve-cmd')

View file

@ -1,16 +1,13 @@
#!/usr/bin/python3 #!/usr/bin/python3
import configparser
import os import os
import re import re
import shlex import shlex
import sys import sys
import config
import db import db
config = configparser.RawConfigParser()
config.read(os.path.dirname(os.path.realpath(__file__)) + "/../conf/config")
repo_path = config.get('serve', 'repo-path') repo_path = config.get('serve', 'repo-path')
repo_regex = config.get('serve', 'repo-regex') repo_regex = config.get('serve', 'repo-regex')
git_shell_cmd = config.get('serve', 'git-shell-cmd') git_shell_cmd = config.get('serve', 'git-shell-cmd')

View file

@ -1,6 +1,5 @@
#!/usr/bin/python3 #!/usr/bin/python3
import configparser
import os import os
import pygit2 import pygit2
import re import re
@ -10,11 +9,9 @@ import sys
import srcinfo.parse import srcinfo.parse
import srcinfo.utils import srcinfo.utils
import config
import db import db
config = configparser.RawConfigParser()
config.read(os.path.dirname(os.path.realpath(__file__)) + "/../conf/config")
notify_cmd = config.get('notifications', 'notify-cmd') notify_cmd = config.get('notifications', 'notify-cmd')
repo_path = config.get('serve', 'repo-path') repo_path = config.get('serve', 'repo-path')