forked from google/python-fire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorators.py
More file actions
97 lines (76 loc) · 3 KB
/
Copy pathdecorators.py
File metadata and controls
97 lines (76 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Copyright (C) 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""These decorators provide function metadata to Python Fire.
SetParseFn and SetParseFns allow you to set the functions Fire uses for parsing
command line arguments to client code.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import inspect
FIRE_METADATA = 'FIRE_METADATA'
FIRE_PARSE_FNS = 'FIRE_PARSE_FNS'
ACCEPTS_POSITIONAL_ARGS = 'ACCEPTS_POSITIONAL_ARGS'
def SetParseFn(fn, *arguments):
"""Sets the fn for Fire to use to parse args when calling the decorated fn.
Args:
fn: The function to be used for parsing arguments.
*arguments: The arguments for which to use the parse fn. If none are listed,
then this will set the default parse function.
Returns:
The decorated function, which now has metadata telling Fire how to perform.
"""
def _Decorator(func):
parse_fns = GetParseFns(func)
if not arguments:
parse_fns['default'] = fn
else:
for argument in arguments:
parse_fns['named'][argument] = fn
_SetMetadata(func, FIRE_PARSE_FNS, parse_fns)
return func
return _Decorator
def SetParseFns(*positional, **named):
"""Set the fns for Fire to use to parse args when calling the decorated fn.
Returns a decorator, which when applied to a function adds metadata to the
function telling Fire how to turn string command line arguments into proper
Python arguments with which to call the function.
A parse function should accept a single string argument and return a value to
be used in it's place when calling the decorated function.
Args:
*positional: The functions to be used for parsing positional arguments.
**named: The functions to be used for parsing named arguments.
Returns:
The decorated function, which now has metadata telling Fire how to perform.
"""
def _Decorator(fn):
parse_fns = GetParseFns(fn)
parse_fns['positional'] = positional
parse_fns['named'].update(named)
_SetMetadata(fn, FIRE_PARSE_FNS, parse_fns)
return fn
return _Decorator
def _SetMetadata(fn, attribute, value):
metadata = GetMetadata(fn)
metadata[attribute] = value
setattr(fn, FIRE_METADATA, metadata)
def GetMetadata(fn):
default = {
ACCEPTS_POSITIONAL_ARGS: not inspect.isclass(fn),
}
return getattr(fn, FIRE_METADATA, default)
def GetParseFns(fn):
metadata = GetMetadata(fn)
default = dict(default=None, positional=[], named={})
return metadata.get(FIRE_PARSE_FNS, default)