Skip to content

Commit

Permalink
- osc.oscargs: added support to handle name clashes in the format ent…
Browse files Browse the repository at this point in the history
…ries/oargs
  • Loading branch information
marcus-h committed Oct 5, 2012
1 parent 4c7861d commit 2a23166
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions osc/oscargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,21 @@
class ResolvedInfo(object):
"""Encapsulate resolved arguments"""

def __init__(self):
def __init__(self, ignore_clashes=True):
"""Constructs a new ResolvedInfo object.
Keyword arguments:
ignore_clashes -- if True name clashes are ignored. A name clash occurs
if multiple oargs with the same "name" are specified.
By default the argument which is assigned to the
right most oarg is saved. If ignore_clashes is set
to False all arguments will be stored in a list
(default: True)
"""
super(ResolvedInfo, self).__init__()
self._data = {}
self._ignore_clashes = ignore_clashes

def add(self, name, value):
"""Add additional components.
Expand All @@ -46,7 +58,15 @@ def add(self, name, value):
be overriden with the new value.
"""
self._data[name] = value
if name in self._data and not self._ignore_clashes:
l = self._data[name]
if hasattr(l, 'extend'):
# it is already a list
l.append(value)
else:
self._data[name] = [l, value]
else:
self._data[name] = value

def __getattr__(self, name):
if name in self._data.keys():
Expand Down Expand Up @@ -361,11 +381,14 @@ def __init__(self, *format_entries, **kwargs):
(default: ''). path might be used for component
resolving.
separators -- list of component separators (default: ['/', '@'])
ignore_clashes -- ignore name clashes in the format_entries
(default: True)
"""
super(OscArgs, self).__init__()
self._logger = logging.getLogger(__name__)
self._entries = []
self._ignore_clashes = kwargs.pop('ignore_clashes', True)
self._parse_entries(format_entries, kwargs.pop('path', ''),
kwargs.pop('separators', ['/', '@']))

Expand Down Expand Up @@ -457,7 +480,7 @@ def _check_resolved(self, info, resolved):
def _resolve(self, args, use_wc=False, path=''):
entries = self._entries[:]
args = list(args)
info = ResolvedInfo()
info = ResolvedInfo(self._ignore_clashes)
while args:
arg = args.pop(0)
self._logger.debug("argument: " + arg)
Expand Down

0 comments on commit 2a23166

Please sign in to comment.