pywurfl.ql

Package pywurfl :: Module ql |
[hide private]
[frames] | no frames]

# Module ql

source code

pywurfl Query Language

pywurfl QL is a WURFL query language that looks very similar to SQL.

# Language Definition

# Select statement

## select (device|id|ua)

The select statement consists of the keyword 'select' followed by the select type which can be one of these keywords: 'device', 'ua', 'id'. The select statement is the first statement in all queries.

## device

When 'select' is followed by the keyword 'device', a device object will be returned for each device that matches the 'where' expression (see below).

## ua

When 'select' is followed by the keyword 'ua', an user-agent string will be returned for each device that matches the 'where' expression (see below).

## id

When 'select' is followed by the keyword 'id', a WURFL id string will be returned for each device that matches the 'where' expression (see below).

# Where statement

## where condition

## where condition and/or condition

## where any/all and/or condition

The where statement follows a select statement and can consist of the following elements: 'where condition', 'any statement', 'all statement'.

## Where condition

A where condition consists of a capability name followed by a test operator followed by a value. For example, "ringtone = true".

## Any statement

An any statement consists of the keyword 'any' followed by a parenthesized, comma delimited list of capability names, followed by a test operator and then followed by a value. All capabilities listed in an any statement will be 'ored' together. There must be a minimum of two capabilities listed.

For example: "any(ringtone_mp3, ringtone_wav) = true".

## All statement

An all statement consists of the keyword 'all' followed by a parenthesized, comma delimited list of capability names, followed by a test operator and then followed by a value. All capabilities listed in an all statement will be 'anded' together. There must be a minimum of two capabilities listed.

For example: "all(ringtone_mp3, ringtone_wav) = true".

## Test operators

The following are the test operators that the query language can recognize:

       = != < > >= <=

Comparing strings follow Python's rules.

## Values

Test values can be integers, strings in quotes and the tokens "true" or "false" for boolean tests.

# Binary operators

There are two binary operators defined in the language "and" and "or". They can be used between any where statement tests and follow conventional precedence rules:

     ringtone=true or ringtone_mp3=false and preferred_markup="wml_1_1"
                               -- becomes --
     (ringtone=true or (ringtone_mp3=false and preferred_markup="wml_1_1"))

# Example Queries

select id where ringtone=true

select id where ringtone=false and ringtone_mp3=true

select id where rows > 3

select id where all(ringtone_mp3, ringtone_aac, ringtone_qcelp)=true

select ua where preferred_markup = "wml_1_1"

# EBNF

query ::= select_statement where_statement

select_statement ::= 'select' ('device' | 'id' | 'ua')

where_statement ::= 'where' + where_expression

where_expression ::= where_test (boolop where_test)*

where_test ::= (any_statement | all_statement | expr_test)

any_statement ::= 'any' '(' expr_list ')' operator expr

all_statement ::= 'all' '(' expr_list ')' operator expr

capability ::= alphanums ('_' alphanums)*

expr_test ::= expr operator expr

expr_list ::= expr (',' expr)*

expr ::= types attributes_methods_concat | capability attributes_methods_concat

attributes_methods_concat ::= ('.' method '(' method_args? ')')*

method_args ::= (method_arg (',' method_arg)*)

method_arg ::= (types | expr)

method ::= ('_' alphanums)*

operator ::= ('='|'!='|'<'|'>'|'>='|'<=')

types ::= (<quote> string <quote> | integer | boolean)

boolean ::= ('true' | 'false')

boolop ::= ('and' | 'or')

Author: Armand Lynch <lyncha@users.sourceforge.net>

Copyright: Copyright 2006-2010, Armand Lynch

License: LGPL

Classes | [hide private]
| QueryLanguageError Base exception class for pywurfl.ql
| _Type
| TypeNone
| TypeNum
| TypeStr
| TypeBool
| TypeList

Functions | [hide private]
_toNum(s, l, toks) Convert to pywurfl number type | source code
_toBool(s, l, toks) Convert to pywurfl boolean type | source code
_toStr(s, l, toks) Convert to pywurfl string type | source code
pyparsing.ParserElement |
define_language() Defines the pywurfl query language. | source code
dict |
get_operators() Returns a dictionary of operator mappings for the query language. | source code
function |
expr_test(lexpr, op, rexpr) Returns an exp test function. | source code
_evaluate(devobj, expression) Evaluate an expression with respect to a device object | source code
function |
combine_funcs(funcs) Combines a list of functions with binary operators. | source code
function |
reduce_funcs(func, seq) Reduces a sequence of function objects to one function object by applying a binary function recursively to the sequence: | source code
function |
reduce_statement(exp) Produces a function that represents the "any" or "all" expression passed in by exp: | source code
function |
test_generator(ql_result) Produces a function that encapsulates all the tests from a where statement and takes a Device class or object as a parameter: | source code
function |
QL(devices) Return a function that can run queries against the WURFL. | source code

Variables | [hide private]
| __doc__ = ``...``
| __contributors__ = ``'``Gabriele Fantini <gabriele.fantini@staff.d``...``
| __url__ = ``'``http://celljam.net/``'``
| ops = ``{``'``!=``'``:` <built-in function ne>`,` `'``<``'``:` <built-in function l`...``

Function Details | [hide private]

### define_language() | source code
Defines the pywurfl query language. Returns: pyparsing.ParserElement The definition of the pywurfl query language.

### expr_test(lexpr, op, rexpr) | source code
Returns an exp test function. Parameters: `lexpr` (expr) - An expr `op` (string) - A binary test operator `rexpr` (expr) - An expr Returns: function

### combine_funcs(funcs) | source code
Combines a list of functions with binary operators. Parameters: `funcs` (list) - A python list of function objects with descriptions of binary operators interspersed. For example [func1, 'and', func2, 'or', func3] Returns: function

### reduce_funcs(func, seq) | source code
Reduces a sequence of function objects to one function object by applying a binary function recursively to the sequence:
       In:
           func = and
           seq = [func1, func2, func3, func4]
       Out:
           and(func1, and(func2, and(func3, func4)))
Parameters: `func` (function) - A function that acts as a binary operator. `seq` (list) - An ordered sequence of function objects Returns: function

### reduce_statement(exp) | source code
Produces a function that represents the "any" or "all" expression passed in by exp:
       In:
           any(ringtone_mp3, ringtone_awb) = true
       Out:
           ((ringtone_mp3 = true) or (ringtone_awb = true))
Parameters: `exp` (pyparsing.ParseResults) - The result from parsing an 'any' or 'all' statement. Returns: function

### test_generator(ql_result) | source code
Produces a function that encapsulates all the tests from a where statement and takes a Device class or object as a parameter:
       In (a result object from the following query):
         select id where ringtone=true and any(ringtone_mp3, ringtone_awb)=true

       Out:
         def func(devobj):
             if (devobj.ringtone == True and
                 (devobj.ringtone_mp3 == True or
                  devobj.ringtone_awb == True)):
                 return True
             else:
                 return False
         return func
Parameters: `ql_result` - The result from calling pyparsing.parseString() Returns: function

### QL(devices) | source code
Return a function that can run queries against the WURFL. Parameters: `devices` (pywurfl.Devices) - The device class hierarchy from pywurfl Returns: function

Variables Details | [hide private]

  ### __doc__ Value:
      """
      pywurfl Query Language

      pywurfl QL is a WURFL query language that looks very similar to SQL.

      Language Definition
      ===================

      ...

  ### __contributors__ Value:
      'Gabriele Fantini <gabriele.fantini@staff.dada.net>'

  ### ops Value:
      {'!=': <built-in function ne>,
       '<': <built-in function lt>,
       '<=': <built-in function le>,
       '=': <built-in function eq>,
       '>': <built-in function gt>,
       '>=': <built-in function ge>,
       'and': <function and_ at 0x84fe72c>,
       'or': <function or_ at 0x853780c>}