Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
c279eb1c
Commit
c279eb1c
authored
Jun 22, 2016
by
Calen Pennington
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a new Task subclass that will pass through unknown options as the unknown_options argument
parent
7c647e5e
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
153 additions
and
0 deletions
+153
-0
pavelib/utils/passthrough_opts.py
+153
-0
No files found.
pavelib/utils/passthrough_opts.py
0 → 100644
View file @
c279eb1c
"""
Provides:
PassthroughOptionParser:
A subclass of :class:`optparse.OptionParser` that captures unknown options
into its ``passthrough_options`` attribute.
PassthroughTask:
A subclass of :class:`paver.tasks.Task` that supplies unknown options
as the `passthrough_options` argument to the decorated function
"""
from
optparse
import
OptionParser
,
BadOptionError
import
paver.tasks
from
mock
import
patch
try
:
from
gettext
import
gettext
except
ImportError
:
def
gettext
(
message
):
"""Dummy gettext"""
return
message
_
=
gettext
class
PassthroughOptionParser
(
OptionParser
):
"""
An :class:`optparse.OptionParser` which captures any unknown options into
the ``passthrough_options`` attribute. Handles both "--long-options" and
"-s" short options.
"""
def
__init__
(
self
,
*
args
,
**
kwargs
):
self
.
unknown_options
=
[]
# N.B. OptionParser is an old-style class, which is why
# this isn't using super()
OptionParser
.
__init__
(
self
,
*
args
,
**
kwargs
)
def
_process_long_opt
(
self
,
rargs
,
values
):
# This is a copy of the OptionParser._process_long_opt method,
# modified to capture arguments that aren't understood
arg
=
rargs
.
pop
(
0
)
# Value explicitly attached to arg? Pretend it's the next
# argument.
if
"="
in
arg
:
(
opt
,
next_arg
)
=
arg
.
split
(
"="
,
1
)
rargs
.
insert
(
0
,
next_arg
)
had_explicit_value
=
True
else
:
opt
=
arg
had_explicit_value
=
False
try
:
opt
=
self
.
_match_long_opt
(
opt
)
except
BadOptionError
:
self
.
passthrough_options
.
append
(
arg
)
if
had_explicit_value
:
rargs
.
pop
(
0
)
return
option
=
self
.
_long_opt
[
opt
]
if
option
.
takes_value
():
nargs
=
option
.
nargs
if
len
(
rargs
)
<
nargs
:
if
nargs
==
1
:
self
.
error
(
_
(
"
%
s option requires an argument"
)
%
opt
)
else
:
self
.
error
(
_
(
"
%
s option requires
%
d arguments"
)
%
(
opt
,
nargs
))
elif
nargs
==
1
:
value
=
rargs
.
pop
(
0
)
else
:
value
=
tuple
(
rargs
[
0
:
nargs
])
del
rargs
[
0
:
nargs
]
elif
had_explicit_value
:
self
.
error
(
_
(
"
%
s option does not take a value"
)
%
opt
)
else
:
value
=
None
option
.
process
(
opt
,
value
,
values
,
self
)
def
_process_short_opts
(
self
,
rargs
,
values
):
arg
=
rargs
.
pop
(
0
)
stop
=
False
i
=
1
passthrough_opts
=
[]
for
char
in
arg
[
1
:]:
opt
=
"-"
+
char
option
=
self
.
_short_opt
.
get
(
opt
)
i
+=
1
# we have consumed a character
if
not
option
:
passthrough_opts
.
append
(
char
)
continue
if
option
.
takes_value
():
# Any characters left in arg? Pretend they're the
# next arg, and stop consuming characters of arg.
if
i
<
len
(
arg
):
rargs
.
insert
(
0
,
arg
[
i
:])
stop
=
True
nargs
=
option
.
nargs
if
len
(
rargs
)
<
nargs
:
if
nargs
==
1
:
self
.
error
(
_
(
"
%
s option requires an argument"
)
%
opt
)
else
:
self
.
error
(
_
(
"
%
s option requires
%
d arguments"
)
%
(
opt
,
nargs
))
elif
nargs
==
1
:
value
=
rargs
.
pop
(
0
)
else
:
value
=
tuple
(
rargs
[
0
:
nargs
])
del
rargs
[
0
:
nargs
]
else
:
# option doesn't take a value
value
=
None
option
.
process
(
opt
,
value
,
values
,
self
)
if
stop
:
break
if
passthrough_opts
:
self
.
passthrough_options
.
append
(
'-{}'
.
format
(
""
.
join
(
passthrough_opts
)))
class
PassthroughTask
(
paver
.
tasks
.
Task
):
"""
A :class:`paver.tasks.Task` subclass that supplies any options that it doesn't
understand to the task function as the ``passthrough_options`` argument.
"""
@property
def
parser
(
self
):
with
patch
.
object
(
paver
.
tasks
.
optparse
,
'OptionParser'
,
PassthroughOptionParser
):
return
super
(
PassthroughTask
,
self
)
.
parser
def
__call__
(
self
,
*
args
,
**
kwargs
):
paver
.
tasks
.
environment
.
passthrough_options
=
self
.
_parser
.
passthrough_options
# pylint: disable=no-member
try
:
return
super
(
PassthroughTask
,
self
)
.
__call__
(
*
args
,
**
kwargs
)
finally
:
del
paver
.
tasks
.
environment
.
unknown_options
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment