Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1#!/usr/bin/env python3 

2# -*- coding: utf-8 -*- 

3import argparse 

4import sys 

5from textwrap import dedent 

6 

7from .transliteration import cyrillic_to_latin 

8from .transliteration import latin_to_cyrillic 

9 

10 

11def create_parser() -> argparse.ArgumentParser: 

12 parser = argparse.ArgumentParser( 

13 description=dedent( 

14 """\ 

15 Serbian Cyrillic ↔ Latin transliterator. 

16 

17 Allows transliteration of UTF-8 encoded strings between Serbian Cyrillic 

18 and Latin scripts. 

19 """ 

20 ) 

21 ) 

22 

23 parser.add_argument( 

24 "infile", 

25 nargs="?", 

26 type=argparse.FileType("r+", encoding="utf-8"), 

27 default=sys.stdin, 

28 help="Input file. If omitted, reads STDIN.", 

29 ) 

30 parser.add_argument( 

31 "-o", 

32 "--outfile", 

33 type=argparse.FileType("w", encoding="ascii"), 

34 default=sys.stdout, 

35 help="Output file. If omitted, writes to STDOUT.", 

36 ) 

37 

38 trans_direction = parser.add_mutually_exclusive_group(required=True) 

39 trans_direction.add_argument( 

40 "--cl", help="Cyrillic to Latin transliteration.", action="store_true" 

41 ) 

42 trans_direction.add_argument( 

43 "--lc", help="Latin to Cyrillic transliteration.", action="store_true" 

44 ) 

45 

46 return parser 

47 

48 

49def main(): 

50 parser = create_parser() 

51 args = parser.parse_args() 

52 

53 with args.infile as infile, args.outfile as outfile: 

54 if args.lc: 

55 trans_function = latin_to_cyrillic 

56 if args.cl: 

57 trans_function = cyrillic_to_latin 

58 

59 for line in infile: 

60 line: str 

61 outfile.write(trans_function(line)) 

62 

63 

64if __name__ == "__main__": 

65 main()