1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# encoding: utf-8 |
3
|
|
|
|
4
|
|
|
import codecs |
5
|
|
|
import fnmatch |
6
|
|
|
import os |
7
|
|
|
|
8
|
|
|
from . import Journal |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
def get_files(journal_config): |
12
|
|
|
"""Searches through sub directories starting with journal_config and find all text files""" |
13
|
|
|
filenames = [] |
14
|
|
|
for root, dirnames, f in os.walk(journal_config): |
15
|
|
|
for filename in fnmatch.filter(f, "*.txt"): |
16
|
|
|
filenames.append(os.path.join(root, filename)) |
17
|
|
|
return filenames |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
class Folder(Journal.Journal): |
21
|
|
|
"""A Journal handling multiple files in a folder""" |
22
|
|
|
|
23
|
|
|
def __init__(self, **kwargs): |
24
|
|
|
self.entries = [] |
25
|
|
|
self._diff_entry_dates = [] |
26
|
|
|
super(Folder, self).__init__(**kwargs) |
27
|
|
|
|
28
|
|
|
def open(self): |
29
|
|
|
filenames = [] |
30
|
|
|
self.entries = [] |
31
|
|
|
filenames = get_files(self.config["journal"]) |
32
|
|
|
for filename in filenames: |
33
|
|
|
with codecs.open(filename, "r", "utf-8") as f: |
34
|
|
|
journal = f.read() |
35
|
|
|
self.entries.extend(self._parse(journal)) |
36
|
|
|
self.sort() |
37
|
|
|
return self |
38
|
|
|
|
39
|
|
|
def write(self): |
40
|
|
|
"""Writes only the entries that have been modified into proper files.""" |
41
|
|
|
# Create a list of dates of modified entries. Start with diff_entry_dates |
42
|
|
|
modified_dates = self._diff_entry_dates |
43
|
|
|
seen_dates = set(self._diff_entry_dates) |
44
|
|
|
for e in self.entries: |
45
|
|
|
if e.modified: |
46
|
|
|
if e.date not in seen_dates: |
47
|
|
|
modified_dates.append(e.date) |
48
|
|
|
seen_dates.add(e.date) |
49
|
|
|
|
50
|
|
|
# For every date that had a modified entry, write to a file |
51
|
|
|
for d in modified_dates: |
52
|
|
|
write_entries = [] |
53
|
|
|
filename = os.path.join( |
54
|
|
|
self.config["journal"], |
55
|
|
|
d.strftime("%Y"), |
56
|
|
|
d.strftime("%m"), |
57
|
|
|
d.strftime("%d") + ".txt", |
58
|
|
|
) |
59
|
|
|
dirname = os.path.dirname(filename) |
60
|
|
|
# create directory if it doesn't exist |
61
|
|
|
if not os.path.exists(dirname): |
62
|
|
|
os.makedirs(dirname) |
63
|
|
|
for e in self.entries: |
64
|
|
|
if ( |
65
|
|
|
e.date.year == d.year |
66
|
|
|
and e.date.month == d.month |
67
|
|
|
and e.date.day == d.day |
68
|
|
|
): |
69
|
|
|
write_entries.append(e) |
70
|
|
|
journal = "\n".join([e.__str__() for e in write_entries]) |
71
|
|
|
with codecs.open(filename, "w", "utf-8") as journal_file: |
72
|
|
|
journal_file.write(journal) |
73
|
|
|
# look for and delete empty files |
74
|
|
|
filenames = [] |
75
|
|
|
filenames = get_files(self.config["journal"]) |
76
|
|
|
for filename in filenames: |
77
|
|
|
if os.stat(filename).st_size <= 0: |
78
|
|
|
# print("empty file: {}".format(filename)) |
79
|
|
|
os.remove(filename) |
80
|
|
|
|
81
|
|
|
def parse_editable_str(self, edited): |
82
|
|
|
"""Parses the output of self.editable_str and updates it's entries.""" |
83
|
|
|
mod_entries = self._parse(edited) |
84
|
|
|
diff_entries = set(self.entries) - set(mod_entries) |
85
|
|
|
for e in diff_entries: |
86
|
|
|
self._diff_entry_dates.append(e.date) |
87
|
|
|
# Match those entries that can be found in self.entries and set |
88
|
|
|
# these to modified, so we can get a count of how many entries got |
89
|
|
|
# modified and how many got deleted later. |
90
|
|
|
for entry in mod_entries: |
91
|
|
|
entry.modified = not any(entry == old_entry for old_entry in self.entries) |
92
|
|
|
self.entries = mod_entries |
93
|
|
|
|