Commit 55d0e3d3 authored by Lysander Trischler's avatar Lysander Trischler
Browse files

Implement tags

parent b42f338f
......@@ -109,19 +109,22 @@ class _Subject(ctypes.Structure):
class Twt:
def __init__(self, twter=None, created=None, hash=None, subject=None, mentions=None, links=None):
def __init__(self, twter=None, created=None, hash=None,
subject=None, mentions=None, links=None,
tags=None):
self.twter = twter
self.created = created
self.hash = hash
self.subject = subject
self.mentions = mentions
self.links = links
self.tags = tags
def __repr__(self):
return f"{self.__class__.__name__}<twter={self.twter!r} " + \
f"created={self.created!r} hash={self.hash!r} " + \
f"subject={self.subject!r} mentions={self.mentions!r} " + \
f"links={self.links!r}>"
f"links={self.links!r} tags={self.tags!r}>"
def __eq__(self, other):
return isinstance(other, self.__class__) and \
......@@ -130,7 +133,8 @@ class Twt:
self.hash == other.hash and \
self.subject == other.subject and \
self.mentions == other.mentions and \
self.links == other.links
self.links == other.links and \
self.tags == other.tags
class _Twt(ctypes.Structure):
......@@ -141,7 +145,9 @@ class _Twt(ctypes.Structure):
("mentions", ctypes.POINTER(ctypes.POINTER(_Twter))),
("mentions_len", ctypes.c_int),
("links", ctypes.POINTER(ctypes.POINTER(_Link))),
("links_len", ctypes.c_int)]
("links_len", ctypes.c_int),
("tags", ctypes.POINTER(ctypes.POINTER(_Link))),
("tags_len", ctypes.c_int)]
def to_python(self):
twt = Twt()
......@@ -162,6 +168,10 @@ class _Twt(ctypes.Structure):
for i in range(self.links_len):
twt.links.append(self.links[i].contents.to_python())
twt.tags = []
for i in range(self.tags_len):
twt.tags.append(self.tags[i].contents.to_python())
return twt
......
......@@ -29,7 +29,8 @@ struct twt {
int mentions_len;
struct link *links;
int links_len;
//struct link *tags[];
struct link *tags;
int tags_len;
};
struct twt_file {
......@@ -160,6 +161,14 @@ func convertTwt(t types.Twt) *C.struct_twt {
twt.links = (*C.struct_link)(linksPtr)
twt.links_len = C.int(len(t.Links()))
tagsPtr := C.malloc(C.size_t(len(t.Tags())) * C.size_t(unsafe.Sizeof(uintptr(0))))
tags := (*[1<<30 - 1]*C.struct_link)(tagsPtr)
for i, tag := range t.Tags() {
tags[i] = convertLink(tag)
}
twt.tags = (*C.struct_link)(tagsPtr)
twt.tags_len = C.int(len(t.Tags()))
return twt
}
......@@ -171,6 +180,7 @@ func freeTwt(t *C.struct_twt) {
freeSubject(t.subject)
freeTwters(t.mentions, t.mentions_len)
freeLinks(t.links, t.links_len)
freeLinks(t.tags, t.tags_len)
C.free(unsafe.Pointer(t))
}
}
......
......@@ -19,7 +19,8 @@ class ParseFileTest(unittest.TestCase):
hash="slrnx6a",
subject=Subject(text="#slrnx6a", tag=Link(text="slrnx6a", target=None)),
mentions=[],
links=[])]), twtfile)
links=[],
tags=[])]), twtfile)
def test_two_lines(self):
twtfile = parse_file(
......@@ -32,13 +33,15 @@ class ParseFileTest(unittest.TestCase):
hash="slrnx6a",
subject=Subject(text="#slrnx6a", tag=Link(text="slrnx6a", target=None)),
mentions=[],
links=[]),
links=[],
tags=[]),
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)
links=[],
tags=[])]), twtfile)
def test_markdown_links(self):
twtfile = parse_file(
......@@ -52,7 +55,8 @@ class ParseFileTest(unittest.TestCase):
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)
Link(text="nice", target="https://example.com/test")],
tags=[])]), twtfile)
def test_subjects(self):
twtfile = parse_file(
......@@ -66,13 +70,15 @@ class ParseFileTest(unittest.TestCase):
subject=Subject(text="#<1234567 https://example.com/1234567>",
tag=Link(text="1234567", target="https://example.com/1234567")),
mentions=[],
links=[]),
links=[],
tags=[Link(text="1234567", target="https://example.com/1234567")]),
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)
links=[],
tags=[])]), twtfile)
def test_mentions(self):
twtfile = parse_file(
......@@ -87,7 +93,21 @@ class ParseFileTest(unittest.TestCase):
url="https://example.org/~eugen/twtxt.txt",
avatar="",
tagline="")],
links=[])]), twtfile)
links=[],
tags=[])]), twtfile)
def test_tags(self):
twtfile = parse_file(
"2021-08-03T16:26:53+02:00\tHave a look at #<eggs https://example.com/eggs>!",
TWTER)
self.assertEqual(TwtFile(twter=TWTER, twts=[
Twt(twter=TWTER,
created=datetime.datetime(2021, 8, 3, 16, 26, 53, tzinfo=UTC_PLUS_2),
hash="5thpeka",
subject=Subject(text="#5thpeka", tag=Link(text="5thpeka", target=None)),
mentions=[],
links=[],
tags=[Link(text="eggs", target="https://example.com/eggs")])]), twtfile)
if __name__ == "__main__":
......
Supports Markdown
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