/
emoji_to_python.py
executable file
·55 lines (43 loc) · 1.33 KB
/
emoji_to_python.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#! /usr/bin/env python
"""
Functions to convert code from emojicode to python
USAGE:
emoji_to_python(inputfile) - Returns string python code
write_emoji_to_python(inputfile, outputfile) -
"""
import sys
import tokenize
from snake.replace_emoji import replace_emoji
def parse_file(f):
lines = replace_emoji(tokenize.tokenize(f.readline))
untokenize = tokenize.untokenize(lines)
return untokenize
def emoji_to_python(inpath):
"""
Open an 'emojicode' file and convert it to python
:param inpath: File to convert to python code
:returns: UTF-8 string python code
"""
with open(inpath, 'rb') as f:
return parse_file(f).decode('utf8')
def write_emoji_to_python(inpath, outpath):
"""
Open an 'emojicode' file and convert it to python - write to a file
:param inpath: file to convert
:param outpath: output file
"""
with open(outpath, 'w', encoding='utf8') as outfile:
outfile.write(emoji_to_python(inpath))
if __name__ == '__main__':
"""
-i to intepret the code
outputfilepath to output to file
"""
args = sys.argv
if len(args) != 3:
sys.exit("USAGE: python emoji_to_python.py [-i] inputfilepath [outputfilepath]")
elif '-i' in args:
args.remove('-i')
eval(emoji_to_python(args[1]))
else:
write_emoji_to_python(*args)