Commit 7e07dabd authored by Lysander Trischler's avatar Lysander Trischler

Support twt mentions

parent cdc4c41c
Pipeline #134 failed with stages
in 0 seconds
......@@ -82,11 +82,12 @@ class _Subject(ctypes.Structure):
class Twt:
def __init__(self, twter=None, created=None, hash=None, subject=None, links=None):
def __init__(self, twter=None, created=None, hash=None, subject=None, mentions=None, links=None):
self.twter = twter
self.created = created
self.hash = hash
self.subject = subject
self.mentions = mentions
self.links = links
......@@ -95,6 +96,8 @@ class _Twt(ctypes.Structure):
("created", ctypes.c_char_p),
("hash", ctypes.c_char_p),
("subject", ctypes.POINTER(_Subject)),
("mentions", ctypes.POINTER(ctypes.POINTER(_Twter))),
("mentions_len", ctypes.c_int),
("links", ctypes.POINTER(ctypes.POINTER(_Link))),
("links_len", ctypes.c_int)]
......@@ -109,6 +112,10 @@ class _Twt(ctypes.Structure):
if self.subject is not None:
twt.subject = self.subject.contents.to_python()
twt.mentions = []
for i in range(self.mentions_len):
twt.mentions.append(self.mentions[i].contents.to_python())
twt.links = []
for i in range(self.links_len):
twt.links.append(self.links[i].contents.to_python())
......
......@@ -25,7 +25,8 @@ struct twt {
char *created;
char *hash;
struct subject *subject;
//struct twter *mentions[];
struct twter *mentions;
int mentions_len;
struct link *links;
int links_len;
//struct link *tags[];
......@@ -121,6 +122,16 @@ func freeTwter(t *C.struct_twter) {
}
}
func freeTwters(t *C.struct_twter, length C.int) {
if t != nil {
twters := (*[1<<30 - 1]*C.struct_twter)(unsafe.Pointer(t))
for i := 0; i < int(length); i++ {
freeTwter(twters[i])
}
C.free(unsafe.Pointer(t))
}
}
func convertTwt(t types.Twt) *C.struct_twt {
ptr := C.malloc(C.sizeof_struct_twt)
twt := (*C.struct_twt)(ptr)
......@@ -129,7 +140,17 @@ func convertTwt(t types.Twt) *C.struct_twt {
twt.hash = C.CString(t.Hash())
if subject, ok := t.Subject().(*lextwt.Subject); ok && subject != nil {
twt.subject = convertSubject(subject)
} else {
twt.subject = nil
}
mentionsPtr := C.malloc(C.size_t(len(t.Mentions())) * C.size_t(unsafe.Sizeof(uintptr(0))))
mentions := (*[1<<30 - 1]*C.struct_twter)(mentionsPtr)
for i, mention := range t.Mentions() {
mentions[i] = convertTwter(mention.Twter())
}
twt.mentions = (*C.struct_twter)(mentionsPtr)
twt.mentions_len = C.int(len(t.Mentions()))
linksPtr := C.malloc(C.size_t(len(t.Links())) * C.size_t(unsafe.Sizeof(uintptr(0))))
links := (*[1<<30 - 1]*C.struct_link)(linksPtr)
......@@ -148,6 +169,7 @@ func freeTwt(t *C.struct_twt) {
free_string(t.created)
free_string(t.hash)
freeSubject(t.subject)
freeTwters(t.mentions, t.mentions_len)
freeLinks(t.links, t.links_len)
C.free(unsafe.Pointer(t))
}
......@@ -169,6 +191,8 @@ func convertSubject(s types.Subject) *C.struct_subject {
subject.text = C.CString(s.Text())
if tag, ok := s.Tag().(*lextwt.Tag); ok && tag != nil {
subject.tag = convertTag(tag)
} else {
subject.tag = nil
}
return subject
}
......
......@@ -20,6 +20,7 @@ class ParseFileTest(unittest.TestCase):
created=datetime.datetime(2021, 8, 2, 10, 27, 42, tzinfo=UTC_PLUS_2),
hash="slrnx6a",
subject=Subject(text="#slrnx6a", tag=Link(text="slrnx6a", target=None)),
mentions=[],
links=[]),
twtfile.twts[0])
......@@ -36,12 +37,14 @@ class ParseFileTest(unittest.TestCase):
created=datetime.datetime(2021, 8, 2, 10, 27, 42, tzinfo=UTC_PLUS_2),
hash="slrnx6a",
subject=Subject(text="#slrnx6a", tag=Link(text="slrnx6a", target=None)),
mentions=[],
links=[]),
twtfile.twts[0], "first twt does not match")
self.assertTwtEqual(Twt(twter=TWTER,
created=datetime.datetime(2021, 8, 3, 9, 28, 45, tzinfo=UTC_PLUS_2),
hash="zm7fnka",
subject=Subject(text="#zm7fnka", tag=Link(text="zm7fnka", target=None)),
mentions=[],
links=[]),
twtfile.twts[1], "second twt does not match")
......@@ -58,6 +61,7 @@ class ParseFileTest(unittest.TestCase):
created=datetime.datetime(2021, 8, 3, 11, 16, 13, tzinfo=UTC_PLUS_2),
hash="zv6vujq",
subject=Subject(text="#zv6vujq", tag=Link(text="zv6vujq", target=None)),
mentions=[],
links=[Link(text="wonderful", target="https://example.com/"),
Link(text="nice", target="https://example.com/test")]),
twtfile.twts[0])
......@@ -75,15 +79,35 @@ class ParseFileTest(unittest.TestCase):
hash="g2xdgsq",
subject=Subject(text="#<1234567 https://example.com/1234567>",
tag=Link(text="1234567", target="https://example.com/1234567")),
mentions=[],
links=[]),
twtfile.twts[0], "first twt does not match")
self.assertTwtEqual(Twt(twter=TWTER,
created=datetime.datetime(2021, 8, 3, 12, 33, 17, tzinfo=UTC_PLUS_2),
hash="wqw7ipq",
subject=Subject(text="re: foo", tag=None),
mentions=[],
links=[]),
twtfile.twts[1], "second twt does not match")
def test_mentions(self):
twtfile = parse_file(
"2021-08-03T15:04:09+02:00\tHello @<eugen https://example.org/~eugen/twtxt.txt>!",
TWTER)
self.assertIsNotNone(twtfile, "parse_file returned None")
self.assertTwterEqual(TWTER, twtfile.twter, "twter of twt file does not match")
self.assertEqual(1, len(twtfile.twts), "number of twts does not match")
self.assertTwtEqual(Twt(twter=TWTER,
created=datetime.datetime(2021, 8, 3, 15, 4, 9, tzinfo=UTC_PLUS_2),
hash="vu5np6q",
subject=Subject(text="#vu5np6q", tag=Link(text="vu5np6q", target=None)),
mentions=[Twter(nick="eugen",
url="https://example.org/~eugen/twtxt.txt",
avatar="",
tagline="")],
links=[]),
twtfile.twts[0])
def msg(self, message_prefix):
"""Construct a message factory using the prefix, if present."""
......@@ -109,6 +133,14 @@ class ParseFileTest(unittest.TestCase):
self.assertEqual(expected.hash, actual.hash, msg("twt hash does not match"))
self.assertSubjectEqual(expected.subject, actual.subject, "twt subject does not match")
if expected.mentions is None:
self.assertIsNone(actual.mentions, "twt mentions do not match")
else:
self.assertIsNotNone(actual.mentions, "twt mentions do not match")
self.assertEqual(len(expected.mentions), len(actual.mentions), "number of twt mentions does not match")
for i in range(len(expected.mentions)):
self.assertTwterEqual(expected.mentions[i], actual.mentions[i], "twt mention at index %d does not match" % i)
if expected.links is None:
self.assertIsNone(actual.links, "twt links do not match")
else:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment