zvodd
3/27/2015 - 6:32 PM

Compile Actiona/Actionaz file from JavaScript files.

Compile Actiona/Actionaz file from JavaScript files.

import argparse, os, os.path, subprocess, platform
# import inspect
# from pprint import pprint

ACTIONAZ_EXE = ""
ACTIONAZ_VERSIONS = {
    '37': dict(script_version="1.0.0", version="3.7.0"),
    '38': dict(script_version="1.1.0", version="3.8.0")
    }
if platform.system().lower() == "windows":
    try:
        import _winreg as winreg
    except ImportError:
        import winreg
    with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT,
                            "ActionaScriptFile\shell\open\command",
                            0, winreg.KEY_QUERY_VALUE) as key:
        value, _ = winreg.QueryValueEx(key,"")
        #ACTIONAZ_EXE = r"D:\Program Files\Actiona\actiona.exe" 
        ACTIONAZ_EXE = value.split('"')[1]

elif platform.system().lower() == "linux":
    ACTIONAZ_EXE = r"actiona"


def main ():
    parser = argparse.ArgumentParser(description='Compile Actionaz File')
    parser.add_argument('file', type=str, nargs='+', help='js source file(s) to compile into actionaz file,'
                    + ' output file based on first filename and location.')
    parser.add_argument('-r', '--run', action='store_true', default=False)
    parser.add_argument('-a', '--actiona-version', type=str, default='38', choices=ACTIONAZ_VERSIONS.keys())
    args = parser.parse_args()

    # import inspect 
    # from pprint import pprint
    # pprint(inspect.getmembers(args))
    # return 

    new_file_name = ""
    all_code = ""

    for i,jsfile in enumerate(args.file):
        if not os.path.isfile(jsfile):
            print ('"{}" is not a file'.format(jsfile))
            return 2
        full_name = jsfile
        fpath, simple_name =  os.path.split(full_name)
        fpath = os.path.abspath(fpath)
        dir_name = os.path.basename(fpath)
        if i == 0:
            #output filename based on first input file path and name
            new_acaz_file_name = simple_name + ".ascr"
            new_file_name =  os.path.join(fpath,new_acaz_file_name)
        # read js into string
        code = ""
        with open(full_name, 'r') as jsfile:
            code = gen_actaz_code_action(jsfile.read())
        all_code += code

    # set up and combine strings
    settings = dict(os="Windows")
    settings.update(ACTIONAZ_VERSIONS[args.actiona_version])
    aa_file_str = gen_actaz_file(all_code, **settings)

    # create ascr file
    with open(new_file_name, 'w') as new_acaz_file:
        new_acaz_file.write(aa_file_str)

    if args.run:
        print ("'{}'' running '{}'".format(ACTIONAZ_EXE, new_file_name))
        DETACHED_PROCESS = 0x00000008
        subprocess.Popen([ACTIONAZ_EXE, new_file_name], close_fds=True, creationflags=DETACHED_PROCESS)
        # os.system('"'+ACTIONAZ_EXE+'"'+' '+new_file_name)



def gen_actaz_file(code, version="3.8.0", os="Windows", script_version="1.1.0"):
    return """<?xml version="1.0" encoding="UTF-8"?>
<scriptfile>
    <settings program="actionaz" version="{version}" scriptVersion="{script_version}" os="{os}"/>
    <actions>
        <action name="ActionCode" version="1.0.0"/>
    </actions>
    <parameters/>
    <resources/>
    {code}
</scriptfile>
    """.format(code=code, version=version, os=os, script_version=script_version)

def gen_actaz_code_action (code):
    return """    <script pauseBefore="0" pauseAfter="0">
        <action name="ActionCode">
            <exception id="0" action="0" line=""/>
            <exception id="1" action="0" line=""/>
            <exception id="2" action="1" line=""/>
            <parameter name="code">
                <subParameter name="value" code="1">{code}</subParameter>
            </parameter>
        </action>
    </script>
    """.format(code=code)

if __name__ == "__main__":
    main()