-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.py
139 lines (117 loc) · 3.76 KB
/
context.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
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
import click
import os
import shutil
import sys
from pathlib import Path
import textwrap as tw
# load configuration file
#conf = ConfigParser()
#CONFPATH = Path(__file__).parent / "context.conf"
#conf.read(CONFPATH)
DEF_SEP = "\n\n"
TERM_DESC_SEP = " - "
REF_SEP = "§"
SEC_SEP = "#"
class color:
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
GRAY = '\033[90m'
END = '\033[0m'
class Context:
def __init__(self, defs=None):
self.defs = defs or []
def add(self, term, desc, ref=None):
self.defs.append(term, desc, ref)
def define(self, term):
term = term.strip()
for def_ in self.defs:
if def_[0].lower() == term.lower():
termsize = shutil.get_terminal_size((80, 20))
print(Context.format_def(def_, termsize.columns))
break
else:
print(color.RED + 'def not found !!', file=sys.stderr)
def update(self, context):
self.defs.extend(context.defs)
@classmethod
def from_path(csl, path):
super_context= csl()
for p, folders, files in os.walk(path):
p = Path(p)
for f in files:
context = csl.from_file(p / f)
super_context.update(context)
return super_context
@classmethod
def from_file(cls, path):
with open(path) as f:
lines = f.readlines()
# for now filter sections
rem_secs = lambda line : not line.strip().startswith(SEC_SEP)
lines = filter(rem_secs, lines)
content = ''.join(lines)
defs = content.split(DEF_SEP)
defs = map(Context.filter_def, defs)
defs = list(defs)
return cls(defs)
@staticmethod
def filter_def(def_):
# split term, description and references
def_ = def_.strip()
try:
term, rest = def_.split(TERM_DESC_SEP, maxsplit=1)
desc, *refs = rest.split(REF_SEP)
except ValueError:
print('Could not read:', def_, file=sys.stderr)
term = def_
desc = ''
refs = []
else:
# remove double and surrounding white spaces
clean = lambda str_ : ' '.join([part for part in str_.split() if part])
term = clean(term)
desc = clean(desc)
refs = map(clean, refs)
return term, desc, refs
@staticmethod
def format_def(def_, width):
term, desc, refs = def_
lines = []
w = tw.TextWrapper(
initial_indent = color.BOLD + color.YELLOW + term + color.END + TERM_DESC_SEP,
subsequent_indent = ' ' * (len(term) + len(TERM_DESC_SEP)),
width = width,
)
lines.extend(w.wrap(desc))
for ref in refs:
ref = color.GRAY + REF_SEP + " " + ref + color.END
lines.append(tw.indent(ref, prefix=w.subsequent_indent))
return '\n'.join(lines)
def __str__(self):
termsize = shutil.get_terminal_size((80, 20))
def_blocks = []
#max_len = max(len(term) for term, *_ in self.defs)
print('yea')
for def_ in self.defs:
text = Context.format_def(def_, termsize.columns)
def_blocks.append(text)
return DEF_SEP.join(def_blocks)
if __name__ == "__main__":
PATH = "~/Documents/context"
PATH = Path(PATH).expanduser()
c = Context.from_path(PATH)
if sys.argv[1] == 'define':
for term in sys.argv[2:]:
c.define(term)
elif sys.argv[1] == 'list':
for term,_,_ in c.defs:
print(term)
else:
raise Exception("wrong argument: " + str(sys,argv[1]))