Source code for jgdv.structs.dkey.special.path_key

 1#!/usr/bin/env python3
 2"""
 3
 4"""
 5
 6# Imports:
 7from __future__ import annotations
 8
 9# ##-- stdlib imports
10import datetime
11import enum
12import functools as ftz
13import itertools as itz
14import logging as logmod
15import pathlib as pl
16import re
17import string
18import time
19import types
20import weakref
21from uuid import UUID, uuid1
22
23# ##-- end stdlib imports
24
25# ##-- 1st party imports
26from jgdv.structs.strang import CodeReference
27
28# ##-- end 1st party imports
29
30from .._interface import DKeyMark_e
31from .._util._interface import ExpInst_d, InstructionFactory_p
32from ..dkey import DKey
33from ..keys import MultiDKey, NonDKey, SingleDKey
34
35# ##-- types
36# isort: off
37import abc
38import collections.abc
39from typing import TYPE_CHECKING, Generic, cast, assert_type, assert_never
40# Protocols:
41from typing import Protocol, runtime_checkable
42# Typing Decorators:
43from typing import no_type_check, final, override, overload
44
45if TYPE_CHECKING:
46    from jgdv import Maybe, Ident, RxStr, Rx
47    from typing import Final
48    from typing import ClassVar, Any, LiteralString
49    from typing import Never, Self, Literal
50    from typing import TypeGuard
51    from collections.abc import Iterable, Iterator, Callable, Generator
52    from collections.abc import Sequence, Mapping, MutableMapping, Hashable
53##--|
54
55# isort: on
56# ##-- end types
57
58##-- logging
59logging = logmod.getLogger(__name__)
60##-- end logging
61
[docs] 62class PathDKey(DKey[list], mark=pl.Path): 63 """ 64 A Simple key that always expands to a path, and is then normalised 65 """ 66 __slots__ = () 67 68 def __init__(self, *args, **kwargs): 69 super().__init__(*args, **kwargs) 70 self.data.convert = "p" 71 self.data.expansion_type = pl.Path 72 self.data.typecheck = pl.Path 73
[docs] 74 def exp_coerce_h(self, inst:ExpInst_d, factory:InstructionFactory_p, opts:dict) -> Maybe[ExpInst_d]: 75 val = pl.Path(inst.value) 76 if 'relative' not in opts: 77 val = val.expanduser().resolve() 78 return factory.literal_inst(val)
79
[docs] 80 def exp_final_h(self, inst:ExpInst_d, root:Maybe[ExpInst_d], factory:InstructionFactory_p, opts:dict) -> Maybe[ExpInst_d]: 81 return factory.literal_inst(inst.value)