Package pysmug :: Module ismug
[hide private]
[frames] | no frames]

Source Code for Module pysmug.ismug

 1  # Copyright (c) 2008 Brian Zimmer <bzimmer@ziclix.com> 
 2  # 
 3  # Permission is hereby granted, free of charge, to any person obtaining a copy of 
 4  # this software and associated documentation files (the "Software"), to deal in 
 5  # the Software without restriction, including without limitation the rights to 
 6  # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
 7  # of the Software, and to permit persons to whom the Software is furnished to do 
 8  # so, subject to the following conditions: 
 9  # 
10  # The above copyright notice and this permission notice shall be included in all 
11  # copies or substantial portions of the Software. 
12  # 
13  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
14  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
15  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
16  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
17  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
18  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
19  # SOFTWARE. 
20   
21  from pyparsing import Literal, CaselessLiteral, Word, delimitedList, Optional, \ 
22    Combine, Group, alphas, nums, alphanums, ParseException, Forward, oneOf, quotedString, \ 
23    ZeroOrMore, restOfLine, Keyword, upcaseTokens, removeQuotes, Suppress 
24   
25 -def test( str ):
26 print 27 print str,"->" 28 try: 29 tokens = simpleSQL.parseString( str ) 30 print " tokens = ", tokens 31 print " tokens.columns =", tokens.columns 32 print " tokens.tables =", tokens.tables 33 print " tokens.where =", tokens.where 34 except ParseException, err: 35 print " "*err.loc + "^\n" + err.msg 36 print err
37 38 selectStmt = Forward() 39 selectToken = Keyword("select", caseless=True) 40 fromToken = Keyword("from", caseless=True) 41 42 ident = Word( alphas, alphanums + "_$" ).setName("identifier") 43 ident2 = quotedString.setParseAction(removeQuotes) | Word( alphanums, alphanums + "_") 44 columnName = ident2 #delimitedList( ident2, ".", combine=True)#.setParseAction( upcaseTokens ) 45 columnNameList = Group((Literal("images") | Literal("albums")) + Suppress("(") + Group(delimitedList( columnName )) + Suppress(")")) 46 tableName = ident2 #delimitedList( ident2, ".", combine=True)#.setParseAction( upcaseTokens ) 47 tableNameList = tableName #Group( delimitedList( tableName ) ) 48 49 whereExpression = Forward() 50 and_ = Keyword("and", caseless=True) 51 or_ = Keyword("or", caseless=True) 52 in_ = Keyword("in", caseless=True) 53 contains_ = Keyword("contains", caseless=True) 54 55 E = CaselessLiteral("E") 56 binop = oneOf("= != < > >= <= eq ne lt le gt ge", caseless=True) 57 arithSign = Word("+-",exact=1) 58 # realNum = Combine( Optional(arithSign) + ( Word( nums ) + "." + Optional( Word(nums) ) | 59 # ( "." + Word(nums) ) ) + 60 # Optional( E + Optional(arithSign) + Word(nums) ) ) 61 intNum = Combine( Optional(arithSign) + Word( nums ) + Optional( E + Optional("+") + Word(nums) ) ) 62 63 columnRval = intNum | quotedString | columnName # need to add support for alg expressions 64 whereCondition = Group( 65 ( columnName + binop + columnRval ) | 66 ( columnName + contains_ + columnRval ) | 67 ( columnName + in_ + "(" + delimitedList( columnRval ) + ")" ) | 68 ( columnName + in_ + "(" + selectStmt + ")" ) | 69 ( "(" + whereExpression + ")" ) 70 ) 71 whereExpression << whereCondition + ZeroOrMore( ( and_ | or_ ) + whereExpression ) 72 73 # define the grammar 74 selectStmt << ( selectToken + 75 ( '*' | columnNameList ).setResultsName( "columns" ) + 76 fromToken + 77 tableNameList.setResultsName( "tables" ) + 78 Optional( CaselessLiteral("where").suppress() + whereExpression, "" ).setResultsName("where") ) 79 80 simpleSQL = selectStmt 81 82 sqlComment = "--" + restOfLine 83 simpleSQL.ignore( sqlComment ) 84 85 #test('update categories set Name = FooBar where Name = Nature;') 86 #test('update albums set Category = Travel where Category = Vacation;') 87 #test('update images set Caption = "This is the caption" where imageId = 278995595;') 88 test('select images(FileName) from "Israel" where Keywords contains "blah";') 89 test('select images(FileName) from 4687221 where Keywords contains "blah" and ISO = 200;') 90 test('select images(FileName, Size) from 4687221_XoUoj where Keywords contains "blah";') 91 #test('sync /Users/bzimmer/Pictures/Photos/FooBar with 1327371_E8h4u;') 92 #test('select images from 1327371_E8h4u where "2008-04-13 15:10:09" > CaptureDate > "2008-04-12 15:10:09" {exif=True};') 93