Skip to content

Commit

Permalink
Merge branch 'release/1.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
Fantomas42 committed Feb 15, 2014
2 parents eb639f2 + 1edf7e1 commit 00bb3a4
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 15 deletions.
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Options

usage: check-buildout-updates [-h] [-s SOURCE] [-i INCLUDES] [-e EXCLUDES]
[-w] [--indent INDENTATION]
[--sorting {alpha,length}]
[--service-url SERVICE_URL] [--timeout TIMEOUT]
[-t THREADS] [-v] [-q]

Expand All @@ -60,6 +61,9 @@ Options
multiple times)
-w, --write Write the updates in the source file
--indent INDENTATION Spaces used when indenting "key = value" (default: 32)
--sorting {alpha,length}
Sorting algorithm used on the keys when writing source
file (default: None)
--service-url SERVICE_URL
The service to use for checking the packages (default:
http://pypi.python.org/pypi)
Expand Down
10 changes: 5 additions & 5 deletions buildout.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ recipe = zc.recipe.egg
eggs = python-coveralls

[versions]
futures = 2.1.5
futures = 2.1.6
blessings = 1.5.1
coverage = 3.7
coverage = 3.7.1
nose-progressive = 1.5
pbp.recipe.noserunner = 0.2.6
pep8 = 1.4.6
Expand All @@ -64,9 +64,9 @@ zc.recipe.egg = 2.0.1
nose = 1.3.0
flake8 = 2.1.0
mccabe = 0.2.1
python-coveralls = 2.4.1
python-coveralls = 2.4.2
PyYAML = 3.10
argparse = 1.2.1
requests = 2.0.1
requests = 2.2.1
sh = 1.09
six = 1.4.1
six = 1.5.2
25 changes: 20 additions & 5 deletions bvc/configparser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Config parser for Buildout Versions Checker"""
import re
from operator import itemgetter
try:
from ConfigParser import RawConfigParser
except ImportError: # Python 3
Expand All @@ -15,13 +16,27 @@ class VersionsConfigParser(RawConfigParser):
"""
optionxform = str

def write_section(self, fd, section, indentation):
def alpha_sorter(self, items):
return sorted(items, key=itemgetter(0))

def length_sorter(self, items):
return sorted(self.alpha_sorter(items),
key=lambda x: len(x[0]))

def write_section(self, fd, section, indentation, sorting):
"""
Write a section of an .ini-format
and all the keys within.
"""
string_section = '[%s]\n' % section
for key, value in self._sections[section].items():

items = self._sections[section].items()
try:
items = getattr(self, '%s_sorter' % sorting)(items)
except (TypeError, AttributeError):
pass

for key, value in items:
if key == '__name__':
continue
if value is None:
Expand All @@ -44,14 +59,14 @@ def write_section(self, fd, section, indentation):

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

def write(self, source, indentation=32):
def write(self, source, indentation=32, sorting=None):
"""
Write an .ini-format representation of the
configuration state with a readable indentation.
"""
with open(source, 'wb') as fd:
sections = list(self._sections.keys())
for section in sections[:-1]:
self.write_section(fd, section, indentation)
self.write_section(fd, section, indentation, sorting)
fd.write('\n'.encode('utf-8'))
self.write_section(fd, sections[-1], indentation)
self.write_section(fd, sections[-1], indentation, sorting)
7 changes: 6 additions & 1 deletion bvc/scripts/check_buildout_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def cmdline(argv=sys.argv[1:]):
parser.add_argument(
'--indent', dest='indentation', type=int, default=32,
help='Spaces used when indenting "key = value" (default: 32)')
parser.add_argument(
'--sorting', dest='sorting', default='', choices=['alpha', 'length'],
help='Sorting algorithm used on the keys when writing source file '
'(default: None)')
parser.add_argument(
'--service-url', dest='service_url',
default='http://pypi.python.org/pypi',
Expand Down Expand Up @@ -87,7 +91,8 @@ def cmdline(argv=sys.argv[1:]):
config.add_section('versions')
for package, version in checker.updates.items():
config.set('versions', package, version)
config.write(source, options.indentation)

config.write(source, options.indentation, options.sorting)
logger.info('- %s updated.' % source)

sys.exit(0)
6 changes: 5 additions & 1 deletion bvc/scripts/indent_buildout.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def cmdline(argv=sys.argv[1:]):
parser.add_argument(
'--indent', dest='indentation', type=int, default=32,
help='Spaces used when indenting "key = value" (default: 32)')
parser.add_argument(
'--sorting', dest='sorting', default='', choices=['alpha', 'length'],
help='Sorting algorithm used on the keys when writing source file '
'(default: None)')
parser.add_argument(
'-v', action='count', dest='verbosity', default=1,
help='Increase verbosity (specify multiple times for more)')
Expand Down Expand Up @@ -46,7 +50,7 @@ def cmdline(argv=sys.argv[1:]):
config = VersionsConfigParser()
config_readed = config.read(source)
if config_readed:
config.write(source, options.indentation)
config.write(source, options.indentation, options.sorting)
logger.warning('- %s (re)indented at %s spaces.' % (
source, options.indentation))
else:
Expand Down
68 changes: 66 additions & 2 deletions bvc/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def test_write_section(self):
config_parser.set('Section', 'Option', 'Value')
config_parser.set('Section', 'Option-void', None)
config_parser.set('Section', 'Option-multiline', 'Value1\nValue2')
config_parser.write_section(config_file, 'Section', 24)
config_parser.write_section(config_file, 'Section', 24, '')
config_file.seek(0)
self.assertEquals(
config_file.read().decode('utf-8'),
Expand All @@ -256,14 +256,52 @@ def test_write_section(self):
' Value2\n')
config_file.close()

def test_write_section_alpha_sorting(self):
config_file = NamedTemporaryFile()
config_parser = VersionsConfigParser()
config_parser.add_section('Section')
config_parser.set('Section', 'Option-multiline', 'Value1\nValue2')
config_parser.set('Section', 'Option-void', None)
config_parser.set('Section', 'Option', 'Value')
config_parser.write_section(config_file, 'Section', 24, 'alpha')
config_file.seek(0)
self.assertEquals(
config_file.read().decode('utf-8'),
'[Section]\n'
'Option = Value\n'
'Option-multiline = Value1\n'
' Value2\n'
'Option-void = \n')
config_file.close()

def test_write_section_length_sorting(self):
config_file = NamedTemporaryFile()
config_parser = VersionsConfigParser()
config_parser.add_section('Section')
config_parser.set('Section', 'Option-multiline', 'Value1\nValue2')
config_parser.set('Section', 'Option-void', None)
config_parser.set('Section', 'Option-size', None)
config_parser.set('Section', 'Option', 'Value')
config_parser.write_section(config_file, 'Section', 24, 'length')
config_file.seek(0)
self.assertEquals(
config_file.read().decode('utf-8'),
'[Section]\n'
'Option = Value\n'
'Option-size = \n'
'Option-void = \n'
'Option-multiline = Value1\n'
' Value2\n')
config_file.close()

def test_write_section_low_indentation(self):
config_file = NamedTemporaryFile()
config_parser = VersionsConfigParser()
config_parser.add_section('Section')
config_parser.set('Section', 'Option', 'Value')
config_parser.set('Section', 'Option-void', None)
config_parser.set('Section', 'Option-multiline', 'Value1\nValue2')
config_parser.write_section(config_file, 'Section', 12)
config_parser.write_section(config_file, 'Section', 12, '')
config_file.seek(0)
self.assertEquals(
config_file.read().decode('utf-8'),
Expand Down Expand Up @@ -300,6 +338,32 @@ def test_write(self):
' Value2\n')
config_file.close()

def test_write_alpha_sorting(self):
config_file = NamedTemporaryFile()
config_parser = VersionsConfigParser()
config_parser.add_section('Section 1')
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, sorting='alpha')
config_file.seek(0)
self.assertEquals(
config_file.read().decode('utf-8'),
'[Section 1]\n'
'Option = Value\n'
'Option-add += Value added\n'
'Option-void = \n'
'\n'
'[Section 2]\n'
'<= Value1\n'
' Value2\n'
'Option-multiline = Value1\n'
' Value2\n')
config_file.close()

def test_write_low_indentation(self):
config_file = NamedTemporaryFile()
config_parser = VersionsConfigParser()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from setuptools import setup
from setuptools import find_packages

__version__ = '1.3.1'
__version__ = '1.4'
__license__ = 'BSD License'

__author__ = 'Fantomas42'
Expand Down

0 comments on commit 00bb3a4

Please sign in to comment.