From abcaa25380922d8e0cc7ad83337babc4a5ca46d8 Mon Sep 17 00:00:00 2001 From: Cameron Dale Date: Sat, 8 Dec 2007 20:52:45 -0800 Subject: [PATCH] Clean up all the imports. --- actions.py | 11 +++++------ bencode.py | 6 +++--- const.py | 1 - khashmir.py | 36 +++++++++++++----------------------- knet.py | 6 ++++-- knode.py | 4 +--- krpc.py | 14 +++++--------- ktable.py | 24 +++++++++++------------- node.py | 7 ++++--- setup.py | 3 --- test.py | 5 ----- test_khashmir.py | 17 +++++++++-------- test_krpc.py | 11 +++++++---- 13 files changed, 62 insertions(+), 83 deletions(-) diff --git a/actions.py b/actions.py index db2a777..013a9a7 100644 --- a/actions.py +++ b/actions.py @@ -3,11 +3,10 @@ from time import time -from const import reactor -import const +from twisted.internet import reactor +import const from khash import intify -from ktable import KTable, K class ActionBase: """ base class for some long running asynchronous proccesses like finding nodes or values """ @@ -71,7 +70,7 @@ class FindNode(ActionBase): return l = self.found.values() l.sort(self.sort) - for node in l[:K]: + for node in l[:const.K]: if node.id == self.target: self.finished=1 return self.callback([node]) @@ -87,7 +86,7 @@ class FindNode(ActionBase): if self.outstanding == 0: ## all done!! self.finished=1 - reactor.callLater(0, self.callback, l[:K]) + reactor.callLater(0, self.callback, l[:const.K]) def makeMsgFailed(self, node): def defaultGotNodes(err, self=self, node=node): @@ -160,7 +159,7 @@ class GetValue(FindNode): l = self.found.values() l.sort(self.sort) - for node in l[:K]: + for node in l[:const.K]: if (not self.queried.has_key(node.id)) and node.id != self.table.node.id: #xxx t.timeout = time.time() + GET_VALUE_TIMEOUT try: diff --git a/bencode.py b/bencode.py index 93af407..b3f9a68 100644 --- a/bencode.py +++ b/bencode.py @@ -2,10 +2,10 @@ # see LICENSE.txt for license information from types import IntType, LongType, StringType, ListType, TupleType, DictType -import re +from re import compile from cStringIO import StringIO -int_filter = re.compile('(0|-?[1-9][0-9]*)e') +int_filter = compile('(0|-?[1-9][0-9]*)e') def decode_int(x, f): m = int_filter.match(x, f) @@ -13,7 +13,7 @@ def decode_int(x, f): raise ValueError return (long(m.group(1)), m.end()) -string_filter = re.compile('(0|[1-9][0-9]*):') +string_filter = compile('(0|[1-9][0-9]*):') def decode_string(x, f): m = string_filter.match(x, f) diff --git a/const.py b/const.py index fc5d1c0..58d539e 100644 --- a/const.py +++ b/const.py @@ -16,7 +16,6 @@ try: except IOError: print "no resolv.conf!" """ -from twisted.internet import reactor # magic id to use before we know a peer's id NULL_ID = 20 * '\0' diff --git a/khashmir.py b/khashmir.py index f585e16..2918036 100644 --- a/khashmir.py +++ b/khashmir.py @@ -1,31 +1,21 @@ ## Copyright 2002-2004 Andrew Loewenstern, All Rights Reserved # see LICENSE.txt for license information -from const import reactor -import const - -import time - -from sha import sha +from time import time +from random import randrange +import sqlite ## find this at http://pysqlite.sourceforge.net/ -from ktable import KTable, K -from knode import * +from twisted.internet.defer import Deferred +from twisted.internet import protocol +from twisted.internet import reactor +import const +from ktable import KTable +from knode import KNodeBase, KNodeRead, KNodeWrite from khash import newID, newIDInRange - from actions import FindNode, GetValue, KeyExpirer, StoreValue import krpc -from twisted.internet.defer import Deferred -from twisted.internet import protocol -from twisted.application import service, internet -from twisted.web import server -import sys - -from random import randrange - -import sqlite ## find this at http://pysqlite.sourceforge.net/ - class KhashmirDBExcept(Exception): pass @@ -44,7 +34,7 @@ class KhashmirBase(protocol.Factory): self.udp = krpc.hostbroker(self) self.udp.protocol = krpc.KRPC self.listenport = reactor.listenUDP(port, self.udp) - self.last = time.time() + self.last = time() self._loadRoutingTable() KeyExpirer(store=self.store) self.refreshTable(force=1) @@ -173,7 +163,7 @@ class KhashmirBase(protocol.Factory): method needs to be a properly formed Node object with a valid ID. """ old = self.table.insertNode(n, contacted=contacted) - if old and (time.time() - old.lastSeen) > const.MIN_PING_INTERVAL and old.id != self.node.id: + if old and (time() - old.lastSeen) > const.MIN_PING_INTERVAL and old.id != self.node.id: # the bucket is full, check to see if old node is still around and if so, replace it ## these are the callbacks used when we ping the oldest node in a bucket @@ -231,7 +221,7 @@ class KhashmirBase(protocol.Factory): pass for bucket in self.table.buckets: - if force or (time.time() - bucket.lastAccessed >= const.BUCKET_STALENESS): + if force or (time() - bucket.lastAccessed >= const.BUCKET_STALENESS): id = newIDInRange(bucket.min, bucket.max) self.findNode(id, callback) @@ -338,7 +328,7 @@ class KhashmirWrite(KhashmirRead): self.findNode(key, _storeValueForKey) def krpc_store_value(self, key, value, id, _krpc_sender): - t = "%0.6f" % time.time() + t = "%0.6f" % time() c = self.store.cursor() try: c.execute("insert into kv values (%s, %s, %s);", (sqlite.encode(key), sqlite.encode(value), t)) diff --git a/knet.py b/knet.py index e974a79..eb9215c 100644 --- a/knet.py +++ b/knet.py @@ -3,11 +3,13 @@ # create a network of khashmir nodes # usage: knet.py -from khashmir import Khashmir -from twisted.internet import reactor from random import randrange import sys, os +from twisted.internet import reactor + +from khashmir import Khashmir + class Network: def __init__(self, size=0, startport=5555, localip='127.0.0.1'): self.num = size diff --git a/knode.py b/knode.py index 6e4f4e3..d2cea72 100644 --- a/knode.py +++ b/knode.py @@ -1,10 +1,8 @@ ## Copyright 2002-2004 Andrew Loewenstern, All Rights Reserved # see LICENSE.txt for license information +from const import NULL_ID from node import Node -from twisted.internet.defer import Deferred -from const import reactor, NULL_ID - class KNodeBase(Node): def checkSender(self, dict): diff --git a/krpc.py b/krpc.py index b4dd6e4..8a60092 100644 --- a/krpc.py +++ b/krpc.py @@ -1,18 +1,14 @@ ## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved # see LICENSE.txt for license information -from twisted.internet.defer import Deferred -from twisted.protocols import basic from bencode import bencode, bdecode -from twisted.internet import protocol - -from twisted.internet import reactor -import time - +from time import asctime import sys from traceback import format_exception -import khash as hash +from twisted.internet.defer import Deferred +from twisted.internet import protocol +from twisted.internet import reactor KRPC_TIMEOUT = 20 @@ -111,7 +107,7 @@ class KRPC: olen = len(out) self.transport.write(out, addr) if self.noisy: - print "%s %s >>> %s - %s %s %s" % (time.asctime(), addr, self.factory.node.port, + print "%s %s >>> %s - %s %s %s" % (asctime(), addr, self.factory.node.port, ilen, msg[REQ], olen) elif msg[TYP] == RSP: # if response diff --git a/ktable.py b/ktable.py index b2cd6f3..e0ff8df 100644 --- a/ktable.py +++ b/ktable.py @@ -1,13 +1,11 @@ ## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved # see LICENSE.txt for license information -import time -from bisect import * -from types import * +from time import time +from bisect import bisect_left -import khash as hash -import const -from const import K, HASH_LENGTH, NULL_ID +from const import K, HASH_LENGTH, NULL_ID, MAX_FAILURES +import khash from node import Node class KTable: @@ -28,7 +26,7 @@ class KTable: """ if isinstance(id, str): - num = hash.intify(id) + num = khash.intify(id) elif isinstance(id, Node): num = id.num elif isinstance(id, int) or isinstance(id, long): @@ -166,7 +164,7 @@ class KTable: except IndexError: return None else: - if n.msgFailed() >= const.MAX_FAILURES: + if n.msgFailed() >= MAX_FAILURES: self.invalidateNode(n) class KBucket: @@ -174,10 +172,10 @@ class KBucket: self.l = contents self.min = min self.max = max - self.lastAccessed = time.time() + self.lastAccessed = time() def touch(self): - self.lastAccessed = time.time() + self.lastAccessed = time() def getNodeWithInt(self, num): if num in self.l: return num @@ -214,11 +212,11 @@ import unittest class TestKTable(unittest.TestCase): def setUp(self): - self.a = Node().init(hash.newID(), 'localhost', 2002) + self.a = Node().init(khash.newID(), 'localhost', 2002) self.t = KTable(self.a) def testAddNode(self): - self.b = Node().init(hash.newID(), 'localhost', 2003) + self.b = Node().init(khash.newID(), 'localhost', 2003) self.t.insertNode(self.b) self.assertEqual(len(self.t.buckets[0].l), 1) self.assertEqual(self.t.buckets[0].l[0], self.b) @@ -230,7 +228,7 @@ class TestKTable(unittest.TestCase): def testFail(self): self.testAddNode() - for i in range(const.MAX_FAILURES - 1): + for i in range(MAX_FAILURES - 1): self.t.nodeFailed(self.b) self.assertEqual(len(self.t.buckets[0].l), 1) self.assertEqual(self.t.buckets[0].l[0], self.b) diff --git a/node.py b/node.py index f8d2801..35dadc6 100644 --- a/node.py +++ b/node.py @@ -1,9 +1,10 @@ ## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved # see LICENSE.txt for license information +from time import time +from types import InstanceType + import khash -import time -from types import * class Node: """encapsulate contact info""" @@ -29,7 +30,7 @@ class Node: return self def updateLastSeen(self): - self.lastSeen = time.time() + self.lastSeen = time() self.fails = 0 def msgFailed(self): diff --git a/setup.py b/setup.py index 52d6c52..fcdb4a4 100755 --- a/setup.py +++ b/setup.py @@ -1,8 +1,5 @@ #!/usr/bin/env python -import os -import sys - try: import distutils.core import distutils.command.build_ext diff --git a/test.py b/test.py index 633d85c..9cf621d 100644 --- a/test.py +++ b/test.py @@ -3,10 +3,5 @@ import unittest -import ktable, khashmir -import hash, node, knode -import actions -import btemplate -import test_krpc tests = unittest.defaultTestLoader.loadTestsFromNames(['hash', 'node', 'knode', 'actions', 'ktable', 'test_krpc']) result = unittest.TextTestRunner().run(tests) diff --git a/test_khashmir.py b/test_khashmir.py index e9c426a..24a8760 100644 --- a/test_khashmir.py +++ b/test_khashmir.py @@ -1,11 +1,12 @@ -from unittest import * -from khashmir import * -import khash -from copy import copy - +from unittest import defaultTestLoader, TextTestRunner, TestCase +from sha import sha from random import randrange +import os, sys + +from twisted.internet import reactor -import os +from khashmir import Khashmir +from khash import newID if __name__ =="__main__": tests = defaultTestLoader.loadTestsFromNames([sys.argv[0][:-3]]) @@ -113,8 +114,8 @@ class MultiTest(TestCase): def testStoreRetrieve(self): for i in range(10): - K = khash.newID() - V = khash.newID() + K = newID() + V = newID() for a in range(3): self.done = 0 diff --git a/test_krpc.py b/test_krpc.py index c068408..3c41d07 100644 --- a/test_krpc.py +++ b/test_krpc.py @@ -1,12 +1,15 @@ ## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved # see LICENSE.txt for license information -from unittest import * -from krpc import * +from unittest import defaultTestLoader, TestCase, TextTestRunner +import sys -KRPC.noisy = 0 +from twisted.internet import protocol +from twisted.internet import reactor -import sys +from krpc import KRPC, hostbroker, KRPC_ERROR_METHOD_UNKNOWN + +KRPC.noisy = 0 if __name__ =="__main__": tests = defaultTestLoader.loadTestsFromNames([sys.argv[0][:-3]]) -- 2.39.5