コード例 #1
0
def interpreter(lang=None):
    """
    zhpy interpreter

Accept args:
    lang:
        interpreter language
    """
    try:
        import readline
        import rlcompleter
        readline.parse_and_bind("tab: complete")
    except ImportError:
        pass

    con = ZhPyConsole()
    if lang == 'tw':
        banner = '周蟒 %s 於 %s 基於 Python %s'%(version, sys.platform,
                                                  sys.version.split()[0])
    elif lang == 'cn':
        banner = '周蟒 %s 于 %s 基于 Python %s'%(version, sys.platform,
                                                  sys.version.split()[0])
    else:
        banner = 'zhpy %s in %s on top of Python %s'%(version, sys.platform,
                                                  sys.version.split()[0])
    annotator()
    # able to import modules in current directory
    sys.path.insert(0, '')
    con.interact(banner)
コード例 #2
0
def interpreter(lang=None):
    """
    zhpy interpreter

Accept args:
    lang:
        interpreter language
    """
    try:
        import readline
        import rlcompleter
        readline.parse_and_bind("tab: complete")
    except ImportError:
        pass

    con = ZhPyConsole()
    if lang == 'tw':
        banner = '周蟒 %s 於 %s 基於 Python %s'%(version, sys.platform,
                                                  sys.version.split()[0])
        if sys.platform == 'win32':
            banner = unicode(banner, 'utf-8').encode(sys.stdout.encoding)
    elif lang == 'cn':
        banner = '周蟒 %s 于 %s 基于 Python %s'%(version, sys.platform,
                                                  sys.version.split()[0])
        if sys.platform == 'win32':
            banner = unicode(banner, 'utf-8').encode(sys.stdout.encoding)
    else:
        banner = 'zhpy %s in %s on top of Python %s'%(version, sys.platform,
                                                  sys.version.split()[0])
    annotator()
    # able to import modules in current directory
    sys.path.insert(0, '')
    con.interact(banner)
コード例 #3
0
def interpreter(lang=None):
    """
    zhpy interpreter

Accept args:
    lang:
        interpreter language
    """
    try:
        import readline
        import rlcompleter
        readline.parse_and_bind("tab: complete")
    except ImportError:
        pass

    con = ZhPyConsole()
    if lang == 'tw':
        banner = '周蟒 %s 於 %s 基於 Python %s'%(version, sys.platform,
                                                  sys.version.split()[0])
    elif lang == 'cn':
        banner = '周蟒 %s 于 %s 基于 Python %s'%(version, sys.platform,
                                                  sys.version.split()[0])
    else:
        banner = 'zhpy %s in %s on top of Python %s'%(version, sys.platform,
                                                  sys.version.split()[0])
    annotator()
    con.interact(banner)
コード例 #4
0
def interpreter():
    """
    zhpy interpreter
    """
    try:
        import readline
        import rlcompleter
        readline.parse_and_bind("tab: complete")
    except ImportError:
        pass

    con = ZhPyConsole()
    banner = 'zhpy %s in %s on top of Python %s' % (version, sys.platform,
                                                    sys.version.split()[0])
    annotator()
    con.interact(banner)
コード例 #5
0
def interpreter():
    """
    zhpy interpreter
    """
    try:
        import readline
        import rlcompleter
        readline.parse_and_bind("tab: complete")
    except ImportError:
        pass

    con = ZhPyConsole()
    banner = 'zhpy %s in %s on top of Python %s'%(version, sys.platform,
                                                  sys.version.split()[0])
    annotator()
    con.interact(banner)
コード例 #6
0
ファイル: zhpy_cmd.py プロジェクト: BGCX261/zhpy-svn-to-git
def commandtool():
    """command line tool method
    
    input:
        speficy the input source
    output:
        speficy the output source
    python:
        compile to python and run
    cmp:
        input raw zhpy source and run
    encoding:
        specify the encoding
    """
    parser = OptionParser(
        usage="usage: %prog [-i|-p] input [-o] [output] [--e] [encoding]",
        version="zhpy %s" % version)
    parser.add_option("-i",
                      "--input",
                      help="speficy the input source",
                      dest="input",
                      default=None)
    parser.add_option("-o",
                      "--output",
                      help="speficy the output source",
                      dest="output",
                      default=None)
    parser.add_option("-p",
                      "--python",
                      help="compile to python and run",
                      dest="python",
                      default=None)
    parser.add_option("-c",
                      "--cmd",
                      help="input raw zhpy source and run",
                      dest="cmp",
                      default=None)
    parser.add_option("-e",
                      "--encoding",
                      help="specify the encoding",
                      dest="encoding",
                      default="")
    (options, args) = parser.parse_args()

    os.chdir(os.getcwd())
    #run as script
    if options.cmp:
        test = options.cmp
        annotator()
        if options.encoding:
            result = convertor(test, options.encoding)
        else:
            result = convertor(test)
        try_run(result)
        return
    #run as command
    #TODO: accept args
    argv = sys.argv[1:]
    if len(argv) >= 1:
        if (options.input is None) and argv[0].endswith("py"):
            options.input = argv[0]
        if options.python:
            options.input = options.python
        #if options.input:

        test = file(options.input, "r").read()
        annotator()
        if options.encoding:
            result = convertor(test, options.encoding)
        else:
            result = convertor(test)

        if len(argv) == 2:
            if argv[0].endswith("py") and argv[1].endswith("py"):
                options.output = argv[1]
            if options.python:
                filename = os.path.splitext(options.python)[0]
                file("n_" + filename + ".py", "w").write(result)
                print "compile to python and run: %s" % ("n_" + filename +
                                                         ".py")
        if options.output:
            file(options.output, "w").write(result)
        else:
            try_run(result)
    else:
        from zhpy_interpreter import interpreter
        interpreter()
コード例 #7
0
ファイル: zhpy_cmd.py プロジェクト: BGCX261/zhpy-svn-to-git
def commandtool():
    """command line tool method
    
    input: speficy the input source

    output: speficy the output source
    
    python: compile to python and run
    
    cmp: input raw zhpy source and run
    
    encoding: specify the encoding
    
    info: zhpy information
    """
    parser = OptionParser(
            usage="usage: %prog [-i|-p] input [-o] [output] [--e] [encoding]",
            version="zhpy %s"%version)
    parser.add_option("-i", "--input",
            help="speficy the input source",
            dest="input", default = None)
    parser.add_option("-o", "--output",
            help="speficy the output source",
            dest="output", default = None)
    parser.add_option("-p", "--python",
            help="compile to python and run",
            dest="python", default = None)
    parser.add_option("-c", "--cmd",
            help="input raw zhpy source and run",
            dest="cmp", default = None)
    parser.add_option("-e", "--encoding",
            help="specify the encoding",
            dest="encoding", default = "")
    parser.add_option("-V", "--info",
            help="zhpy information",
            action="store_true", dest="info")
    (options, args) = parser.parse_args()
    
    if options.info:
        from zhpy_info import info
        info()
        return
    
    os.chdir(os.getcwd())
    #run as script
    if options.cmp:
        test = options.cmp
        annotator()
        if options.encoding:
            result = convertor(test, options.encoding)
        else:
            result = convertor(test)
        try_run(result)
        return
    #run as command
    #TODO: accept args
    argv = sys.argv[1:]
    if len(argv) >= 1:
        if options.python:
            options.input = options.python
        # able to run as script without subname
        if (options.input is None):
            options.input = argv[0]

        #if options.input:
        print "input", options.input
        test = file(options.input, "r").read()
        annotator()
        if options.encoding:
            result = convertor(test, options.encoding)
        else:
            result = convertor(test)
            
        if len(argv) == 2:
            if argv[0].endswith("py") and argv[1].endswith("py"):
                options.output = argv[1]
            if options.python:
                filename = os.path.splitext(options.python)[0]
                file("n_"+filename+".py","w").write(result)
                print "compile to python and run: %s"%("n_"+filename+".py")
        if options.output:
            file(options.output,"w").write(result)
        else:
            try_run(result)
    else:
        from zhpy_interpreter import interpreter
        interpreter()
コード例 #8
0
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
    --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
    # run as interpreter
    if len(argv) == 0:
        from interpreter import interpreter
        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()
        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
            interpreter('tw')
            sys.exit()
        elif argv[0] == '--cn':
            from interpreter import interpreter
            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':
            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_" + filename + ".twpy"
            if (len(argv) != 0) and (argv[0] == '-v'
                                     or argv[0] == '--verbose'):
                verbose = True
        # python to cnpy
        elif argv[0] == '--cn':
            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_" + 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]
            del (argv[:2])
            target = "n_" + filename + ".py"
            python = True
            print "compile to python and run: %s" % ("n_" + filename + ".py")
            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)
        try_run(result)
        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()
                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()
コード例 #9
0
def commandline():
    """zhpy, the python language on 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
    
help:
    get information:
        
    ::
    
        $ zhpy --info

    interpreter usage:

    ::
          
        $ zhpy
        $ zhpy --tw
        $ zhpy --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]
    
    """
    argv = sys.argv[1:]
    os.chdir(os.getcwd())

    source = None
    target = None
    encoding = None
    raw_source = None
    verbose = False
    python = False

    # run as interpreter
    if len(argv) == 0:
        from interpreter import interpreter
        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()
        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
            interpreter('tw')
            sys.exit()
        elif argv[0] == '--cn':
            from interpreter import interpreter
            interpreter('cn')
            sys.exit()
        else:
            print commandline.__doc__
            sys.exit()
    # accept "-c -e -v" or "-i -o -e -v" or "-p -e" or "-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 not len(argv) and (argv[0] == '-v'
                                      or argv[0] == '--verbose'):
                    verbose = True

        elif argv[0] == '-i' or argv[0] == '--input':
            source = argv[1]
            del (argv[:2])
            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 not len(argv) 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]
            del (argv[:2])
            target = "n_" + filename + ".py"
            python = True
            print "compile to python and run: %s" % ("n_" + filename + ".py")
            if len(argv) >= 2 and (argv[0] == '-e' or argv[0] == '--encoding'):
                encoding = argv[1]
                del (argv[:2])
                if not len(argv) 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, encoding)
        else:
            result = convertor(raw_source)
        try_run(result)
        sys.exit()

    if encoding:
        print "encoding", encoding

    if source:
        if verbose:
            print "input", source
        # convertor
        try:
            test = file(source, "r").read()
            annotator()
            if encoding:
                result = convertor(test, encoding)
            else:
                result = convertor(test)
        except:
            print "zhpy Exception: you may input unproper source"
            sys.exit()
    if target:
        if verbose:
            print "output", target
        file(target, "w").write(result)
        if python:
            try_run(result)
    else:
        try_run(result)
コード例 #10
0
ファイル: pyzh.py プロジェクト: BGCX261/zhpy-svn-to-git
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.
"""

from zhpy import annotator
from zhdc import twdict, cndict, revert_dict

hexval = '0123456789abcdef'

annotator()
# make reverse traditional chinese dicts
rev_twdict = revert_dict(twdict)
# make reverse simplified chinese dicts
rev_cndict = revert_dict(cndict)


def rev_merger(anno_dict, use_dict, verbose=True):
    """
    merge extra bindings into reverse dict
    
    >>> keys = [('遊戲', 'pygame'), ('系統', 'sys')]
    >>> rev_merger(keys, rev_twdict)
    add pygame=遊戲
    add sys=系統
    >>> 'pygame' in rev_twdict
コード例 #11
0
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
    --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
    # run as interpreter
    if len(argv) == 0:
        from interpreter import interpreter
        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()
        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
            interpreter('tw')
            sys.exit()
        elif argv[0] == '--cn':
            from interpreter import interpreter
            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':
            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_"+filename+".twpy"
            if (len(argv)!=0) and (argv[0] == '-v' or argv[0] == '--verbose'):
                verbose = True
        # python to cnpy
        elif argv[0] == '--cn':
            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_"+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]
            del(argv[:2])            
            target = "n_"+filename+".py"
            python = True
            print "compile to python and run: %s"%("n_"+filename+".py")
            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)
        try_run(result)
        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()
                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()
コード例 #12
0
ファイル: pyzh.py プロジェクト: BGCX261/zhpy-svn-to-git
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.
"""

 
from zhpy import annotator
from zhdc import twdict, cndict, revert_dict

hexval = '0123456789abcdef'

annotator()
# make reverse traditional chinese dicts
rev_twdict = revert_dict(twdict)
# make reverse simplified chinese dicts
rev_cndict = revert_dict(cndict)
    
def rev_merger(anno_dict, use_dict, verbose=True):
    """
    merge extra bindings into reverse dict
    
    >>> keys = [('遊戲', 'pygame'), ('系統', 'sys')]
    >>> rev_merger(keys, rev_twdict)
    add pygame=遊戲
    add sys=系統
    >>> 'pygame' in rev_twdict
    True
コード例 #13
0
def commandline():
    """zhpy, the python language on chinese
    
Accept options:
    input: speficy the input source
    output: speficy the output source
    python: compile to python and run
    cmp: input raw zhpy source and run
    encoding: specify the encoding
    info: zhpy information
    verbose: show zhpy progress in detail
    # zhpy: compile python code to zhpy

help:
    command usage: zhpy [-i|-p] input [-o] [output] [-e] [encoding] [-v]
    script usage: zhpy [-c] source [-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   
    
    """
    argv = sys.argv[1:]
    os.chdir(os.getcwd())
    
    source = None
    target = None
    encoding = None
    raw_source = None
    verbose = False
    python = False
    
    # run as interpreter
    if len(argv) == 0:
        from interpreter import interpreter
        interpreter()
        sys.exit()
    # run as script
    # not accept any option
    elif not argv[0].startswith('-'):
        source = argv[0]
        sys.argv = argv
    # run as command
    # accept "-i -o -e" or "-p -e" or "-c -e"
    elif len(argv)==1:
        if argv[0] == '--info':
            from info import info
            info()
            sys.exit()
        if argv[0] == '-h' or argv[0] == '--help':
            print commandline.__doc__
            sys.exit()
    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 not len(argv) and (argv[0] == '-v' or argv[0] == '--verbose'):
                    verbose = True

        elif argv[0] == '-i' or argv[0] == '--input':
            source = argv[1]
            del(argv[:2])
            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 not len(argv) 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]
            del(argv[:2])
            target = "n_"+filename+".py"
            python = True
            print "compile to python and run: %s"%("n_"+filename+".py")
            if len(argv)>=2 and (argv[0] == '-e' or argv[0] == '--encoding'):
                encoding = argv[1]
                del(argv[:2])
                if not len(argv) 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, encoding)
        else:
            result = convertor(raw_source)
        try_run(result)
        sys.exit()
    
    if encoding:
        print "encoding", encoding

    if source:
        if verbose:
            print "input", source
        # convertor
        test = file(source, "r").read()
        annotator()
        if encoding:
            result = convertor(test, encoding)
        else:
            result = convertor(test)
    if target:
        if verbose:
            print "output", target
        file(target,"w").write(result)
        if python:
            try_run(result)
    else:
        try_run(result)