-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileio.py
More file actions
52 lines (42 loc) · 1.44 KB
/
fileio.py
File metadata and controls
52 lines (42 loc) · 1.44 KB
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
import os
import yaml
import subprocess
import tempfile
EDITOR = os.environ.get('EDITOR', 'vim')
def editor_create_entry(previous_contents:str="", title:str="", tags:str="") -> str:
if previous_contents:
initial_message = bytes(previous_contents.encode('utf-8'))
else:
if tags:
tags = ' '.join(['@@{0}'.format(t.strip()) for t in tags.split(',')])
if not (title or tags):
initial_message = b""
elif title and tags:
initial_message = bytes('{0}\n\n{1}'.format(title, tags), 'utf-8')
elif title:
initial_message = bytes(title, 'utf-8')
elif tags:
initial_message = bytes(tags, 'utf-8')
with tempfile.NamedTemporaryFile(suffix=".tmp") as tf:
tf.write(initial_message)
tf.flush()
# if vim, start at EOF
if 'vim' in EDITOR:
cmd = [EDITOR, '+', tf.name]
else:
cmd = [EDITOR, tf.name]
subprocess.call(cmd)
# do the parsing with `tf` using regular File operations.
# for instance:
tf.seek(0)
edited_message = tf.read()
return edited_message.decode("utf-8")
def write_config(cfg_file, new_cfg):
with open(cfg_file, 'w') as fp:
yaml.safe_dump(new_cfg, fp)
def read_config(cfg_file):
if os.path.exists(cfg_file):
with open(cfg_file) as fp:
return yaml.safe_load(fp)
else:
return {'daybooks': {}}