7. pypsi.cmdline - User input processing¶
Classes used for parsing user input.
7.1. Commands and expressions¶
-
class
pypsi.cmdline.Expression(operand, operator, value)[source]¶ Bases:
objectHolds a string-based expression in the form of
operand operator value. This class makes parsing expressions that may be in a single string or a list of strings. For example, thepypsi.plugins.VarCommandcommand accepts input in the form of:name = value. This class allows for the user to input any of the following lines and the same Expression object would be created, regardless of how the input lines are tokenized:some_var = 2=>['some_var', '=', '2']some_var= 2=>['some_var=', '2']some_var =2=>['some_var', '=2']some_var=2=>['some_var=2']
-
classmethod
parse(args)[source]¶ Create an Expression from a list of strings.
Parameters: args (list) – arguments Returns: a tuple of (remaining, expression), whereremainingis the list of remaining string arguments ofargsafter parsing has completed, andexpressionin the parsedExpression, orNoneif the expression is invalid.
-
class
pypsi.cmdline.CommandInvocation(name, args=None, stdout=None, stderr=None, stdin=None, chain=None)[source]¶ Bases:
objectAn invocation of a command.
-
__call__(shell)[source]¶ Invoke the command by proxying streams, running the command, and cleaning the resetting streams.
Returns: the commnd’s return code.
-
get_input(stream)[source]¶ Open an input stream, if specified.
Returns file: the stream opened for reading if specified, otherwise None.
-
get_output(output)[source]¶ Open an output stream, if specified.
Returns file: the stream opened for writting if specified, otherwise const:None.
-
get_stream(path, mode, safe=False)[source]¶ Open a file path.
Parameters: Raises: IORedirectionError – stream could not be opened
-
setup(shell)[source]¶ Retrieve the Pypsi command to execute and setup the streams for stdout, stderr, and stdin depending on whether I/O redirection is being performed.
Raises: - CommandNotFoundError – command specified does not exist
- IORedirectionError – I/O redirection error occurred
-
should_continue(prev_rc)[source]¶ Returns: whether this invocation is chained and, using the previous invocation’s return code, determine if the next command in the chain should be executed.
-
args= None¶ List of command arguments
-
name= None¶ Command name
-
stderr= None¶ stderr redirection
-
stdin= None¶ stderr redirection
-
stdout= None¶ stdout redirection
-
7.2. Tokens¶
7.2.1. Constants¶
These constants are returned by each token’s add_char() function to determine where tokens begin, end, and what they contain.
-
pypsi.cmdline.TokenContinue= 0¶ The token accepts more characters.
-
pypsi.cmdline.TokenEnd= 1¶ The token does not accept the current chracter.
-
pypsi.cmdline.TokenTerm= 2¶ The token is finished and the current chracter should not be processed again.
7.2.2. Classes¶
-
class
pypsi.cmdline.Token(index, features=None)[source]¶ Bases:
objectBase class for all tokens.
Parameters: index (int) – the starting index of this token
-
class
pypsi.cmdline.WhitespaceToken(index, c=' ', features=None)[source]¶ Bases:
pypsi.cmdline.TokenWhitespace token that can contain any number of whitespace characters.
-
class
pypsi.cmdline.StringToken(index, c, quote=None, features=None)[source]¶ Bases:
pypsi.cmdline.TokenA string token. This token may be bound by matching quotes and/or contain escaped whitespace characters.
Parameters:
-
class
pypsi.cmdline.OperatorToken(index, operator)[source]¶ Bases:
pypsi.cmdline.TokenAn operator token. An operator can consist of one or more repetitions of the same operator character. For example, the string “>>” would be parsed as one OperatorToken, whereas the string “<>” would be parsed as two separate OperatorToken objects.
Parameters: operator (str) – the operator -
add_char(c)[source]¶ Add a character to this token.
Parameters: c (str) – the current character Returns int: TokenEnd or TokenContinue
-
Operators= '<>|&;'¶ Valid operator characters
-
7.3. Statements¶
-
class
pypsi.cmdline.StatementParser(features=None)[source]¶ Bases:
objectParses raw user input into a
Statement.-
build(tokens)[source]¶ Create a
Statementobject from tokenized input and statement context. This method will first remove all remaining escape sequences and thencondense()all the tokens before building the statement.Parameters: tokens (list) – list of Tokenobjects to process as a statementRaises: StatementSyntaxErroron errorReturns: ( Statement) the parsed statement
-
clean_escapes(tokens)[source]¶ Remove all escape sequences.
Parameters: tokens (list) – Tokenobjects to remove escape sequences
-
condense(tokens)[source]¶ Condenses sequential like
Tokenobjects into a singleTokenof the same type. For example, two sequentialStringTokenobjects will be concatenated into a singleStringToken.Parameters: tokens (list) – Tokenobjects to condenseReturns: condensed list of Tokenobjects
-
-
class
pypsi.cmdline.Statement(invokes=None)[source]¶ Bases:
objectA parsed statement containing a list of
CommandInvocationinstances.Parameters: invokes (list[CommandInvocation]) – list of command invocations -
__iter__()[source]¶ Returns: an iterator for the CommandInvocationinstances
-
append(invoke)[source]¶ Append a new command invocation.
Parameters: invoke (CommandInvocation) – parsed command invocation
-
7.3.1. Exceptions¶
-
class
pypsi.cmdline.StatementSyntaxError(message, index)[source]¶ Bases:
SyntaxErrorInvalid statement syntax was entered.
Parameters: