#
# Copyright (c) SAS Institute Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


import sys

statuses = {
    'UNKNOWN'  : -1,
    'WAITING'  : 0,   # Queued for processing
    'RUNNING'  : 100,
    'BLOCKED'  : 101, # Wating on input, used for image uploads
    'BUILT'    : 200,
    'FINISHED' : 300,
    'FAILED'   : 301,
    'ERROR'    : 301,
    'KILLED'   : 302,
    'NO_JOB'   : 401
    }


sys.modules[__name__].__dict__.update(statuses)
statusNames = dict([(statuses[x[0]], x[0].capitalize().replace('_', ' ')) \
                        for x in statuses.iteritems()])

stoppedStatuses = [ NO_JOB, KILLED, ERROR, FAILED, FINISHED ]
terminalStatuses = [ KILLED, ERROR, FAILED, FINISHED ]

statusCodeNames = dict([(x[1], 'STATUS_' + x[0].replace('_', '')) \
                            for x in statuses.iteritems()])

STATUSES = statuses.keys()

# code generator run by make to generate javascript constants
# should only be run by the makefile in mint/web/content/javascript
def codegen():
    s = "// this Javascript was generated by mint/jobstatus.py\n"
    s += "// do not edit or check into source control\n"

    s += "var statusNames = {"
    i = []
    for k, v in statusNames.items():
        i.append("    '%d':  '%s'" % (k, v,))
    s += ", ".join(i)
    s += "};"

    for k, v in statusCodeNames.items():
        s += "var %s = %d;\n" % (v, k,)
    return s


if __name__ == "__main__": #pragma: no cover
    import sys
    if len(sys.argv) > 1 and sys.argv[1] == "--genjs":
        print codegen()
        sys.exit(0)
    else:
        sys.exit(1)
