FIQL Parser API¶
© Copyright 2014 Serge Domkowski.
Note
This code includes a few modifications to rules in the FIQL draft.
The rule defined for Comparison has been modified to deal with an
inconsistency in the draft documentation. The change fixes an issue where
the string “==” was NOT a valid Comparison and thus made most of
the examples in the FIQL draft incorrect.
The accepted arg chars have been modified to include “:”. This change
fixes the issue where RFC 3339 compliant DateTime values were not valid
unless the “:” was percent-encoded. This contradicted the FIQL draft
date_str examples. Since “:” is a valid character in an HTTP query
*( pchar / "/" / "?" ), I opted to fix the issue by simply allowing
the “:” in addition to the other arg chars.
Constants¶
Compiled and uncompiled regular expressions representing the various syntax rules used in the FIQL specification.
-
fiql_parser.constants.PCT_ENCODING_REGEX¶ Regular expression representing Percent-Encoding (RFC 3986#section-2.1).
-
fiql_parser.constants.UNRESERVED_REGEX¶ Regular expression repesenting Unreserved Characters (RFC 3986#section-2.3).
-
fiql_parser.constants.FIQL_DELIM_REGEX¶ Regular expression representing the FIQL Delimiter (FIQL Draft#section-3.2).
-
fiql_parser.constants.COMPARISON_REGEX¶ Regular expression representing the FIQL Comparison operator; e.g., “=gt=” (FIQL Draft#section-3.2). This rule includes a modification to the rule in the FIQL draft that correctly allows for a string with no ALPHA characters (Example: “==”).
-
fiql_parser.constants.SELECTOR_REGEX¶ Regular expression representing the FIQL Selector (FIQL Draft#section-3.2). The Selector identifies the portion of an entry that a Constraint applies to.
-
fiql_parser.constants.ARG_CHAR_REGEX¶ Regular expression representing the characters allowed in a FIQL Argument (FIQL Draft#section-3.2). This rule includes a modification to the rule in the FIQL draft that allows for “:” in arguments (Example: “2015-08-27T10:30:00Z”).
-
fiql_parser.constants.ARGUMENT_REGEX¶ Regular expression represeting the FIQL Argument (FIQL Draft#section-3.2). The Argument identifies the value that the Comparison operator should use when validating the Constraint.
-
fiql_parser.constants.CONSTRAINT_REGEX¶ Regular expression representing the FIQL Constraint (FIQL Draft#section-3.2). The Constraint, when processed, yields a
booleanvalue.
-
fiql_parser.constants.CONSTRAINT_COMP¶ Compiled version of
CONSTRAINT_REGEX.
-
fiql_parser.constants.COMPARISON_COMP¶ Compiled version of
CONSTRAINT_REGEXas a full string.
-
fiql_parser.constants.COMPARISON_MAP¶ Mappings for common FIQL comparisons.
Type: dict
Exceptions¶
The code in this package is intended to be used in one of two ways; Building the object representation of a FIQL expression directly, or building the object representation of a FIQL expression by parsing it from a FIQL string.
The Exception classes contained in this module are intended to
provide the flexibility to differentiate between errors resulting from
attempting to construct the expression object and those resulting from
incorrectly formatted FIQL strings.
-
exception
fiql_parser.exceptions.FiqlException[source]¶ Bases:
ExceptionBase Exception class for FIQL errors.
-
exception
fiql_parser.exceptions.FiqlFormatException[source]¶ Bases:
fiql_parser.exceptions.FiqlParserExceptionException class for FIQL string parsing errors.
-
exception
fiql_parser.exceptions.FiqlObjectException[source]¶ Bases:
fiql_parser.exceptions.FiqlExceptionException class for FIQL expression object errors.
-
exception
fiql_parser.exceptions.FiqlParserException[source]¶ Bases:
fiql_parser.exceptions.FiqlExceptionException class for FIQL input parsing errors.
Operator¶
FIQL has two operators. “;” which is the logical AND and “,” for the
logical OR where AND has a logical precedence which is higher than that
of OR.
The operator module includes the code used for managing comparison operator
acceptance, precedence, and representation of the FIQL Operator.
-
fiql_parser.operator.OPERATOR_MAP¶ Mappings of FIQL operators to common terms and their associated precedence.
Type: dict of tuple
-
fiql_parser.operator.REV_OPERATOR_MAP¶ Reverse mappings for common FIQL operators.
Type: dict
Constraint¶
The FIQL Constraint is the building block of the FIQL Expression. A
FIQL Constraint is, on its own, a very simple Expression.
The constraint module includes the code used for managing comparison
acceptance and representation of the FIQL Constraint.
-
fiql_parser.constraint.REV_COMPARISON_MAP¶ Reverse mappings for common FIQL comparisons.
Type: dict
-
class
fiql_parser.constraint.Constraint(selector, comparison=None, argument=None)[source]¶ Bases:
fiql_parser.expression.BaseExpressionThe
Constraintis the smallest logical unit for a FIQLExpression. It itself must evaluate toTrueorFalseand contains no smaller unit which itself can evaluate toTrueorFalse.-
selector¶ Constraint
selector.Type: string
-
comparison¶ Constraint
comparisonoperator.Type: string
-
argument¶ Constraint
argument.Type: string
-
op_and(*elements)[source]¶ Create an
Expressionusing thisConstraintand the specified additionalelementsjoined using an “AND”OperatorParameters: *elements (BaseExpression) – The Expressionand/orConstraintelements which the “AND”Operatorapplies to in addition to thisConstraint.Returns: - Newly created
Expressionincluding this Constraint, the elements passed in, and the “AND”Operator.
Return type: Expression - Newly created
-
op_or(*elements)[source]¶ Create an
Expressionusing thisConstraintand the specified additionalelementsjoined using an “OR”OperatorParameters: *elements (BaseExpression) – The Expressionand/orConstraintelements which the “OR”Operatorapplies to in addition to thisConstraint.Returns: - Newly created
Expressionincluding this Constraint, the elements passed in, and the “OR”Operator.
Return type: Expression - Newly created
-
Expression¶
It would be very difficult to build a FIQL Expressions without taking into
account the Expressions part of it.
The expression module includes the code used for ensuring that any FIQL
Expression created with this package is a valid FIQL Expression.
-
class
fiql_parser.expression.BaseExpression[source]¶ Bases:
objectBoth
ConstraintandExpressionclasses extend theBaseExpressionclass. A FIQLConstraintis a simple FIQLExpression. As such, they share certain attributes.Note
The parent of any child of
BaseExpressionis always anExpression. This is a bit contrary to what might be expected as anExpressionitself is a child class ofBaseExpression.This quark is a side effect of the definition of the FIQL
Constraint. A FIQLConstraintcan not be contained within another FIQLConstraintas a sub-expression. Both a FIQLConstraintand FIQLExpressioncan only be sub-expressions of an actual FIQLExpression.-
parent¶ The
Expressionwhich contains this object.Type: Expression
-
get_parent()[source]¶ Get the parent
Expressionfor this object.Returns: The Expressionwhich contains this object.Return type: Expression Raises: FiqlObjectException– Parent isNone.
-
set_parent(parent)[source]¶ Set parent
Expressionfor this object.Parameters: parent (Expression) – The Expressionwhich contains this object.Raises: FiqlObjectException– Parent must be of typeExpression.
-
-
class
fiql_parser.expression.Expression[source]¶ Bases:
fiql_parser.expression.BaseExpressionThe
Expressionis the largest logical unit of a FIQLExpression. It must, like theConstraintevaluate toTrueorFalse. TheExpressioncan both contain and be contained by anExpression. It, unlike theOperatorandConstraint, MUST contain specific attributes in order to be valid.This class contains the bulk of the logic to ensure that an
Expressiongenerated by this code is a valid FIQLExpression.Note
This
Expressionclass uses a singleOperatorto join multipleConstraints. This format has the advantage of working cleanly with many ORMs and being far more easily converted to the more string friendly format ofConstraint,Operator,Constraint, etc. than the more string friendly format can be converted to the other.-
elements¶ List of
ConstraintandExpressionelements in thisExpression.Type: list
-
add_element(element)[source]¶ Add an element of type
Operator,Constraint, orExpressionto theExpression.Parameters: element – Constraint,Expression, orOperator.Returns: selfReturn type: Expression Raises: FiqlObjectException– Element is not a valid type.
-
add_operator(operator)[source]¶ Add an
Operatorto theExpression.The
Operatormay result in a newExpressionif anOperatoralready exists and is of a different precedence.There are three possibilities when adding an
Operatorto anExpressiondepending on whether anOperatoralready exists:- No
Operatoron the workingExpression; Simply set theOperatorand returnself. Operatoralready exists and is higher in precedence; TheOperatorand lastConstraintbelong in a sub-expression of the workingExpression.Operatoralready exists and is lower in precedence; TheOperatorbelongs to the parent of the workingExpressionwhether one currently exists or not. To remain in the context of the topExpression, this method will return the parent here rather thanself.
Parameters: operator (Operator) – What we are adding. Returns: selfor relatedExpression.Return type: Expression Raises: FiqlObjectExpression– Operator is not a validOperator.- No
-
create_nested_expression()[source]¶ Create a nested
Expression, add it as an element to thisExpression, and return it.Returns: The newly created nested Expression.Return type: Expression
-
has_constraint()[source]¶ Return whether or not the working
Expressionhas anyConstraints.Returns: Number of logical elements within this Expression.Return type: integer
-
op_and(*elements)[source]¶ Update the
Expressionby joining the specified additionalelementsusing an “AND”OperatorParameters: *elements (BaseExpression) – The Expressionand/orConstraintelements which the “AND”Operatorapplies to.Returns: selfor relatedExpression.Return type: Expression
-
op_or(*elements)[source]¶ Update the
Expressionby joining the specified additionalelementsusing an “OR”OperatorParameters: *elements (BaseExpression) – The Expressionand/orConstraintelements which the “OR”Operatorapplies to.Returns: selfor relatedExpression.Return type: Expression
-
Parser¶
The parser module includes the code used to convert a string representing
a FIQL Expression into an object representing the same FIQL
Expression.
The Expression object returned is ideally suited for use in filtering
database queries with many ORMs.
-
fiql_parser.parser.from_python_to_expression(constraints)[source]¶ Construct the
Expressioninstance from a list or tuple (If it contains only one constraint).Parameters: constraints (list or tuple) – Expression as a tuple or list containing the constraints. Returns: The constructed Expression.Return type: Expression Raises: FiqlParserException– Unable to determine the input is expression or constraint.Example
>>> expression = from_python_to_expression( ... [ ... 'OR', ... ('a', '==', 'wee'), ... ['AND', ... ['AND', ('foo', None, None), ('bar', '>', '45')], ... ('key', None, None) ... ] ... ] ... )
-
fiql_parser.parser.iter_parse(fiql_str)[source]¶ Iterate through the FIQL string. Yield a tuple containing the following FIQL components for each iteration:
- preamble: Any operator or opening/closing parenthesis preceding a constraint or at the very end of the FIQL string.
- selector: The selector portion of a FIQL constraint or
Noneif yielding the last portion of the string. - comparison: The comparison portion of a FIQL constraint or
Noneif yielding the last portion of the string. - argument: The argument portion of a FIQL constraint or
Noneif yielding the last portion of the string.
For usage see
parse_str_to_expression().Parameters: fiql_str (string) – The FIQL formatted string we want to parse. Yields: tuple – Preamble, selector, comparison, argument.
-
fiql_parser.parser.parse_str_to_expression(fiql_str)[source]¶ Parse a FIQL formatted string into an
Expression.Parameters: fiql_str (string) – The FIQL formatted string we want to parse. Returns: An Expressionobject representing the parsed FIQL string.Return type: Expression Raises: FiqlFormatException– Unable to parse string due to incorrect formatting.Example
>>> expression = parse_str_to_expression( ... "name==bar,dob=gt=1990-01-01" ... )