288 lines
6.4 KiB
Python
288 lines
6.4 KiB
Python
|
|
#!/usr/bin/env python
|
||
|
|
|
||
|
|
from collections import Counter
|
||
|
|
import sys, re, os, enum, itertools, shlex
|
||
|
|
from functools import partial, partialmethod
|
||
|
|
from time import sleep
|
||
|
|
from dataclasses import dataclass, field
|
||
|
|
from operator import itemgetter
|
||
|
|
from defl import log, cl, Dath, Time, Run, sp, ShQu
|
||
|
|
from defl._typing_ import *
|
||
|
|
from defl._typing_ import T, F, N, U
|
||
|
|
from defl._rich_ import *
|
||
|
|
from defl._pydantic_ import *
|
||
|
|
import defl
|
||
|
|
from pathlib import Path as PLPath
|
||
|
|
from defl._path._temp_ import TempPath
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
|
||
|
|
|
||
|
|
def test_001(): ...
|
||
|
|
|
||
|
|
|
||
|
|
def test_002():
|
||
|
|
a = Dath('a')
|
||
|
|
assert isinstance(a._path, PLPath)
|
||
|
|
|
||
|
|
|
||
|
|
def test_003():
|
||
|
|
@dataclass(slots=T, kw_only=T, frozen=F)
|
||
|
|
class A:
|
||
|
|
a: Dath
|
||
|
|
|
||
|
|
a = A(a=Dath('a'))
|
||
|
|
assert isinstance(a, A)
|
||
|
|
assert isinstance(a.a, Dath)
|
||
|
|
assert isinstance(a.a._path, PLPath)
|
||
|
|
|
||
|
|
|
||
|
|
def test_004():
|
||
|
|
a = str(Dath('~'))
|
||
|
|
b = str(PLPath('~'))
|
||
|
|
assert a != b
|
||
|
|
|
||
|
|
|
||
|
|
def test_005():
|
||
|
|
a = str(Dath('~'))
|
||
|
|
b = os.environ['HOME']
|
||
|
|
assert a == b
|
||
|
|
|
||
|
|
|
||
|
|
def test_006():
|
||
|
|
p = '/a/b/c/d e'
|
||
|
|
a = shlex.quote(str(PLPath(p)))
|
||
|
|
b = Dath(p).quote()
|
||
|
|
assert a == b
|
||
|
|
|
||
|
|
|
||
|
|
def test_007():
|
||
|
|
assert repr(Dath('a')) == Dath('a').__repr__() == 'λ(a)'
|
||
|
|
|
||
|
|
|
||
|
|
def test_008():
|
||
|
|
a = str(Dath('a') / 'b/c/d/e')
|
||
|
|
b = 'a/b/c/d/e'
|
||
|
|
assert a == b
|
||
|
|
|
||
|
|
|
||
|
|
def test_009():
|
||
|
|
Dath().is_file()
|
||
|
|
Dath().is_dir()
|
||
|
|
|
||
|
|
|
||
|
|
def test_010():
|
||
|
|
a = Dath('/tmp/').chdir()
|
||
|
|
assert isinstance(a, Dath)
|
||
|
|
a = a.absolute().str
|
||
|
|
b = '/tmp'
|
||
|
|
assert a == b
|
||
|
|
|
||
|
|
|
||
|
|
def test_012():
|
||
|
|
assert Dath('a') == PLPath('a')
|
||
|
|
|
||
|
|
|
||
|
|
def test_013():
|
||
|
|
assert Dath('a') == Dath('a')
|
||
|
|
|
||
|
|
|
||
|
|
def test_014():
|
||
|
|
assert (a := PLPath('/').parts) == ('/',), a
|
||
|
|
assert (a := Dath('/').parts) == ('/',), a
|
||
|
|
assert (a := (Dath('/') / 'aa/bb/').parts) == ('/', 'aa', 'bb'), a
|
||
|
|
assert (a := (Dath('/') / 'aa/bb').parts) == ('/', 'aa', 'bb'), a
|
||
|
|
|
||
|
|
assert (a := PLPath('.').parts) == (), a
|
||
|
|
assert (a := Dath('.').parts) == (), a
|
||
|
|
assert (a := (Dath('.') / 'aa/bb/').parts) == ('aa', 'bb'), a
|
||
|
|
assert (a := (Dath('.') / 'aa/bb').parts) == ('aa', 'bb'), a
|
||
|
|
|
||
|
|
|
||
|
|
def test_015():
|
||
|
|
a = [
|
||
|
|
"Dath('/aa/bb/cc/')",
|
||
|
|
"Dath('/aa') / 'bb' / 'cc'",
|
||
|
|
"Dath('/aa') / 'bb/cc'",
|
||
|
|
"Dath('/') / 'aa/bb/cc'",
|
||
|
|
"Dath('/', 'aa', 'bb', 'cc')",
|
||
|
|
"Dath(['/', 'aa'], ['bb', 'cc'])",
|
||
|
|
"Dath('/', ['aa', 'bb'], 'cc')",
|
||
|
|
"Dath('/', 'aa/bb', ['cc'])",
|
||
|
|
"Dath('/aa/bb', ['cc'])",
|
||
|
|
"Dath('/aa/bb/cc', '')",
|
||
|
|
"Dath('/aa/bb/cc/', '')",
|
||
|
|
"Dath(['/', 'aa', 'bb', 'cc'])",
|
||
|
|
"Dath(['/', 'aa', 'bb', 'cc'], '///')",
|
||
|
|
"Dath(['///', 'aa', 'bb', 'cc'], '///')",
|
||
|
|
"Dath(tuple(['/', 'aa', 'bb', 'cc']))",
|
||
|
|
]
|
||
|
|
for i in a:
|
||
|
|
ev = eval(i)
|
||
|
|
assert ev.parts == ('/', 'aa', 'bb', 'cc'), (i, ev.parts)
|
||
|
|
|
||
|
|
|
||
|
|
def test_016():
|
||
|
|
a = [
|
||
|
|
"Dath('aa/bb/cc/')",
|
||
|
|
"Dath('aa', 'bb', 'cc')",
|
||
|
|
"Dath(tuple(['aa', 'bb', 'cc']))",
|
||
|
|
]
|
||
|
|
for i in a:
|
||
|
|
ev = eval(i)
|
||
|
|
assert ev.parts == ('aa', 'bb', 'cc'), (i, ev.parts)
|
||
|
|
|
||
|
|
|
||
|
|
def test_017():
|
||
|
|
a = [
|
||
|
|
"Dath(['00/11///22/33'])",
|
||
|
|
"Dath(['00'], '/11///22/33')",
|
||
|
|
"Dath(['00/11///22///33'])",
|
||
|
|
"Dath('00', '/', '11', '///', '/', '/', '/', '/', '/', '22', '/', '33')",
|
||
|
|
"Dath(['00', '/', '11', '/', '/', '/', '///', '///', '/', '22', '/', '33', '///'])",
|
||
|
|
]
|
||
|
|
for i in a:
|
||
|
|
ev = eval(i)
|
||
|
|
assert ev.parts == ('00', '11', '22', '33'), (i, ev.parts)
|
||
|
|
|
||
|
|
|
||
|
|
def test_018():
|
||
|
|
a = Dath('/00/a/22/33/00/b/22/33/00/c/22/33/00/d/22/33')
|
||
|
|
b = a.regFindIter('^.00/.*c/22')
|
||
|
|
assert [x for x in b][0].span() == (0, 30)
|
||
|
|
b = a.regFindIter('^(.*)$')
|
||
|
|
assert [x for x in b][0].span() == (0, 44)
|
||
|
|
b = a.regSub('^.00/?', '/')
|
||
|
|
assert str(b) == '/a/22/33/00/b/22/33/00/c/22/33/00/d/22/33'
|
||
|
|
b = a.regSub('.*/')
|
||
|
|
assert str(b) == '33'
|
||
|
|
b = a.regSearch('/22/33/00/b/22/33/00/c/22/33/00/d/22')
|
||
|
|
assert b
|
||
|
|
|
||
|
|
|
||
|
|
def test_019():
|
||
|
|
@dantDC()
|
||
|
|
class A:
|
||
|
|
path: PLPath | Dath
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def From(cls, path: Dath | PLPath) -> Self:
|
||
|
|
return cls(path=path)
|
||
|
|
|
||
|
|
a = A.From(path=Dath('a'))
|
||
|
|
assert inst(a.path, Dath)
|
||
|
|
with pytest.raises(AttributeError):
|
||
|
|
# | AttributeError: 'Dath' object has no attribute '_path'
|
||
|
|
a._path # | TODO for some reason pydantic is not calling `Dath.__init__()`
|
||
|
|
|
||
|
|
a = A.From(path=PLPath('a'))
|
||
|
|
assert inst(a, A)
|
||
|
|
assert inst(a.path, PLPath)
|
||
|
|
b = Dath(a.path)
|
||
|
|
assert b._path == PLPath('a')
|
||
|
|
|
||
|
|
|
||
|
|
def test_020():
|
||
|
|
tp = TempPath(path=Dath('/a/b/c/')).setExt('d').setName('e')
|
||
|
|
tpg = tp.generate()
|
||
|
|
|
||
|
|
tpg1 = next(tpg)
|
||
|
|
assert tpg1.parent == Dath('/a/b/c')
|
||
|
|
r = r'^e_[0-9]{6}[0-9]{6}.d'
|
||
|
|
assert re.search(r, tpg1.name), f'{tpg1.name} != {r}'
|
||
|
|
|
||
|
|
tpg2 = next(tpg)
|
||
|
|
assert tpg2.parent == Dath('/a/b/c')
|
||
|
|
r = r'^e_[0-9]{6}[0-9]{6}_00001.d'
|
||
|
|
assert re.search(r, tpg2.name), f'{tpg2.name} != {r}'
|
||
|
|
|
||
|
|
|
||
|
|
def test_020():
|
||
|
|
with pytest.raises(TempPath.TempPathNoIndexInFmtError):
|
||
|
|
tp = TempPath(path=Dath('/a/b/c/'), fmt=[]).setExt('d').setName('e')
|
||
|
|
|
||
|
|
|
||
|
|
def test_021():
|
||
|
|
defl.sp.tmp.makeTemp('a', autoRemove=T, create=F, direct=F)
|
||
|
|
|
||
|
|
|
||
|
|
# def test_022():
|
||
|
|
# def test_023():
|
||
|
|
# def test_024():
|
||
|
|
# def test_025():
|
||
|
|
# def test_026():
|
||
|
|
# def test_027():
|
||
|
|
# def test_028():
|
||
|
|
# def test_029():
|
||
|
|
# def test_030():
|
||
|
|
# def test_031():
|
||
|
|
# def test_032():
|
||
|
|
# def test_033():
|
||
|
|
# def test_034():
|
||
|
|
# def test_035():
|
||
|
|
# def test_036():
|
||
|
|
# def test_037():
|
||
|
|
# def test_038():
|
||
|
|
# def test_039():
|
||
|
|
# def test_040():
|
||
|
|
# def test_041():
|
||
|
|
# def test_042():
|
||
|
|
# def test_043():
|
||
|
|
# def test_044():
|
||
|
|
# def test_045():
|
||
|
|
# def test_046():
|
||
|
|
# def test_047():
|
||
|
|
# def test_048():
|
||
|
|
# def test_049():
|
||
|
|
# def test_050():
|
||
|
|
# def test_051():
|
||
|
|
# def test_052():
|
||
|
|
# def test_053():
|
||
|
|
# def test_054():
|
||
|
|
# def test_055():
|
||
|
|
# def test_056():
|
||
|
|
# def test_057():
|
||
|
|
# def test_058():
|
||
|
|
# def test_059():
|
||
|
|
# def test_060():
|
||
|
|
# def test_061():
|
||
|
|
# def test_062():
|
||
|
|
# def test_063():
|
||
|
|
# def test_064():
|
||
|
|
# def test_065():
|
||
|
|
# def test_066():
|
||
|
|
# def test_067():
|
||
|
|
# def test_068():
|
||
|
|
# def test_069():
|
||
|
|
# def test_070():
|
||
|
|
# def test_071():
|
||
|
|
# def test_072():
|
||
|
|
# def test_073():
|
||
|
|
# def test_074():
|
||
|
|
# def test_075():
|
||
|
|
# def test_076():
|
||
|
|
# def test_077():
|
||
|
|
# def test_078():
|
||
|
|
# def test_079():
|
||
|
|
# def test_080():
|
||
|
|
# def test_081():
|
||
|
|
# def test_082():
|
||
|
|
# def test_083():
|
||
|
|
# def test_084():
|
||
|
|
# def test_085():
|
||
|
|
# def test_086():
|
||
|
|
# def test_087():
|
||
|
|
# def test_088():
|
||
|
|
# def test_089():
|
||
|
|
# def test_090():
|
||
|
|
# def test_091():
|
||
|
|
# def test_092():
|
||
|
|
# def test_093():
|
||
|
|
# def test_094():
|
||
|
|
# def test_095():
|
||
|
|
# def test_096():
|
||
|
|
# def test_097():
|
||
|
|
# def test_098():
|
||
|
|
# def test_099():
|
||
|
|
# def test_100():
|