diff options
author | Jens Georg <mail@jensge.org> | 2018-12-26 07:44:30 +0100 |
---|---|---|
committer | Jens Georg <mail@jensge.org> | 2018-12-26 07:44:30 +0100 |
commit | 57dc0d62a6eaf451adc66a56ea7d31d38571029d (patch) | |
tree | 637b2ca993cf94e290a3ea06fd036e511e49557c /tools | |
parent | 3aaa95aa37ec0e555f06f8e6fe94836197315f92 (diff) | |
download | gupnp-57dc0d62a6eaf451adc66a56ea7d31d38571029d.tar.gz |
binding-tool: Fix (most) flake8 warnings
Diffstat (limited to 'tools')
-rwxr-xr-x | tools/gupnp-binding-tool-1.2 | 157 |
1 files changed, 81 insertions, 76 deletions
diff --git a/tools/gupnp-binding-tool-1.2 b/tools/gupnp-binding-tool-1.2 index b6a5e70..b9275f2 100755 --- a/tools/gupnp-binding-tool-1.2 +++ b/tools/gupnp-binding-tool-1.2 @@ -18,22 +18,25 @@ # TODO: # - finish code cleanup -# - currently allowedValueList is not used: could use it to turn +# - currently allowedValueList is not used: could use it to turn # current char* value to an enum -# - could warn if values outside allowedValueRange are used in *_action_set() -# - add option to generate skeleton source code that uses the bindings? +# - could warn if values outside allowedValueRange are used in *_action_set() +# - add option to generate skeleton source code that uses the bindings? -import os.path, re, xml.etree.ElementTree as ET + +import os.path +import re +import xml.etree.ElementTree as ET from optparse import OptionParser -# upnp type: (C type, GType, type for g_value_get_* +# upnp type: (C type, GType, type for g_value_get_* # and g_value_set_*) typemap = { 'ui1': ('guint ', 'G_TYPE_UINT', 'uint'), 'ui2': ('guint ', 'G_TYPE_UINT', 'uint'), 'ui4': ('guint ', 'G_TYPE_UINT', 'uint'), - 'i1': ('gint' , 'G_TYPE_INT', 'int'), + 'i1': ('gint', 'G_TYPE_INT', 'int'), 'i2': ('gint ', 'G_TYPE_INT', 'int'), 'i4': ('gint ', 'G_TYPE_INT', 'int'), 'int': ('gint ', 'G_TYPE_INT', 'int'), @@ -88,7 +91,7 @@ class Variable: self.dummy = False -def camelCaseToLowerCase (str): +def camelCaseToLowerCase(str): # http://www.djangosnippets.org/snippets/585/ tmp = re.sub('(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))', '_\\1', str) lower_case = tmp.lower().strip('_') @@ -99,27 +102,27 @@ def getActions(action_elements, prefix, variables): """ Parse the action element list into a list of Action objects. """ - + actions = [] - + for actionElement in action_elements: a = Action() actions.append(a) a.name = actionElement.find("{urn:schemas-upnp-org:service-1-0}name").text - + if a.name is None: raise Exception("No name found for action") - a.c_name = camelCaseToLowerCase (a.name) + a.c_name = camelCaseToLowerCase(a.name) a.c_prefixed_name = prefix + a.c_name - + for argElement in actionElement.findall("{urn:schemas-upnp-org:service-1-0}argumentList/{urn:schemas-upnp-org:service-1-0}argument"): arg = Argument() arg.name = argElement.find("{urn:schemas-upnp-org:service-1-0}name").text if arg.name is None: raise Exception("No name found for argument") - arg.c_name = camelCaseToLowerCase (arg.name) - + arg.c_name = camelCaseToLowerCase(arg.name) + var_name = argElement.find("{urn:schemas-upnp-org:service-1-0}relatedStateVariable").text for var in variables: if var.name == var_name: @@ -127,14 +130,14 @@ def getActions(action_elements, prefix, variables): break if arg.related_var is None: raise Exception("%s: related state variable %s not found" % (arg.name, var_name)) - + arg.direction = argElement.find("{urn:schemas-upnp-org:service-1-0}direction").text - + if arg.direction == "in": a.in_args.append(arg) else: a.out_args.append(arg) - + return actions @@ -142,67 +145,67 @@ def getVariables(var_elements, prefix): """ Parse the state variable element list into a list of Variable objects. """ - + variables = [] for varElement in var_elements: var = Variable() variables.append(var) - + var.name = varElement.find("{urn:schemas-upnp-org:service-1-0}name").text if var.name.startswith("A_ARG_TYPE_"): # dummy state variable, only there to give type info to getter/setter var.dummy = True - - var.c_name = camelCaseToLowerCase (var.name) + + var.c_name = camelCaseToLowerCase(var.name) var.c_prefixed_name = prefix + var.c_name - + if (varElement.get("sendEvents") == "no"): var.notified = False - + dataType = varElement.find("{urn:schemas-upnp-org:service-1-0}dataType").text - if not dataType in typemap: + if dataType not in typemap: raise Exception("Unknown dataType %s" % dataType) - (var.ctype, var.gtype, g_value_type) = typemap[dataType]; + (var.ctype, var.gtype, g_value_type) = typemap[dataType] var.get_function = "g_value_get_" + g_value_type var.set_function = "g_value_set_" + g_value_type - + return variables def printClientSyncActionBinding(a): - indent = (2 + len (a.c_prefixed_name)) * " " + indent = (2 + len(a.c_prefixed_name)) * " " print("static inline gboolean") print("%s (GUPnPServiceProxy *proxy," % a.c_prefixed_name) - + for arg in a.in_args: print("%sconst %sin_%s," % (indent, arg.related_var.ctype, arg.c_name)) - + for arg in a.out_args: print("%s%s*out_%s," % (indent, arg.related_var.ctype, arg.c_name)) - + print("%sGError **error)" % indent) - + print("{") print(" return gupnp_service_proxy_send_action") print(" (proxy, \"%s\", error," % a.name) - + for arg in a.in_args: print(" \"%s\", %s, in_%s," % (arg.name, arg.related_var.gtype, arg.c_name)) print(" NULL,") - + for arg in a.out_args: print(" \"%s\", %s, out_%s," % (arg.name, arg.related_var.gtype, arg.c_name)) print(" NULL);") - + print("}\n") def printClientAsyncActionBinding(a): # User-callback prototype - indent = (24 + len (a.c_prefixed_name)) * " " + indent = (24 + len(a.c_prefixed_name)) * " " print("typedef void (*%s_reply) (GUPnPServiceProxy *proxy," % a.c_prefixed_name) for arg in a.out_args: print("%sconst %sout_%s," % (indent, arg.related_var.ctype, arg.c_name)) @@ -211,7 +214,7 @@ def printClientAsyncActionBinding(a): print("") # Generated async callback handler, calls user-provided callback - indent = (30 + len (a.c_prefixed_name)) * " " + indent = (30 + len(a.c_prefixed_name)) * " " print("static void _%s_async_callback (GUPnPServiceProxy *proxy," % a.c_prefixed_name) print("%sGUPnPServiceProxyAction *action," % indent) print("%sgpointer user_data)" % indent) @@ -238,7 +241,7 @@ def printClientAsyncActionBinding(a): print("") # Entry point - indent = (8 + len (a.c_prefixed_name)) * " " + indent = (8 + len(a.c_prefixed_name)) * " " print("static inline GUPnPServiceProxyAction *") print("%s_async (GUPnPServiceProxy *proxy," % a.c_prefixed_name) for arg in a.in_args: @@ -264,18 +267,18 @@ def printClientAsyncActionBinding(a): def printClientVariableNotifyBinding(v): - ctype = re.sub ("^gchar", "const gchar", v.ctype); - + ctype = re.sub("^gchar", "const gchar", v.ctype) + # callback prototype - indent = (22 + len (v.c_prefixed_name)) * " " + indent = (22 + len(v.c_prefixed_name)) * " " print("typedef void") print("(*%s_changed_callback) (GUPnPServiceProxy *proxy," % v.c_prefixed_name) print("%s%s%s," % (indent, ctype, v.c_name)) print("%sgpointer userdata);" % indent) print("") - + # private callback - indent = (20 + len (v.c_prefixed_name)) * " " + indent = (20 + len(v.c_prefixed_name)) * " " print("static void") print("_%s_changed_callback (GUPnPServiceProxy *proxy," % v.c_prefixed_name) print("%sconst gchar *variable," % indent) @@ -295,9 +298,9 @@ def printClientVariableNotifyBinding(v): print(" g_slice_free1 (sizeof (*cbdata), cbdata);") print("}") print("") - + # public notify_add function - indent = (13 + len (v.c_prefixed_name)) * " " + indent = (13 + len(v.c_prefixed_name)) * " " print("static inline gboolean") print("%s_add_notify (GUPnPServiceProxy *proxy," % v.c_prefixed_name) print("%s%s_changed_callback callback," % (indent, v.c_prefixed_name)) @@ -320,12 +323,12 @@ def printClientVariableNotifyBinding(v): def printServerVariableQueryBinding(v): # User callback proto - indent = (28 + len (v.ctype)+ len (v.c_prefixed_name)) * " " + indent = (28 + len(v.ctype)+len(v.c_prefixed_name)) * " " print("typedef %s(*%s_query_callback) (GUPnPService *service," % (v.ctype, v.c_prefixed_name)) print("%sgpointer userdata);" % indent) print("") - - indent = (12 + len (v.c_prefixed_name)) * " " + + indent = (12 + len(v.c_prefixed_name)) * " " print("static void") print("_%s_query_cb (GUPnPService *service," % v.c_prefixed_name) print("%sgchar *variable," % indent) @@ -335,8 +338,8 @@ def printServerVariableQueryBinding(v): print(" GUPnPAsyncData *cbdata;") print(" %s%s;" % (v.ctype, v.c_name)) print("") - - indent = (36 + len (v.c_name) + len (v.c_prefixed_name)) * " " + + indent = (36 + len(v.c_name) + len(v.c_prefixed_name)) * " " print(" cbdata = (GUPnPAsyncData *) userdata;") print(" %s = ((%s_query_callback)cbdata->cb) (service," % (v.c_name, v.c_prefixed_name)) print("%scbdata->userdata);" % indent) @@ -344,8 +347,8 @@ def printServerVariableQueryBinding(v): print(" %s (value, %s);" % (v.set_function, v.c_name)) print("}") print("") - - indent = (16 + len (v.c_prefixed_name)) * " " + + indent = (16 + len(v.c_prefixed_name)) * " " print("gulong") print("%s_query_connect (GUPnPService *service," % v.c_prefixed_name) print("%s%s_query_callback callback," % (indent, v.c_prefixed_name)) @@ -369,8 +372,8 @@ def printServerVariableQueryBinding(v): def printServerActionBinding(a): # getter and setter func for GUPnPServiceAction - indent = (13 + len (a.c_prefixed_name)) * " " - if len (a.in_args) > 0: + indent = (13 + len(a.c_prefixed_name)) * " " + if len(a.in_args) > 0: print("static inline void") print("%s_action_get (GUPnPServiceAction *action," % (a.c_prefixed_name)) for arg in a.in_args[:-1]: @@ -383,7 +386,7 @@ def printServerActionBinding(a): print(" NULL);") print("}") print("") - if len (a.out_args) > 0: + if len(a.out_args) > 0: print("static inline void") print("%s_action_set (GUPnPServiceAction *action," % (a.c_prefixed_name)) for arg in a.out_args[:-1]: @@ -396,8 +399,8 @@ def printServerActionBinding(a): print(" NULL);") print("}") print("") - - indent = (17 + len (a.c_prefixed_name)) * " " + + indent = (17 + len(a.c_prefixed_name)) * " " print("static inline gulong") print("%s_action_connect (GUPnPService *service," % a.c_prefixed_name) print("%sGCallback callback," % indent) @@ -410,11 +413,12 @@ def printServerActionBinding(a): print("}") print("") + def PrintServerVariableNotifyBinding(v): - indent = (18 + len (v.c_prefixed_name)) * " " + indent = (18 + len(v.c_prefixed_name)) * " " print("void") print("%s_variable_notify (GUPnPService *service," % v.c_prefixed_name) - print("%sconst %s%s)" % (indent ,v.ctype, v.c_name)) + print("%sconst %s%s)" % (indent, v.ctype, v.c_name)) print("{") print(" gupnp_service_notify (service,") print(" \"%s\", %s, %s," % (v.name, v.gtype, v.c_name)) @@ -422,25 +426,26 @@ def PrintServerVariableNotifyBinding(v): print("}") print("") + def parseSCPD(scpd, prefix): """ Parse the scpd file into lists of Action and Variable objects. """ if prefix != "": prefix = prefix.lower() + "_" - + action_elements = scpd.findall("{urn:schemas-upnp-org:service-1-0}actionList/{urn:schemas-upnp-org:service-1-0}action") var_elements = scpd.findall("{urn:schemas-upnp-org:service-1-0}serviceStateTable/{urn:schemas-upnp-org:service-1-0}stateVariable") - + variables = getVariables(var_elements, prefix) actions = getActions(action_elements, prefix, variables) - + return (actions, variables) def printClientBindings(binding_name, actions, variables): define = "GUPNP_GENERATED_CLIENT_BINDING_%s" % binding_name - + print("/* Generated by gupnp-binding-tool */") print("") print("#include <libgupnp/gupnp.h>") @@ -463,7 +468,7 @@ def printClientBindings(binding_name, actions, variables): if (not v.dummy) and v.notified: print("\n/* state variable %s */\n" % v.name) printClientVariableNotifyBinding(v) - + print("") print("G_END_DECLS") print("") @@ -472,7 +477,7 @@ def printClientBindings(binding_name, actions, variables): def printServerBindings(binding_name, actions, variables): define = "GUPNP_GENERATED_CLIENT_BINDING_%s" % binding_name - + print("/* Generated by gupnp-binding-tool */") print("") print("#include <libgupnp/gupnp.h>") @@ -494,7 +499,7 @@ def printServerBindings(binding_name, actions, variables): print(" g_slice_free1 (sizeof (*cbdata), cbdata);") print("}") print("") - + for a in actions: print("\n/* action %s */\n" % a.name) printServerActionBinding(a) @@ -504,40 +509,40 @@ def printServerBindings(binding_name, actions, variables): printServerVariableQueryBinding(v) if v.notified: PrintServerVariableNotifyBinding(v) - + print("") print("G_END_DECLS") print("") print("#endif /* %s */" % define) -def main (): +def main(): parser = OptionParser("Usage: %prog [options] service-filename") - parser.add_option("-p", "--prefix", dest="prefix", + parser.add_option("-p", "--prefix", dest="prefix", metavar="PREFIX", default="", help="set prefix for generated function names") - parser.add_option("-m", "--mode", type="choice", dest="mode", + parser.add_option("-m", "--mode", type="choice", dest="mode", metavar="MODE", default="client", choices=("client", "server"), help="select generation mode, 'client' or 'server'") - - (options, args) = parser.parse_args() + + (options, args) = parser.parse_args() if len(args) != 1: parser.error("Expected 1 argument, got %d" % len(args)) - + # get a name for header ifdef if options.prefix == "": base = re.sub("[^a-zA-Z0-9]", "_", os.path.basename(args[0])) binding_name = base.upper() else: binding_name = options.prefix.upper() - + # parse scpd file, extract action list and state variable list scpd = ET.parse(args[0]) - (actions, variables) = parseSCPD (scpd, options.prefix) + (actions, variables) = parseSCPD(scpd, options.prefix) if (len(actions) == 0 and len(variables) == 0): - raise Exception ("No actions or variables found in document") - + raise Exception("No actions or variables found in document") + # generate bindings if (options.mode == "client"): printClientBindings(binding_name, actions, variables) |