read-write-edit-tools 0.1.0__tar.gz → 0.2.0__tar.gz
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.
- {read_write_edit_tools-0.1.0/src/read_write_edit_tools.egg-info → read_write_edit_tools-0.2.0}/PKG-INFO +392 -204
- {read_write_edit_tools-0.1.0 → read_write_edit_tools-0.2.0}/README.md +391 -203
- {read_write_edit_tools-0.1.0 → read_write_edit_tools-0.2.0}/pyproject.toml +1 -1
- {read_write_edit_tools-0.1.0 → read_write_edit_tools-0.2.0/src/read_write_edit_tools.egg-info}/PKG-INFO +392 -204
- {read_write_edit_tools-0.1.0 → read_write_edit_tools-0.2.0}/src/rwe/__init__.py +1 -1
- {read_write_edit_tools-0.1.0 → read_write_edit_tools-0.2.0}/src/rwe/cli.py +128 -83
- read_write_edit_tools-0.2.0/tests/test_cli.py +726 -0
- read_write_edit_tools-0.1.0/tests/test_cli.py +0 -360
- {read_write_edit_tools-0.1.0 → read_write_edit_tools-0.2.0}/LICENSE +0 -0
- {read_write_edit_tools-0.1.0 → read_write_edit_tools-0.2.0}/setup.cfg +0 -0
- {read_write_edit_tools-0.1.0 → read_write_edit_tools-0.2.0}/src/read_write_edit_tools.egg-info/SOURCES.txt +0 -0
- {read_write_edit_tools-0.1.0 → read_write_edit_tools-0.2.0}/src/read_write_edit_tools.egg-info/dependency_links.txt +0 -0
- {read_write_edit_tools-0.1.0 → read_write_edit_tools-0.2.0}/src/read_write_edit_tools.egg-info/entry_points.txt +0 -0
- {read_write_edit_tools-0.1.0 → read_write_edit_tools-0.2.0}/src/read_write_edit_tools.egg-info/top_level.txt +0 -0
- {read_write_edit_tools-0.1.0 → read_write_edit_tools-0.2.0}/src/rwe/__main__.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: read-write-edit-tools
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: Fast hex-line-numbered file read, write, and edit CLI
|
|
5
5
|
License-Expression: Apache-2.0
|
|
6
6
|
Project-URL: Homepage, https://github.com/llaa33219/read-write-edit-tools
|
|
@@ -33,16 +33,17 @@ The tool deliberately has a narrow scope:
|
|
|
33
33
|
- `read` displays text with stable, 1-based hexadecimal line addresses.
|
|
34
34
|
- `write` creates a new file and **refuses to overwrite anything that already
|
|
35
35
|
exists**.
|
|
36
|
-
- `edit` replaces
|
|
37
|
-
|
|
36
|
+
- `edit` replaces original lines with whole replacement blocks, or deletes
|
|
37
|
+
them, after verifying each addressed line's exact current content.
|
|
38
38
|
|
|
39
39
|
It is not a general-purpose editor, binary-file tool, search tool, merge tool,
|
|
40
|
-
or interactive terminal UI. It does not delete
|
|
41
|
-
|
|
40
|
+
or interactive terminal UI. It does not delete files, append to files,
|
|
41
|
+
create parent directories, or provide pattern-based substitution.
|
|
42
42
|
|
|
43
43
|
> **The essential `edit` rule:** every address in one `edit` command refers to
|
|
44
|
-
> the file as it existed **before that command started**.
|
|
45
|
-
>
|
|
44
|
+
> the file as it existed **before that command started**. Each pair's
|
|
45
|
+
> replacement is one whole block inserted at that pair's lowest addressed
|
|
46
|
+
> position, so its size does not change any other address in the same
|
|
46
47
|
> command. The complete example below shows this explicitly.
|
|
47
48
|
|
|
48
49
|
## Contents
|
|
@@ -165,9 +166,9 @@ create or modify their target files exactly as documented:
|
|
|
165
166
|
uvx --from read-write-edit-tools rwe write ephemeral-demo.txt 'First line
|
|
166
167
|
Second line'
|
|
167
168
|
|
|
168
|
-
uvx --from read-write-edit-tools rwe edit ephemeral-demo.txt '01:
|
|
169
|
-
|
|
170
|
-
|
|
169
|
+
uvx --from read-write-edit-tools rwe edit ephemeral-demo.txt '01: First line
|
|
170
|
+
02: Second line' 'Replacement for original line 01
|
|
171
|
+
An additional line'
|
|
171
172
|
|
|
172
173
|
uvx --from read-write-edit-tools rwe read ephemeral-demo.txt
|
|
173
174
|
```
|
|
@@ -243,7 +244,7 @@ development.
|
|
|
243
244
|
```text
|
|
244
245
|
rwe read FILE [RANGE]
|
|
245
246
|
rwe write FILE CONTENT
|
|
246
|
-
rwe edit FILE
|
|
247
|
+
rwe edit FILE OLD NEW [OLD NEW ...]
|
|
247
248
|
```
|
|
248
249
|
|
|
249
250
|
| Command | Purpose | Modifies a file? |
|
|
@@ -251,7 +252,7 @@ rwe edit FILE EDITS
|
|
|
251
252
|
| `rwe read FILE` | Print up to 1000 lines with hexadecimal addresses | No |
|
|
252
253
|
| `rwe read FILE RANGE` | Print a single line or inclusive range | No |
|
|
253
254
|
| `rwe write FILE CONTENT` | Create a file containing exactly `CONTENT` | Yes, only if `FILE` does not exist |
|
|
254
|
-
| `rwe edit FILE
|
|
255
|
+
| `rwe edit FILE OLD NEW [OLD NEW ...]` | Replace addressed lines from the original file | Yes |
|
|
255
256
|
| `rwe --help` or `rwe -h` | Print a short command reference | No |
|
|
256
257
|
| `rwe --version` or `rwe -V` | Print the version | No |
|
|
257
258
|
|
|
@@ -264,11 +265,13 @@ rwe edit FILE EDITS
|
|
|
264
265
|
| `FILE` | Path to the file to read, create, or edit |
|
|
265
266
|
| `RANGE` | One hexadecimal address, or `START:END`; see below |
|
|
266
267
|
| `CONTENT` | The complete new file content as **one shell argument** |
|
|
267
|
-
| `
|
|
268
|
+
| `OLD` | Original lines to replace: `HEX: content` lines copied from `read` |
|
|
269
|
+
| `NEW` | The replacement lines as **one shell argument**; `''` deletes the addressed lines |
|
|
268
270
|
|
|
269
|
-
For `write
|
|
270
|
-
`
|
|
271
|
-
|
|
271
|
+
For `write`, a final argument of exactly `-` reads `CONTENT` from standard
|
|
272
|
+
input. For `edit`, at most one argument among all the `OLD` and `NEW`
|
|
273
|
+
arguments may be `-`. `read FILE -` is **not** standard-input syntax; for
|
|
274
|
+
`read`, `-` is interpreted as a range and is invalid.
|
|
272
275
|
|
|
273
276
|
### Quick examples
|
|
274
277
|
|
|
@@ -285,14 +288,18 @@ rwe read notes.txt
|
|
|
285
288
|
rwe read notes.txt 03
|
|
286
289
|
rwe read notes.txt 01:03
|
|
287
290
|
|
|
288
|
-
# Replace original lines 01 and
|
|
289
|
-
rwe edit notes.txt '01:
|
|
290
|
-
|
|
291
|
+
# Replace original lines 01 and 02 in one operation.
|
|
292
|
+
rwe edit notes.txt '01: First line
|
|
293
|
+
02: Second line' 'New first line
|
|
294
|
+
New second line'
|
|
291
295
|
|
|
292
296
|
# Replace original line 02 with two lines.
|
|
293
|
-
rwe edit notes.txt '02: Replacement line
|
|
297
|
+
rwe edit notes.txt '02: Second line' 'Replacement line
|
|
294
298
|
An additional line'
|
|
295
299
|
|
|
300
|
+
# Replace original lines 01 and 03 as two regions; line 02 stays in place.
|
|
301
|
+
rwe edit notes.txt '01: First line' 'New first line' '03: Third line' 'New third line'
|
|
302
|
+
|
|
296
303
|
# Re-read after every edit to obtain the new addresses.
|
|
297
304
|
rwe read notes.txt
|
|
298
305
|
```
|
|
@@ -318,7 +325,7 @@ Addresses are hexadecimal (`0`–`9`, `a`–`f`), and the first line is `01`, no
|
|
|
318
325
|
|
|
319
326
|
Output uses lowercase hexadecimal. Input accepts lowercase or uppercase, so
|
|
320
327
|
`b7` and `B7` refer to the same line. Leading zeros are allowed on input:
|
|
321
|
-
`1`, `01`, and `001` all address line 1. In one `
|
|
328
|
+
`1`, `01`, and `001` all address line 1. In one `OLD` argument, using more
|
|
322
329
|
than one spelling of the same address is a duplicate-address error.
|
|
323
330
|
|
|
324
331
|
`00` is always invalid, including `000` and other all-zero forms.
|
|
@@ -379,7 +386,7 @@ Put `--` immediately after the subcommand:
|
|
|
379
386
|
```sh
|
|
380
387
|
rwe read -- -notes.txt
|
|
381
388
|
rwe write -- -new.txt 'content'
|
|
382
|
-
rwe edit -- -notes.txt '01:
|
|
389
|
+
rwe edit -- -notes.txt '01: - line one' '0a: - line ten'
|
|
383
390
|
```
|
|
384
391
|
|
|
385
392
|
Paths containing spaces follow normal shell quoting rules:
|
|
@@ -390,9 +397,9 @@ rwe read 'my notes.txt'
|
|
|
390
397
|
|
|
391
398
|
## Complete workflow
|
|
392
399
|
|
|
393
|
-
This example demonstrates the most easily misunderstood
|
|
394
|
-
addresses remain fixed throughout one `edit`,
|
|
395
|
-
|
|
400
|
+
This example demonstrates the most easily misunderstood behaviors: original
|
|
401
|
+
addresses remain fixed throughout one `edit`, and the replacement is one
|
|
402
|
+
whole block whose size can differ from the number of addressed lines.
|
|
396
403
|
|
|
397
404
|
### 1. Create the original file
|
|
398
405
|
|
|
@@ -419,16 +426,23 @@ Output:
|
|
|
419
426
|
### 3. Expand line 01 and replace line 03 in the same command
|
|
420
427
|
|
|
421
428
|
```sh
|
|
422
|
-
rwe edit example.txt '01:
|
|
429
|
+
rwe edit example.txt '01: First line
|
|
430
|
+
02: Second line
|
|
431
|
+
03: Third line' 'New first line
|
|
423
432
|
Inserted second line
|
|
424
|
-
|
|
433
|
+
Second line
|
|
434
|
+
New third line'
|
|
425
435
|
```
|
|
426
436
|
|
|
427
|
-
The
|
|
437
|
+
The command addresses all three original lines at once. `OLD` holds the
|
|
438
|
+
verified original content copied from the `read` output above, and `NEW`
|
|
439
|
+
holds the whole replacement block:
|
|
428
440
|
|
|
429
|
-
1.
|
|
430
|
-
|
|
431
|
-
2.
|
|
441
|
+
1. `First line` becomes `New first line`, followed by a new `Inserted second
|
|
442
|
+
line`.
|
|
443
|
+
2. `Second line` is kept as `Second line`; addressing a line is not a promise
|
|
444
|
+
to change it, and the guard would have rejected a mistyped copy.
|
|
445
|
+
3. `Third line` becomes `New third line`.
|
|
432
446
|
|
|
433
447
|
### 4. Read the result
|
|
434
448
|
|
|
@@ -439,31 +453,31 @@ The edit argument has two blocks:
|
|
|
439
453
|
04: New third line
|
|
440
454
|
```
|
|
441
455
|
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
so it moved from line `02` to line `03` in the result. The replacement for
|
|
445
|
-
original line `03` consequently appears at line `04`.
|
|
456
|
+
Original `Second line` was re-supplied unchanged in `NEW`, so it moved from
|
|
457
|
+
line `02` to line `03` in the result.
|
|
446
458
|
|
|
447
|
-
Internally, `rwe` validates all addresses
|
|
448
|
-
|
|
459
|
+
Internally, `rwe` validates all addresses and all content matches against
|
|
460
|
+
the original file, removes the addressed lines, and inserts `NEW` at the
|
|
461
|
+
lowest addressed position. The practical consequences are:
|
|
449
462
|
|
|
450
463
|
- Every address in one call uses the same original snapshot.
|
|
451
|
-
-
|
|
452
|
-
|
|
453
|
-
|
|
464
|
+
- `OLD` lines may be written in ascending, descending, or mixed address
|
|
465
|
+
order; the result is the same.
|
|
466
|
+
- The size of `NEW` never shifts any other address in that same call.
|
|
454
467
|
- You cannot address a newly inserted line elsewhere in the same call.
|
|
455
468
|
- Addresses are not persistent IDs. After the command finishes, run `read`
|
|
456
469
|
again and use its addresses for the next `edit`.
|
|
457
470
|
|
|
458
|
-
For example, the following
|
|
459
|
-
|
|
471
|
+
For example, the following edit is also valid and has the same addressing
|
|
472
|
+
semantics:
|
|
460
473
|
|
|
461
474
|
```sh
|
|
462
|
-
rwe edit example.txt '
|
|
463
|
-
01: Replacement for current original line
|
|
475
|
+
rwe edit example.txt '02: Inserted second line
|
|
476
|
+
01: New first line' 'Replacement for current original line 02
|
|
477
|
+
Replacement for current original line 01'
|
|
464
478
|
```
|
|
465
479
|
|
|
466
|
-
Here `
|
|
480
|
+
Here `02` and `01` refer to the result shown immediately above, because this
|
|
467
481
|
is a new invocation with a new original snapshot.
|
|
468
482
|
|
|
469
483
|
## `read`: display numbered content
|
|
@@ -677,238 +691,390 @@ The byte count is the encoded content length, not a character count.
|
|
|
677
691
|
### Basic form
|
|
678
692
|
|
|
679
693
|
```sh
|
|
680
|
-
rwe edit FILE
|
|
694
|
+
rwe edit FILE OLD NEW [OLD NEW ...]
|
|
681
695
|
```
|
|
682
696
|
|
|
683
|
-
`
|
|
684
|
-
|
|
697
|
+
`OLD` names the original lines to replace. Each of its lines is copied from
|
|
698
|
+
`rwe read` output:
|
|
685
699
|
|
|
686
700
|
```text
|
|
687
|
-
HEX_ADDRESS:
|
|
701
|
+
HEX_ADDRESS: original content
|
|
688
702
|
```
|
|
689
703
|
|
|
690
|
-
|
|
704
|
+
`NEW` is the replacement: plain lines with no prefixes, passed as **one
|
|
705
|
+
argument**. For example, given:
|
|
691
706
|
|
|
692
|
-
```
|
|
693
|
-
|
|
694
|
-
02:
|
|
707
|
+
```text
|
|
708
|
+
01: First line
|
|
709
|
+
02: Second line
|
|
710
|
+
03: Third line
|
|
695
711
|
```
|
|
696
712
|
|
|
697
|
-
|
|
698
|
-
contain zero characters, one line, or multiple lines.
|
|
699
|
-
|
|
700
|
-
### Multiple blocks and multiline replacements
|
|
713
|
+
this command replaces original line 2:
|
|
701
714
|
|
|
702
715
|
```sh
|
|
703
|
-
rwe edit notes.txt '
|
|
704
|
-
|
|
716
|
+
rwe edit notes.txt '02: Second line' 'Replacement for original line 02'
|
|
717
|
+
```
|
|
718
|
+
|
|
719
|
+
The result is:
|
|
720
|
+
|
|
721
|
+
```text
|
|
722
|
+
01: First line
|
|
705
723
|
02: Replacement for original line 02
|
|
706
|
-
|
|
724
|
+
03: Third line
|
|
707
725
|
```
|
|
708
726
|
|
|
709
|
-
|
|
727
|
+
Every `edit` therefore starts from `read` output: copy the lines being
|
|
728
|
+
changed into `OLD`, then write the replacement in `NEW`.
|
|
710
729
|
|
|
711
|
-
|
|
712
|
-
- Original `02` becomes one line.
|
|
713
|
-
- Original `b7` becomes one line.
|
|
730
|
+
### The mis-edit guard
|
|
714
731
|
|
|
715
|
-
The
|
|
716
|
-
|
|
732
|
+
The content after the colon in `OLD` must **exactly match** the current line
|
|
733
|
+
in the file. If any addressed line differs, the whole command is rejected and
|
|
734
|
+
the file is left unchanged. A stale address or a typo produces:
|
|
717
735
|
|
|
718
|
-
|
|
736
|
+
```text
|
|
737
|
+
rwe: line 1 does not match the given original content; file left unchanged (expected 'Frist line', found 'First line')
|
|
738
|
+
```
|
|
739
|
+
|
|
740
|
+
The guard compares the full line, byte for byte, so copying `OLD` from an old
|
|
741
|
+
`read`, or from memory, fails safely instead of editing the wrong line. Copy
|
|
742
|
+
`OLD` lines from a fresh `rwe read` of the file being edited.
|
|
719
743
|
|
|
720
|
-
|
|
721
|
-
2. Build one or more replacement blocks using those addresses.
|
|
722
|
-
3. Pass all blocks in one `edit` invocation.
|
|
723
|
-
4. `rwe` validates every block before saving anything.
|
|
724
|
-
5. Replacements are applied from the highest original address to the lowest.
|
|
725
|
-
6. Read the resulting file before planning another edit.
|
|
744
|
+
### Whole-block replacement
|
|
726
745
|
|
|
727
|
-
|
|
728
|
-
|
|
746
|
+
All addressed original lines are removed, and `NEW` is inserted as one block
|
|
747
|
+
at the **lowest** addressed position. The number of replacement lines is
|
|
748
|
+
free; it need not match the number of addresses.
|
|
749
|
+
|
|
750
|
+
Shrinking, three original lines replaced by two:
|
|
729
751
|
|
|
730
752
|
```text
|
|
731
|
-
01:
|
|
732
|
-
|
|
753
|
+
01: one
|
|
754
|
+
02: two
|
|
755
|
+
03: three
|
|
756
|
+
```
|
|
757
|
+
|
|
758
|
+
```sh
|
|
759
|
+
rwe edit notes.txt '01: one
|
|
760
|
+
02: two
|
|
761
|
+
03: three' 'first
|
|
762
|
+
second'
|
|
733
763
|
```
|
|
734
764
|
|
|
765
|
+
Result:
|
|
766
|
+
|
|
735
767
|
```text
|
|
736
|
-
|
|
737
|
-
|
|
768
|
+
01: first
|
|
769
|
+
02: second
|
|
738
770
|
```
|
|
739
771
|
|
|
740
|
-
|
|
741
|
-
applied sequentially.
|
|
772
|
+
Growing, two original lines replaced by five:
|
|
742
773
|
|
|
743
|
-
|
|
774
|
+
```sh
|
|
775
|
+
rwe edit notes.txt '01: x
|
|
776
|
+
02: y' 'p
|
|
777
|
+
q
|
|
778
|
+
r
|
|
779
|
+
s
|
|
780
|
+
t'
|
|
781
|
+
```
|
|
744
782
|
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
783
|
+
Result:
|
|
784
|
+
|
|
785
|
+
```text
|
|
786
|
+
01: p
|
|
787
|
+
02: q
|
|
788
|
+
03: r
|
|
789
|
+
04: s
|
|
790
|
+
05: t
|
|
791
|
+
```
|
|
754
792
|
|
|
755
|
-
|
|
793
|
+
Deleting, with `NEW` as the empty string, removes the addressed lines
|
|
794
|
+
entirely. Given `a`, `b`, `c`:
|
|
756
795
|
|
|
757
|
-
|
|
758
|
-
|
|
796
|
+
```sh
|
|
797
|
+
rwe edit notes.txt '02: b' ''
|
|
798
|
+
```
|
|
799
|
+
|
|
800
|
+
Result:
|
|
759
801
|
|
|
760
802
|
```text
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
B7: content
|
|
764
|
-
dead: content
|
|
803
|
+
01: a
|
|
804
|
+
02: c
|
|
765
805
|
```
|
|
766
806
|
|
|
767
|
-
|
|
768
|
-
mean original line 1.
|
|
807
|
+
### Non-contiguous addresses
|
|
769
808
|
|
|
770
|
-
|
|
809
|
+
Addresses need not be adjacent, and separated regions can stay separated.
|
|
810
|
+
Given:
|
|
771
811
|
|
|
772
812
|
```text
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
3.14: content
|
|
813
|
+
01: a
|
|
814
|
+
02: b
|
|
815
|
+
03: c
|
|
777
816
|
```
|
|
778
817
|
|
|
779
|
-
|
|
780
|
-
|
|
818
|
+
this command replaces original lines 1 and 3 as two independent regions and
|
|
819
|
+
leaves line 2 untouched in place:
|
|
781
820
|
|
|
782
|
-
|
|
783
|
-
|
|
821
|
+
```sh
|
|
822
|
+
rwe edit notes.txt '01: a' 'x' '03: c' 'y'
|
|
823
|
+
```
|
|
784
824
|
|
|
785
|
-
|
|
825
|
+
The result is:
|
|
786
826
|
|
|
787
|
-
|
|
788
|
-
|
|
827
|
+
```text
|
|
828
|
+
01: x
|
|
829
|
+
02: b
|
|
830
|
+
03: y
|
|
831
|
+
```
|
|
832
|
+
|
|
833
|
+
Each pair replaces one region on its own: its `OLD` lines are removed and
|
|
834
|
+
its `NEW` is inserted at that pair's lowest address. The middle line is not
|
|
835
|
+
addressed, so it stays in place. With `NEW` as `''` for both pairs instead,
|
|
836
|
+
both addressed lines are removed and only `b` remains:
|
|
789
837
|
|
|
790
838
|
```text
|
|
791
|
-
01:
|
|
792
|
-
01:Replacement # Replacement content is "Replacement"
|
|
839
|
+
01: b
|
|
793
840
|
```
|
|
794
841
|
|
|
795
|
-
|
|
842
|
+
Non-contiguous addresses in one `OLD` are rejected. The lines in a single
|
|
843
|
+
`OLD` must form one contiguous run, for example `'01: a\n02: b'` but not
|
|
844
|
+
`'01: a\n03: c'`; split separated regions into `OLD` `NEW` pairs instead,
|
|
845
|
+
one pair per region, and each region stays separate.
|
|
846
|
+
|
|
847
|
+
### Several regions in one command
|
|
848
|
+
|
|
849
|
+
Each pair replaces one region on its own, so two separated regions can be
|
|
850
|
+
replaced in one command instead of two invocations. Given:
|
|
796
851
|
|
|
797
852
|
```text
|
|
798
|
-
01:
|
|
853
|
+
01: a
|
|
854
|
+
02: b
|
|
855
|
+
03: c
|
|
856
|
+
04: d
|
|
857
|
+
05: e
|
|
858
|
+
06: f
|
|
799
859
|
```
|
|
800
860
|
|
|
801
|
-
|
|
861
|
+
the first pair replaces `01`-`02` with `X`, the second replaces `05`-`06`
|
|
862
|
+
with `Y`, and untouched lines `03`-`04` stay in place:
|
|
802
863
|
|
|
803
|
-
|
|
804
|
-
|
|
864
|
+
```sh
|
|
865
|
+
rwe edit notes.txt '01: a
|
|
866
|
+
02: b' 'X' '05: e
|
|
867
|
+
06: f' 'Y'
|
|
868
|
+
```
|
|
805
869
|
|
|
806
|
-
|
|
870
|
+
Result:
|
|
807
871
|
|
|
808
|
-
|
|
872
|
+
```text
|
|
873
|
+
01: X
|
|
874
|
+
02: c
|
|
875
|
+
03: d
|
|
876
|
+
04: Y
|
|
877
|
+
```
|
|
878
|
+
|
|
879
|
+
The regions may sit anywhere in the file, and each pair's `NEW` has its
|
|
880
|
+
own size. Given:
|
|
809
881
|
|
|
810
882
|
```text
|
|
811
|
-
01:
|
|
812
|
-
02:
|
|
813
|
-
03:
|
|
814
|
-
04:
|
|
883
|
+
01: a
|
|
884
|
+
02: b
|
|
885
|
+
03: keep1
|
|
886
|
+
04: keep2
|
|
887
|
+
05: c
|
|
888
|
+
06: d
|
|
889
|
+
07: f
|
|
815
890
|
```
|
|
816
891
|
|
|
817
|
-
this command
|
|
892
|
+
this command replaces `01`-`02` with `대체` and `05`-`07` with `대체2`:
|
|
818
893
|
|
|
819
894
|
```sh
|
|
820
|
-
rwe edit
|
|
821
|
-
|
|
895
|
+
rwe edit f.txt '01: a
|
|
896
|
+
02: b' '대체' '05: c
|
|
897
|
+
06: d
|
|
898
|
+
07: f' '대체2'
|
|
899
|
+
```
|
|
900
|
+
|
|
901
|
+
Result:
|
|
902
|
+
|
|
903
|
+
```text
|
|
904
|
+
01: 대체
|
|
905
|
+
02: keep1
|
|
906
|
+
03: keep2
|
|
907
|
+
04: 대체2
|
|
908
|
+
```
|
|
909
|
+
|
|
910
|
+
All pairs use the same original snapshot, and pair order is irrelevant:
|
|
911
|
+
swapping the two pairs above produces the same result. The pairs may also
|
|
912
|
+
interleave, for example one pair addressing `01` and `05` while another
|
|
913
|
+
addresses `03`.
|
|
914
|
+
|
|
915
|
+
### `NEW` terminator semantics
|
|
916
|
+
|
|
917
|
+
`NEW` is one argument holding plain replacement lines. One final newline at
|
|
918
|
+
the end of the argument is a terminator, not an extra line:
|
|
822
919
|
|
|
920
|
+
- `NEW` `''`, the empty string, deletes the addressed lines.
|
|
921
|
+
- `NEW` holding one newline character leaves **one blank line**.
|
|
922
|
+
- `NEW` holding two newline characters leaves **two blank lines**.
|
|
923
|
+
- In shell single quotes, `'x` NEWLINE `y'` and `'x` NEWLINE `y` NEWLINE `'`
|
|
924
|
+
both produce the two lines `x` and `y`.
|
|
925
|
+
|
|
926
|
+
These are byte-exact: one blank line means one LF, not zero and not two. In a
|
|
927
|
+
shell, a newline character inside single quotes is written by ending the line
|
|
928
|
+
inside the quoted argument, so this leaves exactly one blank line where `a`
|
|
929
|
+
was:
|
|
930
|
+
|
|
931
|
+
```sh
|
|
932
|
+
rwe edit notes.txt '01: a' '
|
|
823
933
|
'
|
|
824
934
|
```
|
|
825
935
|
|
|
826
|
-
|
|
936
|
+
An empty replacement **line** is therefore different from an empty
|
|
937
|
+
replacement: blank `NEW` lines leave empty lines in the file, while `NEW` `''`
|
|
938
|
+
removes the addressed lines. Emptying every line of a file this way still
|
|
939
|
+
leaves one blank line per address, so the file never reaches 0 bytes; to
|
|
940
|
+
truly empty it, address every line with `NEW` `''`, or remove the file and
|
|
941
|
+
recreate it with `write ''`.
|
|
827
942
|
|
|
828
|
-
|
|
829
|
-
- Original `03` becomes `Replacement` followed by an empty line.
|
|
943
|
+
### Original-address rules
|
|
830
944
|
|
|
831
|
-
|
|
945
|
+
1. Read the file to obtain current addresses and exact line content.
|
|
946
|
+
2. Copy every line you intend to replace into `OLD`.
|
|
947
|
+
3. Write the complete replacement in `NEW`, all in one argument.
|
|
948
|
+
4. `rwe` validates every address and every content match before saving
|
|
949
|
+
anything.
|
|
950
|
+
5. Re-read the resulting file before planning another edit.
|
|
951
|
+
|
|
952
|
+
All addresses refer to the file as it existed **before the command started**.
|
|
953
|
+
`OLD` line order is irrelevant; these two `OLD` arguments are equivalent:
|
|
832
954
|
|
|
833
955
|
```text
|
|
834
|
-
01:
|
|
835
|
-
02:
|
|
836
|
-
03: Replacement
|
|
837
|
-
04:
|
|
838
|
-
05: D
|
|
956
|
+
01: First
|
|
957
|
+
02: Second
|
|
839
958
|
```
|
|
840
959
|
|
|
841
|
-
|
|
960
|
+
```text
|
|
961
|
+
02: Second
|
|
962
|
+
01: First
|
|
963
|
+
```
|
|
842
964
|
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
- To make the last block end in a blank line, include an extra blank line
|
|
849
|
-
before the closing shell quote.
|
|
965
|
+
Both address original lines 1 and 2, and in both cases the result is the
|
|
966
|
+
same. Because the whole block is replaced at once, the size of `NEW` cannot
|
|
967
|
+
shift any other address in the same command. Addresses are not persistent
|
|
968
|
+
IDs: after the command finishes, run `read` again and use its addresses for
|
|
969
|
+
the next `edit`.
|
|
850
970
|
|
|
851
|
-
###
|
|
971
|
+
### What `edit` does not do
|
|
972
|
+
|
|
973
|
+
- There is no range syntax. `OLD` names individual lines, one per header;
|
|
974
|
+
`01:03` is not a range. To replace a run of lines, address each of them.
|
|
975
|
+
- There is no regular-expression or search-and-replace mode. `OLD` content is
|
|
976
|
+
compared literally, byte for byte.
|
|
977
|
+
- Deletion is supported only through `NEW` `''`; there is no separate delete
|
|
978
|
+
header or delete-only form.
|
|
979
|
+
- There is no "current line 3 after the previous replacement" addressing
|
|
980
|
+
mode.
|
|
981
|
+
- An empty file has no addressable lines, so it cannot be populated with
|
|
982
|
+
`edit`; remove it and use `write` instead.
|
|
983
|
+
|
|
984
|
+
### `OLD` line format
|
|
852
985
|
|
|
853
|
-
|
|
986
|
+
Every line of `OLD` must be a header:
|
|
854
987
|
|
|
855
988
|
```text
|
|
856
|
-
|
|
857
|
-
|
|
989
|
+
1: content
|
|
990
|
+
01: content
|
|
991
|
+
B7: content
|
|
992
|
+
dead: content
|
|
858
993
|
```
|
|
859
994
|
|
|
860
|
-
|
|
861
|
-
|
|
995
|
+
There are no continuation lines in `OLD`: each line addresses exactly one
|
|
996
|
+
original line, and no escaping is ever needed, because replacement text lives
|
|
997
|
+
in `NEW`. A plain line without an address, or a blank line, is rejected:
|
|
862
998
|
|
|
863
|
-
```
|
|
864
|
-
|
|
865
|
-
\dead: This must remain literal content
|
|
866
|
-
02: Replacement for original line 02'
|
|
999
|
+
```text
|
|
1000
|
+
expected 'HEX: original content', got: 'plain line'
|
|
867
1001
|
```
|
|
868
1002
|
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
1003
|
+
The address need not use the width shown by `read`; `1` and `01` both address
|
|
1004
|
+
original line 1.
|
|
1005
|
+
|
|
1006
|
+
### Spaces and tabs after the colon
|
|
873
1007
|
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
1008
|
+
One ASCII space or one tab immediately after the colon in `OLD` is treated as
|
|
1009
|
+
a separator and removed. It is optional:
|
|
1010
|
+
|
|
1011
|
+
```text
|
|
1012
|
+
01: Second line # original content is "Second line"
|
|
1013
|
+
01:Second line # original content is "Second line"
|
|
1014
|
+
```
|
|
1015
|
+
|
|
1016
|
+
If there are multiple spaces, only the first is removed, so `01: indented`
|
|
1017
|
+
names the original line ` indented` (two leading spaces). `read` output
|
|
1018
|
+
always uses exactly one space, so lines copied from `read` need no
|
|
1019
|
+
adjustment.
|
|
1020
|
+
|
|
1021
|
+
In `NEW`, all whitespace is literal content; nothing is stripped.
|
|
877
1022
|
|
|
878
1023
|
### Edit from standard input
|
|
879
1024
|
|
|
880
|
-
|
|
1025
|
+
At most one argument among all the `OLD` and `NEW` arguments may be `-`,
|
|
1026
|
+
read from standard input:
|
|
1027
|
+
|
|
1028
|
+
```sh
|
|
1029
|
+
rwe edit notes.txt - 'New text' <<'EOF'
|
|
1030
|
+
01: First line
|
|
1031
|
+
02: Second line
|
|
1032
|
+
EOF
|
|
1033
|
+
```
|
|
1034
|
+
|
|
1035
|
+
A quoted here-document `<<'EOF'` passes quotes and backslashes literally,
|
|
1036
|
+
which suits `OLD` lines and quoted code in `NEW`. For content containing
|
|
1037
|
+
single quotes, read `OLD` from standard input and quote `NEW` with double
|
|
1038
|
+
quotes, or the reverse:
|
|
881
1039
|
|
|
882
1040
|
```sh
|
|
883
|
-
rwe edit
|
|
884
|
-
01:
|
|
885
|
-
03: Another original-line replacement
|
|
1041
|
+
rwe edit f.txt - 'x = "new"' <<'EOF'
|
|
1042
|
+
01: x = 'old'
|
|
886
1043
|
EOF
|
|
887
1044
|
```
|
|
888
1045
|
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
terminates the last block.
|
|
1046
|
+
Here `OLD` comes from the here-document, and `NEW` is the single line
|
|
1047
|
+
`x = "new"`. Passing `-` for more than one argument is an error.
|
|
892
1048
|
|
|
893
1049
|
### Validation is all-or-nothing before saving
|
|
894
1050
|
|
|
895
1051
|
The file is not intentionally changed if any of these conditions occurs:
|
|
896
1052
|
|
|
897
|
-
-
|
|
898
|
-
|
|
899
|
-
-
|
|
900
|
-
-
|
|
1053
|
+
- The number of `OLD` and `NEW` arguments is odd, so they do not form pairs
|
|
1054
|
+
(a usage error)
|
|
1055
|
+
- `OLD` is empty or contains no valid header
|
|
1056
|
+
- A line of `OLD` is not of the form `HEX: original content`
|
|
1057
|
+
- An address is zero or not hexadecimal
|
|
1058
|
+
- An address appears more than once, in one `OLD` or across pairs,
|
|
1059
|
+
including aliases such as `1` and `01`
|
|
901
1060
|
- Any address is beyond the original file's last line
|
|
1061
|
+
- Addresses in one OLD are not contiguous
|
|
1062
|
+
- Any addressed line's content does not exactly match the file, in any pair
|
|
1063
|
+
(the mis-edit guard)
|
|
902
1064
|
- Any original line is not valid UTF-8
|
|
903
|
-
-
|
|
1065
|
+
- `OLD` or `NEW` from standard input is not valid UTF-8
|
|
904
1066
|
- The target is not a regular file after resolving symbolic links
|
|
1067
|
+
- More than one argument is `-` (standard input)
|
|
905
1068
|
|
|
906
1069
|
For example, this rejects the **entire** operation rather than applying the
|
|
907
|
-
valid first
|
|
1070
|
+
valid first pair:
|
|
908
1071
|
|
|
909
1072
|
```sh
|
|
910
|
-
rwe edit three-lines.txt '01: Valid replacement
|
|
911
|
-
|
|
1073
|
+
rwe edit three-lines.txt '01: First line' 'Valid replacement' 'ff: Anything' 'X'
|
|
1074
|
+
```
|
|
1075
|
+
|
|
1076
|
+
```text
|
|
1077
|
+
rwe: line ff is outside the file (3 lines total)
|
|
912
1078
|
```
|
|
913
1079
|
|
|
914
1080
|
On success, `rwe` prints:
|
|
@@ -917,8 +1083,23 @@ On success, `rwe` prints:
|
|
|
917
1083
|
Edited notes.txt (2 original line addresses)
|
|
918
1084
|
```
|
|
919
1085
|
|
|
920
|
-
The count is the number of distinct original addresses
|
|
921
|
-
replacement lines.
|
|
1086
|
+
The count is the number of distinct original addresses across all pairs,
|
|
1087
|
+
not the number of replacement lines.
|
|
1088
|
+
|
|
1089
|
+
### Migrating from 0.1.x
|
|
1090
|
+
|
|
1091
|
+
Version 0.2.0 replaces the single-argument edit form and the `HEX-` delete
|
|
1092
|
+
blocks of 0.1.x:
|
|
1093
|
+
|
|
1094
|
+
- 0.1.x passed one `EDITS` argument whose blocks looked like
|
|
1095
|
+
`01: new content`, and deleted lines with headers such as `02-`.
|
|
1096
|
+
- 0.2.0 passes pairs: `OLD` holds the **verified original lines** copied
|
|
1097
|
+
from `read` (`01: old content`), and `NEW` holds the replacement with no
|
|
1098
|
+
prefixes. Deletion is `NEW` `''`. One pair is the minimum; the same
|
|
1099
|
+
command accepts further `OLD NEW` pairs.
|
|
1100
|
+
|
|
1101
|
+
The old form no longer parses: `rwe edit notes.txt '01: text'` fails with a
|
|
1102
|
+
usage error because `edit` now requires one or more `OLD NEW` pairs.
|
|
922
1103
|
|
|
923
1104
|
## Input, output, paths, and encoding
|
|
924
1105
|
|
|
@@ -948,9 +1129,9 @@ needs only another stream's data.
|
|
|
948
1129
|
- `read` recognizes LF (`\n`) and strips a preceding CR for CRLF display.
|
|
949
1130
|
- A lone carriage return is not treated as a line separator in the original
|
|
950
1131
|
file.
|
|
951
|
-
- CRLF and CR characters inside the `
|
|
952
|
-
LF for parsing; original file line-ending preservation is handled
|
|
953
|
-
separately when
|
|
1132
|
+
- CRLF and CR characters inside the `OLD` or `NEW` argument are normalized
|
|
1133
|
+
to LF for parsing; original file line-ending preservation is handled
|
|
1134
|
+
separately when the replacement is rendered.
|
|
954
1135
|
- No character-set auto-detection or encoding option is provided.
|
|
955
1136
|
- Binary files are unsupported.
|
|
956
1137
|
|
|
@@ -969,24 +1150,24 @@ symbolic link itself remains a link.
|
|
|
969
1150
|
### What is preserved by `edit`
|
|
970
1151
|
|
|
971
1152
|
- Existing file permission bits are copied to the temporary file.
|
|
972
|
-
- Each replaced original line keeps its original LF or CRLF ending.
|
|
973
1153
|
- Unselected lines retain their original bytes after the whole file has
|
|
974
1154
|
passed UTF-8 validation.
|
|
975
|
-
-
|
|
976
|
-
|
|
977
|
-
-
|
|
978
|
-
|
|
979
|
-
|
|
1155
|
+
- The replacement uses the file's first encountered line-ending style,
|
|
1156
|
+
defaulting to LF. Unselected lines keep their original LF or CRLF endings.
|
|
1157
|
+
- A replacement of a final line without a newline remains without a final
|
|
1158
|
+
newline. When a multiline replacement expands an unterminated final line,
|
|
1159
|
+
the last new line remains unterminated.
|
|
980
1160
|
- A symbolic link remains a symbolic link while its target is edited.
|
|
981
1161
|
|
|
982
1162
|
### Atomic save model
|
|
983
1163
|
|
|
984
1164
|
`edit` follows this sequence:
|
|
985
1165
|
|
|
986
|
-
1. Parse
|
|
1166
|
+
1. Parse the `OLD` addresses and the `NEW` replacement lines.
|
|
987
1167
|
2. Resolve the target and read the original file.
|
|
988
1168
|
3. Validate the file type, UTF-8 content, and every address.
|
|
989
|
-
4.
|
|
1169
|
+
4. Remove the addressed original lines bottom-up and build the replacement
|
|
1170
|
+
in memory.
|
|
990
1171
|
5. Create a temporary file in the target's directory.
|
|
991
1172
|
6. Copy the original permission bits and write the complete result.
|
|
992
1173
|
7. Replace the target with `os.replace`.
|
|
@@ -1010,7 +1191,9 @@ locking, or conflict detection.
|
|
|
1010
1191
|
file or parent directory. Atomic replacement is not a guarantee against
|
|
1011
1192
|
every power-loss or filesystem-failure scenario.
|
|
1012
1193
|
- **Parent directories:** `write` does not create them.
|
|
1013
|
-
- **
|
|
1194
|
+
- **Replacement addressing:** `edit` replaces one whole block of lines. The
|
|
1195
|
+
addresses name individual original lines; there is no range syntax such as
|
|
1196
|
+
`01:03` in `OLD`, and no regex or search mode.
|
|
1014
1197
|
- **Configuration:** there are no configuration files, environment-variable
|
|
1015
1198
|
options, plugins, or interactive prompts.
|
|
1016
1199
|
- **Read performance:** only selected lines are decoded and formatted, but the
|
|
@@ -1070,15 +1253,20 @@ Addresses are hexadecimal. `64` means decimal 100. Decimal 64 is hexadecimal
|
|
|
1070
1253
|
|
|
1071
1254
|
The file has at least 256 lines, so all displayed addresses use three digits.
|
|
1072
1255
|
|
|
1073
|
-
####
|
|
1256
|
+
#### “`dead: text` in `NEW` is inserted literally”
|
|
1074
1257
|
|
|
1075
|
-
|
|
1076
|
-
|
|
1258
|
+
Replacement lines carry no prefixes and are never parsed as addresses. A line
|
|
1259
|
+
such as `dead: text` in `NEW` becomes file content exactly as supplied. In
|
|
1260
|
+
`OLD`, every line must be a valid `HEX: original content` header, so a line
|
|
1261
|
+
such as `dead: text` there is rejected as malformed
|
|
1262
|
+
(`expected 'HEX: original content', got: ...`).
|
|
1077
1263
|
|
|
1078
|
-
####
|
|
1264
|
+
#### “My `edit` left a blank line I did not want”
|
|
1079
1265
|
|
|
1080
|
-
|
|
1081
|
-
|
|
1266
|
+
With the verified-`OLD` syntax, removing a line is explicit: give the line's
|
|
1267
|
+
verified original content in `OLD` and pass `NEW` as the empty string `''`.
|
|
1268
|
+
An empty **`NEW` line** is different: it leaves one blank line in place, and
|
|
1269
|
+
a file emptied that way never reaches 0 bytes.
|
|
1082
1270
|
|
|
1083
1271
|
#### “I only see 1000 lines”
|
|
1084
1272
|
|
|
@@ -1132,9 +1320,9 @@ calls. It covers:
|
|
|
1132
1320
|
- Single, finite, and open-ended ranges
|
|
1133
1321
|
- Empty files, unterminated final lines, and the 1000-line cap
|
|
1134
1322
|
- Exclusive creation and exact multiline content
|
|
1135
|
-
-
|
|
1136
|
-
-
|
|
1137
|
-
-
|
|
1323
|
+
- Whole-block replacement, non-contiguous addresses, and deletion
|
|
1324
|
+
- Mis-edit guard, duplicate and out-of-range rejection
|
|
1325
|
+
- Blank-line `NEW`, standard input, and terminator semantics
|
|
1138
1326
|
- CRLF and final-newline preservation
|
|
1139
1327
|
- Permissions, symbolic links, invalid UTF-8, and failed replacement cleanup
|
|
1140
1328
|
- Help, version, usage errors, and the real `python -m rwe` surface
|
|
@@ -1306,15 +1494,15 @@ Suppose the new version is `0.2.0`.
|
|
|
1306
1494
|
|
|
1307
1495
|
- `src/rwe/cli.py` contains a small hand-written argument dispatcher rather
|
|
1308
1496
|
than `argparse`, avoiding that startup cost.
|
|
1309
|
-
- Hexadecimal validation and
|
|
1497
|
+
- Hexadecimal validation and `OLD` line parsing avoid importing `re`.
|
|
1310
1498
|
- `tempfile` is imported only when `edit` needs it, keeping `read` and `write`
|
|
1311
1499
|
startup lighter.
|
|
1312
1500
|
- `read` formats at most 1000 selected lines, then counts remaining newlines
|
|
1313
1501
|
in 1 MiB binary chunks to determine total width.
|
|
1314
1502
|
- `write` uses `O_WRONLY | O_CREAT | O_EXCL` for atomic no-overwrite creation.
|
|
1315
|
-
- `edit`
|
|
1316
|
-
|
|
1317
|
-
`os.replace`.
|
|
1503
|
+
- `edit` verifies every `OLD` line against the original file, removes the
|
|
1504
|
+
addressed lines, inserts each pair's `NEW` at that pair's lowest
|
|
1505
|
+
address, and saves via a sibling temporary file and `os.replace`.
|
|
1318
1506
|
- Expected failures use the internal `RweError` exception and produce concise
|
|
1319
1507
|
English messages rather than tracebacks.
|
|
1320
1508
|
|