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 Python3, Gtk3 and GLib #5

Closed
wants to merge 4 commits into from
Closed
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
4 changes: 2 additions & 2 deletions activity/activity.info
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[Activity]
name = Frotz
activity_version = 3
bundle_id = vu.lux.olpc.Frotz
exec = sugar-activity frotz.FrotzActivity
bundle_id = org.sugarlabs.Frotz
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any particular reason why you changed the bundle id?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was giving bundle_id not found error when I tested with python2. But after making changes the error has gone. The activity was not starting before with python2. You can try this one on python2 by changing. Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reasons not to change the bundle_id include;

  • when an upgrade of an activity occurs on a system, Journal objects from the old version can continue to be used on the new version; otherwise those Journal objects cannot be opened,
  • when an activity is shared between a system using the old bundle_id and another system using the new bundle_id, the sharing will work; otherwise the shared instance will not be displayed in the Neighbourhood View,
  • when an upgrade happens while Sugar is running, Sugar will represent only one activity by this name on the menu; otherwise Sugar may represent both the old and new activities on the menu at once if there is a copy of the activity in ~/Activities as well as /usr/share/sugar/activities,
  • locating source code repositories is easy because we can search for the name; otherwise it becomes harder.

I don't think a bundle_id not found error is a correct reason to change the bundle_id. Next time that happens, try to diagnose why it happened rather than jumping to conclusions about the cause. 😁

exec = sugar-activity3 frotz.FrotzActivity
icon = activity-frotz
mime_types = application/x-zmachine
license = GPLv3+
Expand Down
71 changes: 37 additions & 34 deletions frotz.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,28 @@
#
# You should have received a copy of the GNU General Public License
# along with Frotz.activity. If not, see <http://www.gnu.org/licenses/>.

import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Vte', '2.91')
import os
import sys

import logging
from gettext import gettext as _

import gtk
import gobject
from gi.repository import Gtk, Gdk,GLib
import dbus

from sugar.activity import activity
from sugar.activity import activityfactory
from sugar import env
from sugar.graphics.toolbutton import ToolButton
from sugar.graphics.palette import Palette
import ConfigParser
from sugar3.activity import activity
from sugar3.graphics.toolbarbox import ToolbarBox
from sugar3.activity import activityfactory
from sugar3 import env
from sugar3.graphics.toolbutton import ToolButton
from sugar3.graphics.palette import Palette
import configparser
import os.path
import pango
from gi.repository import Pango
from gi.repository import Vte

import platform, sys
from ctypes import cdll
Expand All @@ -48,13 +51,13 @@
pass # FIXME
else:
if platform.architecture()[0] == '64bit':
vte_path = "x86-64"
vte_path = "x86_64"
else:
vte_path = "x86"
vte = cdll.LoadLibrary("lib/%s/libvte.so.9" % vte_path)
sys.path.append("lib/%s" % vte_path)
vte = cdll.LoadLibrary("/usr/lib/"+vte_path+"-linux-gnu/libvte-2.91.so.0")
sys.path.append("/usr/lib/"+vte_path+"-linux-gnu" )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason why you added this to the PATH?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The activity was giving error before given in #4 in python2. The error is gone after changing . You can try this one also. by running on sugar-live-build. Thanks

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The activity was written to work with early versions of Fedora that did not have up to date VTE package. Newer VTE was in the lib directory, for each architecture supported. It also happened in the Lemonade activity. This method was acceptable at the time but was not compliant with the license of VTE, and we no longer need to do it because VTE is sufficiently recent on any system that has Python 3 GTK support. So as you port to GTK 3, you should have assessed if the embedded copy of VTE was needed, and (as it is not) removed it and the code that refers to it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And of course remove the lib directory.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks I didn't know that. I thought the error was just related to not loading the required lib. So I just made necessary changes for working. I will take care about it further.



import vte

class FrotzActivity(activity.Activity):

Expand All @@ -66,7 +69,7 @@ def __init__(self, handle):
self.set_title(_('Frotz'))
self.connect('key-press-event', self.__key_press_cb)

toolbox = activity.ActivityToolbox(self)
toolbox = ToolbarBox()

self._edit_toolbar = activity.EditToolbar()
toolbox.add_toolbar(_('Edit'), self._edit_toolbar)
Expand All @@ -91,13 +94,13 @@ def __init__(self, handle):
self.set_toolbox(toolbox)
toolbox.show()

box = gtk.HBox(False, 4)
box = Gtk.HBox(False, 4)

self._vte = VTE()
self._vte.show()
self._vte.connect("child-exited", self._quit_cb)

scrollbar = gtk.VScrollbar(self._vte.get_adjustment())
scrollbar = Gtk.VScrollbar(self._vte.get_adjustment())
scrollbar.show()

box.pack_start(self._vte)
Expand All @@ -112,10 +115,10 @@ def __init__(self, handle):
default_game_file = os.path.join(activity.get_bundle_path(), "Advent.z5")
# when we return to the idle state, launch the default game
# if read_file is called, that will override this
gobject.idle_add(self.start_game, default_game_file)
GLib.idle_add(self.start_game, default_game_file)

def _quit_cb(self, foo=None):
print "Quitting..."
print("Quitting...")
sys.exit(0)

def start_game(self, game_file):
Expand Down Expand Up @@ -143,19 +146,19 @@ def read_file(self, file_path):

def open_url(self, url):
"""Ask the journal to open an URL for us."""
from sugar import profile
from sugar3 import profile
from shutil import rmtree
from sugar.datastore import datastore
from sugar.activity.activity import show_object_in_journal
from sugar3.datastore import datastore
from sugar3.activity.activity import show_object_in_journal
from tempfile import mkdtemp
tmpfolder = mkdtemp('.tmp', 'url', os.path.join(self.get_activity_root(), 'instance'))
tmpfilepath = os.path.join(tmpfolder, 'url')
try:
tmpfile = open(tmpfilepath, 'w')
tmpfile.write(url)
tmpfile.close()
os.chmod(tmpfolder, 0755)
os.chmod(tmpfilepath, 0755)
os.chmod(tmpfolder, 0o755)
os.chmod(tmpfilepath, 0o755)
jobject = datastore.create()
metadata = {
'title': url,
Expand All @@ -165,7 +168,7 @@ def open_url(self, url):
'icon-color': profile.get_color().to_string(),
'mime_type': 'text/uri-list',
}
for k, v in metadata.items():
for k, v in list(metadata.items()):
jobject.metadata[k] = v # the dict.update method is missing =(
jobject.file_path = tmpfilepath
datastore.write(jobject)
Expand All @@ -187,28 +190,28 @@ def _paste_cb(self, button):
self._vte.paste_clipboard()

def __key_press_cb(self, window, event):
if event.state & gtk.gdk.CONTROL_MASK and event.state & gtk.gdk.SHIFT_MASK:
if event.state & Gtk.gdk.CONTROL_MASK and event.state & Gtk.gdk.SHIFT_MASK:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like an incomplete port, look for the correct method and make the necessary changes.
If you're not sure what the correct methods are you can check here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks I will take a look at these ones.


if gtk.gdk.keyval_name(event.keyval) == "C":
if Gtk.gdk.keyval_name(event.keyval) == "C":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This too.

if self._vte.get_has_selection():
self._vte.copy_clipboard()
return True
elif gtk.gdk.keyval_name(event.keyval) == "V":
elif Gtk.gdk.keyval_name(event.keyval) == "V":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this.

self._vte.paste_clipboard()
return True

return False

class VTE(vte.Terminal):
class VTE(Vte.Terminal):
def __init__(self):
vte.Terminal.__init__(self)
Vte.Terminal.__init__(self)
self._configure_vte()

#os.chdir(os.environ["HOME"])
self.fork_command()

def _configure_vte(self):
conf = ConfigParser.ConfigParser()
conf = configparser.ConfigParser()
conf_file = os.path.join(env.get_profile_path(), 'terminalrc')

if os.path.isfile(conf_file):
Expand All @@ -223,7 +226,7 @@ def _configure_vte(self):
else:
font = 'Monospace 8'
conf.set('terminal', 'font', font)
self.set_font(pango.FontDescription(font))
self.set_font(Pango.FontDescription(font))

if conf.has_option('terminal', 'fg_color'):
fg_color = conf.get('terminal', 'fg_color')
Expand All @@ -235,8 +238,8 @@ def _configure_vte(self):
else:
bg_color = '#FFFFFF'
conf.set('terminal', 'bg_color', bg_color)
self.set_colors(gtk.gdk.color_parse (fg_color),
gtk.gdk.color_parse (bg_color),
self.set_colors(Gtk.gdk.color_parse (fg_color),
Gtk.gdk.color_parse (bg_color),
Comment on lines +241 to +242
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this too.

[])

if conf.has_option('terminal', 'cursor_blink'):
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
from sugar.activity import bundlebuilder
bundlebuilder.start('Frotz')
from sugar3.activity import bundlebuilder
bundlebuilder.start()