Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to Python 3 #15

Merged
merged 5 commits into from
Jul 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ Sugar Datastore
Sugar Datastore provides activities with a way to store data and
metadata, and the journal with querying and full text search.

https://www.sugarlabs.org/

https://wiki.sugarlabs.org/

Installing on Debian or Ubuntu
------------------------------
Expand All @@ -23,10 +20,10 @@ desktop](https://github.com/sugarlabs/sugar).
Building
--------

Sugar Artwork follows the [GNU Coding
Sugar Datastore follows the [GNU Coding
Standards](https://www.gnu.org/prep/standards/).

Install all dependencies; Python GI API bindings for GLib, Python
Install all dependencies; Python GI API bindings for GLib, Python 3
bindings for Xapian, Python bindings for D-Bus, and Sugar Toolkit.

Clone the repository, run `autogen.sh`, then `make` and `make
Expand Down
53 changes: 28 additions & 25 deletions bin/copy-from-journal
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
#
# Simple script to export a file from the datastore
# Reinier Heeres, <reinier@heeres.eu>, 2007-12-24
Expand Down Expand Up @@ -26,23 +26,24 @@ def build_argument_parser():
parser = argparse.ArgumentParser(usage=usage)

parser.add_argument('outfile',
help="Name the output file with the explicit name",
metavar="OUTFILE")
help="Name the output file with the explicit name",
metavar="OUTFILE")
parser.add_argument("-o", "--object_id", action="store", dest="object_id",
help="Retrieve object with explicit ID OBJECT_ID",
metavar="OBJECT_ID", default=None)
help="Retrieve object with explicit ID OBJECT_ID",
metavar="OBJECT_ID", default=None)

parser.add_argument("-q", "--query", action="store", dest="query",
help="Full-text-search the metadata for SEARCH_STR",
metavar="SEARCH_STR", default=None)
help="Full-text-search the metadata for SEARCH_STR",
metavar="SEARCH_STR", default=None)

parser.add_argument("-m", "--metadata", action="store_true",
dest="show_meta",
help="Show all non-preview metadata [default: hide]",
default=False)
dest="show_meta",
help="Show all non-preview metadata [default: hide]",
default=False)

return parser


if __name__ == "__main__":

argument_parser = build_argument_parser()
Expand All @@ -69,31 +70,33 @@ if __name__ == "__main__":
# returned to two, as anything more than one means the criteria
# were not limited enough.
objects, count = \
datastore.find(query, limit=RETURN_LIMIT, sorting='-mtime')
datastore.find(query, limit=RETURN_LIMIT, sorting='-mtime')
if count > 1:
print 'WARNING: %d objects found; getting most recent.' % count
for i in xrange(1, RETURN_LIMIT):
print(
'WARNING: %d objects found; getting most recent.' %
count)
for i in range(1, RETURN_LIMIT):
objects[i].destroy()

if count > 0:
dsentry = objects[0]

# If neither an explicit object ID nor a query gave us data, fail.
if dsentry is None:
print 'ERROR: unable to determine journal object to copy.'
print('ERROR: unable to determine journal object to copy.')
argument_parser.print_help()
exit(0)

# Print metadata if that is what the user asked for.
if args.show_meta:
print 'Metadata:'
for key, val in dsentry.metadata.get_dictionary().iteritems():
print('Metadata:')
for key, val in dsentry.metadata.get_dictionary().items():
if key != 'preview':
print '%20s -> %s' % (key, val)
print('%20s -> %s' % (key, val))

# If no file is associated with this object, we can't save it out.
if dsentry.get_file_path() == "":
print 'ERROR: no file associated with object, just metadata.'
print('ERROR: no file associated with object, just metadata.')
dsentry.destroy()
exit(0)

Expand All @@ -105,22 +108,22 @@ if __name__ == "__main__":
if outext == "":
mimetype = dsentry.metadata['mime_type']
outext = sugar3.mime.get_primary_extension(mimetype)
if outext == None:
if outext is None:
outext = "dsobject"
outext = '.' + outext

# Lastly, actually copy the file out of the datastore and onto the
# filesystem.
shutil.copyfile(dsentry.get_file_path(), outroot + outext)
print '%s -> %s' % (dsentry.get_file_path(), outroot + outext)
print('%s -> %s' % (dsentry.get_file_path(), outroot + outext))

# Cleanup.
dsentry.destroy()

except DBusException:
print 'ERROR: Unable to connect to the datastore.\n'\
'Check that you are running in the same environment as the '\
'datastore service.'
print('ERROR: Unable to connect to the datastore.\n'
'Check that you are running in the same environment as the '
'datastore service.')

except Exception, e:
print 'ERROR: %s' % (e)
except Exception as e:
print('ERROR: %s' % (e))
38 changes: 19 additions & 19 deletions bin/copy-to-journal
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
#
# Simple script to import a file to the datastore
# Reinier Heeres, <reinier@heeres.eu>, 2007-12-20
Expand Down Expand Up @@ -27,22 +27,22 @@ def build_option_parser():

parser.add_argument("file", help="File to be copied to journal")
parser.add_argument("-t", "--title", action="store", dest="title",
help="Set the title of the journal entry to TITLE", metavar="TITLE",
default=None)
help="Set the title of the journal entry",
metavar="TITLE", default=None)
parser.add_argument("-d", "--description", action="store",
dest="description", metavar="DESC",
help="Set the description of the journal entry to DESC",
default=None)
dest="description", metavar="DESC",
help="Set the description of the journal entry",
default=None)
parser.add_argument("-m", "--mimetype", action="store",
dest="mimetype", metavar="MIMETYPE",
help="Set the file's MIME-type to MIMETYPE",
default=None)
dest="mimetype", metavar="MIMETYPE",
help="Set the file's MIME-type", default=None)
parser.add_argument("-T", "--tag", action="append", dest="tag_list",
help="Add tag TAG to the journal entry's tags; " \
"this option can be repeated",
metavar="TAG")
help="Add tag TAG to the journal entry's tags; "
"this option can be repeated",
metavar="TAG")
return parser


if __name__ == "__main__":

argument_parser = build_option_parser()
Expand All @@ -54,7 +54,7 @@ if __name__ == "__main__":
fname = args.file
absname = os.path.abspath(fname)
if not os.path.exists(absname):
print 'Error: File does not exist.'
print('Error: File does not exist.')
argument_parser.print_help()
exit(0)

Expand Down Expand Up @@ -87,14 +87,14 @@ if __name__ == "__main__":
entry.metadata['tags'] = tag_string

datastore.write(entry)
print 'Created as %s' % (entry.object_id)
print('Created as %s' % (entry.object_id))

entry.destroy()

except DBusException:
print 'ERROR: Unable to connect to the datastore.\n'\
'Check that you are running in the same environment as the '\
'datastore service.'
print('ERROR: Unable to connect to the datastore.\n'
'Check that you are running in the same environment as the '
'datastore service.')

except Exception, e:
print 'ERROR: %s' % (e)
except Exception as e:
print('ERROR: %s' % (e))
9 changes: 4 additions & 5 deletions bin/datastore-service
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
import sys
import os
import signal
import logging
from gi.repository import GObject
import dbus.service
import dbus.mainloop.glib
import dbus.glib
from carquinyol.datastore import DataStore
from sugar3 import logger

Expand All @@ -33,6 +30,7 @@ def handle_shutdown(signum, frame):
mainloop.quit()
raise SystemExit("Shutting down on signal %s" % signum)


bus.set_exit_on_disconnect(False)
bus.add_signal_receiver(handle_disconnect,
signal_name='Disconnected',
Expand All @@ -47,9 +45,10 @@ def main():
mainloop.run()
except KeyboardInterrupt:
logging.info("DataStore shutdown by user")
except:
except BaseException:
logging.error("Datastore shutdown with error", exc_info=sys.exc_info())


main()

ds.stop()
4 changes: 2 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ AM_MAINTAINER_MODE
AC_DISABLE_STATIC
AC_PROG_LIBTOOL

PYTHON=python2
AM_PATH_PYTHON([2.5])
PYTHON=python3
AM_PATH_PYTHON([3])
AM_CHECK_PYTHON_HEADERS(,[AC_MSG_ERROR(could not find Python headers)])

AC_OUTPUT([
Expand Down
4 changes: 2 additions & 2 deletions m4/python.m4
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ AC_DEFUN([AM_CHECK_PYTHON_HEADERS],
[AC_REQUIRE([AM_PATH_PYTHON])
AC_MSG_CHECKING(for headers required to compile python extensions)
dnl deduce PYTHON_INCLUDES
py_prefix=`$PYTHON -c "import sys; print sys.prefix"`
py_exec_prefix=`$PYTHON -c "import sys; print sys.exec_prefix"`
py_prefix=`$PYTHON -c "import sys; print(sys.prefix)"`
py_exec_prefix=`$PYTHON -c "import sys; print(sys.exec_prefix)"`
PYTHON_INCLUDES="-I${py_prefix}/include/python${PYTHON_VERSION}"
if test "$py_prefix" != "$py_exec_prefix"; then
PYTHON_INCLUDES="$PYTHON_INCLUDES -I${py_exec_prefix}/include/python${PYTHON_VERSION}"
Expand Down
Loading