+from csv import DictReader, DictWriter, excel_tab
from dataclasses import dataclass
+from pathlib import Path
+from typing import Optional
+from nom.utils import NomError
-# TODO: This should probably use Pydantic.
+# TODO: Use proper types, not strings. (Pydantic?)
@dataclass
-class Entry:
+class EntryListItem:
+ id_: str
title: str
- url: str
- updated: str # TODO: Make this datetime when I add filters
- # summary: str # TODO: Add this when you feel like stripping HTML
+ url: Optional[str] = None
+ date: Optional[str] = "test"
+ feed_url: Optional[str] = None
+ feed_alias: Optional[str] = None
+ viewed: Optional[bool] = False
+ summary: Optional[str] = None # TODO: Add this when you feel like stripping HTML
# TODO: What if there's a pipe in one of the fields?
- def write_line(self, delimiter: str ='|'):
- return delimiter.join([self.title, self.url, self.updated])
\ No newline at end of file
+ def to_str(self, delimiter: str ='|'):
+ # values = [value for value in self.__dict__.values()]
+ return delimiter.join([self.title, self.url, self.date])
+
+ def to_dict(self):
+ return self.__dict__
+
+ @classmethod
+ def from_dict(cls, dct: dict):
+ return cls(**dct)
+
+
+class EntryList:
+
+ def __init__(self, delimiter='|'):
+ self.entries : list[EntryListItem] = []
+ self.dicts : list[dict] = []
+ self.delimiter = delimiter
+ self.fieldnames = EntryListItem("","").to_dict().keys()
+
+ def add_entry(self, entry):
+ self.entries.append(entry)
+
+ # TODO: "Append" doesn't feel right.
+ def append_feed(self,feed):
+ pass
+
+ def from_file(self, file: Path):
+ pass
+
+ def to_file(self, file: Path):
+ if not self.dicts:
+ raise NomError("There are no entries to write.")
+
+ with open(file, "w") as f:
+ dialect = excel_tab
+ dialect.delimiter="|"
+ writer = DictWriter(f, fieldnames=self.fieldnames, dialect=dialect)
+ writer.writeheader()
+ writer.writerows(self.dicts)
+
+
+if __name__ == "__main__":
+ dct = dict(id_="1", title="Entry One", url="https://path/to/entry1.html")
+ path=Path("/home/kyle/projects/nom/tests/data/entry.csv")
+ elist = EntryList()
+ entry = EntryListItem.from_dict(dct)
+ elist.add_entry(entry)
+ elist.dicts = [entry.__dict__ for entry in elist.entries]
+ elist.to_file(path)
import feedparser
import requests
-from nom.entry import Entry
+from nom.entry import EntryListItem
from nom.utils import url2filename
self.d = d
self.name = d.feed.title
self.url = url # how is this different from d.feed.link?
- self.entries : list[Entry] = [
- Entry(
+ self.entries : list[EntryListItem] = [
+ EntryListItem(
e.title,
e.link,
e.updated
def to_stdout(self, file: Optional[Path]=None):
for entry in self.entries:
if entry:
- print(entry.write_line())
+ print(entry.to_str())
class FeedList:
--- /dev/null
+from tempfile import NamedTemporaryFile
+
+from nom.entry import EntryListItem
+
+e1=EntryListItem(
+ id_="1",
+ title="Entry One",
+ url="https://path/to/entry1.html",
+ date="dummy-date",
+ feed_url="https://path/to/feed1.xml",
+ feed_alias=None,
+ viewed=False,
+ summary="Summary of entry one."
+)
+
+
+def test_eli_constructors():
+ assert EntryListItem.from_dict(e1.to_dict()) == e1
+
+ #with NamedTemporaryFile(delete_on_close=False) as tmp_file:
+ # to_file(tmp_file.name, data)
+ # tmp_file.close()
+ # with open(tmp_file.name, 'r') as f:
+ # reader = csv.DictReader(f, delimiter='|')
+ # rows = list(reader)
\ No newline at end of file