Skip to content

Commit

Permalink
Merge branch 'feature/handle-buildout-operators' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Fantomas42 committed Dec 4, 2013
2 parents f10114f + fee135f commit 887db85
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 7 deletions.
26 changes: 19 additions & 7 deletions bvc/configparser.py
@@ -1,9 +1,12 @@
"""Config parser for Buildout Versions Checker"""
import re
try:
from ConfigParser import RawConfigParser
except ImportError: # Python 3
from configparser import RawConfigParser

OPERATORS = re.compile(r'[+-<]$')


class VersionsConfigParser(RawConfigParser):
"""
Expand All @@ -19,13 +22,22 @@ def write_section(self, fd, section, indentation):
"""
string_section = '[%s]\n' % section
for key, value in self._sections[section].items():
if key != '__name__':
if value is None:
value = ''
string_section += '%s= %s\n' % (
key.ljust(indentation),
str(value).replace(
'\n', '\n'.ljust(indentation + 3)))
if key == '__name__':
continue
if value is None:
value = ''
operator = ''
key_indentation = indentation
buildout_operator = OPERATORS.search(key)
if buildout_operator:
operator = buildout_operator.group(0)
key_indentation -= 1
key = key[:-1]
key = key.ljust(key_indentation) + operator
value = str(value).replace('\n', '\n'.ljust(
key_indentation + 3 + int(bool(operator))))
string_section += '%s= %s\n' % (key, value)

fd.write(string_section.encode('utf-8'))

def write(self, source, indentation=32):
Expand Down
105 changes: 105 additions & 0 deletions bvc/tests.py
Expand Up @@ -281,17 +281,22 @@ def test_write(self):
config_parser.add_section('Section 2')
config_parser.set('Section 1', 'Option', 'Value')
config_parser.set('Section 1', 'Option-void', None)
config_parser.set('Section 1', 'Option-add+', 'Value added')
config_parser.set('Section 2', 'Option-multiline', 'Value1\nValue2')
config_parser.set('Section 2', '<', 'Value1\nValue2')
config_parser.write(config_file.name)
config_file.seek(0)
self.assertEquals(
config_file.read().decode('utf-8'),
'[Section 1]\n'
'Option = Value\n'
'Option-void = \n'
'Option-add += Value added\n'
'\n'
'[Section 2]\n'
'Option-multiline = Value1\n'
' Value2\n'
' <= Value1\n'
' Value2\n')
config_file.close()

Expand All @@ -316,6 +321,106 @@ def test_write_low_indentation(self):
' Value2\n')
config_file.close()

def test_parse_and_write_buildout_operators(self):
config_file = NamedTemporaryFile()
config_file.write(
'[Section]\nAd+=dition\nSub-=straction'.encode('utf-8'))
config_file.seek(0)
config_parser = VersionsConfigParser()
config_parser.read(config_file.name)
config_file.seek(0)
config_parser.write(config_file.name, 24)
config_file.seek(0)
self.assertEquals(
config_file.read().decode('utf-8'),
'[Section]\n'
'Ad += dition\n'
'Sub -= straction\n')
config_file.close()

def test_parse_and_write_buildout_operators_offset(self):
config_file = NamedTemporaryFile()
config_file.write(
'[Section]\nAd +=dition\nSub - = straction'.encode('utf-8'))
config_file.seek(0)
config_parser = VersionsConfigParser()
config_parser.read(config_file.name)
config_file.seek(0)
config_parser.write(config_file.name, 24)
config_file.seek(0)
self.assertEquals(
config_file.read().decode('utf-8'),
'[Section]\n'
'Ad += dition\n'
'Sub -= straction\n')
config_file.close()

def test_parse_and_write_buildout_operators_multilines(self):
config_file = NamedTemporaryFile()
config_file.write(
'[Section]\nAdd+=Multi\n Lines'.encode('utf-8'))
config_file.seek(0)
config_parser = VersionsConfigParser()
config_parser.read(config_file.name)
config_file.seek(0)
config_parser.write(config_file.name, 24)
config_file.seek(0)
self.assertEquals(
config_file.read().decode('utf-8'),
'[Section]\n'
'Add += Multi\n'
' Lines\n')
config_file.close()

def test_parse_and_write_buildout_macros(self):
config_file = NamedTemporaryFile()
config_file.write(
'[Section]\n<=Macro\n Template'.encode('utf-8'))
config_file.seek(0)
config_parser = VersionsConfigParser()
config_parser.read(config_file.name)
config_file.seek(0)
config_parser.write(config_file.name, 24)
config_file.seek(0)
self.assertEquals(
config_file.read().decode('utf-8'),
'[Section]\n'
' <= Macro\n'
' Template\n')
config_file.close()

def test_parse_and_write_buildout_macros_offset(self):
config_file = NamedTemporaryFile()
config_file.write(
'[Section]\n< = Macro\n Template'.encode('utf-8'))
config_file.seek(0)
config_parser = VersionsConfigParser()
config_parser.read(config_file.name)
config_file.seek(0)
config_parser.write(config_file.name, 24)
config_file.seek(0)
self.assertEquals(
config_file.read().decode('utf-8'),
'[Section]\n'
' <= Macro\n'
' Template\n')
config_file.close()

def test_parse_and_write_buildout_conditional_sections(self):
config_file = NamedTemporaryFile()
config_file.write('[Section:Condition]\nKey=Value\n'.encode('utf-8'))
config_file.seek(0)
config_parser = VersionsConfigParser()
config_parser.read(config_file.name)
config_file.seek(0)
config_parser.write(config_file.name, 24)
config_file.seek(0)
self.assertEquals(
config_file.read().decode('utf-8'),
'[Section:Condition]\n'
'Key = Value\n')
config_file.close()


class IndentCommandLineTestCase(LogsTestCase,
StdOutTestCase):
Expand Down

0 comments on commit 887db85

Please sign in to comment.