2011-02-10 8 views
0
import unittest 

service = ('u', 'urn:schemas-upnp-org:service:SwitchPower:1') 
binary_light_type = 'urn:schemas-upnp-org:device:BinaryLight:1' 

def on_new_device(dev): 
    """ Callback triggered when a new device is found. 
    """ 
    print 'Got new device:', dev.udn 
    print "Type 'list' to see the whole list" 

    if not dev: 
     return 

def get_switch_service(device): 
    return device.services[service[1]] 

def create_control_point(): 
    """ Creates the control point and binds callbacks to device events. 
    """ 
    c = ControlPoint() 
    c.subscribe('new_device_event', on_new_device) 
    c.subscribe('removed_device_event', on_removed_device) 
    return c 

def main(): 
    """ Main loop iteration receiving input commands. 
    """ 
    c = create_control_point() 
    c.start() 
    run_async_function(_handle_cmds, (c,)) 
    reactor.add_after_stop_func(c.stop) 
    reactor.main() 


def _exit(c): 
    """ Stops the _handle_cmds loop 
    """ 
    global running_handle_cmds 
    running_handle_cmds = False 

def _search(c): 
    """ Start searching for devices of type upnp:rootdevice and repeat 
    search every 600 seconds (UPnP default) 
    """ 
    c.start_search(600, 'upnp:rootdevice') 

def _get_status(c): 
    """ Gets the binary light status and print if it's on or off. 
    """ 
    try: 
     service = get_switch_service(c.current_server) 
     status_response = service.GetStatus() 
     if status_response['ResultStatus'] == '1': 
      print 'Binary light status is on' 
     else: 
      print 'Binary light status is off' 
    except Exception, e: 
     if not hasattr(c, 'current_server') or not c.current_server: 
      print 'BinaryLight device not set.Please use set_light <n>' 
     else: 
      print 'Error in get_status():', e 

def _get_target(c): 
    """ Gets the binary light target and print if it's on or off. 
    """ 
    try: 
     service = get_switch_service(c.current_server) 
     status_response = service.GetTarget() 
     if status_response['RetTargetValue'] == '1': 
      print 'Binary light target is on' 
     else: 
      print 'Binary light target is off' 
    except Exception, e: 
     if not hasattr(c, 'current_server') or not c.current_server: 
      print 'BinaryLight device not set.Please use set_light <n>' 
     else: 
      print 'Error in get_target():', e 
def _stop(c): 
    """ Stop searching 
    """ 
    c.stop_search() 

def _list_devices(c): 
    """ Lists the devices that are in network. 
    """ 
    k = 0 
    for d in c.get_devices().values(): 
     print 'Device no.:', k 
     print 'UDN:', d.udn 
     print 'Name:', d.friendly_name 
     print 'Device type:', d.device_type 
     print 'Services:', d.services.keys() # Only print services name 
     print 'Embedded devices:', [dev.friendly_name for dev in \ 
      d.devices.values()] # Only print embedded devices names 
     print 
     k += 1 

running_handle_cmds = True 
commands = {'exit': _exit, 

      'search': _search, 
      'stop': _stop, 
      'list': _list_devices, 


      'get_status': _get_status, 
      'get_target': _get_target} 


def _handle_cmds(c): 
    while running_handle_cmds: 
     try: 
      input = raw_input('>>> ').strip() 
      if len(input.split(" ")) > 0: 
       try: 
        if len(input.split(" ")) > 1: 
         commands[input.split(" ")[0]](c, input.split(" ")[1]) 
        else: 
         commands[input.split(" ")[0]](c) 
       except KeyError, IndexError: 
        print 'Invalid command, try help' 
       except TypeError: 
        print 'Wrong usage, try help to see' 
     except KeyboardInterrupt, EOFError: 
      c.stop() 
      break 

    # Stops the main loop 
    reactor.main_quit() 

# Here's our "unit tests". 
class IsOddTests(unittest.TestCase): 

    def testOne(self): 
     self.failUnless(_search(c)) 

    def testTwo(self): 
     self.failIf(_search(upnp:rootdevice)) 

def main(): 
    unittest.main() 

このファイルを実行しようとすると、2つのエラーが発生します。上記のコードは、テストケースの場合はwriitenです。Pythonでエラーが発生しています

====================================================================== 
ERROR: testOne (__main__.IsOddTests) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "controlpt.py", line 145, in testOne 
    self.failUnless(_get_status(c)) 
NameError: global name 'c' is not defined 

====================================================================== 
ERROR: testTwo (__main__.IsOddTests) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "controlpt.py", line 148, in testTwo 
    self.failIf(_search(0)) 
    File "controlpt.py", line 55, in _search 
    c.start_search(600, 'upnp:rootdevice') 
AttributeError: 'int' object has no attribute 'start_search' 

---------------------------------------------------------------------- 
Ran 2 tests in 0.000s 

答えて

1

一つ目は単純です:を定義しているもので、それを呼び出します。

2番目の方法も簡単です。start_search属性を持つオブジェクトを使用して呼び出します。

それ以外はわかりません。あなたは、このコードが何であるか、それが何をすべきかを説明していません。

+0

私はこのコードを使用しました。最初のテストのどこで定義するのですか? – user1492832

+0

おそらく 'testOne()'にあります。 –

+0

だから、それは何でしょうか?というのは??私はこのファイルのみを使用していて、pythonのcontrolpt.pyでそれを実行しています...どこを変更すればいいですか? – user1492832

関連する問題