import sys
from lib.bot import *
from lib.utils import *
from lib.event import *
from lib.cache import *
from cymrulib import *

MINIMUM_BGP_PREFIX_IPV4 = 24
MINIMUM_BGP_PREFIX_IPV6 = 128 # FIXME

class CymruExpertBot(Bot):
    
    def init(self):
        self.cache = Cache(
                            self.parameters.cache_host,
                            self.parameters.cache_port,
                            self.parameters.cache_id,
                            self.parameters.cache_ttl
                          )

    
    def process(self):
        event = self.receive_message()
        
        if not event:
            self.acknowledge_message()
            return
            
        if not event.contains("ip"):
            self.send_message(event)
            self.acknowledge_message()
            return
            
        ip, ip_version, ip_integer = is_ip(event.value("ip"))

        if not ip:
            self.send_message(event)
            self.acknowledge_message()
            return
        
        if ip_version == 4:
            cache_key = bin(ip_integer)[2 : MINIMUM_BGP_PREFIX_IPV4 + 2]
        else:
            cache_key = bin(ip_integer)[2 : MINIMUM_BGP_PREFIX_IPV6 + 2]
        
        query_result = self.cache.get(cache_key)
        
        if not query_result:
            query_result = Cymru.query(ip, ip_version)
            self.cache.set(cache_key, query_result)
        
        asn, bgp, cc, registry, allocated, as_name = Cymru.parse(query_result)

        event.clear('cymru_asn')
        event.clear('cymru_bgp_prefix')
        event.clear('cymru_cc')
        event.clear('cymru_registry')
        event.clear('cymru_allocated')
        event.clear('cymru_as name')

        event.add('cymru_asn', asn)
        event.add('cymru_bgp_prefix', bgp)
        event.add('cymru_cc', cc)
        event.add('cymru_registry', registry)
        event.add('cymru_allocated', allocated)
        event.add('cymru_as_name', as_name)

        self.send_message(event)
        self.acknowledge_message()
        
if __name__ == "__main__":
    bot = CymruExpertBot(sys.argv[1])
    bot.start()

@@ -1573,22 +1573,16 @@
 .clear('
-cymru_
 asn')%0D%0A 
@@ -1601,22 +1601,16 @@
 .clear('
-cymru_
 bgp_pref
@@ -1636,24 +1636,24 @@
 .clear('
-cymru_cc
+registry
 ')%0D%0A    
@@ -1669,30 +1669,25 @@
 .clear('
-cymru_registry
+allocated
 ')%0D%0A    
@@ -1703,31 +1703,23 @@
 .clear('
-cymru_allocated
+as_name
 ')%0D%0A    
@@ -1745,15 +1745,10 @@
 mru_
-as name
+cc
 ')%0D%0A
@@ -1768,22 +1768,16 @@
 nt.add('
-cymru_
 asn', as
@@ -1799,22 +1799,16 @@
 nt.add('
-cymru_
 bgp_pref
@@ -1841,49 +1841,8 @@
 dd('
-cymru_cc', cc)%0D%0A        event.add('cymru_
 regi
@@ -1878,22 +1878,16 @@
 nt.add('
-cymru_
 allocate
@@ -1921,22 +1921,16 @@
 nt.add('
-cymru_
 as_name'
@@ -1940,16 +1940,51 @@
 s_name)%0D
+%0A        event.add('cymru_cc', cc)%0D
 %0A%0D%0A     

Update cymru.py