kbr / fritzconnection

Python-Tool to communicate with the AVM Fritz!Box by the TR-064 protocol and the AHA-HTTP-Interface
MIT License
304 stars 59 forks source link

Question: what is the replacement for print_api? #23

Closed WolfgangFahl closed 4 years ago

WolfgangFahl commented 4 years ago

print_api has moved in version 1.0 and i had to comment it out. What is the replacement?

kbr commented 4 years ago

Inspection can be done by the command line. There is also an enhanced documentation out now: https://fritzconnection.readthedocs.io/

WolfgangFahl commented 4 years ago

You might want to readd a simpler solution like this. I didn't get any of the command line stuff working and didn't bother to. I just need the API:

def print_api(self):
        for service_name,service in self.fc.services.items():
            for action_name,action in service.actions.items():
                print("'%s','%s'" % (service_name,action_name))
                for argument in action.arguments.values():
                    if argument.direction == 'in':
                        direction = '-> in'
                    else:
                        direction = '   out ->'
                        var = service.state_variables.get(argument.relatedStateVariable, '')
                    line = f'    {argument.name:30}{direction:14}{var.dataType}'
                    print(line)
WolfgangFahl commented 4 years ago

While at it i experimented with the code-generation approach below. With the result e.g. in "tr064.py" you can code like this:

from tr064 import WANCommonIFC1
ifc1=WANCommonIFC1(fritz.fc)
ifc1.GetTotalBytesSent()
print (ifc1.NewTotalBytesSent)
    def generate_apiclasses(self):
        for service_name,service in self.fc.services.items():
            print ("class %s(): " % (self.fixname(service_name)))
            print ("    def __init__(self,fc):")
            print ("        self.fc=fc")
            print ("")
            for action_name,action in service.actions.items():
                params="self"
                args="{"
                delim=""
                pdelim=","
                outargs=0
                for argument in action.arguments.values():
                    var = service.state_variables.get(argument.relatedStateVariable, '')
                    if argument.direction == 'in':
                        args+="%s'%s':%s" % (delim,argument.name,self.fixname(argument.name))
                        params+="%s%s" % (pdelim,self.fixname(argument.name))   
                    else:
                        outargs+=1     
                    delim=","
                    pdelim=","
                args+="}" 
                action_name=self.fixname(action_name)
                print("    def %s(%s):" % (action_name,params))
                if outargs>0:
                    print("        l%s=self.fc.call_action('%s','%s',arguments=%s)" % (action_name,service_name,action_name,args))   
                else:    
                    print("        self.fc.call_action('%s','%s',arguments=%s)" % (service_name,action_name,args))   
                for argument in action.arguments.values():
                    var = service.state_variables.get(argument.relatedStateVariable, '')
                    if argument.direction == 'out':
                        print ("        self.%s=l%s['%s'] # %s" % (self.fixname(argument.name),action_name,argument.name,var.dataType))
                print("")
kbr commented 4 years ago

Currently there is no replacement. Maybe the function comes back in a later version. Because of lack of traffic I like to close this issue.