diff --git a/plugins/util/color.py b/plugins/util/color.py deleted file mode 100644 index e0aaa06..0000000 --- a/plugins/util/color.py +++ /dev/null @@ -1,41 +0,0 @@ -# Colors. Plugin by blha303, color/control id info from http://stackoverflow.com/a/13382032 - -colors = {'white': '0', 'black': '1', 'darkblue': '2', 'darkgreen': '3', - 'red': '4', 'darkred': '5', 'darkviolet': '6', 'orange': '7', - 'yellow': '8', 'lightgreen': '9', 'cyan': '10', 'lightcyan': '11', - 'blue': '12', 'violet': '13', 'darkgray': '14', 'lightgray': '15'} - -control = {'bold': '\x02', 'color': '\x03', 'italic': '\x09', - 'strikethrough': '\x13', 'reset': '\x0f', 'underline': '\x15', - 'underline2': '\x1f', 'reverse': '\x16'} - - -def color(color): - return control['color'] + colors[color] - - -def bold(): - return control['bold'] - - -def italic(): - return control['italic'] - - -def strike(): - return control['strikethrough'] - - -def reset(): - return control['reset'] - - -def underline(other=False): - if other: - return control['underline2'] - else: - return control['underline'] - - -def reverse(): - return control['reverse'] diff --git a/plugins/util/formatting.py b/plugins/util/formatting.py deleted file mode 100644 index 2ab6ba0..0000000 --- a/plugins/util/formatting.py +++ /dev/null @@ -1,36 +0,0 @@ -def raw(format_string): - """Replace based irc formatting""" - stuff = {} - stuff['col'] = {'[white]': '\x030', - '[black]': '\x031', - '[dblue]': '\x032', - '[dgreen]': '\x033', - '[dred]': '\x034', - '[brown]': '\x035', - '[purple]': '\x036', - '[gold]': '\x037', - '[yellow]': '\x038', - '[green]': '\x039', - '[cyan]': '\x0310', - '[lblue]': '\x0311', - '[blue]': '\x0312', - '[pink]': '\x0313', - '[gray]': '\x0314', - '[lgray]': '\x0315', - '[err]': '\x034\x02' - '[/err]': '\x030\x02'} - stuff['style'] = {'[b]': '\x02', - '[clear]': '\x0f'} - stuff['sym'] = {'[point]': '\x07'} - stuff['text'] = {'[url]': 'http://'} - final = {} - for x in stuff: - final.update(stuff[x]) - for x in final: - format_string = format_string.replace(x, final[x]) - return format_string - - -def err(format_string): - """Format the string with standard error styling""" - return "\x034\x02{}\x0f".format(format_string) diff --git a/plugins/util/__init__.py b/util/__init__.py similarity index 100% rename from plugins/util/__init__.py rename to util/__init__.py diff --git a/util/bucket.py b/util/bucket.py new file mode 100644 index 0000000..3a4a699 --- /dev/null +++ b/util/bucket.py @@ -0,0 +1,40 @@ +from time import time + + +class TokenBucket(object): + """An implementation of the token bucket algorithm. + + >>> bucket = TokenBucket(80, 0.5) + >>> print bucket.consume(10) + True + >>> print bucket.consume(90) + False + """ + def __init__(self, tokens, fill_rate): + """tokens is the total tokens in the bucket. fill_rate is the + rate in tokens/second that the bucket will be refilled.""" + self.capacity = float(tokens) + self._tokens = float(tokens) + self.fill_rate = float(fill_rate) + self.timestamp = time() + + def consume(self, tokens): + """Consume tokens from the bucket. Returns True if there were + sufficient tokens otherwise False.""" + if tokens <= self.tokens: + self._tokens -= tokens + else: + return False + return True + + def refill(self): + self._tokens = self.capacity + + def get_tokens(self): + now = time() + if self._tokens < self.capacity: + delta = self.fill_rate * (now - self.timestamp) + self._tokens = min(self.capacity, self._tokens + delta) + self.timestamp = now + return self._tokens + tokens = property(get_tokens) \ No newline at end of file diff --git a/plugins/util/hook.py b/util/hook.py similarity index 100% rename from plugins/util/hook.py rename to util/hook.py diff --git a/plugins/util/http.py b/util/http.py similarity index 100% rename from plugins/util/http.py rename to util/http.py diff --git a/plugins/util/execute.py b/util/pyexec.py similarity index 100% rename from plugins/util/execute.py rename to util/pyexec.py diff --git a/plugins/util/text.py b/util/text.py similarity index 100% rename from plugins/util/text.py rename to util/text.py diff --git a/plugins/util/textgen.py b/util/textgen.py similarity index 100% rename from plugins/util/textgen.py rename to util/textgen.py diff --git a/plugins/util/timeformat.py b/util/timeformat.py similarity index 100% rename from plugins/util/timeformat.py rename to util/timeformat.py diff --git a/plugins/util/timesince.py b/util/timesince.py similarity index 100% rename from plugins/util/timesince.py rename to util/timesince.py diff --git a/plugins/util/urlnorm.py b/util/urlnorm.py similarity index 100% rename from plugins/util/urlnorm.py rename to util/urlnorm.py diff --git a/plugins/util/web.py b/util/web.py similarity index 100% rename from plugins/util/web.py rename to util/web.py