-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathtypes.py
More file actions
681 lines (471 loc) · 18 KB
/
types.py
File metadata and controls
681 lines (471 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
import dataclasses
import re
import traceback
from dataclasses import dataclass
from datetime import datetime
from types import MappingProxyType
from typing import Any
from typing import Callable
from typing import cast
from typing import Dict
from typing import Iterable
from typing import List
from typing import Mapping
from typing import NamedTuple
from typing import Optional
from typing import Sequence
from typing import Tuple
from typing import Type
from typing import TypeVar
from typing import Union
from typing_extensions import Literal
from typing_extensions import Protocol
from typing_extensions import runtime_checkable
from reader.exceptions import ReaderError
_T = TypeVar('_T')
class _namedtuple_compat:
"""Add namedtuple-like methods to a dataclass."""
# TODO: can we get rid of _namedtuple_compat?
@classmethod
def _make(cls: Type[_T], iterable: Iterable[Any]) -> _T:
iterable = tuple(iterable)
attrs_len = len(dataclasses.fields(cls))
if len(iterable) != attrs_len:
raise TypeError(
'Expected %d arguments, got %d' % (attrs_len, len(iterable))
)
return cls(*iterable)
_replace = dataclasses.replace
def _asdict(self) -> Dict[str, Any]:
return dict(self.__dict__)
# See https://github.com/lemon24/reader/issues/159 for a discussion
# of how feed- and entry-like objects are uniquely identified, and
# how to keep it consistent.
# See https://github.com/lemon24/reader/issues/153#issuecomment-620228329
# for how the feed and entry attributes should match the Atom spec.
# Public API
@dataclass(frozen=True)
class Feed(_namedtuple_compat):
"""Data type representing a feed.
All :class:`~datetime.datetime` attributes are timezone-naive,
and always represent UTC.
"""
#: The URL of the feed.
url: str
#: The date the feed was last updated, according to the feed.
updated: Optional[datetime] = None
#: The title of the feed.
title: Optional[str] = None
#: The URL of a page associated with the feed.
link: Optional[str] = None
#: The author of the feed.
author: Optional[str] = None
#: User-defined feed title.
user_title: Optional[str] = None
# added is required, but we want it after feed data; the cast is for mypy.
#: The date when the feed was added.
#:
#: .. versionadded:: 1.3
added: datetime = cast(datetime, None)
#: The date when the feed was last retrieved by reader.
#:
#: .. versionadded:: 1.3
last_updated: Optional[datetime] = None
#: If a :exc:`ParseError` happend during the last update, its cause.
#:
#: .. versionadded:: 1.3
last_exception: Optional['ExceptionInfo'] = None
#: Whether updates are enabled for this feed.
#:
#: .. versionadded:: 1.11
updates_enabled: bool = True
@property
def object_id(self) -> str:
"""Alias for :attr:`~Feed.url`.
.. versionadded:: 1.12
"""
return self.url
_EI = TypeVar('_EI', bound='ExceptionInfo')
@dataclass(frozen=True)
class ExceptionInfo(_namedtuple_compat):
"""Data type representing information about an exception.
.. versionadded:: 1.3
"""
# Similar to traceback.TracebackException and boltons.tbutils.ExceptionInfo.
# If ever make this richer, we might as well use one of them.
#: The fully qualified name of the exception type.
type_name: str
#: String representation of the exception value.
value_str: str
#: String representation of the exception traceback.
traceback_str: str
@classmethod
def from_exception(cls: Type[_EI], exc: BaseException) -> _EI:
return cls(
f'{type(exc).__module__}.{type(exc).__qualname__}',
str(exc),
''.join(traceback.format_exception(type(exc), exc, exc.__traceback__)),
)
@dataclass(frozen=True)
class Entry(_namedtuple_compat):
"""Data type representing an entry.
All :class:`~datetime.datetime` attributes are timezone-naive,
and always represent UTC.
"""
# WARNING: When changing attributes, keep Entry and EntryData in sync.
@property
def feed_url(self) -> str:
"""The feed URL."""
return self.feed.url
# TODO: .id and .updated will still be set to some default value if the entry doesn't have them; we should at least document this.
# I'm not sure its useful to expose the original values. If we do it, it would be minimally invasive to add them as new attributes (even if it means id/updated don't always reflect their value in the feed); the names should work with the schemes proposed in #153 and #159.
#: The entry id.
id: str
#: The date the entry was last updated, according to the feed.
updated: datetime
#: The title of the entry.
title: Optional[str] = None
#: The URL of a page associated with the entry.
link: Optional[str] = None
#: The author of the feed.
author: Optional[str] = None
#: The date the entry was first published.
published: Optional[datetime] = None
#: A summary of the entry.
summary: Optional[str] = None
#: Full content of the entry.
#: A sequence of :class:`Content` objects.
content: Sequence['Content'] = ()
#: External files associated with the entry.
#: A sequence of :class:`Enclosure` objects.
enclosures: Sequence['Enclosure'] = ()
#: Whether the entry was read or not.
read: bool = False
#: Whether the entry is important or not.
important: bool = False
#: The date when the entry was last updated by reader.
#:
#: .. versionadded:: 1.3
last_updated: datetime = cast(datetime, None)
#: The URL of the original feed of the entry.
#:
#: If the feed URL never changed, the same as :attr:`~Entry.feed_url`.
#:
#: .. versionadded:: 1.8
original_feed_url: str = cast(str, None)
# feed should not have a default, but I'd prefer objects that aren't
# entry data to be at the end, and dataclasses don't support keyword-only
# arguments yet.
#
# We could use a null object as the default (Feed('')), but None
# increases the chance we'll catch feed= not being set at runtime;
# we don't check for it in __post_init__ because it's still useful
# to have it None in tests. The cast is to please mypy.
#: The entry's feed.
feed: Feed = cast(Feed, None)
@property
def object_id(self) -> Tuple[str, str]:
"""Alias for (:attr:`~Entry.feed_url`, :attr:`~Entry.id`).
.. versionadded:: 1.12
"""
return self.feed_url, self.id
@dataclass(frozen=True)
class Content(_namedtuple_compat):
"""Data type representing a piece of content."""
#: The content value.
value: str
#: The content type.
type: Optional[str] = None
#: The content language.
language: Optional[str] = None
@dataclass(frozen=True)
class Enclosure(_namedtuple_compat):
"""Data type representing an external file."""
#: The file URL.
href: str
#: The file content type.
type: Optional[str] = None
#: The file length.
length: Optional[int] = None
_HS = TypeVar('_HS', bound='HighlightedString')
@dataclass(frozen=True)
class HighlightedString:
"""A string that has some of its parts highlighted."""
# TODO: show if we're at the start/end of the value
#: The underlying string.
value: str = ''
#: The highlights; non-overlapping slices with positive start/stop
#: and None step.
highlights: Sequence[slice] = ()
def __post_init__(self) -> None:
for highlight in self.highlights:
reason = ''
if highlight.start is None or highlight.stop is None:
reason = 'start and stop must not be None'
elif highlight.step is not None:
reason = 'step must be None'
elif highlight.start < 0 or highlight.stop < 0:
reason = 'start and stop must be equal to or greater than 0'
elif highlight.start > len(self.value) or highlight.stop > len(self.value):
reason = (
'start and stop must be less than or equal to the string length'
)
elif highlight.start > highlight.stop:
reason = 'start must be not be greater than stop'
if reason:
raise ValueError(f'invalid highlight: {reason}: {highlight}')
highlights = sorted(self.highlights, key=lambda s: (s.start, s.stop))
prev_highlight = None
for highlight in highlights:
if not prev_highlight:
prev_highlight = highlight
continue
if prev_highlight.stop > highlight.start:
raise ValueError(
f'highlights must not overlap: {prev_highlight}, {highlight}'
)
object.__setattr__(self, 'highlights', tuple(highlights))
def __str__(self) -> str:
return self.value
@classmethod
def extract(cls: Type[_HS], text: str, before: str, after: str) -> _HS:
"""Extract highlights with before/after markers from text.
>>> HighlightedString.extract( '>one< two', '>', '<')
HighlightedString(value='one two', highlights=(slice(0, 3, None),))
Args:
text (str): The original text, with highlights marked by ``before`` and ``after``.
before (str): Highlight start marker.
after (str): Highlight stop marker.
Returns:
HighlightedString: A highlighted string.
"""
pattern = f"({'|'.join(re.escape(s) for s in (before, after))})"
parts = []
slices = []
index = 0
start = None
for part in re.split(pattern, text):
if part == before:
if start is not None:
raise ValueError("highlight start marker in highlight")
start = index
continue
if part == after:
if start is None:
raise ValueError("unmatched highlight end marker")
slices.append(slice(start, index))
start = None
continue
parts.append(part)
index += len(part)
if start is not None:
raise ValueError("highlight is never closed")
return cls(''.join(parts), tuple(slices))
def split(self) -> Iterable[str]:
"""Split the highlighted string into parts.
>>> list(HighlightedString('abcd', [slice(1, 3)]))
['a', 'bc', 'd']
Yields:
str: The parts (always an odd number);
parts with odd indexes are highlighted,
parts with even indexes are not.
"""
start = 0
for highlight in self.highlights:
yield self.value[start : highlight.start]
yield self.value[highlight]
start = highlight.stop
yield self.value[start:]
def apply(
self,
before: str,
after: str,
func: Optional[Callable[[str], str]] = None,
) -> str:
"""Apply before/end markers on the highlighted string.
The opposite of :meth:`extract`.
>>> HighlightedString('abcd', [slice(1, 3)]).apply('>', '<')
'a>bc<d'
>>> HighlightedString('abcd', [slice(1, 3)]).apply('>', '<', str.upper)
'A>BC<D'
Args:
before (str): Highlight start marker.
after (str): Highlight stop marker.
func (callable((str), str) or none): If given, a function
to apply to the string parts before adding the markers.
Returns:
str: The string, with highlights marked by ``before`` and ``after``.
"""
def inner() -> Iterable[str]:
for index, part in enumerate(self.split()):
if index % 2 == 1:
yield before
if func:
part = func(part)
yield part
if index % 2 == 1:
yield after
return ''.join(inner())
@dataclass(frozen=True)
class EntrySearchResult(_namedtuple_compat):
"""Data type representing the result of an entry search.
:attr:`metadata` and :attr:`content` are dicts where
the key is the path of an entry attribute,
and the value is a :class:`HighlightedString` snippet
corresponding to that attribute, with HTML stripped.
>>> result = next(reader.search_entries('hello internet'))
>>> result.metadata['.title'].value
'A Recent Hello Internet'
>>> reader.get_entry(result).title
'A Recent Hello Internet'
"""
#: The feed URL.
feed_url: str
#: The entry id.
id: str
#: Matching entry metadata, in arbitrary order.
#: Currently entry.title and entry.feed.user_title/.title.
metadata: Mapping[str, HighlightedString] = MappingProxyType({})
#: Matching entry content, sorted by relevance.
#: Any of entry.summary and entry.content[].value.
content: Mapping[str, HighlightedString] = MappingProxyType({})
# TODO: entry: Optional[Entry]; model it through typing if possible
@property
def object_id(self) -> Tuple[str, str]:
"""Alias for (:attr:`~EntrySearchResult.feed_url`, :attr:`~EntrySearchResult.id`).
.. versionadded:: 1.12
"""
return self.feed_url, self.id
# Semi-public API (typing support)
# TODO: Could we use some kind of str-compatible enum here?
FeedSortOrder = Literal['title', 'added']
EntrySortOrder = Literal['recent', 'random']
SearchSortOrder = Literal['relevant', 'recent', 'random']
# https://github.com/python/typing/issues/182
JSONValue = Union[str, int, float, bool, None, Dict[str, Any], List[Any]]
JSONType = Union[Dict[str, JSONValue], List[JSONValue]]
# Using protocols here so we have both duck typing and type checking.
# Simply catching AttributeError (e.g. on feed.url) is not enough for mypy,
# see https://github.com/python/mypy/issues/8056.
@runtime_checkable
class FeedLike(Protocol):
# We don't use "url: str" because we don't care if url is writable.
@property
def url(self) -> str: # pragma: no cover
...
@runtime_checkable
class EntryLike(Protocol):
@property
def id(self) -> str: # pragma: no cover
...
@property
def feed_url(self) -> str: # pragma: no cover
...
FeedInput = Union[str, FeedLike]
EntryInput = Union[Tuple[str, str], EntryLike]
def _feed_argument(feed: FeedInput) -> str:
if isinstance(feed, FeedLike):
return feed.url
if isinstance(feed, str):
return feed
raise ValueError(f'invalid feed argument: {feed!r}')
def _entry_argument(entry: EntryInput) -> Tuple[str, str]:
if isinstance(entry, EntryLike):
return _feed_argument(entry.feed_url), entry.id
if isinstance(entry, tuple) and len(entry) == 2:
feed_url, entry_id = entry
if isinstance(feed_url, str) and isinstance(entry_id, str):
return entry
raise ValueError(f'invalid entry argument: {entry!r}')
# str explicitly excluded, to allow for a string-based query language;
# https://github.com/lemon24/reader/issues/184#issuecomment-689587006
TagFilterInput = Union[
None, bool, Sequence[Union[str, bool, Sequence[Union[str, bool]]]]
]
class MissingType:
def __repr__(self) -> str:
return "no value"
#: Sentinel object used to detect if the `default` argument was provided."""
MISSING = MissingType()
@dataclass(frozen=True)
class FeedCounts(_namedtuple_compat):
"""Count information about feeds.
.. versionadded:: 1.11
"""
#: Total number of feeds.
total: Optional[int] = None
#: Number of broken feeds.
broken: Optional[int] = None
#: Number of feeds that have updates enabled.
updates_enabled: Optional[int] = None
@dataclass(frozen=True)
class EntryCounts(_namedtuple_compat):
"""Count information about entries.
.. versionadded:: 1.11
"""
#: Total number of entries.
total: Optional[int] = None
#: Number of read entries.
read: Optional[int] = None
#: Number of important entries.
important: Optional[int] = None
#: Number of entries that have enclosures.
has_enclosures: Optional[int] = None
@dataclass(frozen=True)
class EntrySearchCounts(_namedtuple_compat):
"""Count information about entry search results.
.. versionadded:: 1.11
"""
# This could have inherited EntryCounts,
# but attribute docstrings won't show show up with autoclass;
# https://github.com/sphinx-doc/sphinx/issues/741
# We do want a different type in case we additional attributes
# related to search stuff (what matched etc.)
#: Total number of entries.
total: Optional[int] = None
#: Number of read entries.
read: Optional[int] = None
#: Number of important entries.
important: Optional[int] = None
#: Number of entries that have enclosures.
has_enclosures: Optional[int] = None
@dataclass(frozen=True)
class UpdatedFeed:
"""The result of a successful feed update.
.. versionadded:: 1.14
"""
#: The URL of the feed.
url: str
#: The number of new entries.
new: int
#: The number of updated entries.
updated: int
class UpdateResult(NamedTuple):
"""Named tuple representing the result of a feed update.
.. versionadded:: 1.14
"""
#: The URL of the feed.
url: str
#: One of:
#:
#: :class:`UpdatedFeed`
#:
#: If the update was successful; a summary of the updated feed.
#:
#: :obj:`None`
#:
#: If the server indicated the feed has not changed
#: since the last update.
#:
#: :exc:`ReaderError`
#:
#: If there was an error while updating the feed.
#:
value: Union[UpdatedFeed, None, ReaderError]
# The exception type is ReaderError and not ParseError
# to allow suppressing new errors without breaking the API:
# adding a new type to the union breaks the API,
# not raising an exception type anymore doesn't.
# Currently, storage or plugin-raised exceptions
# prevent updates for the following feeds (:issue:`218`),
# but that's not necessarily by design.