Example #1
0
def test_change(fs, capsys):
    nd_setup(fs)
    fs.create_file('/notes/cwd/foo.md', contents='some text')
    assert cli.main([
        'change', '-p', '-a', 'tag1,tag2', '-c', '2012-02-03', '-t',
        'A Bland Note', 'foo.md'
    ]) == 0
    assert Path('/notes/cwd/foo.md').read_text() == 'some text'
    out, err = capsys.readouterr()
    lines = set(out.splitlines())
    # It's a little weird that we generate Cmds with relative paths, when most of the time the repos deal with
    # fully resolved paths. I don't think it affects anything right now, but may need to change at some point.
    assert lines == {
        str(AddTagCmd('foo.md', 'tag1')),
        str(AddTagCmd('foo.md', 'tag2')),
        str(SetTitleCmd('foo.md', 'A Bland Note')),
        str(SetCreatedCmd('foo.md', datetime(2012, 2, 3)))
    }

    assert cli.main([
        'change', '-a', 'tag1,tag2', '-c', '2012-02-03', '-t', 'A Bland Note',
        'foo.md'
    ]) == 0
    assert Path('/notes/cwd/foo.md').read_text() == """---
created: 2012-02-03 00:00:00
keywords:
- tag1
- tag2
title: A Bland Note
...
some text"""
    assert cli.main(['change', '-d', 'tag1', '-t', 'A Better Note',
                     'foo.md']) == 0
    assert Path('/notes/cwd/foo.md').read_text() == """---
Example #2
0
def test_change_tags(fs):
    doc = """<html>
    <head>
        <meta name="keywords" content="one, two"/>
    </head>
    <body>
        text
    </body>
</html>"""
    expected = """<html>
    <head>
        <meta name="keywords" content="three, two"/>
    </head>
    <body>
        text
    </body>
</html>"""
    path = Path('/fakenotes/test.html')
    fs.create_file(path, contents=doc)
    acc = HTMLAccessor(str(path))
    acc.edit(AddTagCmd(str(path), 'THREE'))
    acc.edit(DelTagCmd(str(path), 'ONE'))
    assert acc.save()
    assert BeautifulSoup(
        path.read_text(),
        'lxml',
    ) == BeautifulSoup(expected, 'lxml')
Example #3
0
def test_meta_boundaries_in_body(fs):
    doc1 = """---
title: Hi
...

keywords:
- foo
...
whatever
"""
    doc2 = """---
title: Hi
---

keywords:
- foo
---
whatever
"""
    Path('/fakenotes').mkdir()
    for doc in [doc1, doc2]:
        path = '/fakenotes/doc.md'
        Path(path).write_text(doc)
        acc = MarkdownAccessor(path)
        info = acc.info()
        assert info.title == 'Hi'
        assert info.tags == set()
        acc.edit(AddTagCmd(path, 'testing'))
        acc.save()
        if doc == doc1:
            doc = doc.replace('\n...', '\n---', 1)
        doc = doc.replace('title', 'keywords:\n- testing\ntitle', 1)
        assert Path(path).read_text() == doc
Example #4
0
    def change(self, paths: Set[str], add_tags=Set[str], del_tags=Set[str], title=Optional[str],
               created=Optional[datetime]) -> None:
        """Applies all the specified changes to the specified paths.

        This is a convenience method that wraps :meth:`notesdir.repos.base.Repo.change`
        """
        edits = []
        for path in paths:
            edits.extend(AddTagCmd(path, t.lower()) for t in add_tags)
            edits.extend(DelTagCmd(path, t.lower()) for t in del_tags)
            if title is not None:
                edits.append(SetTitleCmd(path, title))
            if created is not None:
                edits.append(SetCreatedCmd(path, created))
        self.repo.change(edits)
Example #5
0
def test_change_metadata_tags(fs):
    doc = """---
keywords:
- one
- two
...
text"""
    expected = """---
keywords:
- three
- two
...
text"""
    path = '/fakenotes/test.md'
    fs.create_file(path, contents=doc)
    acc = MarkdownAccessor(path)
    acc.edit(AddTagCmd(path, 'THREE'))
    acc.edit(DelTagCmd(path, 'ONE'))
    assert acc.save()
    assert Path(path).read_text() == expected
Example #6
0
def test_change(fs):
    path = str(Path(__file__).parent.joinpath('test.pdf'))
    fs.add_real_file(path, read_only=False)
    acc = PDFAccessor(path)
    acc.edit(SetTitleCmd(path, 'Why Donuts Are Great'))
    acc.edit(
        SetCreatedCmd(path,
                      datetime.fromisoformat('1999-02-04T06:08:10+00:00')))
    acc.edit(AddTagCmd(path, 'tag3'))
    acc.edit(DelTagCmd(path, 'tag2'))
    assert acc.save()
    with open(path, 'rb') as file:
        pdf = PdfFileReader(file)
        assert 'I like donuts' in pdf.getPage(0).extractText()
        # Make sure we didn't destroy preexisting metadata
        assert pdf.getDocumentInfo()['/Creator'] == 'Pages'
    info = PDFAccessor(path).info()
    assert info.title == 'Why Donuts Are Great'
    assert info.created == datetime.fromisoformat('1999-02-04T06:08:10+00:00')
    assert info.tags == {'tag1', 'tag3'}
Example #7
0
 def add_tag(self, path: str, tag: str) -> None:
     """Convenience method equivalent to calling change with one :class`notesdir.models.AddTagCmd`"""
     self.change([AddTagCmd(path, tag)])