Skip to content

Commit

Permalink
more defensive Highlight.dt handling.
Browse files Browse the repository at this point in the history
see #1
  • Loading branch information
karlicoss committed Oct 15, 2019
1 parent 372dc25 commit 2ec68f4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
23 changes: 19 additions & 4 deletions src/kobuddy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import (Dict, Iterator, List, NamedTuple, Optional, Sequence, Set,
Tuple, Union, Callable)
Tuple, Union)

import dataset # type: ignore
import pytz
Expand Down Expand Up @@ -147,11 +147,26 @@ def __init__(self, row: Dict[str, str], book: Book):
self.row = row
self._book = book

# modified is either same as created or 0 timestamp? anyway, not very interesting
def _error(self, msg: str) -> Exception:
return RuntimeError(f'Error while processing {self.row}: {msg}')

@property
def dt(self) -> datetime:
# I checked and it's definitely utc
return unwrap(_parse_utcdt(self.row['DateCreated']))
"""
Returns DateCreated.
On some devices may not be set, so falls back to DateModified (see https://github.com/karlicoss/kobuddy/issues/1 )
Returns date is in UTC (tested on Kobo Aura One).
"""
# on Kobo Aura One modified was either same as created or 0 timestamp
date_attrs = ('DateCreated', 'DateModified')
for dattr in date_attrs:
# TODO could warn/log if it's not using DateModified
res = _parse_utcdt(self.row[dattr])
if res is not None:
return res
raise self._error(f"Couldn't infer date from {date_attrs}")

# @property
# def book(self) -> Book: # TODO FIXME should handle it carefully in kobo provider users
Expand Down
2 changes: 1 addition & 1 deletion src/kobuddy/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import functools
import logging
from typing import Callable, Dict, Iterable, List, Optional, Type, TypeVar
from typing import Callable, Dict, Iterable, List, Optional, TypeVar


def get_logger():
Expand Down
39 changes: 39 additions & 0 deletions tests/test_rows.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from collections import OrderedDict

import kobuddy
from kobuddy import Highlight, Book


def test_hl_no_datecreated():
book = Book(title='test title', author='test author', content_id='fwfwf', isbn='2424')
row = OrderedDict([
('BookmarkID', 'bbbbb'),
('VolumeID', 'vvvv'),
('ContentID', 'cccc'),
('StartContainerPath', 'span#kobo\\.31\\.4'),
('StartContainerChildIndex', -99),
('StartOffset', 117),
('EndContainerPath', 'span#kobo\\.31\\.5'),
('EndContainerChildIndex', -99),
('EndOffset', 141),
('Text', '<blah blah quote>'),
('Annotation', None),
('ExtraAnnotationData', None),
('DateCreated', None),
('ChapterProgress', 0.75),
('Hidden', 'false'),
('Version', ''),
('DateModified', '2014-05-17T21:05:35Z'),
('Creator', None),
('UUID', None),
('UserID', '6fc7'),
('SyncTime', '2019-05-21T16:46:17Z'),
('Published', 'false'),
])
hl = Highlight(row, book)
# TODO can we automate this?
for attr in [a for a in dir(Highlight) if not a.startswith('_')]:
value = getattr(hl, attr)
assert value is not None


2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ deps = pytest
# TODO ugh. unclear how to reuse setup.cfg in tox
commands =
pip install -e .[testing]
python3 -m pytest {posargs}
python3 -m pytest --doctest-modules {posargs}

[testenv:mypy]
skip_install = true
Expand Down

0 comments on commit 2ec68f4

Please sign in to comment.