Skip to content

Commit

Permalink
Rename id_ -> id
Browse files Browse the repository at this point in the history
  • Loading branch information
tpwo committed Nov 7, 2023
1 parent 8d0595e commit 135afca
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 24 deletions.
14 changes: 7 additions & 7 deletions src/pyzet/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def _parse_args(args: Namespace) -> int:


def _get_last_zettel_id(repo_path: Path) -> str:
return get_all(Path(repo_path, C.ZETDIR), is_reversed=True)[0].id_
return get_all(Path(repo_path, C.ZETDIR), is_reversed=True)[0].id


def _get_config(args: Namespace) -> Config:
Expand Down Expand Up @@ -447,14 +447,14 @@ def edit_zettel(args: Namespace, config: Config) -> int:
else:
if _file_was_modified(zet.path, config):
output = _get_files_touched_last_commit(config).decode('utf-8')
if output == f'{C.ZETDIR}/{zet.id_}/{C.ZETTEL_FILENAME}\n':
if output == f'{C.ZETDIR}/{zet.id}/{C.ZETTEL_FILENAME}\n':
# If we touch the same zettel as in the last commit,
# than we automatically squash the new changes with the
# last commit, so the Git history can be simplified.
call_git(config, 'add', (zet.path.as_posix(),))
call_git(config, 'commit', ('--amend', '--no-edit'))
print(
f'{zet.id_} was edited and auto-squashed with the last'
f'{zet.id} was edited and auto-squashed with the last'
' commit\nForce push might be required'
)
else:
Expand All @@ -463,9 +463,9 @@ def edit_zettel(args: Namespace, config: Config) -> int:
zet.path,
_get_edit_commit_msg(zet.path, zet.title, config),
)
print(f'{zet.id_} was edited')
print(f'{zet.id} was edited')
else:
print(f"{zet.id_} wasn't modified")
print(f"{zet.id} wasn't modified")
return 0


Expand Down Expand Up @@ -524,7 +524,7 @@ def remove_zettel(args: Namespace, config: Config) -> int:
else:
raise SystemExit
prompt = (
f'{zet.id_} will be deleted including all files '
f'{zet.id} will be deleted including all files '
'that might be inside. Are you sure? (y/N): '
)
if input(prompt) != 'y':
Expand All @@ -544,7 +544,7 @@ def remove_zettel(args: Namespace, config: Config) -> int:
# doesn't exist.
zet.path.parent.rmdir()
logging.info(f"remove: delete folder '{zet.path.parent}'")
print(f'{zet.id_} was removed')
print(f'{zet.id} was removed')

return 0

Expand Down
12 changes: 6 additions & 6 deletions src/pyzet/zettel.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Zettel(NamedTuple):
"""

title: str
id_: str
id: str
tags: tuple[str, ...]
path: Path

Expand Down Expand Up @@ -65,7 +65,7 @@ def get_zettel(path: Path) -> Zettel:
title = get_markdown_title(title_line.strip(), md_path.name)
tags = get_tags(tags_line.strip()) if tags_line.startswith(4 * ' ') else ()
logging.debug(f"get_zettel: '{md_path.name}' with title '{title}'")
return Zettel(title=title, id_=id_, tags=tags, path=md_path)
return Zettel(title=title, id=id_, tags=tags, path=md_path)


def get_from_grep(args: Namespace, config: Config) -> Zettel:
Expand Down Expand Up @@ -154,7 +154,7 @@ def get(path: Path) -> Zettel:
id_ = path.parent.name

return Zettel(
id_=id_,
id=id_,
title=get_markdown_title(title_line, id_),
path=path,
tags=tags,
Expand All @@ -169,13 +169,13 @@ def get_zettel_repr(zet: Zettel, args: Namespace) -> str:
except ValueError: # No tags found
pass
if args.pretty:
return f'{get_timestamp(zet.id_)} -- {zet.title}{tags}'
return f'{get_timestamp(zet.id)} -- {zet.title}{tags}'
try:
if args.link:
return get_md_link(zet)
except AttributeError: # 'Namespace' object has no attribute 'link'
pass
return f'{zet.id_} -- {zet.title}{tags}'
return f'{zet.id} -- {zet.title}{tags}'


def get_timestamp(id_: str) -> datetime:
Expand All @@ -190,7 +190,7 @@ def get_md_link(zet: Zettel) -> str:
as links to zettels are usually just used in references section of a
zettel.
"""
return f'* [{zet.id_}](../{zet.id_}) {zet.title}'
return f'* [{zet.id}](../{zet.id}) {zet.title}'


def get_markdown_title(title_line: str, id_: str) -> str:
Expand Down
57 changes: 46 additions & 11 deletions tests/zettel_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import pytest

import pyzet.constants as C
from pyzet import zettel
from pyzet.zettel import get_all
from pyzet.zettel import get_markdown_title
from pyzet.zettel import get_zettel
from pyzet.zettel import Zettel


Expand All @@ -17,19 +17,19 @@ def test_get_all():
expected = [
Zettel(
title='Zet test entry',
id_='20211016205158',
id='20211016205158',
tags=('another-tag', 'tag-after-two-spaces', 'test-tag'),
path=Path('testing/zet/zettels/20211016205158/README.md'),
),
Zettel(
title='Another zet test entry',
id_='20211016223643',
id='20211016223643',
tags=('test-tag',),
path=Path('testing/zet/zettels/20211016223643/README.md'),
),
Zettel(
title='Zettel with UTF-8',
id_='20220101220852',
id='20220101220852',
tags=(),
path=Path('testing/zet/zettels/20220101220852/README.md'),
),
Expand All @@ -42,19 +42,19 @@ def test_get_all_reverse():
expected = [
Zettel(
title='Zettel with UTF-8',
id_='20220101220852',
id='20220101220852',
tags=(),
path=Path('testing/zet/zettels/20220101220852/README.md'),
),
Zettel(
title='Another zet test entry',
id_='20211016223643',
id='20211016223643',
tags=('test-tag',),
path=Path('testing/zet/zettels/20211016223643/README.md'),
),
Zettel(
title='Zet test entry',
id_='20211016205158',
id='20211016205158',
tags=('another-tag', 'tag-after-two-spaces', 'test-tag'),
path=Path('testing/zet/zettels/20211016205158/README.md'),
),
Expand All @@ -75,7 +75,7 @@ def test_get_all_skip_file(tmp_path):
expected = [
Zettel(
title='Zettel with UTF-8',
id_='20220101220852',
id='20220101220852',
tags=(),
path=Path(tmp_path, 'zettels/20220101220852/README.md'),
)
Expand All @@ -90,14 +90,49 @@ def test_get_all_dir_not_found():
assert msg == "ERROR: folder fooBarNonexistent doesn't exist."


def test_open_zettel():
def test_get():
expected = Zettel(
title='Zet test entry',
id_='20211016205158',
id='20211016205158',
tags=('another-tag', 'tag-after-two-spaces', 'test-tag'),
path=Path('testing/zet/zettels/20211016205158/README.md'),
)
actual = get_zettel(Path(f'testing/zet/{C.ZETDIR}/20211016205158'))
dir = Path(f'testing/zet/{C.ZETDIR}/20211016205158/{C.ZETTEL_FILENAME}')
actual = zettel.get(dir)
assert actual == expected


def test_get_from_dir():
expected = Zettel(
title='Zet test entry',
id='20211016205158',
tags=('another-tag', 'tag-after-two-spaces', 'test-tag'),
path=Path('testing/zet/zettels/20211016205158/README.md'),
)
dir = Path(f'testing/zet/{C.ZETDIR}/20211016205158')
actual = zettel.get_from_dir(dir)
assert actual == expected


def test_get_from_id():
expected = Zettel(
title='Zet test entry',
id='20211016205158',
tags=('another-tag', 'tag-after-two-spaces', 'test-tag'),
path=Path('testing/zet/zettels/20211016205158/README.md'),
)
actual = zettel.get_from_id('20211016205158', repo=Path('testing/zet'))
assert actual == expected


def test_get_last():
expected = Zettel(
title='Zettel with UTF-8',
id='20220101220852',
tags=(),
path=Path('testing/zet/zettels/20220101220852/README.md'),
)
actual = zettel.get_last(Path('testing/zet'))
assert actual == expected


Expand Down

0 comments on commit 135afca

Please sign in to comment.