[Groonga-commit] groonga/groonga-gobject at 4353c24 [master] Add a Python sample

Back to archive index

Kouhei Sutou null+****@clear*****
Mon Jan 27 23:58:45 JST 2014


Kouhei Sutou	2014-01-27 23:58:45 +0900 (Mon, 27 Jan 2014)

  New Revision: 4353c24af33fc1e51d53d4e94c93a7e4c08a1c0a
  https://github.com/groonga/groonga-gobject/commit/4353c24af33fc1e51d53d4e94c93a7e4c08a1c0a

  Message:
    Add a Python sample

  Added files:
    sample/groonga.py
  Copied files:
    sample/Makefile.am
      (from Makefile.am)
  Modified files:
    Makefile.am
    configure.ac

  Modified: Makefile.am (+1 -0)
===================================================================
--- Makefile.am    2014-01-27 23:52:05 +0900 (ae93cd8)
+++ Makefile.am    2014-01-27 23:58:45 +0900 (31679d9)
@@ -22,6 +22,7 @@ SUBDIRS =					\
 	groonga-gobject				\
 	test					\
 	doc					\
+	sample					\
 	packages
 
 pkgconfigdir = $(libdir)/pkgconfig

  Modified: configure.ac (+1 -0)
===================================================================
--- configure.ac    2014-01-27 23:52:05 +0900 (f82dd40)
+++ configure.ac    2014-01-27 23:58:45 +0900 (27b17f9)
@@ -110,6 +110,7 @@ AC_CONFIG_FILES([
   doc/Makefile
   doc/reference/Makefile
   doc/reference/version.xml
+  sample/Makefile
   packages/Makefile
   packages/apt/Makefile
   packages/yum/Makefile

  Copied: sample/Makefile.am (+4 -17) 66%
===================================================================
--- Makefile.am    2014-01-27 23:52:05 +0900 (ae93cd8)
+++ sample/Makefile.am    2014-01-27 23:58:45 +0900 (ba163c2)
@@ -1,4 +1,4 @@
-# Copyright (C) 2013  Kouhei Sutou <kou �� clear-code.com>
+# Copyright (C) 2014  Kouhei Sutou <kou �� clear-code.com>
 #
 # This library is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Lesser General Public License as published by
@@ -13,19 +13,6 @@
 # You should have received a copy of the GNU Lesser General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-ACLOCAL_AMFLAGS = -I m4 ${ACLOCAL_FLAGS}
-
-DISTCHECK_CONFIGURE_FLAGS =			\
-	--enable-introspection
-
-SUBDIRS =					\
-	groonga-gobject				\
-	test					\
-	doc					\
-	packages
-
-pkgconfigdir = $(libdir)/pkgconfig
-pkgconfig_DATA = groonga-gobject.pc
-
-echo-cutter:
-	@echo $(CUTTER)
+sampledir = $(pkgdatadir)/sample
+sample_DATA =					\
+	groonga.py

  Added: sample/groonga.py (+88 -0) 100755
===================================================================
--- /dev/null
+++ sample/groonga.py    2014-01-27 23:58:45 +0900 (8ed2cdb)
@@ -0,0 +1,88 @@
+#!/usr/bin/env python
+#
+# Copyright (C) 2014  Kouhei Sutou <kou �� clear-code.com>
+#
+# This library is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+from __future__ import print_function
+import argparse
+import os
+import sys
+import readline
+import atexit
+
+from gi.repository import Groonga
+import gi
+
+try:
+    import __builtin__
+    if hasattr(__builtin__, "raw_input"):
+        input_function = raw_input
+    else:
+        input_function = input
+except ImportError:
+    input_function = input
+
+parser = argparse.ArgumentParser(description="Groonga command line interface")
+parser.add_argument("database", metavar="DATABASE", type=str, nargs="?",
+                    help="Groonga database path")
+args = parser.parse_args()
+
+def setup_database(context):
+    if args.database:
+        if os.path.exists(args.database):
+            context.open_database(args.database)
+        else:
+            os.makedirs(os.path.dirname(args.database))
+            context.create_database(args.database)
+    else:
+        context.create_database(None)
+
+def setup_history():
+    history_file = os.path.expanduser("~/.groonga")
+    try:
+        readline.read_history_file(history_file)
+    except IOError:
+        pass
+    atexit.register(readline.write_history_file, history_file)
+
+def eval_print(context, line):
+    try:
+        context.send_command(line)
+    except:
+        print(sys.exc_info()[1])
+    result = context.receive_result()
+    if len(result) > 0:
+        print(result)
+
+def run(context):
+    while True:
+        try:
+            line = input_function("> ")
+            eval_print(context, line)
+        except EOFError:
+            break
+
+def main():
+    context = Groonga.Context()
+    try:
+        setup_database(context)
+        setup_history()
+        run(context)
+    finally:
+        del context
+
+Groonga.init()
+main()
+Groonga.fin()
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index