Change config file to YAML

To support commentlinks, palettes, custom dashboards and other
potential future enhancements, we'll need a config file that can
express complex data structures.  Change from configparser to yaml,
adding dependencies on pyyaml and voluptuous for validation.

Change-Id: I670eed0cee1b15aa35d0fa9aea0996603ae08444
This commit is contained in:
James E. Blair 2014-05-17 17:56:06 -07:00
parent 97d99d12f4
commit 06ef6e54c2
3 changed files with 60 additions and 43 deletions

View File

@ -25,13 +25,14 @@ As compared to the web interface, the main advantages are:
Usage Usage
----- -----
Create a file at ``~/.gerttyrc`` with the following contents:: Create a file at ``~/.gertty.yaml`` with the following contents::
[gerrit] servers:
url=https://review.example.org/ - name: gerrit
username=<gerrit username> url: https://review.example.org/
password=<gerrit password> username: <gerrit username>
git_root=~/git/ password: <gerrit password>
git_root: ~/git/
You can generate or retrieve your Gerrit password by navigating to You can generate or retrieve your Gerrit password by navigating to
Settings, then HTTP Password. Set ``git_root`` to a directory where Settings, then HTTP Password. Set ``git_root`` to a directory where
@ -39,7 +40,7 @@ Gertty should find or clone git repositories for your projects.
If your Gerrit uses a self-signed certificate, you can add:: If your Gerrit uses a self-signed certificate, you can add::
verify_ssl=False verify_ssl: False
To the section. To the section.

View File

@ -14,63 +14,78 @@
import getpass import getpass
import os import os
import ConfigParser import yaml
import voluptuous as v
DEFAULT_CONFIG_PATH='~/.gerttyrc' DEFAULT_CONFIG_PATH='~/.gertty.yaml'
class ConfigSchema(object):
server = {v.Required('name'): str,
v.Required('url'): str,
v.Required('username'): str,
'password': str,
'verify_ssl': bool,
'dburi': str,
v.Required('git_root'): str,
'log_file': str,
}
servers = [server]
def getSchema(self, data):
schema = v.Schema({v.Required('servers'): self.servers})
return schema
class Config(object): class Config(object):
def __init__(self, server=None, path=DEFAULT_CONFIG_PATH): def __init__(self, server=None, path=DEFAULT_CONFIG_PATH):
self.path = os.path.expanduser(path) self.path = os.path.expanduser(path)
self.config = ConfigParser.RawConfigParser()
try: if not os.path.exists(self.path):
with open(self.path, 'r') as f: self.printSample()
self.config.readfp(f, filename=f.name)
except IOError:
self.print_sample()
exit(1) exit(1)
if server is None: self.config = yaml.load(open(self.path))
server = self.config.sections()[0] schema = ConfigSchema().getSchema(self.config)
schema(self.config)
server = self.getServer(server)
self.server = server self.server = server
url = self.config.get(server, 'url') url = server['url']
if not url.endswith('/'): if not url.endswith('/'):
url += '/' url += '/'
self.url = url self.url = url
self.username = self.config.get(server, 'username') self.username = server['username']
if not self.config.has_option(server, 'password'): self.password = server.get('password')
if self.password is None:
self.password = getpass.getpass("Password for %s (%s): " self.password = getpass.getpass("Password for %s (%s): "
% (self.url, self.username)) % (self.url, self.username))
else: self.verify_ssl = server.get('verify_ssl', True)
self.password = self.config.get(server, 'password')
if self.config.has_option(server, 'verify_ssl'):
self.verify_ssl = self.config.getboolean(server, 'verify_ssl')
else:
self.verify_ssl = True
if not self.verify_ssl: if not self.verify_ssl:
os.environ['GIT_SSL_NO_VERIFY']='true' os.environ['GIT_SSL_NO_VERIFY']='true'
self.git_root = os.path.expanduser(self.config.get(server, 'git_root')) self.git_root = os.path.expanduser(server['git_root'])
if self.config.has_option(server, 'dburi'): self.dburi = server.get('dburi',
self.dburi = self.config.get(server, 'dburi') 'sqlite:///' + os.path.expanduser('~/.gertty.db'))
else: log_file = server.get('log_file', '~/.gertty.log')
self.dburi = 'sqlite:///' + os.path.expanduser('~/.gertty.db') self.log_file = os.path.expanduser(log_file)
if self.config.has_option(server, 'log_file'):
self.log_file = os.path.expanduser(self.config.get(server, 'log_file'))
else:
self.log_file = os.path.expanduser('~/.gertty.log')
def print_sample(self): def getServer(self, name=None):
print """Please create a configuration file ~/.gerttyrc for server in self.config['servers']:
if name is None or name == server['name']:
return server
return None
def printSample(self):
print """Please create a configuration file ~/.gertty.yaml
Example: Example:
-----8<-------8<-----8<-----8<--- -----8<-------8<-----8<-----8<---
[gerrit] servers:
url=https://review.example.org/ - name: gerrit
username=<gerrit username> url: https://review.example.org/
password=<gerrit password> username: <gerrit username>
git_root=~/git/ password: <gerrit password>
git_root: ~/git/
-----8<-------8<-----8<-----8<--- -----8<-------8<-----8<-----8<---
Then invoke: Then invoke:

View File

@ -6,4 +6,5 @@ GitPython>=0.3.2.RC1
python-dateutil python-dateutil
requests requests
ordereddict ordereddict
alembic>=0.4.1 PyYAML>=3.1.0
voluptuous>=0.7