-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.py
275 lines (215 loc) · 7.33 KB
/
func.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import datetime
import difflib
import functools
import inspect
import os
import readline
from ast import literal_eval as literal_eval
from pathlib import Path
from shlex import split as shlex
from traceback import print_exc
from typing import Any, Dict, Optional
from pygments import highlight
from pygments.formatters import TerminalFormatter
from pygments.lexers import get_lexer_for_filename
from pygments.util import ClassNotFound
import completiontypes as ct
global commands, buffers, curbuf
commands = {}
buffers = {}
curbuf = ""
modules = []
debug = False
class FileObject:
def __init__(self, fn):
self.filename = fn
self.buffer = []
self.saved = True
self.language = ""
self.unnamed = False
class AttrDict:
def __init__(self, obj):
for k, v in obj.items():
setattr(self, k, v)
def __repr__(self):
return str(self.__dict__)
errorbuffer = FileObject("NullFile")
def buffer() -> FileObject:
"""Return the active buffer"""
try:
return buffers[curbuf]
except:
return errorbuffer
class FnMeta:
def __init__(self, func) -> None:
self.func = func
self.name = func.__name__
self.help = getattr(func, "__doc__", "No help provided.")
self.line = inspect.getsourcelines(func)[-1]
self.file = Path(*Path(inspect.getsourcefile(func)).parts[-2:])
sig = inspect.signature(func)
self.args = [param.name for param in sig.parameters.values()]
self.annotations = {
param.name: param.annotation
for param in sig.parameters.values()
if param.annotation != param.empty
}
def __call__(self, *args, **kwargs):
return self.func(*args, **kwargs)
def meta(self):
s = []
for name, annotation in self.annotations.items():
s.append(f"{name}: {annotation.__qualname__}")
return f"{self.name}({', '.join(s)}) {self.help}\n{self.file}:{self.line}"
def globalcommand(name=None):
def decorator(func):
meta = FnMeta(func)
if name:
meta.name = name
commands[meta.name] = meta
@functools.wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
return decorator
def localcommand(name=None):
def decorator(func):
meta = FnMeta(func)
if name:
meta.name = name
filename = str(Path(meta.file).parts[-1]).split(".")
meta.name = f"{filename[0]}.{meta.name}"
commands[meta.name] = meta
@functools.wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
return decorator
def r_listdir(dir):
"""Recurive Directory Listing"""
o = []
for i in os.listdir(dir):
if os.path.isdir(os.path.join(dir, i)):
o.append(os.path.join(dir, i))
o.extend(reversed(r_listdir(os.path.join(dir, i))))
else:
o.append(os.path.join(dir, i))
return sorted(o, key=len, reverse=True)
class InputCompleter:
matches = []
def completer(self, text: str, state: int):
full = readline.get_line_buffer()
try:
parsed = shlex(full)
except:
parsed = shlex(full + '"') # fixes an error
arg = full.count(" ") - 1
if len(parsed) == 0:
parsed.append("")
cmd = parsed[0]
parsed = parsed[-1]
if state == 0:
if arg == -1:
self.matches = [
cmd for cmd in list(commands.keys()) if cmd.startswith(text)
]
# matches.extend(difflib.get_close_matches(text,list(commands.keys()),n=len(commands.keys())))
if cmd in commands.keys():
if arg >= 0 and arg <= len(commands[cmd].args):
atype = commands[cmd].annotations[commands[cmd].args[arg]]
if hasattr(atype, "complete"):
self.matches = atype.complete(
text
) # allows for customizable completions in modules
self.matches = list(set(self.matches)) # remove duplicates
try:
return self.matches[state]
except Exception as e:
# print_exc()
return None
def getreltime(filename):
if not os.path.exists(filename):
return "Never"
ts = os.path.getmtime(filename)
now = datetime.datetime.now()
dt = datetime.datetime.fromtimestamp(ts)
delta = now - dt
final = ""
if delta.days >= 365:
years = delta.days // 365
final += f"{years} year{'s' if years > 1 else ''} "
delta -= datetime.timedelta(days=years * 365)
if delta.days >= 30:
months = delta.days // 30
final += f"{months} month{'s' if months > 1 else ''} "
delta -= datetime.timedelta(days=months * 30)
if delta.days > 0:
final += f"{delta.days} day{'s' if delta.days > 1 else ''} "
if delta.seconds >= 3600:
hours = delta.seconds // 3600
final += f"{hours} hour{'s' if hours > 1 else ''} "
delta -= datetime.timedelta(seconds=hours * 3600)
if delta.seconds >= 60:
minutes = delta.seconds // 60
final += f"{minutes} minute{'s' if minutes > 1 else ''} "
delta -= datetime.timedelta(seconds=minutes * 60)
if delta.seconds > 0:
final += f"{delta.seconds} second{'s' if delta.seconds > 1 else ''} "
if final == "":
final = "Just Now"
return final
def getbytes(s):
# Join the input string with newline characters
s = "\n".join(s)
# Encode the string to bytes and get the length
size_in_bytes = len(s.encode("utf-8"))
# Convert to KB, MB, GB
size_in_kb = size_in_bytes / 1024
size_in_mb = size_in_kb / 1024
size_in_gb = size_in_mb / 1024
if size_in_gb > 1:
return str(round(size_in_gb, 2)) + " GB"
elif size_in_mb > 1:
return str(round(size_in_mb, 2)) + " MB"
elif size_in_kb > 1:
return str(round(size_in_kb, 2)) + " KB"
else:
return str(round(size_in_bytes, 2)) + " Bytes"
def openfile(fn="", new=False):
global buffer, curbuf
curbuf = fn
if not fn:
fn = f"New File {len(buffers.values())}"
buf = buffer()
if fn == "" or new == True or not os.path.exists(fn):
buf = FileObject(fn)
buf.unnamed = True
else:
with open(fn, "r") as f:
buf = FileObject(fn)
buf.buffer = str(f.read()).split("\n")
buffers[fn] = buf
def autotype(obj) -> Any:
try:
return literal_eval(obj)
except:
return obj
def highlight_code(code, filename):
try:
lexer = get_lexer_for_filename(filename, stripall=True)
formatter = TerminalFormatter()
return str(highlight(code, lexer, formatter))[:-1]
except ClassNotFound:
return code
class ConfigClass:
def exec(self, command, *args, **kwargs):
commands[command](*args, **kwargs)
def extendpromptvars(promptvars):
for i in modules:
if hasattr(i, "promptvars"):
for k, v in i.promptvars().items():
promptvars[k] = (
v # might not be the fastest approach but when has safe ever been meant for speed?
)
config = ConfigClass()
prompt = "$savestatus$bytes $filename: "