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_test (boolop where_test)*

where_test := (any_test | all_test | capability_test)

any_test := 'any' capability_list operator value

all_test := 'all' capability_list operator value

capability := alphanums ('_' alphanums)*

capability_list := '(' capability, capability (',' capability)* ')'

capability_test := capability operator value

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

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

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

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


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

Copyright: Copyright 2006, Armand Lynch

License: LGPL

Classes [hide private]
  QueryLanguageError
Base exception class for pywurfl.ql
Functions [hide private]
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
capability_test(cap, op, val)
Returns a capability test function.
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 that 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__ = ...
  __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.

capability_test(cap, op, val)

source code 
Returns a capability test function.
Parameters:
  • cap (string) - A WURFL capability
  • op (string) - A binary test operator
  • val (string) - The value to test for
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 that 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
===================

...

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 0x847c95c>,
 'or': <function or_ at 0x847cae4>}