omlish 0.0.0.dev234__py3-none-any.whl → 0.0.0.dev235__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
omlish/__about__.py CHANGED
@@ -1,5 +1,5 @@
1
- __version__ = '0.0.0.dev234'
2
- __revision__ = '174a570495a04d3166159c95ab0d35c050a32fcb'
1
+ __version__ = '0.0.0.dev235'
2
+ __revision__ = '4afeb0f0acadfd7890e6e6309be41fe7e65c3185'
3
3
 
4
4
 
5
5
  #
omlish/os/filemodes.py ADDED
@@ -0,0 +1,127 @@
1
+ # ruff: noqa: UP006 UP007
2
+ # @omlish-lite
3
+ import dataclasses as dc
4
+ import os
5
+ import typing as ta
6
+
7
+ from ..lite.check import check
8
+
9
+
10
+ @dc.dataclass(frozen=True)
11
+ class FileMode:
12
+ """
13
+ https://docs.python.org/3/library/functions.html#open
14
+
15
+ 'r' open for reading (default)
16
+ 'w' open for writing, truncating the file first
17
+ 'x' open for exclusive creation, failing if the file already exists
18
+ 'a' open for writing, appending to the end of file if it exists
19
+ 'b' binary mode
20
+ 't' text mode (default)
21
+ '+' open for updating (reading and writing)
22
+
23
+ ==
24
+
25
+ https://en.cppreference.com/w/cpp/io/c/fopen
26
+
27
+ "r" read Open a file for reading read from start return NULL and set error
28
+ "w" write Create a file for writing destroy contents create new
29
+ "a" append Append to a file write to end create new
30
+ "r+" read extended Open a file for read/write read from start return NULL and set error
31
+ "w+" write extended Create a file for read/write destroy contents create new
32
+ "a+" append extended Open a file for read/write write to end create new
33
+
34
+ ==
35
+
36
+ a (prs): w a
37
+ a (new): w
38
+ a+ (prs): r w a
39
+ a+ (new): r w
40
+
41
+ x (new): w
42
+ x+ (new): r w
43
+
44
+ w (prs): w
45
+ w (new): w
46
+ w+ (prs): r w
47
+ w+ (new): r w
48
+
49
+ r (prs): r
50
+ r+ (prs): r w
51
+ """
52
+
53
+ read: bool
54
+ write: bool
55
+
56
+ create: bool
57
+ exists: ta.Literal['beginning', 'truncate', 'fail', 'append']
58
+
59
+ binary: bool
60
+
61
+ #
62
+
63
+ def flags(self) -> int:
64
+ return (
65
+ (
66
+ os.O_RDWR if self.read and self.write else
67
+ os.O_WRONLY if self.write else
68
+ os.O_RDONLY if self.read else
69
+ 0
70
+ ) |
71
+ (os.O_CREAT if self.create else 0) |
72
+ (
73
+ os.O_APPEND if self.exists == 'append' else
74
+ os.O_EXCL if self.exists == 'fail' else
75
+ os.O_TRUNC if self.exists == 'truncate' else
76
+ 0
77
+ )
78
+ )
79
+
80
+ #
81
+
82
+ def render(self) -> str:
83
+ return ''.join([
84
+ (
85
+ 'a' if self.exists == 'append' else
86
+ 'x' if self.exists == 'fail' else
87
+ 'w' if self.exists == 'truncate' else
88
+ 'r'
89
+ ),
90
+ '+' if self.read and self.write else '',
91
+ 'b' if self.binary else '',
92
+ ])
93
+
94
+ @classmethod
95
+ def parse(cls, s: str) -> 'FileMode':
96
+ rwxa: ta.Literal['r', 'w', 'x', 'a', None] = None
97
+ tb: ta.Literal['t', 'b', None] = None
98
+ p: bool | None = None
99
+
100
+ for c in s:
101
+ if c in 'rwxa':
102
+ rwxa = check.replacing_none(rwxa, c) # type: ignore[arg-type]
103
+ elif c in 'tb':
104
+ tb = check.replacing_none(tb, c) # type: ignore[arg-type]
105
+ elif c == '+':
106
+ p = check.replacing_none(p, True)
107
+ else:
108
+ raise ValueError(c)
109
+
110
+ if rwxa is None:
111
+ rwxa = 'r'
112
+ if tb is None:
113
+ tb = 't'
114
+ p = bool(p)
115
+
116
+ return FileMode(
117
+ read=rwxa == 'r' or p,
118
+ write=rwxa != 'r' or p,
119
+ create=rwxa != 'r',
120
+ exists=( # noqa
121
+ 'append' if rwxa == 'a' else
122
+ 'fail' if rwxa == 'x' else
123
+ 'truncate' if rwxa == 'w' else
124
+ 'beginning'
125
+ ),
126
+ binary=tb == 'b',
127
+ )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: omlish
3
- Version: 0.0.0.dev234
3
+ Version: 0.0.0.dev235
4
4
  Summary: omlish
5
5
  Author: wrmsr
6
6
  License: BSD-3-Clause
@@ -1,5 +1,5 @@
1
1
  omlish/.manifests.json,sha256=vQTAIvR8OblSq-uP2GUfnbei0RnmAnM5j0T1-OToh9E,8253
2
- omlish/__about__.py,sha256=25WunqgfyyX8tx5v7hb5oDxmHeKqi70lgB5g0g-9A6o,3380
2
+ omlish/__about__.py,sha256=mOMhMeHgYWv96pndE14EVx-D5bReVhtSMXcqy2QLvnE,3380
3
3
  omlish/__init__.py,sha256=SsyiITTuK0v74XpKV8dqNaCmjOlan1JZKrHQv5rWKPA,253
4
4
  omlish/c3.py,sha256=ubu7lHwss5V4UznbejAI0qXhXahrU01MysuHOZI9C4U,8116
5
5
  omlish/cached.py,sha256=UI-XTFBwA6YXWJJJeBn-WkwBkfzDjLBBaZf4nIJA9y0,510
@@ -514,6 +514,7 @@ omlish/os/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
514
514
  omlish/os/atomics.py,sha256=KhWNeh4mzU3M-TF0v8uR6hUqMfZJW42MeyIK9Jl6R0k,5246
515
515
  omlish/os/deathsig.py,sha256=hk9Yq2kyDdI-cI7OQH7mOfpRbOKzY_TfPKEqgrjVYbA,641
516
516
  omlish/os/fcntl.py,sha256=riQf9iEEEIC28lJp8ud06MU56w2XJHJ9nBFtck_hdhc,1501
517
+ omlish/os/filemodes.py,sha256=njxd9cQChKSi_5-oiAG9OJOzj8CvnyedWWheOWM0uHA,3476
517
518
  omlish/os/files.py,sha256=WJ_42vsZIZukQURN3TTccp-n74ZNhbux_ps3TLbHj18,1106
518
519
  omlish/os/forkhooks.py,sha256=yjodOvs90ClXskv5oBIJbHn0Y7dzajLmZmOpRMKbyxM,5656
519
520
  omlish/os/journald.py,sha256=2nI8Res1poXkbLc31--MPUlzYMESnCcPUkIxDOCjZW0,3903
@@ -705,9 +706,9 @@ omlish/text/indent.py,sha256=YjtJEBYWuk8--b9JU_T6q4yxV85_TR7VEVr5ViRCFwk,1336
705
706
  omlish/text/minja.py,sha256=jZC-fp3Xuhx48ppqsf2Sf1pHbC0t8XBB7UpUUoOk2Qw,5751
706
707
  omlish/text/parts.py,sha256=JkNZpyR2tv2CNcTaWJJhpQ9E4F0yPR8P_YfDbZfMtwQ,6182
707
708
  omlish/text/random.py,sha256=jNWpqiaKjKyTdMXC-pWAsSC10AAP-cmRRPVhm59ZWLk,194
708
- omlish-0.0.0.dev234.dist-info/LICENSE,sha256=B_hVtavaA8zCYDW99DYdcpDLKz1n3BBRjZrcbv8uG8c,1451
709
- omlish-0.0.0.dev234.dist-info/METADATA,sha256=Wi5DB__bQ4RwJcyGfKAYa2KjklQ83NtMlyvt20u9VxQ,4176
710
- omlish-0.0.0.dev234.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
711
- omlish-0.0.0.dev234.dist-info/entry_points.txt,sha256=Lt84WvRZJskWCAS7xnQGZIeVWksprtUHj0llrvVmod8,35
712
- omlish-0.0.0.dev234.dist-info/top_level.txt,sha256=pePsKdLu7DvtUiecdYXJ78iO80uDNmBlqe-8hOzOmfs,7
713
- omlish-0.0.0.dev234.dist-info/RECORD,,
709
+ omlish-0.0.0.dev235.dist-info/LICENSE,sha256=B_hVtavaA8zCYDW99DYdcpDLKz1n3BBRjZrcbv8uG8c,1451
710
+ omlish-0.0.0.dev235.dist-info/METADATA,sha256=kGZm_s1Kmw1dsPChxxw0LPa1lbSL07NPMdgVyf8AnUs,4176
711
+ omlish-0.0.0.dev235.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
712
+ omlish-0.0.0.dev235.dist-info/entry_points.txt,sha256=Lt84WvRZJskWCAS7xnQGZIeVWksprtUHj0llrvVmod8,35
713
+ omlish-0.0.0.dev235.dist-info/top_level.txt,sha256=pePsKdLu7DvtUiecdYXJ78iO80uDNmBlqe-8hOzOmfs,7
714
+ omlish-0.0.0.dev235.dist-info/RECORD,,