Hello all!
I have defined a for loop as follows which scans through a file made up of two columns, when it finds the keyword DEFINE_MENU
the second column on this line refers to the title for a screen. The next instance of the keyword will define a title for a seperate screen and so on to the nth screen. At the moment the code is capable of defining the first menu title.
Is it possible, when I reach the second instance of the keyword DEFINE_MENU
to repeat the loop for the same line, setting the title_flag = 0
thereby repeating itself, capturing the second menu title?
def getInfo():
title_flag = 0
number = 1
menus = {}
items = {}
title = None
file = open('some_file', 'r')
for line in file:
# Test for comments, if they exist pass and move on
if line[0] == '#':
continue
# Hop over blank lines
if re.search(r'^\s+$', line):
continue
# Find the line where the title is defined
if re.search('DEFINE_MENU', line) and title_flag == 0:
type, name = line.split()
title = name
title_flag = 1
continue
# If DEFINE_MENU is found and flag has been raised, this
# signifies a new menu definition so break.
if re.search('DEFINE_MENU', line) and title_flag == 1:
break
if re.search('PRIV', line):
(type, name, priv_holder, *description) = line.split()
else:
(type, name, *description) = line.split()
# If flag has been raised, the line must follow the definition
# of the menu, it must contains info regarding a menu item.
if title_flag == 1:
description = ' '.join(description)
items[str(number)] = name
number += 1
file.close()
menus[title] = items
return menus
Thank you for your input!
Tom
EDIT: First, apologies for the confusion caused. Perhaps I thought of the problem as being simpler than I thought and gave less information than required. I will delve deeper, the input file i am using is of the form:
# MENU TYPE NAME PRIV? DESCRIPTION
DEFINE_MENU CRAGINS
MENU menu_name1 This takes you to menu 1
MENU menu_name2 This takes you to menu 2
VARIABLE var_name1 Alter variable1
VARIABLE var_name2 PRIV Alter variable1
COMMAND command1 Perform command1
DEFINE_MENU MENU2
MENU menu_name3 This takes you to menu 3
MENU menu_name4 This takes you to menu 4
VARIABLE var_name3 Alter variable3
VARIABLE var_name4 PRIV Alter variable4
COMMAND command3 Perform command3
I had raised the flag as I further manipulate the data between the DEFINE_MENU
calls. I have edited the code to include the full method that I have written.
The outcome is currently a dictionary where the key is the menu title and value is the another dictionary containing the menu items (values following the title) as follows:
{'title1': {'1': 'menu_name1', '3': 'var_name1', '2': 'menu_name2', '5': 'command1', '4': 'var_name2'}}
What I would like to have is a larger dictionary containing the menu titles as keys with the lowest level dictionary as the value. I understand that this is complicated so I'm sorry if it unclear, let me know if more information is required.
Thanks again
Tom