|
| 1 | +#!/usr/bin/python |
| 2 | +# |
| 3 | +# Copyright (C) 2007 Google Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# This file demonstrates how to use the Google Data API's Python client library |
| 18 | +# to interface with the Codesearch service. |
| 19 | + |
| 20 | + |
| 21 | +__author__ = 'vbarathan@gmail.com (Prakash Barathan)' |
| 22 | + |
| 23 | + |
| 24 | +from gdata import service |
| 25 | +import gdata.codesearch.service |
| 26 | +import gdata |
| 27 | +import atom |
| 28 | +import getopt |
| 29 | +import sys |
| 30 | + |
| 31 | + |
| 32 | +class CodesearchExample: |
| 33 | + |
| 34 | + def __init__(self): |
| 35 | + """Creates a GData service instance to talk to Codesearch service.""" |
| 36 | + self.service = gdata.codesearch.service.CodesearchService( |
| 37 | + source='Codesearch_Python_Sample-1.0') |
| 38 | + |
| 39 | + def PrintCodeSnippets(self, query): |
| 40 | + """Prints the codesearch results for given query.""" |
| 41 | + feed = self.service.GetSnippetsFeed(query) |
| 42 | + print feed.title.text + " Results for '" + query + "'" |
| 43 | + print '============================================' |
| 44 | + for entry in feed.entry: |
| 45 | + print "" + entry.title.text |
| 46 | + for match in entry.match: |
| 47 | + print "\tline#" + match.line_number + ":" + match.text.replace('\n', '') |
| 48 | + print |
| 49 | + |
| 50 | + |
| 51 | +def main(): |
| 52 | + """The main function runs the CodesearchExample application with user |
| 53 | + specified query.""" |
| 54 | + |
| 55 | + # parse command line options |
| 56 | + try: |
| 57 | + opts, args = getopt.getopt(sys.argv[1:], "", ["query="]) |
| 58 | + except getopt.error, msg: |
| 59 | + print ('python CodesearchExample.py --query [query_text]') |
| 60 | + sys.exit(2) |
| 61 | + |
| 62 | + query = '' |
| 63 | + |
| 64 | + # Process options |
| 65 | + for o, a in opts: |
| 66 | + if o == "--query": |
| 67 | + query = a |
| 68 | + |
| 69 | + if query == '': |
| 70 | + print ('python CodesearchExample.py --query [query]') |
| 71 | + sys.exit(2) |
| 72 | + |
| 73 | + sample = CodesearchExample() |
| 74 | + sample.PrintCodeSnippets(query) |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == '__main__': |
| 78 | + main() |
0 commit comments