#!/usr/bin/python2

import os
import glob
import datetime
import sys
from gi.repository import GLib

GROUP = "Nemo Action"

class Main:
    def __init__(self):
        if len(sys.argv) > 1:
            action_files = glob.glob(os.path.join(sys.argv[1], "*.nemo_action.in"))
        else:
            action_files = glob.glob(os.path.join(os.getcwd(), "*.nemo_action.in"))
        if len(action_files) > 0:
            dt = datetime.datetime
            outstring = """
                This is a dummy file for translating Nemo Action files

                It was generated by the extract_action_strings script on %s UTC.

                """ % (dt.utcnow())
            outstring += "\n"
            for fn in action_files:
                keyfile = GLib.KeyFile.new()
                if keyfile.load_from_file(fn, GLib.KeyFileFlags.NONE):
                    if keyfile.has_group(GROUP):
                        friendly_fn = os.path.split(fn)[1].replace(".", "_")
                        try:
                            name = keyfile.get_string(GROUP, "Name")
                            name_line = '%s_Name = _("%s")\n' % (friendly_fn, name)
                            outstring += (name_line)
                        except GLib.GError:
                            name = None
                        try:
                            tooltip = keyfile.get_string(GROUP, "Comment")
                            tooltip_line = '%s_Tooltip = _("%s")\n' % (friendly_fn, tooltip)
                            outstring += (tooltip_line)
                        except GLib.GError:
                            tooltip = None

            if len(sys.argv) > 1:
                outfilename = os.path.join(sys.argv[1], "action_i18n_strings.py")
            else:
                outfilename = os.path.join(os.getcwd(), "action_i18n_strings.py")
            if os.path.exists(outfilename):
                os.remove(outfilename)
            outfile = open(outfilename, 'w')
            outfile.write(outstring)
            outfile.close()
            print "Extraction complete.  Run makepot now"

if __name__ == "__main__":
    Main()
