Changeset 547
- Timestamp:
- 06/10/08 09:14:48 (6 months ago)
- Files:
-
- cly/trunk/cly/builder.py (modified) (12 diffs)
- cly/trunk/cly/console.py (modified) (1 diff)
- cly/trunk/cly/extra.py (modified) (1 diff)
- cly/trunk/cly/interactive.py (modified) (5 diffs)
- cly/trunk/cly/parser.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
cly/trunk/cly/builder.py
r527 r547 32 32 """The base class for all grammar nodes. 33 33 34 Constructor arguments are: 35 36 ``help=''``: string or callable returning a list of (key, help) tuples 34 :param help: 35 string or callable returning a list of (key, help) tuples 37 36 A help string or a callable returning an iterable of (key, help) 38 37 pairs. There is a useful class called Help which can be used for 39 38 this purpose. 40 39 41 ``name=None``: string40 :param name: 42 41 The name of the node. If ommitted the key used by the parent Node 43 42 is used. The node name also defines the node path: … … 50 49 you find yourself using a particular pattern repeatedly. 51 50 52 ``pattern=None``: regular expression string51 :param pattern: 53 52 The regular expression used to match user input. If not provided, 54 53 the node name is used: … … 58 57 True 59 58 60 ``separator=r'\s+|\s*$'``: regular expression string59 :param separator: 61 60 A regular expression used to match the text separating this node 62 61 and the next. 63 62 64 ``group=0``: integer63 :param group: 65 64 Nodes can be grouped together to provide visual cues. Groups are 66 65 ordered ascending numerically. 67 66 68 ``order=0``: integer67 :param order: 69 68 Within a group, nodes are normally ordered alphabetically. This can 70 69 be overridden by setting this to a value other than 0. 71 70 72 ``match_candidates=False``: boolean71 :param match_candidates: 73 72 The candidates() method returns a list of words that match at the 74 73 current token, which are then used for completion, but can also be … … 78 77 files in the current directory). 79 78 80 ``traversals=1``: integer79 :param traversals: 81 80 The number of times this node can match in any parse context. Alias 82 81 nodes allow for multiple traversal. … … 151 150 continue 152 151 if not isinstance(node, Node): 153 raise InvalidAnonymousNode(' Anonymous node is not a Node object')152 raise InvalidAnonymousNode('"%r" must be a Node object' % node) 154 153 # TODO Convert help to name instead of __anonymous_<n> 155 154 node.name = '__anonymous_%i' % self.__anonymous_children … … 458 457 Constructor arguments: 459 458 460 ``alias``: relative or absolute grammar path461 Path to the aliased node. If the alias contains glob characters (``*``462 or ``?``) all matching nodes are aliased.459 :param alias: 460 Relative or absolute path to the aliased node. If the alias contains 461 glob characters (``*`` or ``?``) all matching nodes are aliased. 463 462 464 463 >>> from cly.parser import Parser, Context … … 670 669 """Build a CLY Grammar from XML. 671 670 672 ``xml``: string673 XML source 674 ``extra_nodes``: dictionary671 :param xml: 672 XML source as a string. 673 :param extra_nodes: 675 674 Dictionary of Node subclasses. 676 ``extra_locals``:675 :param extra_locals: 677 676 Valid locals() when evaluating XML grammar node attributes. 678 677 … … 771 770 772 771 773 def xml_lazy_attr_evaluator(attr, positional_args=None):772 def _xml_lazy_attr_evaluator(attr, positional_args=None): 774 773 """Return a callable that lazily evaluates an expression. 775 774 … … 815 814 816 815 817 def xml_boolean_type(value):816 def _xml_boolean_type(value): 818 817 """Converter for boolean attributes.""" 819 818 return str(value).lower() in ('true', '1', 'yes') 820 819 821 820 822 def xml_group_type(value):821 def _xml_group_type(value): 823 822 """Parse a group="n" attribute.""" 824 823 try: … … 907 906 default_attr_type_map = { 908 907 'traversals': int, 909 'group': xml_group_type,908 'group': _xml_group_type, 910 909 'order': int, 911 'match_candidates': xml_boolean_type,912 'with_context': xml_boolean_type,910 'match_candidates': _xml_boolean_type, 911 'with_context': _xml_boolean_type, 913 912 } 914 913 … … 1010 1009 else: 1011 1010 args = [] 1012 v = xml_lazy_attr_evaluator(v, args)1011 v = _xml_lazy_attr_evaluator(v, args) 1013 1012 1014 1013 attributes[k] = v … … 1023 1022 Constructor arguments 1024 1023 1025 ``doc``:1024 :param doc: 1026 1025 An iterable of two element tuples in the form ``(key, help)``. 1027 1026 cly/trunk/cly/console.py
r527 r547 520 520 if width > term_width or expand_to_fit: 521 521 scale = float(term_width) / width 522 widths = [ int(w * scale) for w in widths]522 widths = [max(1, int(w * scale)) for w in widths] 523 523 524 524 row_alt = -1 cly/trunk/cly/extra.py
r492 r547 7 7 # 8 8 9 """Useful functions for use in conjunction withCLY."""9 """Useful functions for CLY.""" 10 10 11 11 cly/trunk/cly/interactive.py
r520 r547 15 15 customisable completion key, interactive help and more. 16 16 17 Press ``?`` at any location tocontextual help.17 Press ``?`` at any time to view contextual help. 18 18 """ 19 19 … … 180 180 Constructor arguments: 181 181 182 ``parser``: ``Parser`` or ``Grammar`` object182 :param parser: 183 183 The parser/grammar to use for interaction. 184 184 185 ``application='cly'``: string185 :param application: 186 186 The application name. Used to construct the history file name and 187 187 prompt, if not provided. 188 188 189 ``prompt=None``: string189 :param prompt: 190 190 The prompt. 191 191 192 ``user_context=None``: `anything`192 :param user_context: 193 193 A user-specified object to pass to the parser. The parser builds each 194 194 parse ``Context`` with this object, which in turn will deliver this 195 195 object on to terminal nodes that have set ``with_context=True``. 196 196 197 ``with_context=False``: `boolean`197 :param with_context: 198 198 Force current parser Context to be passed to all action nodes, unless 199 199 they explicitly set the member variable ``with_context=False``. 200 200 201 ``history_file=None``: `string`201 :param history_file: 202 202 Defaults to ``~/.<application>_history``. 203 203 204 ``history_length=500``: `integer`204 :param history_length: 205 205 Lines of history to keep. 206 207 :param inhibit_exceptions: 208 As for :meth:`loop`. 209 210 :param with_backtrace: 211 As for :meth:`loop`. 206 212 """ 207 213 … … 235 241 executed command. 236 242 237 default_text243 :param default_text: 238 244 Text to insert into the input buffer prior to editing. 239 245 """ … … 261 267 """Repeatedly read and execute commands from the user. 262 268 263 inhibit_exceptions : boolean269 :param inhibit_exceptions: 264 270 Normally, ``interact_loop`` will pass exceptions back to the caller for 265 271 handling. Setting this to ``True`` will cause an error message to 266 272 be printed, but interaction will continue. 267 273 268 with_backtrace : boolean274 :param with_backtrace: 269 275 Whether to print a full backtrace when ``inhibit_exceptions=True``. 270 276 271 callback277 :param callback: 272 278 Called with the Interact object before each line is displayed. 273 279 """ … … 328 334 self.input_driver.prompt = prompt 329 335 330 prompt = property(_get_prompt, _set_prompt )336 prompt = property(_get_prompt, _set_prompt, doc='Prompt. Can be set.') 331 337 332 338 cly/trunk/cly/parser.py
r520 r547 1 1 # -*- coding: utf-8 -*- 2 2 # 3 # Copyright (C) 2006-200 7Alec Thomas <alec@swapoff.org>3 # Copyright (C) 2006-2008 Alec Thomas <alec@swapoff.org> 4 4 # 5 5 # This software is licensed as described in the file COPYING, which … … 7 7 # 8 8 9 """CLY grammar and help parsers, and supportclasses."""9 """CLY parser classes.""" 10 10 11 11 … … 88 88 """Represents the parsing context of a single command. 89 89 90 The context contains all the information the parser needs to maintain 91 state while parsing the input command. 90 A parse context is created automatically when input is parsed. It contains all 91 the information needed to parse input tokens, including the current cursor 92 position in the input stream, the current node in the grammar, variables 93 collected and a history of nodes traversed. 94 95 Basic usage is:: 96 97 parser = Parser(grammar) 98 context = parser.parse('some input text') 99 print context.vars 100 101 If the input is invalid the context will have consumed as much input as 102 possible. The attributes ``parsed`` and ``remaining`` contain how much text has 103 been consumed and remains, respectively. 104 92 105 """ 93 106 def __init__(self, parser, command, user_context=None): … … 229 242 230 243 class Parser(object): 231 """Parse and execute CLY grammars.""" 244 """Parse and execute CLY grammars. 245 246 A grammar is simply a data structure. To actually utilise it one needs to bind 247 it to a ``Parser`` object and parse some input with it. The parser takes care 248 of creating a ``Context`` for each parse run, parsing the input, and executing 249 any callbacks. 250 251 .. note:: 252 *parser* in this context refers to parsing user input, *not* parsing CLY 253 grammars. 254 """ 232 255 def __init__(self, grammar, user_context=None, with_user_context=False, 233 256 with_context=False):
