Skip to content

Commit

Permalink
Upgrade to python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
zertrin committed Mar 3, 2024
1 parent 2b84bfc commit 125caba
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 105 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.coverage
.idea
*.pyc
rezoirclogs.egg-info
.venv*
__pycache__
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"flake8.args": [
"--max-line-length=120"
]
}
59 changes: 36 additions & 23 deletions rezoirclogs/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,58 @@

log = logging.getLogger(__name__)


class Base(object):
"""Base resource object, useful for debugging"""
__name__ = ''

__name__ = ""
__parent__ = None

def __repr__(self):
return u'<%s: %s>' % (self.__class__.__name__, self.__name__)
return "<%s: %s>" % (self.__class__.__name__, self.__name__)


def jailed(f):
@wraps(f)
def jailed_f(self, path):
if path.startswith(self.root_path):
return f(self, path)

return jailed_f


class Filesystem(object):
"""Jailed wrapper around os.path"""

def __init__(self, root_path):
self.root_path = os.path.abspath(os.path.normpath(root_path))

join = staticmethod(os.path.join)

@cache_region('short_term')
@cache_region("short_term")
@jailed
def listdir(self, path):
log.debug('listdir %s', path)
log.debug("listdir %s", path)
return os.listdir(path)

@cache_region('short_term')
@cache_region("short_term")
@jailed
def isrealfile(self, path):
log.debug('isrealfile %s', path)
log.debug("isrealfile %s", path)
return os.path.isfile(path) and not os.path.islink(path)

@cache_region('short_term')
@cache_region("short_term")
@jailed
def isrealdir(self, path):
log.debug('isrealdir %s', path)
log.debug("isrealdir %s", path)
return os.path.isdir(path) and not os.path.islink(path)

def open(self, path):
log.debug('open %s', path)
return open(path)
log.debug("open %s", path)
return open(path, mode="rt", encoding='utf-8')

def __str__(self):
return '<Filesystem jailed in %s'% self.root_path
return "<Filesystem jailed in %s" % self.root_path


class LogFile(Base):
Expand All @@ -70,7 +74,7 @@ def __iter__(self):

def neighbour(self, n):
wanted = self.date + datetime.timedelta(days=n)
wanted = wanted.strftime('%Y%m%d')
wanted = wanted.strftime("%Y%m%d")
try:
return self.__parent__[wanted]
except KeyError:
Expand Down Expand Up @@ -99,13 +103,13 @@ def __init__(self, filesystem, path, name):
self.name = name

def _make_logfile(self, path, date):
l = LogFile(self.fs, path, date)
l.__name__ = date
l.__parent__ = self
return l
lf = LogFile(self.fs, path, date)
lf.__name__ = date
lf.__parent__ = self
return lf

def __getitem__(self, date):
name = '%s.%s.log' % (self.name, date)
name = "%s.%s.log" % (self.name, date)
nextpath = self.fs.join(self.path, name)
if self.fs.isrealfile(nextpath):
return self._make_logfile(nextpath, date)
Expand All @@ -116,7 +120,7 @@ def __iter__(self):
if name.startswith(self.name):
path = self.fs.join(self.path, name)
if self.fs.isrealfile(path):
date = name.rsplit('.', 2)[1]
date = name.rsplit(".", 2)[1]
yield self._make_logfile(path, date)

def last(self, n):
Expand Down Expand Up @@ -149,7 +153,12 @@ def __getitem__(self, name):
nextpath = self.fs.join(self.path, name)
if self.fs.isrealdir(nextpath):
return self._make_dir(name)
elif any(file.startswith(name) and file.endswith('.log') and self.fs.isrealfile(self.fs.join(self.path, file)) for file in self.fs.listdir(self.path)):
elif any(
file.startswith(name)
and file.endswith(".log")
and self.fs.isrealfile(self.fs.join(self.path, file))
for file in self.fs.listdir(self.path)
):
return self._make_chan(name)
else:
raise KeyError(name)
Expand All @@ -163,10 +172,14 @@ def dirs(self):

@property
def chans(self):
files = (name for name in sorted(self.fs.listdir(self.path))
if self.fs.isrealfile(self.fs.join(self.path, name)) and name.endswith('.log'))

for chan in set(name.rsplit('.', 2)[0] for name in files):
files = (
name
for name in sorted(self.fs.listdir(self.path))
if self.fs.isrealfile(self.fs.join(self.path, name))
and name.endswith(".log")
)

for chan in set(name.rsplit(".", 2)[0] for name in files):
yield self._make_chan(chan)

def __iter__(self):
Expand Down
24 changes: 12 additions & 12 deletions rezoirclogs/tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_read(self):
import os
import tempfile
f = tempfile.NamedTemporaryFile()
f.write('hello')
f.write(b'hello')
f.flush()
fs = self._make_one(os.path.dirname(f.name))
self.assertEqual(fs.open(f.name).read(), 'hello')
Expand All @@ -71,17 +71,17 @@ def test_read(self):
class LogFileTests(unittest2.TestCase):
def test_iter(self):
class Fs(object):
def open(self, path):
return """01:47 -!- K-Yo [[email protected]] has joined #teamrezo
def open(self, path):
return """01:47 -!- K-Yo [[email protected]] has joined #teamrezo
01:47 <ciblout> kage: demain matin, quand tu veux
01:48 * ciblout mange une pomme""".split('\n')

from rezoirclogs.resources import LogFile
logfile = LogFile(Fs(), '', '20100409')
l = list(logfile)
self.assertEqual(l[0].type, 'status')
self.assertEqual(l[1].type, 'normal')
self.assertEqual(l[2].type, 'me')
lf = list(logfile)
self.assertEqual(lf[0].type, 'status')
self.assertEqual(lf[1].type, 'normal')
self.assertEqual(lf[2].type, 'me')


class ChanTests(unittest2.TestCase):
Expand Down Expand Up @@ -123,10 +123,10 @@ def test_previous_next_chan(self):

def test_last(self):
chan = self._make_one()
l = chan.last(5)
self.assertEqual(len(l), 2)
self.assertEqual(l[0].__name__, '20100204')
self.assertEqual(l[1].__name__, '20100203')
lf = chan.last(5)
self.assertEqual(len(lf), 2)
self.assertEqual(lf[0].__name__, '20100204')
self.assertEqual(lf[1].__name__, '20100203')


class DirectoryTests(unittest2.TestCase):
Expand Down Expand Up @@ -225,7 +225,7 @@ def test_search_logfile_after_date(self):
from rezoirclogs.resources import LogFile
from datetime import date
lf = LogFile(self._get_fs(), '#tagada.20101101.log', '20101101')
result = list(lf.search("flower", date(2012,12,12)))
result = list(lf.search("flower", date(2012, 12, 12)))
self.assertEqual(result, [])


Expand Down
44 changes: 22 additions & 22 deletions rezoirclogs/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
import unittest2


class ConvertUnknowEncodingTests(unittest2.TestCase):
def setUp(self):
self.unicode = u'pâté'
self.utf = 'p\xc3\xa2t\xc3\xa9'
self.latin = 'p\xe2t\xe9'
# class ConvertUnknowEncodingTests(unittest2.TestCase):
# def setUp(self):
# self.str = 'pâté'
# self.utf = 'p\xc3\xa2t\xc3\xa9'
# self.latin = 'p\xe2t\xe9'

def test_utf8(self):
from rezoirclogs.utils import convert_unknow_encoding
self.assertEqual(self.unicode, convert_unknow_encoding(self.utf))
# def test_utf8(self):
# from rezoirclogs.utils import convert_unknow_encoding
# self.assertEqual(self.str, convert_unknow_encoding(self.utf))

def test_latin(self):
from rezoirclogs.utils import convert_unknow_encoding
self.assertEqual(self.unicode, convert_unknow_encoding(self.latin))
# def test_latin(self):
# from rezoirclogs.utils import convert_unknow_encoding
# self.assertEqual(self.str, convert_unknow_encoding(self.latin))


class ParseLogLineTests(unittest2.TestCase):
Expand Down Expand Up @@ -104,22 +104,22 @@ def test_unrecognized(self):
self.assertEqual(str(m), "Ceci n'est pas une ligne de log")

def test_exotic_nicknames(self):
lines = [("20:26 <K-Yo> madjar, \o/", "K-Yo"),
("22:14 <+K-Yo> putain, j'ai la même!", "K-Yo"),
("22:14 <@DaLynX> merci remram", "DaLynX"),
("04:54 <@Zertr1> derns!", "Zertr1"),
("04:54:00 <@Zertr1> derns!", "Zertr1"),
("01:59 < kage> c'est moche les GUI en java", "kage"),
("11:59 <~kage> c'est moche les GUI en java", "kage"),
("11:59 <%zeroNounours> test", "zeroNounours"),
("01:59 <&kage> c'est moche les GUI en java", "kage")]
lines = [("20:26 <K-Yo> madjar, \\o/", "K-Yo"),
("22:14 <+K-Yo> putain, j'ai la même!", "K-Yo"),
("22:14 <@DaLynX> merci remram", "DaLynX"),
("04:54 <@Zertr1> derns!", "Zertr1"),
("04:54:00 <@Zertr1> derns!", "Zertr1"),
("01:59 < kage> c'est moche les GUI en java", "kage"),
("11:59 <~kage> c'est moche les GUI en java", "kage"),
("11:59 <%zeroNounours> test", "zeroNounours"),
("01:59 <&kage> c'est moche les GUI en java", "kage")]

for line, nick in lines:
self.assertEqual(self._get_FUT(line).user, nick, line)


class ColorationTests(unittest2.TestCase):
def test_colored(self):
from jinja2 import Markup
from markupsafe import Markup
from rezoirclogs.utils import colored
self.assertEqual(colored('madjar'), Markup(u'<span style="color:#3176B3">madjar</span>'))
self.assertEqual(colored('madjar'), Markup('<span style="color:#3176B3">madjar</span>'))
2 changes: 1 addition & 1 deletion rezoirclogs/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_range(self):
request = self._get_request()
request.GET['range'] = '2-5'
result = logfile(context, request)
for i in xrange(2, 5):
for i in range(2, 5):
self.assertEqual(result['lines'][i].highlighted, True)

def test_range_buggy_get(self):
Expand Down
35 changes: 19 additions & 16 deletions rezoirclogs/utils.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
from pyramid.decorator import reify
import re
from jinja2 import Markup
from markupsafe import Markup

def convert_unknow_encoding(text):
try:
t = text.decode('utf-8')
except UnicodeDecodeError:
t = text.decode('latin-1')
return t

# def convert_unknow_encoding(text):
# try:
# t = text.decode('utf-8')
# except UnicodeDecodeError:
# t = text.decode('latin-1')
# return t


class lazy(object):
def __init__(self, property):
self.property = property

def __get__(self, inst, objtype=None):
if inst is None: #pragma: nocover
if inst is None: # pragma: nocover
return self
inst.populate()
return getattr(inst, self.property)
Expand All @@ -24,20 +24,19 @@ def __get__(self, inst, objtype=None):
_nick_regex = r"[ @~&+%]?([A-Za-z0-9_`[{}^|\]\\-]*)"


class LogLine(unicode):
class LogLine(str):
"""
The log line informations are populated when an attribute is accessed.
This way, LogLine is lazy and can be used as a normal string in other cases.
"""
def __new__(cls, value):
return unicode.__new__(cls, convert_unknow_encoding(value))
return str.__new__(cls, value)

type = lazy('type')
time = lazy('time')
user = lazy('user')
message = lazy('message')


_regex = [
(re.compile(r"(\d\d:\d\d(?::\d\d)??) <%s> ?(.*)" % _nick_regex), 'normal'),
(re.compile(r"(\d\d:\d\d(?::\d\d)??) *\* %s ?(.*)" % _nick_regex), 'me'),
Expand All @@ -56,24 +55,28 @@ def populate(self):
self.message = self
self.time, self.user = None, None


class ColorPool(object):
colors = [ "#E90C82", "#8E55E9", "#B30E0E", "#16B338", "#58B0B3", "#9D54B3", "#B39675", "#3176B3"]
colors = ["#E90C82", "#8E55E9", "#B30E0E", "#16B338", "#58B0B3", "#9D54B3", "#B39675", "#3176B3"]

def __init__(self):
self.nicks = {}

def get_magic_number(self, nick):
sum = 0
for letter in nick:
sum += ord(letter)
return sum%len(self.colors)
return sum % len(self.colors)

def get_color(self, nick):
if not self.nicks.has_key(nick):
if nick not in self.nicks:
self.nicks[nick] = self.colors[self.get_magic_number(nick)]
return self.nicks[nick]


_colorPool = ColorPool()


def colored(nick):
color = _colorPool.get_color(nick)
return Markup('<span style="color:%s">%s</span>')%(color, nick)
return Markup('<span style="color:%s">%s</span>') % (color, nick)
Loading

0 comments on commit 125caba

Please sign in to comment.