omlish 0.0.0.dev56__py3-none-any.whl → 0.0.0.dev57__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.
omlish/__about__.py CHANGED
@@ -1,5 +1,5 @@
1
- __version__ = '0.0.0.dev56'
2
- __revision__ = '1d081aeb233ec7ae534199fc0a6c063d4c79fcf7'
1
+ __version__ = '0.0.0.dev57'
2
+ __revision__ = '6bc82bd3fa6f9a7e33d9f3a69e8d3e1855aa6417'
3
3
 
4
4
 
5
5
  #
@@ -1,13 +1,34 @@
1
1
  import argparse
2
2
  import contextlib
3
+ import dataclasses as dc
4
+ import enum
3
5
  import json
6
+ import subprocess
4
7
  import sys
5
8
  import typing as ta
6
9
 
10
+ from ... import lang
7
11
  from ... import term
8
12
  from .render import JsonRenderer
9
13
 
10
14
 
15
+ if ta.TYPE_CHECKING:
16
+ import tomllib
17
+
18
+ import yaml
19
+
20
+ from .. import dotenv
21
+ from .. import props
22
+
23
+ else:
24
+ tomllib = lang.proxy_import('tomllib')
25
+
26
+ yaml = lang.proxy_import('yaml')
27
+
28
+ dotenv = lang.proxy_import('..dotenv', __package__)
29
+ props = lang.proxy_import('..props', __package__)
30
+
31
+
11
32
  def term_color(o: ta.Any, state: JsonRenderer.State) -> tuple[str, str]:
12
33
  if state is JsonRenderer.State.KEY:
13
34
  return term.SGR(term.SGRs.FG.BRIGHT_BLUE), term.SGR(term.SGRs.RESET)
@@ -17,14 +38,39 @@ def term_color(o: ta.Any, state: JsonRenderer.State) -> tuple[str, str]:
17
38
  return '', ''
18
39
 
19
40
 
41
+ @dc.dataclass(frozen=True)
42
+ class Format:
43
+ names: ta.Sequence[str]
44
+ load: ta.Callable[[ta.TextIO], ta.Any]
45
+
46
+
47
+ class Formats(enum.Enum):
48
+ JSON = Format(['json'], json.load)
49
+ YAML = Format(['yaml', 'yml'], lambda f: yaml.safe_load(f))
50
+ TOML = Format(['toml'], lambda f: tomllib.loads(f.read()))
51
+ ENV = Format(['env', 'dotenv'], lambda f: dotenv.dotenv_values(stream=f))
52
+ PROPS = Format(['properties', 'props'], lambda f: dict(props.Properties().load(f.read())))
53
+
54
+
55
+ FORMATS_BY_NAME: ta.Mapping[str, Format] = {
56
+ n: f
57
+ for e in Formats
58
+ for f in [e.value]
59
+ for n in f.names
60
+ }
61
+
62
+
20
63
  def _main() -> None:
21
64
  parser = argparse.ArgumentParser()
65
+
22
66
  parser.add_argument('file', nargs='?')
67
+ parser.add_argument('-f', '--format')
23
68
  parser.add_argument('-z', '--compact', action='store_true')
24
69
  parser.add_argument('-p', '--pretty', action='store_true')
25
70
  parser.add_argument('-i', '--indent')
26
71
  parser.add_argument('-s', '--sort-keys', action='store_true')
27
72
  parser.add_argument('-c', '--color', action='store_true')
73
+ parser.add_argument('-l', '--less', action='store_true')
28
74
  args = parser.parse_args()
29
75
 
30
76
  separators = None
@@ -40,32 +86,55 @@ def _main() -> None:
40
86
  except ValueError:
41
87
  indent = args.indent
42
88
 
89
+ fmt_name = args.format
90
+ if fmt_name is None:
91
+ if args.file is not None:
92
+ ext = args.file.rpartition('.')[2]
93
+ if ext in FORMATS_BY_NAME:
94
+ fmt_name = ext
95
+ if fmt_name is None:
96
+ fmt_name = 'json'
97
+ fmt = FORMATS_BY_NAME[fmt_name]
98
+
43
99
  with contextlib.ExitStack() as es:
44
100
  if args.file is None:
45
101
  in_file = sys.stdin
46
102
  else:
47
103
  in_file = es.enter_context(open(args.file))
48
104
 
49
- data = json.load(in_file)
105
+ data = fmt.load(in_file)
50
106
 
51
107
  kw: dict[str, ta.Any] = dict(
52
108
  indent=indent,
53
109
  separators=separators,
110
+ sort_keys=args.sort_keys,
54
111
  )
55
112
 
56
113
  if args.color:
57
- JsonRenderer(
58
- sys.stdout,
114
+ out = JsonRenderer.render_str(
115
+ data,
59
116
  **kw,
60
117
  style=term_color,
61
- ).render(data)
62
- print()
118
+ )
63
119
 
64
120
  else:
65
- print(json.dumps(
121
+ out = json.dumps(
66
122
  data,
67
123
  **kw,
68
- ))
124
+ )
125
+
126
+ if args.less:
127
+ subprocess.run(
128
+ [
129
+ 'less',
130
+ *(['-R'] if args.color else []),
131
+ ],
132
+ input=out.encode(),
133
+ check=True,
134
+ )
135
+
136
+ else:
137
+ print(out)
69
138
 
70
139
 
71
140
  if __name__ == '__main__':
omlish/formats/props.py CHANGED
@@ -508,7 +508,7 @@ class Properties(collections.abc.MutableMapping):
508
508
  source_data,
509
509
  encoding: str | None = 'iso-8859-1',
510
510
  metadoc: bool = False,
511
- ) -> None:
511
+ ) -> ta.Self:
512
512
  self.reset(metadoc)
513
513
 
514
514
  if isinstance(source_data, bytes):
@@ -522,6 +522,8 @@ class Properties(collections.abc.MutableMapping):
522
522
 
523
523
  self._parse()
524
524
 
525
+ return self
526
+
525
527
  def store(
526
528
  self,
527
529
  out_stream,
@@ -530,7 +532,7 @@ class Properties(collections.abc.MutableMapping):
530
532
  strict: bool = True,
531
533
  strip_meta: bool = True,
532
534
  timestamp: bool = True,
533
- ) -> None:
535
+ ) -> ta.Self:
534
536
  out_codec_info = codecs.lookup(encoding)
535
537
  wrapped_out_stream = out_codec_info.streamwriter(out_stream, _jbackslash_replace_codec_name)
536
538
  properties_escape_nonprinting = strict and out_codec_info == codecs.lookup('latin_1')
@@ -597,6 +599,8 @@ class Properties(collections.abc.MutableMapping):
597
599
  file=wrapped_out_stream,
598
600
  )
599
601
 
602
+ return self
603
+
600
604
  def list(self, out_stream=sys.stderr) -> None:
601
605
  print('-- listing properties --', file=out_stream)
602
606
  for key in self._properties:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: omlish
3
- Version: 0.0.0.dev56
3
+ Version: 0.0.0.dev57
4
4
  Summary: omlish
5
5
  Author: wrmsr
6
6
  License: BSD-3-Clause
@@ -1,5 +1,5 @@
1
1
  omlish/.manifests.json,sha256=wTGXwNmvtaKsDWtTwwj3Uib5Inaj8ZBn0MB69bE80X4,1419
2
- omlish/__about__.py,sha256=OmcoVJ5WHgdOGJt6I58kb1scFk6M-rJM3RyqDolJYdE,3420
2
+ omlish/__about__.py,sha256=TcVGEzTkX3fMqsIJaOXPCs013BBL_Tc4-lqbwNV4bgs,3420
3
3
  omlish/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
4
  omlish/argparse.py,sha256=Vr70_85EVLJLgEkRtwOr264tMRtqtlN7ncFfXRUk5aM,6914
5
5
  omlish/c3.py,sha256=4vogWgwPb8TbNS2KkZxpoWbwjj7MuHG2lQG-hdtkvjI,8062
@@ -116,11 +116,11 @@ omlish/docker/hub.py,sha256=YcDYOi6t1FA2Sp0RVrmZ9cBXbzFWQ8wTps3wOskA-K0,1955
116
116
  omlish/docker/manifests.py,sha256=LR4FpOGNUT3bZQ-gTjB6r_-1C3YiG30QvevZjrsVUQM,7068
117
117
  omlish/formats/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
118
118
  omlish/formats/dotenv.py,sha256=UjZl3gac-0U24sDjCCGMcCqO1UCWG2Zs8PZ4JdAg2YE,17348
119
- omlish/formats/props.py,sha256=diYjZDsG1s50ImJhkpeinMwjr8952nIVI-0gYhBIvCY,18897
119
+ omlish/formats/props.py,sha256=JwFJbKblqzqnzXf7YKFzQSDfcAXzkKsfoYvad6FPy98,18945
120
120
  omlish/formats/yaml.py,sha256=R3NTkjomsIfjsUNmSf_bOaCUIID3JTyHJHsliQDSYQo,6688
121
121
  omlish/formats/json/__init__.py,sha256=moSR67Qkju2eYb_qVDtaivepe44mxAnYuC8OCSbtETg,298
122
122
  omlish/formats/json/__main__.py,sha256=1wxxKZVkj_u7HCcewwMIbGuZj_Wph95yrUbm474Op9M,188
123
- omlish/formats/json/cli.py,sha256=4zNG69FUQ58F0sxZ-va0ysmthzuVAEFzYpDUoHB-itA,1734
123
+ omlish/formats/json/cli.py,sha256=4zftNijlIOnGUHYn5J1s4yRDRM1K4udBzS3Kh8R2vNc,3374
124
124
  omlish/formats/json/json.py,sha256=y8d8WWgzGZDTjzYc_xe9v4T0foXHI-UP7gjCwnHzUIA,828
125
125
  omlish/formats/json/render.py,sha256=6edhSrxXWW3nzRfokp5qaldT0_YAj-HVEan_rErf-vo,3208
126
126
  omlish/formats/json/backends/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -347,9 +347,9 @@ omlish/text/delimit.py,sha256=ubPXcXQmtbOVrUsNh5gH1mDq5H-n1y2R4cPL5_DQf68,4928
347
347
  omlish/text/glyphsplit.py,sha256=Ug-dPRO7x-OrNNr8g1y6DotSZ2KH0S-VcOmUobwa4B0,3296
348
348
  omlish/text/indent.py,sha256=6Jj6TFY9unaPa4xPzrnZemJ-fHsV53IamP93XGjSUHs,1274
349
349
  omlish/text/parts.py,sha256=7vPF1aTZdvLVYJ4EwBZVzRSy8XB3YqPd7JwEnNGGAOo,6495
350
- omlish-0.0.0.dev56.dist-info/LICENSE,sha256=B_hVtavaA8zCYDW99DYdcpDLKz1n3BBRjZrcbv8uG8c,1451
351
- omlish-0.0.0.dev56.dist-info/METADATA,sha256=LAf0N5afnrlk6A12ObzkTXJ8pDcdKwjpADduKtBz6f4,4167
352
- omlish-0.0.0.dev56.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
353
- omlish-0.0.0.dev56.dist-info/entry_points.txt,sha256=Lt84WvRZJskWCAS7xnQGZIeVWksprtUHj0llrvVmod8,35
354
- omlish-0.0.0.dev56.dist-info/top_level.txt,sha256=pePsKdLu7DvtUiecdYXJ78iO80uDNmBlqe-8hOzOmfs,7
355
- omlish-0.0.0.dev56.dist-info/RECORD,,
350
+ omlish-0.0.0.dev57.dist-info/LICENSE,sha256=B_hVtavaA8zCYDW99DYdcpDLKz1n3BBRjZrcbv8uG8c,1451
351
+ omlish-0.0.0.dev57.dist-info/METADATA,sha256=SSyQEUrMKl0Bkig1soCu8_V7T-AVM8weCHYyMkDp8bc,4167
352
+ omlish-0.0.0.dev57.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
353
+ omlish-0.0.0.dev57.dist-info/entry_points.txt,sha256=Lt84WvRZJskWCAS7xnQGZIeVWksprtUHj0llrvVmod8,35
354
+ omlish-0.0.0.dev57.dist-info/top_level.txt,sha256=pePsKdLu7DvtUiecdYXJ78iO80uDNmBlqe-8hOzOmfs,7
355
+ omlish-0.0.0.dev57.dist-info/RECORD,,