omdev 0.0.0.dev99__py3-none-any.whl → 0.0.0.dev101__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.
omdev/findmagic.py DELETED
@@ -1,91 +0,0 @@
1
- #!/usr/bin/env python3
2
- # @omlish-lite
3
- # @omlish-script
4
- import os.path
5
- import re
6
- import typing as ta
7
-
8
-
9
- def compile_magic_pat(m: str) -> re.Pattern:
10
- return re.compile('^' + re.escape(m) + r'($|(\s.*))')
11
-
12
-
13
- def find_magic(
14
- roots: ta.Sequence[str],
15
- magics: ta.Sequence[str],
16
- exts: ta.Sequence[str],
17
- *,
18
- py: bool = False,
19
- ) -> ta.Iterator[str]:
20
- if isinstance(roots, str):
21
- raise TypeError(roots)
22
- if isinstance(magics, str):
23
- raise TypeError(magics)
24
- if isinstance(exts, str):
25
- raise TypeError(exts)
26
-
27
- if not magics:
28
- raise Exception('Must specify magics')
29
- if not exts:
30
- raise Exception('Must specify extensions')
31
-
32
- pats = [compile_magic_pat(m) for m in magics]
33
-
34
- for root in roots:
35
- for dp, dns, fns in os.walk(root): # noqa
36
- for fn in fns:
37
- if not any(fn.endswith(f'.{x}') for x in exts):
38
- continue
39
-
40
- fp = os.path.join(dp, fn)
41
- try:
42
- with open(fp) as f:
43
- src = f.read()
44
- except UnicodeDecodeError:
45
- continue
46
-
47
- if not any(
48
- any(pat.fullmatch(l) for pat in pats)
49
- for l in src.splitlines()
50
- ):
51
- continue
52
-
53
- if py:
54
- if fn == '__init__.py':
55
- out = dp.replace(os.sep, '.')
56
- elif fn.endswith('.py'):
57
- out = fp[:-3].replace(os.sep, '.')
58
- else:
59
- out = fp
60
- else:
61
- out = fp
62
- yield out
63
-
64
-
65
- # @omlish-manifest
66
- _CLI_MODULE = {'$omdev.cli.types.CliModule': {
67
- 'cmd_name': 'py/findmagic',
68
- 'mod_name': __name__,
69
- }}
70
-
71
-
72
- if __name__ == '__main__':
73
- def _main(argv=None) -> None:
74
- import argparse
75
-
76
- arg_parser = argparse.ArgumentParser()
77
- arg_parser.add_argument('--ext', '-x', dest='exts', action='append')
78
- arg_parser.add_argument('--magic', '-m', dest='magics', action='append')
79
- arg_parser.add_argument('--py', action='store_true')
80
- arg_parser.add_argument('roots', nargs='*')
81
- args = arg_parser.parse_args(argv)
82
-
83
- for out in find_magic(
84
- roots=args.roots,
85
- magics=args.magics,
86
- exts=args.exts,
87
- py=args.py,
88
- ):
89
- print(out)
90
-
91
- _main()