/usr/lib/python2.7/dist-packages/zhpy/commandline.py is in python-zhpy 1.7.3.1-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | #!/usr/bin/python
# -*- coding: utf-8 -*-
"""zhpy command line tool
This is the MIT license:
http://www.opensource.org/licenses/mit-license.php
Copyright (c) 2007~ Fred Lin and contributors. zhpy is a trademark of Fred Lin.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import os
import sys
from zhpy import convertor, zh_ord, zh_exec
def commandline():
"""zhpy, the python language in chinese
Accept options:
-i --input:
speficy the input source
-o --output:
speficy the output source
-p --python:
compile to python and run
-c --cmp:
input raw zhpy source and run
-e --encoding:
specify the encoding
--info:
zhpy information
-v --verbose:
show zhpy progress in detail
-V --version
show zhpy version
--tw:
convert python to twpy
--cn:
convert python to cnpy
help:
get information:
zhpy --info
interpreter usage:
zhpy [--tw | --cn]
command usage:
zhpy [-i | -p] input [-o] [output] [-e] [encoding] [-v]
::
$ zhpy input.py (.twpy, .cnpy) [arguments]
$ zhpy -i input.py (.twpy, .cnpy)
$ zhpy -i input.py -o output.py (.twpy, .cnpy)
$ zhpy -p input.py
script usage:
zhpy [-c] source [-e] [encoding] [-v]
convertor usage:
zhpy [--tw | --cn] input.py [-v]
::
$ zhpy --tw input.py [-v]
$ zhpy --cn input.py [-v]
"""
argv = sys.argv[1:]
os.chdir(os.getcwd())
source = None
target = None
encoding = None
raw_source = None
verbose = False
python = False
tw = False
cn = False
NonEnglish = False
# run as interpreter
if len(argv) == 0:
from interpreter import interpreter
import import_hook
display = os.getenv("LANG")
if display == None:
interpreter()
elif "zh_TW" in display:
interpreter('tw')
elif "zh_CN" in display:
interpreter('cn')
else:
interpreter()
sys.exit()
# run as script
# not accept any option
elif not argv[0].startswith('-'):
source = argv[0]
sys.argv = argv
# run as command
elif len(argv)==1:
if argv[0] == '--info':
from info import info
info()
sys.exit()
if argv[0] == '-V' or argv[0] == '--version':
from release import version
print "zhpy %s on python %s"%(version, sys.version.split()[0])
sys.exit()
elif argv[0] == '-h' or argv[0] == '--help':
print commandline.__doc__
sys.exit()
# run as native interpreter
elif argv[0] == '--tw':
from interpreter import interpreter
import import_hook
interpreter('tw')
sys.exit()
elif argv[0] == '--cn':
from interpreter import interpreter
import import_hook
interpreter('cn')
sys.exit()
else:
print commandline.__doc__
sys.exit()
# accept "-c -e -v"
elif len(argv)>=2:
if argv[0] == '-c' or argv[0] == '--cmp':
raw_source = argv[1]
del(argv[:2])
if len(argv)>=2 and (argv[0] == '-e' or argv[0] == '--encoding'):
encoding = argv[1]
del(argv[:2])
if (len(argv)!=0) and (argv[0] == '-v' or \
argv[0] == '--verbose'):
verbose = True
# python to twpy
elif argv[0] == '--tw':
from pyzh import zh_chr, rev_annotator
rev_annotator()
source = argv[1]
filename = os.path.splitext(source)[0]
# remove extra .tw in filename
profix = os.path.splitext(filename)[-1]
if profix =='.tw':
filename = os.path.splitext(filename)[0]
del(argv[:2])
tw = True
target = "v_"+zh_chr(filename)+".twpy"
if (len(argv)!=0) and (argv[0] == '-v' or argv[0] == '--verbose'):
verbose = True
# python to cnpy
elif argv[0] == '--cn':
from pyzh import zh_chr, rev_annotator
rev_annotator()
source = argv[1]
filename = os.path.splitext(source)[0]
# remove extra .cn in filename
profix = os.path.splitext(filename)[-1]
if profix == '.cn':
filename = os.path.splitext(filename)[0]
del(argv[:2])
cn = True
target = "v_"+zh_chr(filename)+".cnpy"
if (len(argv)!=0) and (argv[0] == '-v' or argv[0] == '--verbose'):
verbose = True
# accept "-i -o -e -v" or "-p -e" or "-c -e -v"
elif argv[0] == '-i' or argv[0] == '--input':
source = argv[1]
del(argv[:2])
if (len(argv)!=0) and (argv[0] == '-v' or argv[0] == '--verbose'):
verbose = True
if len(argv)>=2 and (argv[0] == '-e' or argv[0] == '--encoding'):
encoding = argv[1]
del(argv[:2])
if (len(argv)!=0) and (argv[0] == '-v' or \
argv[0] == '--verbose'):
verbose = True
if len(argv)>=2 and (argv[0] == '-o' or argv[0] == '--output'):
target = argv[1]
del(argv[:2])
if len(argv)>=2 and (argv[0] == '-e' or \
argv[0] == '--encoding'):
encoding = argv[1]
del(argv[:2])
if (len(argv)!=0) and (argv[0] == '-v' or \
argv[0] == '--verbose'):
verbose = True
elif argv[0] == '-p' or argv[0] == '--python':
source = argv[1]
filename = os.path.splitext(source)[0]
# remove extra .tw in filename
profix = os.path.splitext(filename)[-1]
if (profix =='.tw') or (profix =='.cn'):
filename = os.path.splitext(filename)[0]
del(argv[:2])
# chinese filename to uri filename
for i in filename:
if i not in \
list('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.'):
NonEnglish = True
print i, "file name is not in english"
break
if NonEnglish:
target = zh_ord(filename.decode('utf8'))+".py"
print "compile to python and run: %s"%(
zh_ord(filename.decode('utf8'))+".py")
else:
target = "n_"+filename+".py"
print "compile to python and run: %s"%("n_"+filename+".py")
python = True
if (len(argv)!=0) and (argv[0] == '-v' or argv[0] == '--verbose'):
verbose = True
if len(argv)>=2 and (argv[0] == '-e' or argv[0] == '--encoding'):
encoding = argv[1]
del(argv[:2])
if (len(argv)!=0) and (argv[0] == '-v' or \
argv[0] == '--verbose'):
verbose = True
else:
print commandline.__doc__
sys.exit()
#convert
if raw_source:
if verbose:
print "run raw_source", raw_source
#annotator()
if encoding:
result = convertor(raw_source, verbose, encoding)
else:
result = convertor(raw_source, verbose)
zh_exec(result)
import import_hook
sys.exit()
if encoding:
print "encoding", encoding
if source:
if verbose:
print "input", source
# convertor
if(tw or cn):
if verbose:
print "convert python code to",
try:
from pyzh import rev_annotator, python_convertor
test = file(source, "r").read()
if tw:
print "twpy"
rev_annotator('tw', verbose)
result = python_convertor(test, lang='tw')
if cn:
print "cnpy"
rev_annotator('cn', verbose)
result = python_convertor(test, lang='cn')
except Exception, e:
print "zhpy Exception: you may input unproper source"
if verbose:
print e
sys.exit()
else:
try:
test = file(source, "r").read()
#annotator()
import import_hook
if encoding:
result = convertor(test, verbose, encoding)
else:
result = convertor(test, verbose)
except Exception, e:
print "zhpy Exception: you may input unproper source"
if verbose:
print e
sys.exit()
if target:
if verbose:
print "output", target
file(target,"w").write(result)
if python:
zh_exec(result)
else:
zh_exec(result)
if __name__=="__main__":
commandline()
|