mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-06 13:58:36 +01:00
refactor(editor): the //$e header scripts share reading, decoding, the rewrite of the header, the backup and the zip in tools/editor/editor_headers.py instead of three copies; reports, zips and written files unchanged
This commit is contained in:
@@ -8,8 +8,8 @@ without --write it only reports what it would write
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import tarfile
|
||||
import time
|
||||
|
||||
from editor_headers import body_lines, decode, load, write
|
||||
|
||||
FAMILIES = [
|
||||
(re.compile(r'^ss[dow]?\d'), 'main'),
|
||||
@@ -24,8 +24,6 @@ FAMILIES = [
|
||||
|
||||
COLOURS = [('zielon', 'z'), ('czerwon', 'c'), ('pomara', 'p'), ('żółt', 'p'), ('biał', 'b'), ('mleczn', 'b'), ('niebiesk', 'n'), ('ślep', 'x'), ('brak', 'x')]
|
||||
|
||||
LINEMARK = '//$e'
|
||||
|
||||
|
||||
def tokens(text):
|
||||
text = re.sub(r'//[^\n]*', ' ', text)
|
||||
@@ -50,14 +48,9 @@ def statements(words, keyword):
|
||||
|
||||
|
||||
def examine(path, file):
|
||||
raw = open(path, 'rb').read() # NOSONAR
|
||||
try:
|
||||
text = raw.decode('utf-8')
|
||||
encoding = 'utf-8'
|
||||
except UnicodeDecodeError:
|
||||
text = raw.decode('cp1250')
|
||||
encoding = 'cp1250'
|
||||
lines = [line for line in text.splitlines() if not line.startswith(LINEMARK)]
|
||||
raw = load(path)
|
||||
text, encoding = decode(raw, 'utf-8')
|
||||
lines = body_lines(text)
|
||||
body = '\n'.join(lines)
|
||||
words = tokens(body)
|
||||
count = max([int(m) for m in re.findall(r'\(p(\d+)\)', body)] or [0])
|
||||
@@ -176,24 +169,12 @@ def examine(path, file):
|
||||
return {'raw': raw, 'text': text, 'encoding': encoding, 'header': header, 'kind': kind, 'model': model, 'read': read, 'lamps': lamps, 'mount': mount, 'lean': lean, 'count': count}, ''
|
||||
|
||||
|
||||
def rewrite(info):
|
||||
text = info['text']
|
||||
eol = '\r\n' if '\r\n' in text else '\n'
|
||||
bom = text.startswith('')
|
||||
if bom:
|
||||
text = text[1:]
|
||||
kept = [line for line in text.split(eol) if not line.startswith(LINEMARK)]
|
||||
block = [LINEMARK + ' ' + line for line in info['header']]
|
||||
out = eol.join(block + kept)
|
||||
return (('' if bom else '') + out).encode(info['encoding'])
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
print(__doc__)
|
||||
return 1
|
||||
root = os.path.join(sys.argv[1], 'scenery')
|
||||
write = '--write' in sys.argv
|
||||
writing = '--write' in sys.argv
|
||||
files = sorted(f for f in os.listdir(root) if f.endswith('.inc'))
|
||||
files += ['pkp/semafory/' + f for f in sorted(os.listdir(os.path.join(root, 'pkp', 'semafory'))) if f.endswith('.inc')]
|
||||
done = []
|
||||
@@ -217,19 +198,8 @@ def main():
|
||||
kinds[info['kind']] = kinds.get(info['kind'], 0) + 1
|
||||
print('described: %d %s, skipped: %d' % (len(done), kinds, len(skipped)))
|
||||
|
||||
if write:
|
||||
backup = os.path.join(sys.argv[1], 'editor_backup')
|
||||
os.makedirs(backup, exist_ok=True) # NOSONAR
|
||||
archive = os.path.join(backup, time.strftime('signals_%Y%m%d_%H%M%S.tar'))
|
||||
with tarfile.open(archive, 'w') as tar: # NOSONAR
|
||||
for file, _ in done:
|
||||
tar.add(os.path.join(root, file), arcname='scenery/' + file) # NOSONAR
|
||||
for file, info in done:
|
||||
content = rewrite(info)
|
||||
if content != info['raw']:
|
||||
with open(os.path.join(root, file), 'wb') as out: # NOSONAR
|
||||
out.write(content)
|
||||
print('written, originals in', archive)
|
||||
if writing:
|
||||
write(sys.argv[1], 'scenery', 'signals', done)
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user