1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 //    names, trademarks, service marks, or product names of the Licensor
11 //    and its affiliates, except as required to comply with Section 4(c) of
12 //    the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 //     http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_BASE_TF_PY_ARG_H
25 #define PXR_BASE_TF_PY_ARG_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/base/tf/api.h"
29 
30 #include <boost/python/dict.hpp>
31 #include <boost/python/tuple.hpp>
32 #include <string>
33 #include <vector>
34 
35 PXR_NAMESPACE_OPEN_SCOPE
36 
37 /// \class TfPyArg
38 ///
39 /// Class representing a function argument.
40 ///
41 /// This is similar to \c boost::python::arg, except it's not opaque and
42 /// provides more fields for documentation purposes.
43 class TfPyArg
44 {
45 public:
46     /// Create a TfPyArg representing an argument with the given \p name.
47     /// \p typeDoc and \p defaultValueDoc are optional documentation strings
48     /// describing the expected type and default value of this argument.
49     TfPyArg(const std::string& name,
50             const std::string& typeDoc = std::string(),
51             const std::string& defaultValueDoc = std::string())
_name(name)52         : _name(name), _typeDoc(typeDoc), _defaultValueDoc(defaultValueDoc)
53     { }
54 
55     /// Returns argument name.
GetName()56     const std::string& GetName() const
57     { return _name; }
58 
59     /// Returns documentation for default value (if any) for this argument.
GetDefaultValueDoc()60     const std::string& GetDefaultValueDoc() const
61     { return _defaultValueDoc; }
62 
63     /// Returns documentation of type of value required by this argument.
GetTypeDoc()64     const std::string& GetTypeDoc() const
65     { return _typeDoc; }
66 
67 private:
68     std::string _name;
69     std::string _typeDoc;
70     std::string _defaultValueDoc;
71 };
72 
73 typedef std::vector<TfPyArg> TfPyArgs;
74 
75 /// Helper function for processing optional arguments given as a tuple of
76 /// positional arguments and a dictionary of keyword arguments.
77 ///
78 /// This function will match the given positional arguments in \p args with
79 /// the ordered list of allowed arguments in \p optionalArgs. Arguments that
80 /// are matched up in this way will be stored as (name, value) pairs and
81 /// merged with \p kwargs in the returned dictionary.
82 ///
83 /// If \p allowExtraArgs is \c false, any unrecognized keyword or positional
84 /// arguments will cause a Python TypeError to be emitted. Otherwise,
85 /// unmatched arguments will be added to the returned tuple or dict.
86 TF_API
87 std::pair<boost::python::tuple, boost::python::dict>
88 TfPyProcessOptionalArgs(
89     const boost::python::tuple& args,
90     const boost::python::dict& kwargs,
91     const TfPyArgs& expectedArgs,
92     bool allowExtraArgs = false);
93 
94 /// Create a doc string for a function with the given \p functionName,
95 /// \p requiredArguments and \p optionalArguments. An extra \p description
96 /// may also be supplied.
97 TF_API
98 std::string TfPyCreateFunctionDocString(
99     const std::string& functionName,
100     const TfPyArgs& requiredArguments = TfPyArgs(),
101     const TfPyArgs& optionalArguments = TfPyArgs(),
102     const std::string& description = std::string());
103 
104 PXR_NAMESPACE_CLOSE_SCOPE
105 
106 #endif // PXR_BASE_TF_PY_ARG_H
107