MenuParser.py :  » GUI » IceMe » IceMe-1.1.1 » src » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » GUI » IceMe 
IceMe » IceMe 1.1.1 » src » MenuParser.py
import string
import sys
import re

from constants import *

MenuRegex = re.compile(r'(?P<name>.+?)\s[\'"](?P<icon>.+?)[\'"]*\s(?P<command>.+)')

class MenuParser:

    def __init__(self, filename):
        file = open(filename)
        self.lines = file.readlines()
        file.close()

    def __getNextLine(self):
        line = None
        while len(self.lines) > 0:
            line = string.strip(self.lines[0])
            del self.lines[0]
            if line != "" and line[0] != "#":
                return line
        return None

    def getNextEntry(self):
        line = self.__getNextLine()
        if line is None:
            return None
        tag = string.split(line)[0]
        if tag == "separator":
            return [MENUTREE_SEPARATOR, None, None, None]
        elif tag == "prog":
            return [MENUTREE_PROG,] + self.__parseProg(line[5:])
        elif tag == "restart":
            return [MENUTREE_RESTART,] + self.__parseProg(line[8:])
        elif tag == "menu":
            return [MENUTREE_SUBMENU,] + self.__parseMenu(line[5:])
        elif tag == "}":
            return [MENUTREE_SUBMENU_END, None, None, None]
        else:
            sys.stderr.write("*** Unknown tag in menu file. The line was: %s\n" % line)
            return [MENUTREE_UNKNOWN, None, None, None]

    def __parseWord(self, s, start=0):

        inQuotes = 0
        atStart = 1
        word = ""
        i = start
        while i < len(s):
            c = s[i]
            i = i + 1
            if c in string.whitespace:
                if inQuotes:
                    word = word + c
                elif atStart:
                    pass
                else:
                    return (word, i)
            elif c == '"':
                inQuotes = not inQuotes
                atStart = 0
            else:
                word = word + c
                atStart = 0
        return (word, i)

    def __parseProg(self, s):
      match = MenuRegex.search(s)

        name = match.group('name')
        icon = match.group('icon')
        command = match.group('command')

    ## a better regex can fix this.
        if string.find(name,'"') != -1:
          name = name[1:-1]
        if string.find(icon,'"') != -1:
          icon = icon[1:-1]
        if string.find(command,'"') != -1:
          command = command[1:-1]

        return [name,icon,command]
                
                

    def __parseMenu(self, s):
        name, next_pos = self.__parseWord(s)
        icon, next_pos = self.__parseWord(s, next_pos)
        return [name, icon, None]



def prettyprint(filename):
    m = MenuParser(filename)
    level = 0
    while 1:
        entry = m.getNextEntry()
        if entry is None:
            return
        else:
            if entry[0] == MENUTREE_SEPARATOR:
                print "%sseparator" % ("    "*level)
            elif entry[0] == MENUTREE_SUBMENU_END:
                level = level - 1
                print "%s}" % ("    "*level)
            elif entry[0] == MENUTREE_UNKNOWN:
                pass
            else:
                if entry[2] == "":
                    icon = "- "
                else:
                    icon = '"' + entry[2] + '" '
                if entry[0] == MENUTREE_PROG:
                    print '%sprog "%s" %s%s' % ("    "*level, entry[1], icon, entry[3])
                elif entry[0] == MENUTREE_RESTART:
                    print '%srestart "%s" %s%s' % ("    "*level, entry[1], icon, entry[3])
                elif entry[0] == MENUTREE_SUBMENU:
                    print '%smenu "%s" %s{' % ("    "*level, entry[1], icon)
                    level = level + 1


if __name__ == "__main__":
    if len(sys.argv) <= 1:
        sys.stderr.write("Usage: python %s <icewmmenufile>\n" % sys.argv[0])
    else:
        prettyprint(sys.argv[1])
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.