rutt 1.0.0__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.
- rutt/__init__.py +2 -0
- rutt/__main__.py +2 -0
- rutt/cli.py +18 -0
- rutt/codec.py +39 -0
- rutt-1.0.0.dist-info/METADATA +5 -0
- rutt-1.0.0.dist-info/RECORD +9 -0
- rutt-1.0.0.dist-info/WHEEL +5 -0
- rutt-1.0.0.dist-info/entry_points.txt +2 -0
- rutt-1.0.0.dist-info/top_level.txt +1 -0
rutt/__init__.py
ADDED
rutt/__main__.py
ADDED
rutt/cli.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
from .codec import write, read
|
|
3
|
+
|
|
4
|
+
def main():
|
|
5
|
+
p=argparse.ArgumentParser(prog='rutt',description='RUTT compact text format')
|
|
6
|
+
s=p.add_subparsers(dest='cmd')
|
|
7
|
+
x=s.add_parser('make'); x.add_argument('text'); x.add_argument('file')
|
|
8
|
+
x=s.add_parser('convert'); x.add_argument('input'); x.add_argument('output')
|
|
9
|
+
x=s.add_parser('read'); x.add_argument('file')
|
|
10
|
+
x=s.add_parser('edit'); ss=x.add_subparsers(dest='sub'); y=ss.add_parser('set'); y.add_argument('file'); y.add_argument('text'); y=ss.add_parser('add'); y.add_argument('file'); y.add_argument('text')
|
|
11
|
+
a=p.parse_args()
|
|
12
|
+
if a.cmd=='make': write(a.file,a.text)
|
|
13
|
+
elif a.cmd=='convert': write(a.output,open(a.input,encoding='utf-8',newline='').read())
|
|
14
|
+
elif a.cmd=='read': print(read(a.file),end='')
|
|
15
|
+
elif a.cmd=='edit' and a.sub=='set': write(a.file,a.text)
|
|
16
|
+
elif a.cmd=='edit' and a.sub=='add': write(a.file,read(a.file)+a.text)
|
|
17
|
+
else: p.print_help(); return 1
|
|
18
|
+
return 0
|
rutt/codec.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import struct
|
|
2
|
+
|
|
3
|
+
ALPHABET = (" \nабвгдеёжзийклмнопрстуфхцчшщъыьэюя"
|
|
4
|
+
"АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ"
|
|
5
|
+
"abcdefghijklmnopqrstuvwxyz"
|
|
6
|
+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
7
|
+
"0123456789"
|
|
8
|
+
".,-!?:;)(/\\\"'`*=+_%[]<>|@&^#{}%₽$~»«–—")
|
|
9
|
+
CHAR_TO_INDEX = {c:i+1 for i,c in enumerate(ALPHABET)}
|
|
10
|
+
INDEX_TO_CHAR = {i+1:c for i,c in enumerate(ALPHABET)}
|
|
11
|
+
MAGIC=b'RUTT'; VERSION=1; HEADER_SIZE=9
|
|
12
|
+
class RuttError(Exception): pass
|
|
13
|
+
|
|
14
|
+
def encode(text):
|
|
15
|
+
out=bytearray()
|
|
16
|
+
for c in text:
|
|
17
|
+
if c not in CHAR_TO_INDEX: raise RuttError(f"Символ {c!r} отсутствует в алфавите RUTT")
|
|
18
|
+
out.append(CHAR_TO_INDEX[c] & 255)
|
|
19
|
+
return bytes(out)
|
|
20
|
+
|
|
21
|
+
def decode(data):
|
|
22
|
+
out=[]
|
|
23
|
+
for b in data:
|
|
24
|
+
if b not in INDEX_TO_CHAR: raise RuttError(f"Недопустимый индекс символа: {b}")
|
|
25
|
+
out.append(INDEX_TO_CHAR[b])
|
|
26
|
+
return ''.join(out)
|
|
27
|
+
|
|
28
|
+
def write(filename,text):
|
|
29
|
+
data=encode(text)
|
|
30
|
+
with open(filename,'wb') as f:
|
|
31
|
+
f.write(MAGIC); f.write(bytes([VERSION])); f.write(struct.pack('>I',len(text))); f.write(data)
|
|
32
|
+
|
|
33
|
+
def read(filename):
|
|
34
|
+
with open(filename,'rb') as f: raw=f.read()
|
|
35
|
+
if len(raw)<HEADER_SIZE or raw[:4]!=MAGIC: raise RuttError('Это не RUTT-файл.')
|
|
36
|
+
if raw[4]!=VERSION: raise RuttError(f'Неподдерживаемая версия RUTT: {raw[4]}')
|
|
37
|
+
length=struct.unpack('>I',raw[5:9])[0]; data=raw[9:]
|
|
38
|
+
if len(data)!=length: raise RuttError(f'Повреждённый RUTT-файл: ожидалось {length} байт, получено {len(data)}.')
|
|
39
|
+
return decode(data)
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
rutt/__init__.py,sha256=7TMZsF56YzhONPXcKFWvoquy6jGK5SnlECN9_a0fVWY,104
|
|
2
|
+
rutt/__main__.py,sha256=ee5vE0xcUZM3fPmxicvcw-IJXkm6nOwxARREHBWpF8Q,47
|
|
3
|
+
rutt/cli.py,sha256=Z84YgXFL3vufW1hq5OtsgJbbWbOJUygMhb9L0clyKNA,938
|
|
4
|
+
rutt/codec.py,sha256=BeoWD1cBvmVSpM-J-418dYUMEDEBJOytdVc-2eEGBcU,1631
|
|
5
|
+
rutt-1.0.0.dist-info/METADATA,sha256=G9fDBfQi4uxowvle57ojHftObTr6R-Vwg2TsphcvA8s,105
|
|
6
|
+
rutt-1.0.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
7
|
+
rutt-1.0.0.dist-info/entry_points.txt,sha256=qgU0vQD757rwzEPzm0YgmSL-Pr6fn2fA5K3Fp5kXepI,39
|
|
8
|
+
rutt-1.0.0.dist-info/top_level.txt,sha256=WZfDxrtIDAD-294NibMFM31WQ2AZ0lGziTXN4824npY,5
|
|
9
|
+
rutt-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
rutt
|