-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsettings.py
78 lines (63 loc) · 2.33 KB
/
settings.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
71
72
73
74
75
76
77
78
import argparse
import ConfigParser
import os
import sys
shortcuts = [
('O', 'save'),
('X', 'quit'),
('N', 'new note'),
('G', 'goto note'),
('K', 'cut'),
('U', 'paste'),
('B', 'back'),
('F', 'forward'),
('W', 'find'),
('T', 'settings'),
]
def make_dir_if_not_exists(path):
if not os.path.exists(path): os.makedirs(path)
home_dir = os.path.expanduser('~/.nanote/')
make_dir_if_not_exists(home_dir)
config_file_path = os.path.join(home_dir, 'settings')
defaults = [
('path', os.path.join(home_dir, 'notes')),
('default_note', ''),
('tab_width', '4'),
('link_re', '\[{2}[a-zA-Z0-9\_\-\.\:\/]+\]{2}'),
('bold_re', '\*[^ ^\[^\]^*^_][^\[^\]^*^_]*?\*'),
('underline_re', '\_[^ ^\[^\]^*^_][^\[^\]^*^_]*?\_'),
('bullet_re', '^ *\* .*'),
('bullet_symbols', '*o>-+'),
('comment_re', '\#.*'),
]
config = ConfigParser.SafeConfigParser()
if not config.has_section('nanote'): config.add_section('nanote')
for key, value in defaults:
config.set('nanote', key, value)
config.read(config_file_path)
if not os.path.exists(config_file_path):
with open(config_file_path, 'w') as output_file:
config.write(output_file)
# get note search paths
# TODO: parse sys.argv arguments
args = {}
for arg, _ in defaults:
args[arg] = config.get('nanote', arg)
if len(sys.argv) > 1: args['default_note'] = sys.argv[1]
args['tab_width'] = int(args['tab_width'])
NOTE_SEARCH_PATHS = '.:' + args['path']
NOTE_SEARCH_PATHS = [os.path.expanduser(p) for p in NOTE_SEARCH_PATHS.split(':')]
for path in NOTE_SEARCH_PATHS:
make_dir_if_not_exists(path)
def find_note(note_name):
# given a note name (i.e. school:homework), search {path}/school/homework for all
# paths on the note search path
if note_name == '**settings**': return config_file_path
note_name = note_name.replace(':', '/')
note_paths = [os.path.join(path, note_name) for path in NOTE_SEARCH_PATHS]
for path in note_paths:
if os.path.exists(path): return path
return None
def default_note_path(note_name):
note_name = note_name.replace(':', '/')
return os.path.join(NOTE_SEARCH_PATHS[-1], note_name)