evdx 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
evdx/__init__.py ADDED
@@ -0,0 +1,2 @@
1
+ def main() -> None:
2
+ print("Hello from common!")
evdx/checker.py ADDED
@@ -0,0 +1,44 @@
1
+ from dataclasses import dataclass;
2
+ from evdx.unstd import null;
3
+ from typing import Self;
4
+ import inspect;
5
+
6
+ @dataclass
7
+ class Checker():
8
+ _objList:tuple[object,...] = null;
9
+ _expectValAny:tuple[object, ...] = null;
10
+ _expectCnt:int = 0;
11
+
12
+ def onlyOneOf(self, *args:object) -> Self:
13
+ self._objList = args;
14
+ return self;
15
+
16
+ def expect(self, *val:object) -> Self:
17
+ self._expectValAny = val;
18
+ return self;
19
+
20
+ @staticmethod
21
+ def IsAsExcept(o:object, *valAny:object) -> bool:
22
+ for val in valAny:
23
+ if o is val:
24
+ return True
25
+ else:
26
+ if val is null or val is None or val is False or val is True:
27
+ continue
28
+ elif o == val:
29
+ return True
30
+ elif inspect.isfunction(val):
31
+ if val(o):
32
+ return True;
33
+ else:
34
+ continue;
35
+ else:
36
+ continue
37
+ return False
38
+
39
+ def check(self)->bool:
40
+ cnt = 0;
41
+ for o in self._objList:
42
+ if Checker.IsAsExcept(o, self._expectValAny):
43
+ cnt = cnt + 1
44
+ return cnt == self._expectCnt
evdx/errors.py ADDED
@@ -0,0 +1,3 @@
1
+ class ParamCheckError(Exception):
2
+ pass
3
+
evdx/pathutil.py ADDED
@@ -0,0 +1,126 @@
1
+ from __future__ import annotations;
2
+ from pathlib import Path;
3
+ from typing import List;
4
+ import os;
5
+ from os import PathLike;
6
+ from evdx import unstd;
7
+ from evdx.unstd import null;
8
+
9
+ #import unstd;
10
+ #import shutil;
11
+ #import time;
12
+ #from pyex import null;
13
+
14
+
15
+ def home()->Path:
16
+ return Path.home();
17
+
18
+ def homer(identifier:str) -> Path:
19
+ _home = home()
20
+ name = "." + identifier
21
+ return _home.joinpath(name)
22
+
23
+ #也接受str 因为这种混合情景太常见了
24
+ def toStr(path:Path|str)->str:
25
+ return str(Path);
26
+
27
+ def normalize(path:Path|str) -> Path:
28
+ s = os.path.normpath(path)
29
+ return Path(s)
30
+
31
+ def unify(path:Path|str) -> Path:
32
+ s = os.path.abspath(path);
33
+ return cast(s);
34
+
35
+ def cast(path:Path|str) -> Path:
36
+ if(unstd.empty(path)):
37
+ return null;
38
+ elif(isinstance(path, str)):
39
+ return Path(path);
40
+ else:
41
+ assert isinstance(path, Path);
42
+ return path;
43
+
44
+ def exists(path:Path|str)->bool:
45
+ p = cast(path)
46
+ if(p.exists()):
47
+ return True;
48
+ elif(p.is_symlink()):
49
+ return True;
50
+ else:
51
+ return False;
52
+
53
+ def addStr(path:Path, extra:str)->Path:
54
+ s = str(path)
55
+ return Path(s + extra)
56
+
57
+
58
+ def parts(path:Path|str) -> List[str]:
59
+ s = toStr(path);
60
+ s = os.path.normpath(s)
61
+ return s.split(os.sep)
62
+
63
+ #获取文件名或目录名,不访问文件系统
64
+ #注意parts的首个元素,linux上是空字符串,windows是C:/
65
+ #TODO normpath会保留windows上的末尾反斜杠
66
+ #TODO 考虑用Path.parts
67
+ def getName(path:Path|str)->str:
68
+ segs = parts(path)
69
+ return segs[-1]
70
+
71
+ def upper(path:Path|str) -> Path:
72
+ return getParent(path);
73
+
74
+ def getParent(path:Path|str) -> Path:
75
+ segs = parts(path);
76
+ newsegs = segs[:-1];
77
+ return Path(*newsegs);
78
+
79
+ def splitBaseName(path:Path|str) -> tuple[Path,str]:
80
+ segs = parts(path);
81
+ name = segs[-1];
82
+ base = segs[:-1];
83
+ basePath = Path(*base);
84
+ return (basePath, name)
85
+
86
+ def altName(path:Path|str, newName:str) -> Path:
87
+ segs = parts(path);
88
+ segs[-1] = newName;
89
+ return Path(*segs);
90
+
91
+
92
+ class VPath:
93
+ __slots__ = ("_path",)
94
+
95
+ def __init__(self, path: str | PathLike[str]) -> None:
96
+ self._path = Path(path)
97
+
98
+ # 1. 能被标准库当路径使用
99
+ def __fspath__(self) -> str:
100
+ return str(self._path)
101
+
102
+ # 2. 路径拼接,保持链式运算返回 VPath
103
+ def __truediv__(self, other: str | PathLike[str]) -> VPath:
104
+ return VPath(self._path / other)
105
+
106
+ # 3. 取父目录,返回 VPath 而非裸 Path
107
+ @property
108
+ def parent(self) -> VPath:
109
+ return VPath(self._path.parent)
110
+
111
+ # 4. 基本类型判断
112
+ def is_file(self) -> bool:
113
+ return self._path.is_file()
114
+
115
+ def is_dir(self) -> bool:
116
+ return self._path.is_dir()
117
+
118
+ # 5. 基本读写(按需保留,不需要可删)
119
+ def read_text(self, encoding: str = "utf-8") -> str:
120
+ return self._path.read_text(encoding=encoding)
121
+
122
+ def write_text(self, data: str, encoding: str = "utf-8") -> int:
123
+ return self._path.write_text(data, encoding=encoding)
124
+
125
+ def __repr__(self) -> str:
126
+ return f"VPath({self._path!r})"
evdx/pfs.py ADDED
@@ -0,0 +1,265 @@
1
+ from dataclasses import dataclass
2
+ from pathlib import Path;
3
+ from typing import Unpack,TypedDict,Callable;
4
+ from pyex import null;
5
+ import os;
6
+ import time;
7
+ import shutil;
8
+ from evdx import pathutil;
9
+ from evdx import unstd;
10
+ from evdx.errors import ParamCheckError;
11
+ from evdx.checker import Checker;
12
+ #import unstd;
13
+ #from datetime import datetime;
14
+
15
+ EXISTS_ERROR = 0; #存在则报错
16
+ EXISTS_TRUST= 1; #存在则不碰。如果是目录,不碰整个目录树。
17
+ EXISTS_REMOVE = 2; #存在则删除
18
+ EXISTS_MOVE = 3; #存在则重命名
19
+ EXISTS_AVOID = 4; #躲开已存在的文件
20
+
21
+ RENAME_DOT_TSTAMP = 1; #NAME.1234
22
+ RENAME_DEL_TSTAMP = 2; #delete-NAME-1234
23
+ RENAME_MOVE_TSTAMP = 3; #moved-NAME-1234
24
+
25
+ #根据重命名风格生成新名,返回整路径
26
+ def forRename(path:Path|str, flavor:int):
27
+ base, name = pathutil.splitBaseName(path);
28
+ newName:str;
29
+ now_ns = time.time_ns();
30
+ if(flavor == RENAME_DOT_TSTAMP):
31
+ newName = f"{name}.{now_ns}";
32
+ elif(flavor == RENAME_DEL_TSTAMP):
33
+ newName = f"delete-{name}-{now_ns}";
34
+ elif(flavor == RENAME_MOVE_TSTAMP):
35
+ newName = f"moved-{name}-{now_ns}";
36
+ else:
37
+ raise ValueError(flavor);
38
+ return base.joinpath(newName);
39
+
40
+
41
+ @dataclass
42
+ class CopyArgs():
43
+ into:Path|str = null;
44
+ newName:str = null;
45
+ dest:Path|str = null;
46
+ recursive:bool = False;
47
+ #TODO had better be EXISTS_ERROR, but python not ...
48
+ exists:int = 0;
49
+ renameForAvoid:int = RENAME_DOT_TSTAMP;
50
+ renameForMove:int = RENAME_MOVE_TSTAMP;
51
+
52
+
53
+ def evalDest(self, srcName:str)->Path:
54
+ if self.dest is not null:
55
+ assert self.into is null and self.newName is null;
56
+ return pathutil.cast(self.dest);
57
+ elif self.into is not null:
58
+ if(self.newName is not null):
59
+ destName = self.newName
60
+ else:
61
+ destName = srcName;
62
+ intoDir = pathutil.cast(self.into);
63
+ return intoDir.joinpath(destName);
64
+ else:
65
+ return null;
66
+
67
+ class CopyOpt(TypedDict, total=False):
68
+ into:Path|str;
69
+ newName:str;
70
+ dest:Path|str;
71
+ recursive:bool;
72
+ exists:int;
73
+ renameForAvoid:int;
74
+ renameForMove:int;
75
+
76
+ #确保一个目录存在
77
+ def verifyDir(path: str|Path):
78
+ p = Path(path)
79
+ p.mkdir(parents=True, exist_ok=True)
80
+
81
+
82
+
83
+ #TODO 复制目录属性,包括复制到自动创建的父目录
84
+ #复制一个文件或目录 非递归 自动创建父目录
85
+ def copySimple(src:Path, dest:Path):
86
+ if(src.is_file()):
87
+ shutil.copy2(src, dest);
88
+ elif(src.is_dir()):
89
+ dest.mkdir(parents=True)
90
+ else:
91
+ raise TypeError("must be a file or directory" + str(src))
92
+
93
+
94
+ #新名字的相对路径是在操作的文件,最好只是一个文件名。
95
+ #python风格不是
96
+ def rename(src:Path|str, newName:str):
97
+ newPath = pathutil.altName(src, newName);
98
+ os.rename(src, newPath);
99
+
100
+
101
+ #复制
102
+ def copy(src:Path|str, **kwargs:Unpack[CopyOpt]) -> Path:
103
+ srcP = pathutil.cast(src);
104
+ args = CopyArgs(**kwargs);
105
+ srcName = pathutil.getName(srcP);
106
+ destP = args.evalDest(srcName);
107
+ #先检测是否存在
108
+ if(pathutil.exists(destP)):
109
+ if(args.exists == EXISTS_ERROR):
110
+ raise FileExistsError(destP);
111
+ #策略:删除
112
+ elif(args.exists == EXISTS_REMOVE):
113
+ if(destP.is_file() or destP.is_dir()):
114
+ remove(destP, recursive=True);
115
+ else:
116
+ raise TypeError("not support yet:"+str(destP));
117
+ #策略:挪开
118
+ elif(args.exists == EXISTS_MOVE):
119
+ moveTo = forRename(destP, args.renameForMove);
120
+ shutil.move(destP, moveTo);
121
+ #策略:躲开
122
+ elif(args.exists == EXISTS_AVOID):
123
+ destP = forRename(destP, args.renameForAvoid);
124
+ elif(args.exists == EXISTS_TRUST):
125
+ #提前返回
126
+ return destP;
127
+ else:
128
+ raise ValueError(args.exists)
129
+ #至此,目标路径一定不存在
130
+ if(srcP.is_file()):
131
+ copySimple(srcP, destP);
132
+ elif(srcP.is_dir()):
133
+ if(not args.recursive):
134
+ copySimple(srcP, destP);
135
+ else:
136
+ shutil.copytree(srcP, destP);
137
+ else:
138
+ raise TypeError("must be a file or directory:"+str(src));
139
+
140
+ return destP;
141
+
142
+
143
+ @dataclass
144
+ class DelArgs():
145
+ recursive:bool = False;
146
+
147
+ class DelOpt(TypedDict, total=False):
148
+ recursive:bool;
149
+
150
+ def remove(path:Path|str, **kwargs:Unpack[DelOpt]):
151
+ args = DelArgs(**kwargs);
152
+ p = pathutil.cast(path);
153
+ if(p.is_file()):
154
+ os.remove(p)
155
+ elif(p.is_dir()):
156
+ if(not args.recursive):
157
+ os.rmdir(p)
158
+ else:
159
+ shutil.rmtree(p)
160
+ else:
161
+ raise TypeError("not a file or directory:" + str(p));
162
+
163
+
164
+ @dataclass
165
+ class MoveArgs():
166
+ pickFn:Callable[[Path], int] = null;
167
+
168
+
169
+ @dataclass
170
+ class RCtrl():
171
+ enter:bool = True;
172
+ depth:int = 0;
173
+
174
+ @dataclass
175
+ class TransArgs():
176
+ src:Path|str = null;
177
+ srcUnder:Path|str = null;
178
+ dest:Path|str = null;
179
+ destUnder:Path|str = null;
180
+ forEach:Callable[[Path, Path, RCtrl], None] = null;
181
+ depth:int = 0;
182
+ def check(self)->None:
183
+ assert Checker().onlyOneOf(self.srcUnder, self.src).expect(unstd.notNN).check()
184
+ assert Checker().onlyOneOf(self.destUnder, self.dest).expect(unstd.notNN).check()
185
+
186
+ def hasSrcPath(self) -> Path:
187
+ path = self.src;
188
+ if(unstd.NN(path)):
189
+ return null;
190
+ return pathutil.cast(path);
191
+
192
+ def hasDestPath(self) ->Path:
193
+ path = self.dest;
194
+ if(unstd.NN(path)):
195
+ return null;
196
+ return pathutil.cast(path)
197
+
198
+ def hasSrcUnder(self) -> Path:
199
+ under = self.srcUnder;
200
+ if(unstd.NN(under)):
201
+ return null;
202
+ return pathutil.cast(under);
203
+
204
+ def hasDestUnder(self) -> Path:
205
+ under = self.destUnder;
206
+ if(unstd.NN(under)):
207
+ return null;
208
+ return pathutil.cast(under);
209
+
210
+ class TransOpt(TypedDict, total=False):
211
+ src:Path|str;
212
+ srcUnder:Path|str;
213
+ dest:Path|str;
214
+ destUnder:Path|str;
215
+ forEach:Callable[[Path, Path, RCtrl], None];
216
+ depth:int;
217
+
218
+
219
+ #遍历src指定的目录,从上自下逐个执行args.forEach。
220
+ #如果ctrl.enter被设置为false,那么该节点往下的分支会被略过
221
+ #@_depth为了简化实现,不允许用户指定
222
+ def R2(**kwargs:Unpack[TransOpt]):
223
+ args = TransArgs(**kwargs);
224
+ args.check();
225
+ srcP:Path = args.hasSrcPath();
226
+ destP:Path;
227
+ ctrl:RCtrl = RCtrl();
228
+ srcUnder:Path = args.hasSrcUnder()
229
+ destUnder:Path = null;
230
+
231
+ if(not unstd.NN(srcP)):
232
+ destP = args.hasDestPath();
233
+ assert not unstd.NN(srcP);
234
+ if(not unstd.NN(args.forEach)):
235
+ ctrl.depth = args.depth;
236
+ args.forEach(srcP, destP, ctrl);
237
+ if(ctrl.enter and srcP.is_dir()):
238
+ srcUnder = srcP;
239
+ destUnder = destP;
240
+ else:
241
+ assert not unstd.empty(srcUnder) and not unstd.empty(destUnder)
242
+ srcUnder = pathutil.cast(args.srcUnder);
243
+ destUnder = pathutil.cast(args.destUnder);
244
+ #这里要求此R分支不被内部调用,即,只在API入口时存在该分支可能,后续内部都走src/dest
245
+ #而API进入时,_depth总是0
246
+
247
+ if srcUnder is null:
248
+ return;
249
+ with os.scandir(srcUnder) as entries:
250
+ for entry in entries:
251
+ srcSub = srcUnder.joinpath(entry.name);
252
+ destSub = destUnder.joinpath(entry.name);
253
+ R2(src=srcSub, dest=destSub, forEach=args.forEach, depth=args.depth);
254
+
255
+
256
+ @dataclass
257
+ class UnboxArgs():
258
+ remove:bool = False;
259
+
260
+ def unbox(path:Path|str):
261
+ p = pathutil.cast(path)
262
+ #必须是目录
263
+ if(!p.is_dir()):
264
+ raise TypeError("must a directory:"+str(p));
265
+
evdx/ppm.py ADDED
@@ -0,0 +1 @@
1
+ print("hello ppm")
evdx/unbox.py ADDED
@@ -0,0 +1,6 @@
1
+ import argparse;
2
+
3
+ def main():
4
+ parser = argparse.ArgumentParser(prog="unbox", description="unbox a directory");
5
+ parser.add_argument("path", help="the directory path to unbox");
6
+ print("done")
evdx/unstd.py ADDED
@@ -0,0 +1,32 @@
1
+ import time;
2
+ from typing import Any;
3
+
4
+
5
+ class _Null():
6
+ def __init__(self):
7
+ pass
8
+
9
+ null:Any = _Null()
10
+
11
+ def NN(o:object) -> bool:
12
+ if o is None or o is null:
13
+ return True;
14
+ else:
15
+ return False;
16
+ def notNN(o:object) -> bool:
17
+ return not NN(o);
18
+
19
+ #FIXME 暂时不要用了
20
+ def empty(o:object)->bool:
21
+ if not o:
22
+ return True;
23
+ elif o is null:
24
+ return True;
25
+ else:
26
+ return False;
27
+
28
+ def now_ms()->int:
29
+ return time.time_ns() // 1000_000;
30
+
31
+ def now_us()->int:
32
+ return time.time_ns() // 1000;
evdx/xbackup.py ADDED
@@ -0,0 +1,95 @@
1
+ from pathlib import Path;
2
+ from pyex import null;
3
+ from evdx import pfs;
4
+ from evdx import unstd;
5
+ from evdx import pathutil;
6
+ from typing import Self, override;
7
+ from datetime import datetime;
8
+
9
+ class DropBox():
10
+ __slots__ = ("_name", "_boxUnder", "_boxPath");
11
+
12
+ def __init__(self):
13
+ self._name:str = null;
14
+ self._boxUnder:Path = null;
15
+ self._boxPath:Path = null;
16
+ pass
17
+
18
+ def evalBoxPath(self) -> Path:
19
+ if self._boxPath is not null:
20
+ return self._boxPath;
21
+ if self._boxUnder is not null:
22
+ return self._boxUnder.joinpath(self._name)
23
+ raise Exception("missing box path");
24
+
25
+ def setBoxUnder(self, under:Path|str) ->Self :
26
+ self._boxUnder = pathutil.cast(under)
27
+ return self;
28
+
29
+ def setBoxPath(self, boxPath:Path|str) -> Self:
30
+ self._boxPath = pathutil.cast(boxPath)
31
+ return self;
32
+
33
+ def setName(self, name:str) -> Self:
34
+ self._name = name;
35
+ return self;
36
+
37
+
38
+ class CateBox(DropBox):
39
+ __slots__ = ("curCateRel");
40
+
41
+ def __init__(self):
42
+ super().__init__();
43
+ self._name = ".catebox";
44
+ self.curCateRel:str = null;
45
+
46
+ def use(self, cateRel:str):
47
+ self.curCateRel = cateRel;
48
+
49
+ def evalCurCate(self) -> Path:
50
+ assert self.curCateRel is not null;
51
+ boxPath = self.evalBoxPath()
52
+ catePath = boxPath.joinpath(self.curCateRel)
53
+ return catePath
54
+
55
+
56
+ def tryAutoSwitch(self):
57
+ pass
58
+
59
+ def cp(self, src:Path|str):
60
+ self.tryAutoSwitch();
61
+ curCate = self.evalCurCate();
62
+ pfs.copy(src, into=curCate, recursive=True);
63
+
64
+
65
+ class TimelineBox(CateBox):
66
+ __slots__ = ();
67
+ def __init__(self):
68
+ super().__init__();
69
+ self._name = ".timelinebox";
70
+
71
+ def next(self):
72
+ now = datetime.now()
73
+ cateRel = f"{now.year}-{now.month}/{now.day:02d}/{now.hour:02d}.{now.minute:02d}.{now.second:02d}.{now.microsecond//1000}"
74
+ super().use(cateRel)
75
+
76
+
77
+ class AutoTimelineBox(TimelineBox):
78
+ __slots__ = ("_lastTimestamp", "_interval");
79
+
80
+ def __init__(self):
81
+ super().__init__();
82
+ self._name:str = ".autotimelinebox";
83
+ self._lastTimestamp = 0;
84
+ self._interval = 1000;
85
+
86
+ @override
87
+ def tryAutoSwitch(self):
88
+ nowMs:int = unstd.now_ms();
89
+ if(nowMs - self._lastTimestamp >= self._interval):
90
+ super().next()
91
+ self._lastTimestamp = nowMs;
92
+
93
+
94
+
95
+
@@ -0,0 +1,9 @@
1
+ Metadata-Version: 2.3
2
+ Name: evdx
3
+ Version: 0.1.0
4
+ Summary: Add your description here
5
+ Author: evdx
6
+ Author-email: evdx <jiejinmv@163.com>
7
+ Requires-Python: >=3.12
8
+ Description-Content-Type: text/markdown
9
+
@@ -0,0 +1,13 @@
1
+ evdx/__init__.py,sha256=CG54O59EZ1y924eKBU94QxPGKLr7bCYT-TT1EtgcNwI,52
2
+ evdx/checker.py,sha256=yS0vImnhNOlNxl6RjzHarAaJkHoNunjx8yz32jSi014,1239
3
+ evdx/errors.py,sha256=V0QjO23ZeoZbKwFVowdpDe-4CkziQav0P1GwKsLaxp8,44
4
+ evdx/pathutil.py,sha256=wyePDBgu2SsizbSPvSHHBp7R7UIZO4WA-vEUNq3e4w8,3119
5
+ evdx/pfs.py,sha256=gWjNHWIltwEKFcpKBMBpF-EjAE0Nq-v4-hrgysHIVuQ,7795
6
+ evdx/ppm.py,sha256=y9TZQyn01xwvNJmg2P_pM2vu121qnO3fotmME4YWFSU,18
7
+ evdx/unbox.py,sha256=SWe8k_RkeyRCXINUv1auGWcqwuXVf5a7-LN3FtpFqpU,201
8
+ evdx/unstd.py,sha256=sWZ7hX3Auqq0K4jjD2-ig1RFgn3rGMJXGUX1ekgq6Hg,548
9
+ evdx/xbackup.py,sha256=xt8AYhHcBNSBpbgO26BaGTCDATotDVwNkiIkNQJlAoo,2511
10
+ evdx-0.1.0.dist-info/WHEEL,sha256=3OmCxFA0ffBIsX4x0o8CdfSvZAOvO3mzEG8U_z5TvV4,80
11
+ evdx-0.1.0.dist-info/entry_points.txt,sha256=Xt6JSllMcKLGUpfmReYkAxjO4w9_yqCwdZMq54YmFqA,43
12
+ evdx-0.1.0.dist-info/METADATA,sha256=WjumtH-FvL7tDrtNKiHrA21OTafbR4Px-0bnInVcxR8,199
13
+ evdx-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: uv 0.13.8
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,3 @@
1
+ [console_scripts]
2
+ unbox = evdx.unbox:main
3
+