#!/usr/bin/python
#############################################################################
#
#  Linux Desktop Testing Project http://ldtp.freedesktop.org
# 
#  Author:
#     A. Nagappan <nagappan@gmail.com>
#     Lavi.Xu <guofu.xu@palmsource.com>
# 
#  Copyright 2004 - 2006 PalmSource, Inc.
# 
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Library General Public
#  License as published by the Free Software Foundation; either
#  version 2 of the License, or (at your option) any later version.
# 
#  This library is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Library General Public License for more details.
# 
#  You should have received a copy of the GNU Library General Public
#  License along with this library; if not, write to the
#  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
#  Boston, MA 02111-1307, USA.
#
#############################################################################

import ldtp, sys, re

class TagError(Exception):
    def __init__(self):
        print "It's seems that the tag name of WinMain was invalid,\
        try to use * Or ? wildcard to match"
        sys.exit(0)

def warninfo ():
    print 'Generate the tag file which you wanna use parameter in LDTP API.\n \
    \tRun as: python generatemap.py <winwain name> <python file>\n'
    sys.exit(0)

if len(sys.argv) < 3:
    warninfo()

def striptodefname(tag):
    return filterpattern.sub('', tag)

def writetitleandwinmain(fileopened):
    head = fileopened.readline()
    # check whether writes as appended mode
    if not head:
        lines = '#!/usr/bin/python\n\n'
        lines += 'sepsym = ";"\n\n'
        fileopened.writelines(lines)

winmain = sys.argv[1]
# the pattern removes the unwanted string in gotten label
filterpattern = re.compile(r'[^\d\w]')
# the pattern which unwanted class which almost doesnt use 
filterclass = ('flr', 'mbr', 'ukn')

try:
    objlists = ldtp.getobjectlist(winmain)
except LdtpExecutionError:
    raise TagError

try:
    pyfile = file(sys.argv[2], 'a+')
    writetitleandwinmain(pyfile)
except IOError:
    raise 'Cannt create file to save the map file, check again'

for objlist in objlists:
    skip = False
    lines = ''
    parent = None
    tagname = objlist
    defname = striptodefname(objlist)
    for unwanted in filterclass:
        if defname.find(unwanted) == -1:
            continue
        skip = True
        break
    if not skip:
        # check the menu control whether needs a parent of perproty
        if defname[0:3] == 'mnu':
            parenttmp = ldtp.getobjectproperty(winmain, objlist, 'parent')
            if parenttmp[0:3] == 'mnu':
                parent = parenttmp
        # write the definition of tag to a python file
        lines += 'def %s(parent = "%s"):\n' % (defname, parent)
        lines += '\ttag = "%s"\n' % tagname
        lines += '\tif parent != "None":\n'
        lines += '\t\ttag = eval(parent)() + sepsym + tag\n'
        lines += '\treturn tag\n\n'
        pyfile.writelines(lines)
else:
    pyfile.close()




