Skip to content

Commit 33ae79b

Browse files
committed
wip
1 parent 5e597fc commit 33ae79b

File tree

6 files changed

+68
-42
lines changed

6 files changed

+68
-42
lines changed

.github/workflows/unittest.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: unittest
2+
3+
on:
4+
push:
5+
paths:
6+
- 'pythoned/**'
7+
- 'tests/**'
8+
- '.github/workflows/unittest.yml'
9+
10+
jobs:
11+
unittest:
12+
runs-on: ubuntu-22.04
13+
14+
strategy:
15+
matrix:
16+
python-version: ['3.7.17', '3.8.18', '3.9.18', '3.10.13', '3.11.7', '3.12.1', '3.13.1', '3.14.0-alpha.3']
17+
18+
steps:
19+
- uses: actions/checkout@v4
20+
- name: Set up Python ${{ matrix.python-version }}
21+
uses: actions/setup-python@v4
22+
with:
23+
python-version: ${{ matrix.python-version }}
24+
25+
- name: unittest
26+
run: |
27+
python -m unittest

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 🐉 `pythoned`
22

3-
### *PYTHON EDitor: A CLI tool to edit lines using Python expressions*
3+
### *PYTHON EDitor: CLI tool to edit lines using Python expressions*
44

55
> For Pythonistas tired of forgetting the syntax/options of `sed`/`awk`/`grep`/`tr`
66

pythoned/__init__.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,38 @@
1-
from pythoned.edit import edit
1+
import importlib
2+
import os
3+
import re
4+
from types import ModuleType
5+
from typing import Dict, Iterator
6+
7+
8+
_TYPE_ERROR_MSG = "The provided expression must be an str (editing) or a bool (filtering), but got {}."
9+
10+
11+
def edit(lines: Iterator[str], expression) -> Iterator[str]:
12+
modules: Dict[str, ModuleType] = {}
13+
for line in lines:
14+
linesep = ""
15+
if line.endswith(os.linesep):
16+
linesep, line = os.linesep, line[: -len(os.linesep)]
17+
globals = {"_": line, **modules}
18+
try:
19+
value = eval(expression, globals)
20+
except NameError as name_error:
21+
match = re.match(r"name '([A-Za-z]+)'.*", str(name_error))
22+
if match:
23+
module = match.group(1)
24+
else:
25+
raise name_error
26+
try:
27+
modules[module] = importlib.import_module(module)
28+
globals = {"_": line, **modules}
29+
except:
30+
raise name_error
31+
value = eval(expression, globals)
32+
if isinstance(value, str):
33+
yield value + linesep
34+
elif isinstance(value, bool):
35+
if value:
36+
yield line + linesep
37+
else:
38+
raise TypeError(_TYPE_ERROR_MSG.format(type(value)))

pythoned/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import argparse
22
import sys
33

4-
from pythoned.edit import edit
4+
from pythoned import edit
55

66

77
def main() -> int:

pythoned/edit.py

Lines changed: 0 additions & 38 deletions
This file was deleted.

tests/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Iterator
22
import unittest
33

4-
from pythoned.edit import edit
4+
from pythoned import edit
55

66

77
def lines() -> Iterator[str]:

0 commit comments

Comments
 (0)