Skip to content

Commit

Permalink
python cli read file
Browse files Browse the repository at this point in the history
  • Loading branch information
paiv committed Nov 10, 2024
1 parent e1f2c15 commit 877a03f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
24 changes: 19 additions & 5 deletions python/uklatn.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python
# Generated by gentables.py, do not edit.


Expand Down Expand Up @@ -215,26 +216,39 @@ def decode(text, table=None):


def main(args):
text = ' '.join(args.text)
table = args.table
if table is None:
table = 'DSTU_9112_A'

names = {'DSTU_9112_A': 1, 'DSTU_9112_B': 2, 'KMU_55': 3}
table = names[table]

tr = encode
if args.cyrillic and not args.latin:
tr = decode
res = tr(text, table)
print(res)

if args.file:
text = args.file.read()
res = tr(text, table)
print(res, end='')

if args.text:
text = ' '.join(args.text)
res = tr(text, table)
print(res)


if __name__ == '__main__':
import argparse
import argparse, sys
parser = argparse.ArgumentParser()
parser.add_argument('text', nargs='+', help='text to transliterate')
parser.add_argument('text', nargs='*', help='text to transliterate')
parser.add_argument('-f', '--file', type=argparse.FileType('r'), help='read text from file')
parser.add_argument('-t', '--table', choices=['DSTU_9112_A', 'DSTU_9112_B', 'KMU_55'], help='transliteration system (default: DSTU_9112_A)')
parser.add_argument('-l', '--latin', action='store_true', help='convert to Latin script (default)')
parser.add_argument('-c', '--cyrillic', action='store_true', help='convert to Cyrillic script')

args = parser.parse_args()
if (not args.text) and (not args.file):
parser.error(f'the following arguments are required: text or file')

main(args)
2 changes: 1 addition & 1 deletion tools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ js:
@$(PYTHON) gentests.py js >> ../js/testuklatn.js

py:
@echo "# Generated by gentables.py, do not edit.\n" > ../python/uklatn.py
@echo "#!/usr/bin/env python\n# Generated by gentables.py, do not edit.\n" > ../python/uklatn.py
@$(PYTHON) gentables.py py >> ../python/uklatn.py
@echo "# Generated by gentests.py, do not edit.\n" > ../python/tests/uklatn_tests.py
@$(PYTHON) gentests.py py >> ../python/tests/uklatn_tests.py
Expand Down
23 changes: 18 additions & 5 deletions tools/gen/gen_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,28 +243,41 @@ def decode(text, table=None):
def main(args):
text = ' '.join(args.text)
table = args.table
if table is None:
table = {default_table!r}
names = {table_names!r}
table = names[table]
tr = encode
if args.cyrillic and not args.latin:
tr = decode
res = tr(text, table)
print(res)
if args.file:
text = args.file.read()
res = tr(text, table)
print(res, end='')
if args.text:
text = ' '.join(args.text)
res = tr(text, table)
print(res)
if __name__ == '__main__':
import argparse
import argparse, sys
parser = argparse.ArgumentParser()
parser.add_argument('text', nargs='+', help='text to transliterate')
parser.add_argument('text', nargs='*', help='text to transliterate')
parser.add_argument('-f', '--file', type=argparse.FileType('r'), help='read text from file')
parser.add_argument('-t', '--table', choices={table_list!r}, help='transliteration system (default: {default_table})')
parser.add_argument('-l', '--latin', action='store_true', help='convert to Latin script (default)')
parser.add_argument('-c', '--cyrillic', action='store_true', help='convert to Cyrillic script')
args = parser.parse_args()
if (not args.text) and (not args.file):
parser.error(f'the following arguments are required: text or file')
main(args)
'''
text = template.format(**context)
Expand Down

0 comments on commit 877a03f

Please sign in to comment.