From c66d4927070b69e98b96c2cef9256ae4e62fd32e Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Wed, 6 Apr 2016 14:55:59 +0200 Subject: [PATCH 1/2] added note about available branches to readme --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index e025ca7..b5bd6f6 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,16 @@ transWhat is a WhatsApp XMPP Gateway based on [Spectrum 2](http://www.spectrum.im) and [Yowsup 2](https://github.com/tgalal/yowsup). +#### Branches + + - [yowsup-1](http://github.com/stv0g/transwhat/tree/yowsup-1) My original version which is based on @tgalal first Yowsup version (**deprecated** and broken). + - [yowsup-2](http://github.com/stv0g/transwhat/tree/yowsup-2) Major rewrite from @moyamo for @tgalal's new Yowsup 2 (**recommended**). + - [develop](http://github.com/stv0g/transwhat/tree/develop) A develop branch with the latest fixes and improvements. This branch is based on `yowsup-2`. + +For production, please use the `yowsup-2` branch. + +Please file bugreports against the `develop` branch. + ## [Installation](INSTALL.md) ## [Usage](USAGE.md) From a398a69eaa2de2f1494981596a001d8f881d3547 Mon Sep 17 00:00:00 2001 From: moyamo Date: Mon, 11 Apr 2016 20:34:24 +0200 Subject: [PATCH 2/2] Add parser for spectrum configuration file --- config.py | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 config.py diff --git a/config.py b/config.py new file mode 100644 index 0000000..09dc7bb --- /dev/null +++ b/config.py @@ -0,0 +1,118 @@ +# I'm guessing this is the format of the spectrum config file in BNF +# ::= * +# ::= * * | +# ::=
| +#
::= [*] +# ::= * = * + + +class SpectrumConfig: + """ + Represents spectrum2 configuration options. + """ + def __init__(self, path_to_config_file): + """ + Initialises configuration file. + + Args: + path_to_config_file: The absolute path to the configuration file. + """ + self.config_path = path_to_config_file + self.options = self.loadConfig(self.config_path) + # Load backend_logging information + self.options.update(self.loadConfig(self['logging.backend_config'])) + + def loadConfig(self, file_name): + section = {'a': ""} # Current section heading, + # It's a dictionary because variables in python closures can't be + # assigned to. + options = dict() + # Recursive descent parser + def consume_spaces(line): + i = 0 + for c in line: + if c != ' ': + break + i += 1 + return line[i:] + + def read_identifier(line): + i = 0 + for c in line: + if c == ' ' or c==']' or c=='[' or c=='=': + break + i += 1 + # no identifier + if i == 0: + return (None, 'No identifier') + return (line[:i], line[i:]) + + def parse_section(line): + if len(line) == 0 or line[0] != '[': + return (None, 'expected [') + line = line[1:] + identifier, line = read_identifier(line) + if len(line) == 0 or line[0] != ']' or identifier is None: + return (None, line) + return (identifier, line[1:]) + + def parse_assignment(line): + key, line = read_identifier(line) + if key is None: + return (None, None, line) + line = consume_spaces(line) + if len(line) == 0 or line[0] != '=': + return (None, None, 'Expected =') + line = consume_spaces(line[1:]) + value = line[:-1] + return (key, value, '\n') + + def expr(line): + sec, newline = parse_section(line) + if sec is not None: + section['a'] = sec + else: + key, value, newline = parse_assignment(line) + if key is not None: + if section['a'] != '': + options[section['a'] + '.' + key] = value + else: + options[key] = value + else: + return (None, newline) + return (newline, None) + + def parse_line(line, line_number): + line = consume_spaces(line) + if line == '\n': + return + newline, error = expr(line) + if newline is None: + raise ConfigParseError(str(line_number) + ': ' + error + ': ' + repr(line)) + newline = consume_spaces(newline) + if newline != '\n': + raise ConfigParseError(str(line_number) + ': Expected newline got ' + repr(newline)) + + def strip_comments(line): + i = 0 + for c in line: + if c == '#' or c == '\n': + break + i += 1 + return line[:i] + '\n' + + with open(file_name, 'r') as f: + i = 1 + while True: + line = f.readline() + if line == '': + break + parse_line(strip_comments(line), i) + i += 1 + return options + + def __getitem__(self, key): + return self.options[key] + +class ConfigParseError(Exception): + pass