Skip to content

Commit

Permalink
- osc.oscargs: added support for the "plain entry" concept
Browse files Browse the repository at this point in the history
'plain_arg' is a plain argument entry. Any value can be passed to
a plain argument entry ('foo', 'foo/bar', 'api://x/y' etc. are valid).
  • Loading branch information
marcus-h committed Oct 5, 2012
1 parent d5a78bb commit c6bef6b
Showing 1 changed file with 42 additions and 14 deletions.
56 changes: 42 additions & 14 deletions osc/oscargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
arguments.
Some notes about terminology:
'foo/bar' is called an component entry. The component entry 'foo/bar'
consists of two components; the first component is 'foo' and the
second component is 'bar'.
'api://project/package?' is also a component entry which consists of 3
components; the first component is 'api://', the second is 'project'
and the third component is 'package'. The '?' indicates that
'package' is an optional component.
'wc_path' is called a wc path entry.
A wc path is a path to a
* project wc or
* package wc or
* file in a package wc
- 'foo/bar' is called an component entry. The component entry 'foo/bar'
consists of two components; the first component is 'foo' and the
second component is 'bar'.
- 'api://project/package?' is also a component entry which consists of 3
components; the first component is 'api://', the second is 'project'
and the third component is 'package'. The '?' indicates that
'package' is an optional component.
- 'wc_path' is called a wc path entry.
A wc path is a path to a
* project wc or
* package wc or
* file in a package wc
- 'plain_arg' is a plain argument entry. Any value can be passed to
a plain argument entry ('foo', 'foo/bar', 'api://x/y' etc. are valid).
"""

Expand Down Expand Up @@ -240,6 +242,24 @@ def __str__(self):
return 'wc_' + self._name


class PlainEntry(AbstractEntry):
"""Represents a plain entry."""

def __init__(self, name):
self._name = name

def match(self, arg):
"""A match for a plain entry is always successful.
None is never returned.
"""
return {self._name: arg}

def __str__(self):
return 'plain_' + self._name


class Component(object):
"""Represents a regex for a component"""
APIURL_RE = "(?P<%s>.+)://"
Expand Down Expand Up @@ -375,15 +395,23 @@ def _parse_component(self, format_entry, separators):
yield left_sep, format_entry

def _parse_entries(self, format_entries, path, separators):
"""Parse each entry and each component into a ComponentEntry
or WCPathEntry or Component object.
"""Create for each entry in format_entries the corresponding object.
path is a path or the emptry str and separators is a list
of separators.
"""
for entry in format_entries:
if entry.startswith('wc_'):
name = entry.split('_', 1)[1]
self._entries.append(WCPathEntry(name))
continue
elif entry.startswith('plain_'):
name = entry.split('_', 1)[1]
if not name:
raise ValueError('illegal identifier for a plain entry')
self._entries.append(PlainEntry(name))
continue
e = ComponentEntry(path)
m = re.match(OscArgs.APIURL_RE, entry)
if m is not None:
Expand Down

0 comments on commit c6bef6b

Please sign in to comment.