-
Notifications
You must be signed in to change notification settings - Fork 0
/
documents.py
70 lines (56 loc) · 2.05 KB
/
documents.py
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
import json
import os
class DocumentEntry:
def __init__(self, code, name, is_audio=False, is_pdf=False):
self.code = code
self.name = name
self.is_audio = is_audio
self.is_pdf = is_pdf
def __str__(self):
return self.name
class Documents:
"""
Store for document locations. Each document entry may consist:
* pdf path
* audio file path
* or both paths
Document locations are stored in local json file 'documents.json'
"""
FILE_NAME = 'documents.json'
def __init__(self):
self.documents = None
self.tools = {}
self.paths = {}
def load(self):
locations = ['.', os.getenv('HOME') + '/.music-practice']
for location in locations:
file_path = f'{location}/{Documents.FILE_NAME}'
if os.path.isfile(file_path):
with open(file_path) as input_file:
self.documents = json.load(input_file)
break
if self.documents is None:
raise Exception(f'Cannot find database file {Documents.FILE_NAME}')
self.paths = self.documents['paths']
def get_entries(self):
output = []
for key in self.documents['items'].keys():
doc = self.documents['items'][key]
is_audio = 'audio' in doc
is_pdf = 'pdf' in doc
is_audio_only = ' (audio only)' if is_audio and not is_pdf else ''
is_pdf_only = ' (pdf only)' if is_pdf and not is_audio else ''
output.append(DocumentEntry(key, name=f"{doc['name']}{is_audio_only}{is_pdf_only}",
is_audio=is_audio, is_pdf=is_pdf))
return sorted(output, key=lambda item: item.name)
def expand_path(self, path):
res = path
for key, value in self.paths.items():
if key in path:
res = res.replace(key, value)
print('replaced', res)
return res
def get_items(self):
return self.documents['items']
def get_tools(self):
return self.tools