# -*- coding: utf-8 -*- """ Trac Plugin for Monotone Copyright 2006, 2007 Thomas Moschny (thomas.moschny@gmx.de) {{{ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA }}} """ import re TOKEN = re.compile(r''' "(?P(\\\\|\\"|[^"])*)" |\[(?P[a-f0-9]{40}|)\] |(?P\w+) |(?P\s+) ''', re.VERBOSE) def items(raw): ''' Parses Monotone basic_io into (key, value) pairs, thereby ignoring all whitespace. "value" is a list of all values found for "key". Text values are dequoted properly, i.e. all occurences of \" and \\ are replaced with " and \, respectively. ''' # we yield when we find a new key or hit end of input key, values = None, [] for match in TOKEN.finditer(raw): if match.lastgroup == 'key': if key: yield key, values key = match.group('key') values = [] elif match.lastgroup == 'id': values.append(match.group('id')) elif match.lastgroup == 'str': value = match.group('str') # dequote: replace \" with " value = re.sub(r'\\"', '"', value) # dequote: replace \\ with \ value = re.sub(r'\\\\', r'\\', value) values.append(value) if key: yield key, values