Skip to content

Commit

Permalink
Merge pull request #60 from falk-werner/feature/migrate-to-note.spec
Browse files Browse the repository at this point in the history
Feature: use note.spec directory structure
  • Loading branch information
falk-werner committed Feb 22, 2024
2 parents da6d7da + 9e674b6 commit 1ca5b85
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 37 deletions.
35 changes: 27 additions & 8 deletions note.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

APP_NAME = "note.py"

PERSISTENCE_VERSION=2
PERSISTENCE_VERSION=3

DEFAULT_THEME="arc"
DEFAULT_BASE_PATH="{home}/.notepy"
Expand Down Expand Up @@ -119,21 +119,40 @@ def __init__(self, config_file=CONFIG_FILE):
self.__load_config_file()
self.__basepath = self.__basepath_template.format(home=Path.home())
self.__mkdir(self.__basepath)
self.__notespath = os.path.join(self.__basepath, "notes")
self.__mkdir(self.__notespath)
self.__css = self.__load_css()
self.__migrate()

def __migrate(self):
if self.__version < 3:
print("info: migrate from notes directory")
notespath = os.path.join(self.__basepath, "notes")
for name in os.listdir(notespath):
legacy_dir = os.path.join(notespath, name)
if os.path.isdir(legacy_dir):
desired_dir = os.path.join(self.__basepath, name)
try:
if not os.path.isdir(desired_dir):
os.rename(legacy_dir, desired_dir)
else:
shutil.copytree(legacy_dir, desired_dir, dirs_exist_ok=True)
shutil.rmtree(legacy_dir)
print(f"info: successfully migrated {name}")
except OSError as ex:
print(f"error: failed to migrate {name}: {ex}")
try:
os.rmdir(notespath)
print("info: notes directory removed")
except OSError:
print("info: keep notes directory, since it isn't empty")
if self.__version < 2:
print("info: migrate note.md to README.md")
for name in os.listdir(self.__notespath):
legacy_file = os.path.join(self.__notespath, name, "note.md")
for name in os.listdir(self.__basepath):
legacy_file = os.path.join(self.__basepath, name, "note.md")
if os.path.isfile(legacy_file):
desired_file = self.__note_filename(name)
try:
os.rename(legacy_file, desired_file)
print(f"info: sucessfully migrated {name}")
print(f"info: successfully migrated {name}")
except OSError as ex:
print(f"error: failed to migrate {name}: {ex}")

Expand Down Expand Up @@ -239,7 +258,7 @@ def note_path(self, name):
:return: Path of directory that conatins the nore.
:rtype: str
"""
return os.path.join(self.__notespath, quote(name))
return os.path.join(self.__basepath, quote(name))

def list_notes(self):
"""Returns a list of all notes.
Expand All @@ -248,7 +267,7 @@ def list_notes(self):
:rtype: list[str]
"""
notes = []
for name in os.listdir(self.__notespath):
for name in os.listdir(self.__basepath):
display_name = urllib.parse.unquote(name)
notefile = self.__note_filename(display_name)
if os.path.isfile(notefile):
Expand Down
139 changes: 110 additions & 29 deletions test_persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def is_dir(self, filename):
# pylint: disable-next=too-many-arguments
def write_configfile(self,
filename=".notepy.yml",
persistence_version=2,
persistence_version=3,
base_path="base",
geometry="800x600",
font_size=20,
Expand Down Expand Up @@ -122,7 +122,6 @@ def test_empty_notes():
+-- .notepy.yml : notepy config file
+-- base : base
+-- style.css : style
+-- notes : notes directory (empty)
```
"""

Expand All @@ -139,14 +138,8 @@ def test_empty_notes():
# check notes directory
notes_path = os.path.join(fs.root_dir, "base")
items = os.listdir(notes_path)
assert len(items) == 2
assert len(items) == 1
assert fs.is_file(os.path.join("base","style.css"))
assert fs.is_dir(os.path.join("base","notes"))

# check notes/notes directory
notes_path = os.path.join(fs.root_dir, "base", "notes")
items = os.listdir(notes_path)
assert len(items) == 0

# check persistence
assert len(persistence.list_notes()) == 0
Expand All @@ -168,38 +161,36 @@ def test_read_existing():
+-- .notepy.yml : notepy config file
+-- base : base
+-- style.css : style
+-- notes : notes directory
+-- simple : simple note directory
| +-- README.md : simple note contents
+-- tagged : tagged note directory
| +-- README.md : tagged note contents
| +-- tags.txt : tagged note tags
+-- complex : complex note directory
+-- README.md : complex note contents
+-- tags.txt : complex note tags
+-- pic.png : complex note attachment
+-- simple : simple note directory
| +-- README.md : simple note contents
+-- tagged : tagged note directory
| +-- README.md : tagged note contents
| +-- tags.txt : tagged note tags
+-- complex : complex note directory
+-- README.md : complex note contents
+-- tags.txt : complex note tags
+-- pic.png : complex note attachment
```
"""

config_file = fs.write_configfile(persistence_version=1)
config_file = fs.write_configfile()
fs.mkdir("base")
fs.mkdir(os.path.join("base","notes"))

custom_style = "custom_ style"
fs.write_file(os.path.join("base", "style.css"), custom_style)

simple_note_path = os.path.join("base","notes","simple")
simple_note_path = os.path.join("base","simple")
fs.mkdir(simple_note_path)
simple_note_contents = "# Simple Note"
fs.write_file(os.path.join(simple_note_path, "README.md"), simple_note_contents)

tagged_note_path = os.path.join("base","notes","tagged")
tagged_note_path = os.path.join("base","tagged")
fs.mkdir(tagged_note_path)
tagged_note_contents = "# Tagged Note"
fs.write_file(os.path.join(tagged_note_path, "README.md"), tagged_note_contents)
fs.write_file(os.path.join(tagged_note_path, "tags.txt"), "info")

complex_note_path = os.path.join("base","notes","complex")
complex_note_path = os.path.join("base","complex")
fs.mkdir(complex_note_path)
complex_note_contents = "# Complex Note"
fs.write_file(os.path.join(complex_note_path, "README.md"), complex_note_contents)
Expand Down Expand Up @@ -382,6 +373,42 @@ def test_migrate_from_v1():
+-- note.md : contents of the old note
```
V3 filesystem layout:
```
root : filesystem root
+-- .notepy.yml : notepy config file
+-- base : base
+-- old-note : direcotry of the old note
+-- README.md : contents of the old note
```
"""

config_file = fs.write_configfile(persistence_version=1)
fs.mkdir("base")
fs.mkdir(os.path.join("base","notes"))

note_path = os.path.join("base","notes","old-note")
fs.mkdir(note_path)
contents = "Contents of old note"
fs.write_file(os.path.join(note_path, "note.md"), contents)

persistence = Persistence(config_file)
assert fs.is_file(os.path.join("base", "old-note", "README.md"))
assert not fs.is_dir(note_path)

notes = persistence.list_notes()
assert len(notes) == 1
assert len(persistence.list_tags()) == 0
assert "old-note" in notes
assert persistence.read_note("old-note") == contents
assert len(persistence.read_tags("old-note")) == 0

def test_migrate_from_v2():
"""Checks migration from persistence v1.
In persistence v2, a subdirectory "notes" is used
to store each note.
V2 filesystem layout:
```
root : filesystem root
Expand All @@ -391,25 +418,79 @@ def test_migrate_from_v1():
+-- old-note : direcotry of the old note
+-- README.md : contents of the old note
```
V3 filesystem layout:
```
root : filesystem root
+-- .notepy.yml : notepy config file
+-- base : base
+-- old-note : direcotry of the old note
+-- README.md : contents of the old note
```
"""

config_file = fs.write_configfile(persistence_version=1)
config_file = fs.write_configfile(persistence_version=2)
fs.mkdir("base")
fs.mkdir(os.path.join("base","notes"))

note_path = os.path.join("base","notes","old-note")
fs.mkdir(note_path)
contents = "Contents of old note"
fs.write_file(os.path.join(note_path, "note.md"), contents)
fs.write_file(os.path.join(note_path, "README.md"), contents)

persistence = Persistence(config_file)
assert fs.is_dir(note_path)
assert fs.is_file(os.path.join(note_path, "README.md"))
assert not fs.exists(os.path.join(note_path, "note.md"))
assert fs.is_file(os.path.join("base", "old-note", "README.md"))
assert not fs.is_dir(note_path)

notes = persistence.list_notes()
assert len(notes) == 1
assert len(persistence.list_tags()) == 0
assert "old-note" in notes
assert persistence.read_note("old-note") == contents
assert len(persistence.read_tags("old-note")) == 0

def test_migrate_from_v2_with_note_names_notes():
"""Checks migration from persistence v1.
In persistence v2, a subdirectory "notes" is used
to store each note.
V2 filesystem layout:
```
root : filesystem root
+-- .notepy.yml : notepy config file
+-- base : base
+-- notes : notes directory
+-- notes : direcotry of the old note
+-- README.md : contents of the old note
```
V3 filesystem layout:
```
root : filesystem root
+-- .notepy.yml : notepy config file
+-- base : base
+-- old-note : direcotry of the old note
+-- README.md : contents of the old note
```
"""

config_file = fs.write_configfile(persistence_version=2)
fs.mkdir("base")
fs.mkdir(os.path.join("base","notes"))

note_path = os.path.join("base","notes","notes")
fs.mkdir(note_path)
contents = "Contents of old note"
fs.write_file(os.path.join(note_path, "README.md"), contents)

persistence = Persistence(config_file)
assert fs.is_file(os.path.join("base", "notes", "README.md"))
assert not fs.is_dir(note_path)

notes = persistence.list_notes()
assert len(notes) == 1
assert len(persistence.list_tags()) == 0
assert "notes" in notes
assert persistence.read_note("notes") == contents
assert len(persistence.read_tags("notes")) == 0

0 comments on commit 1ca5b85

Please sign in to comment.