| 1 | """ |
|---|
| 2 | Global and local dictionaries, + initialization/utility functions. |
|---|
| 3 | """ |
|---|
| 4 | |
|---|
| 5 | global_dict = {} |
|---|
| 6 | |
|---|
| 7 | def init_global_dict(): |
|---|
| 8 | """ |
|---|
| 9 | Initialize global dictionary with twill commands. |
|---|
| 10 | |
|---|
| 11 | This must be done after all the other modules are loaded, so that all |
|---|
| 12 | of the commands are already defined. |
|---|
| 13 | """ |
|---|
| 14 | exec "from twill.commands import *" in global_dict |
|---|
| 15 | import twill.commands |
|---|
| 16 | command_list = twill.commands.__all__ |
|---|
| 17 | |
|---|
| 18 | import twill.parse |
|---|
| 19 | twill.parse.command_list.extend(command_list) |
|---|
| 20 | |
|---|
| 21 | # local dictionaries. |
|---|
| 22 | _local_dict_stack = [] |
|---|
| 23 | |
|---|
| 24 | ### |
|---|
| 25 | |
|---|
| 26 | # local dictionary management functions. |
|---|
| 27 | |
|---|
| 28 | def new_local_dict(): |
|---|
| 29 | """ |
|---|
| 30 | Initialize a new local dictionary & push it onto the stack. |
|---|
| 31 | """ |
|---|
| 32 | d = {} |
|---|
| 33 | _local_dict_stack.append(d) |
|---|
| 34 | |
|---|
| 35 | return d |
|---|
| 36 | |
|---|
| 37 | def pop_local_dict(): |
|---|
| 38 | """ |
|---|
| 39 | Get rid of the current local dictionary. |
|---|
| 40 | """ |
|---|
| 41 | _local_dict_stack.pop() |
|---|
| 42 | |
|---|
| 43 | ### |
|---|
| 44 | |
|---|
| 45 | def get_twill_glocals(): |
|---|
| 46 | """ |
|---|
| 47 | Return global dict & current local dictionary. |
|---|
| 48 | """ |
|---|
| 49 | global global_dict, _local_dict_stack |
|---|
| 50 | assert global_dict is not None, "must initialize global namespace first!" |
|---|
| 51 | |
|---|
| 52 | if len(_local_dict_stack) == 0: |
|---|
| 53 | new_local_dict() |
|---|
| 54 | |
|---|
| 55 | return global_dict, _local_dict_stack[-1] |
|---|
| 56 | |
|---|
| 57 | ### |
|---|