1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
import logging
import os
import subprocess
try:
import git
git_available = True
except ImportError:
git_available = False
import gadona
from . import Collection, Entry
class New(gadona.Command):
name = 'new'
arguments = [
(['--collection', '-c'], dict(
help='The collection to work on (default .)'
)),
]
def main(self):
collection = Collection(self.settings.collection)
new_entry = Entry(collection)
collection.save_entries([new_entry])
filepath = os.path.join(
collection.itemdir,
new_entry.fname
)
try:
subprocess.call(['sensible-editor', filepath])
except FileNotFoundError as e:
logging.warning(
"Could not open new file with editor: {}".format(str(e))
)
else:
collection.update_cache([filepath])
print(new_entry.fname)
class Edit(gadona.Command):
name = 'edit'
arguments = [
(['--collection', '-c'], dict(
help='The collection to work on (default .)'
)),
(['uid'], dict(
help='uid of an entry to edit',
)),
]
def main(self):
collection = Collection(self.settings.collection)
entry = collection.entry_from_uid(self.settings.uid)
print(collection.itemdir,entry.fname)
filepath = os.path.join(
collection.itemdir,
entry.fname
)
try:
subprocess.call(['sensible-editor', filepath])
except FileNotFoundError as e:
logging.warning(
"Could not open new file with editor: {}".format(str(e))
)
else:
collection.update_cache([filepath])
class Index(gadona.Command):
name = 'index'
arguments = [
(['--collection', '-c'], dict(
help='The collection to work on (default .)'
)),
(['files'], dict(
help='List of files to index (default: everything)',
default=None,
nargs='*'
)),
]
def main(self):
collection = Collection(self.settings.collection)
if self.settings.files:
files = (os.path.basename(f) for f in self.settings.files)
else:
files = None
indexed = collection.update_cache(fnames=files)
print("Found and indexed {} entries".format(indexed))
class Search(gadona.Command):
name = 'search'
arguments = [
(['--collection', '-c'], dict(
help='The collection to work on (default .)'
)),
(['--template', '-t'], dict(
help='Am',
)),
(['--offset'], dict(
)),
(['--pagesize'], dict(
)),
(['query'], dict(
help='Xapian query to search in the collection',
nargs='+'
)),
]
def main(self):
# TODO: implement "searching" for everything
if self.settings.offset:
logging.warning(
"offset exposes an internal knob and MAY BE" +
" REMOVED from a future release of lesana"
)
if self.settings.pagesize:
logging.warning(
"pagesize exposes an internal knob and MAY BE" +
" REMOVED from a future release of lesana"
)
offset = self.settings.offset or 0
pagesize = self.settings.pagesize or 12
collection = Collection(self.settings.collection)
#TODO: pass the entries to a proper template
for entry in collection.search(
' '.join(self.settings.query),
offset,
pagesize):
print(entry.fname)
class Init(gadona.Command):
name = 'init'
arguments = [
(['--collection', '-c'], dict(
help='The directory to work on (default .)',
default='.'
)),
(['--no-git'], dict(
help='Skip setting up git in this directory',
action="store_false",
dest='git'
)),
]
def main(self):
c_dir = self.settings.collection
if self.settings.git:
# Try to initalize a git repo
if git_available:
repo = git.Repo.init(c_dir, bare=False)
else:
log.warning("python3-git not available, could not initalise the git repository.")
repo = None
# Add .lesana directory to .gitignore and add it to the
# staging
lesana_ignored = False
try:
with open(os.path.join(c_dir, '.gitignore'), 'r') as fp:
for line in fp:
print("reading line", line)
if '.lesana' in line:
lesana_ignored = True
continue
except FileNotFoundError:
pass
if not lesana_ignored:
with open(os.path.join(c_dir, '.gitignore'), 'a') as fp:
fp.write('#Added by lesana init\n.lesana')
if repo:
repo.index.add(['.gitignore'])
# TODO: Add hook to index files as they are pulled
# If it doesn't exist, create a skeleton of settings.yaml file
# and then open it for editing
filepath = os.path.join(c_dir, 'settings.yaml')
if not os.path.exists(filepath):
# TODO: write this in a file and just copy that
with open(filepath, 'w') as fp:
fp.write('name: \n')
fp.write('lang: english\n')
fp.write('fields:\n')
try:
subprocess.call(['sensible-editor', filepath])
except FileNotFoundError as e:
logging.warning(
"Could not open new file with editor: {}".format(str(e))
)
if self.settings.git and repo:
repo.index.add(['settings.yaml'])
|