Add unpack_dependencies function

This commit is contained in:
Mathieu Comandon 2016-10-20 15:48:33 -07:00
parent 3cf714e3a2
commit e01cb320b3
2 changed files with 44 additions and 0 deletions

View file

@ -47,3 +47,25 @@ def version_sort(versions, reverse=False):
version.append(suffix)
return version
return sorted(versions, key=version_key, reverse=reverse)
def unpack_dependencies(string):
"""Parse a string to allow for complex dependencies
Works in a similar fashion as Debian dependencies, separate dependencies
are comma separated and multiple choices for satisfying a dependency are
separated by pipes.
Example: quake-steam | quake-gog, some-quake-mod returns:
[('quake-steam', 'quake-gog'), 'some-quake-mod']
"""
dependencies = string.split(',')
dependencies = [dep.strip() for dep in dependencies if dep.strip()]
for index, dependency in enumerate(dependencies):
if '|' in dependency:
choices = tuple([choice.strip()
for choice in dependencies[index].split('|')
if choice.strip()])
dependencies[index] = choices
dependencies = [dep for dep in dependencies if dep]
return dependencies

View file

@ -128,3 +128,25 @@ class TestEvilConfigParser(TestCase):
parser.set('Test', 'key', 'value')
with open(self.config_path, 'wb') as config:
parser.write(config)
class TestUnpackDependencies(TestCase):
def test_single_dependency(self):
string = 'quake'
dependencies = strings.unpack_dependencies(string)
self.assertEqual(dependencies, ['quake'])
def test_multiple_dependencies(self):
string = 'quake, quake-1,quake-steam, quake-gog '
dependencies = strings.unpack_dependencies(string)
self.assertEqual(dependencies, ['quake', 'quake-1', 'quake-steam', 'quake-gog'])
def test_dependency_options(self):
string = 'quake, quake-1,quake-steam | quake-gog|quake-humble '
dependencies = strings.unpack_dependencies(string)
self.assertEqual(dependencies, ['quake', 'quake-1', ('quake-steam', 'quake-gog', 'quake-humble')])
def test_strips_extra_commas(self):
string = ', , , ,, ,,,,quake, quake-1,quake-steam | quake-gog|quake-humble |||| , |, | ,|,| , '
dependencies = strings.unpack_dependencies(string)
self.assertEqual(dependencies, ['quake', 'quake-1', ('quake-steam', 'quake-gog', 'quake-humble')])