Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 1 | #! /usr/bin/python |
| 2 | |
| 3 | # Copyright 2014 Linaro Limited |
| 4 | # Author: Steve McIntyre <steve.mcintyre@linaro.org> |
| 5 | # |
| 6 | # This program is free software; you can redistribute it and/or modify |
| 7 | # it under the terms of the GNU General Public License as published by |
| 8 | # the Free Software Foundation; either version 2 of the License, or |
| 9 | # (at your option) any later version. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU General Public License for more details. |
| 15 | # |
| 16 | # You should have received a copy of the GNU General Public License |
| 17 | # along with this program; if not, write to the Free Software |
| 18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 19 | # MA 02110-1301, USA. |
| 20 | # |
| 21 | # VLANd simple config parser |
| 22 | # |
| 23 | |
| 24 | import ConfigParser |
| 25 | import os, sys, re |
| 26 | |
| 27 | if __name__ == '__main__': |
| 28 | vlandpath = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0]))) |
| 29 | sys.path.insert(0, vlandpath) |
| 30 | sys.path.insert(0, "%s/.." % vlandpath) |
| 31 | |
Steve McIntyre | 2998035 | 2014-12-10 19:16:51 +0000 | [diff] [blame] | 32 | from errors import CriticalError, InputError, ConfigError |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 33 | |
| 34 | class DBConfigClass: |
| 35 | """ Simple container for stuff to make for nicer syntax """ |
| 36 | |
| 37 | def __repr__(self): |
| 38 | return "<DBConfig: server: %s, port: %s, dbname: %s, username: %s, password: %s" % (self.server, self.port, self.dbname, self.username, self.password) |
| 39 | |
| 40 | class SwitchConfigClass: |
| 41 | """ Simple container for stuff to make for nicer syntax """ |
| 42 | def __repr__(self): |
| 43 | return "<SwitchConfig: name: %s, section: %s, driver: %s, username: %s, password: %s, enable_password: %s" % (self.name, self.section, self.driver, self.username, self.password, self.enable_password) |
| 44 | |
| 45 | class VlanConfig: |
| 46 | """VLANd config class""" |
| 47 | def __init__(self, filenames): |
Steve McIntyre | d773cac | 2014-12-10 23:27:07 +0000 | [diff] [blame] | 48 | |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 49 | config = ConfigParser.RawConfigParser({ |
| 50 | # Set default values |
| 51 | 'server': None, |
| 52 | 'port': None, |
| 53 | 'dbname': None, |
| 54 | 'username': None, |
| 55 | 'password': None, |
| 56 | 'name': None, |
| 57 | 'driver': None, |
| 58 | 'enable_password': None |
| 59 | }) |
| 60 | |
| 61 | config.read(filenames) |
| 62 | |
| 63 | # Parse out the config file |
| 64 | # Must have a [database] section |
| 65 | # May have a [logging] section |
| 66 | # May have multiple [switch 'foo'] sections |
| 67 | if not config.has_section('database'): |
| 68 | raise ConfigError('No database configuration section found') |
| 69 | |
| 70 | self.database = DBConfigClass() |
| 71 | self.switches = {} |
| 72 | |
| 73 | sw_regex = re.compile('(switch)\ (.*)', flags=re.I) |
| 74 | for section in config.sections(): |
| 75 | if section == 'database': |
| 76 | try: |
| 77 | self.database.server = config.get(section, 'server') |
Steve McIntyre | 38a30eb | 2014-12-11 16:10:55 +0000 | [diff] [blame^] | 78 | except: |
| 79 | raise ConfigError('Invalid database configuration (server)') |
| 80 | |
| 81 | try: |
| 82 | self.database.port = config.get(section, 'port') |
| 83 | except: |
| 84 | raise ConfigError('Invalid database configuration (port)') |
| 85 | |
| 86 | try: |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 87 | self.database.dbname = config.get(section, 'dbname') |
Steve McIntyre | 38a30eb | 2014-12-11 16:10:55 +0000 | [diff] [blame^] | 88 | except: |
| 89 | raise ConfigError('Invalid database configuration (dbname)') |
| 90 | |
| 91 | try: |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 92 | self.database.username = config.get(section, 'username') |
Steve McIntyre | 38a30eb | 2014-12-11 16:10:55 +0000 | [diff] [blame^] | 93 | except: |
| 94 | raise ConfigError('Invalid database configuration (username)') |
| 95 | |
| 96 | try: |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 97 | self.database.password = config.get(section, 'password') |
| 98 | except: |
Steve McIntyre | 38a30eb | 2014-12-11 16:10:55 +0000 | [diff] [blame^] | 99 | raise ConfigError('Invalid database configuration (password)') |
Steve McIntyre | 6f59b97 | 2014-12-10 16:43:37 +0000 | [diff] [blame] | 100 | # Others are optional, but these are not |
| 101 | if self.database.dbname is None or self.database.username is None: |
| 102 | raise ConfigError('Database configuration section incomplete') |
| 103 | elif section == 'logging': |
| 104 | pass # Fill this in later... |
| 105 | else: |
| 106 | match = sw_regex.match(section) |
| 107 | if match: |
| 108 | # Constraint: switch names must be unique! See if |
| 109 | # there's already a switch with this name |
| 110 | name = config.get(section, 'name') |
| 111 | for key in self.switches.keys(): |
| 112 | if name == key: |
| 113 | raise ConfigError('Found switches with the same name (%s)' % name) |
| 114 | self.switches[name] = SwitchConfigClass() |
| 115 | self.switches[name].name = name |
| 116 | self.switches[name].section = section |
| 117 | self.switches[name].driver = config.get(section, 'driver') |
| 118 | self.switches[name].username = config.get(section, 'username') |
| 119 | self.switches[name].password = config.get(section, 'password') |
| 120 | self.switches[name].enable_password = config.get(section, 'enable_password') |
| 121 | else: |
| 122 | raise ConfigError('Unrecognised config section %s' % section) |
| 123 | |
| 124 | def __del__(self): |
| 125 | pass |
| 126 | |
| 127 | if __name__ == '__main__': |
| 128 | config = VlanConfig(filenames=('./vland.cfg',)) |
| 129 | print config.database |
| 130 | for switch in config.switches: |
| 131 | print config.switches[switch] |
| 132 | |