Author Topic: Lets create a PCLinuxOS webkit browser (python)  (Read 28267 times)

Offline Neal ManBear

  • Administrator
  • Super Villain
  • *****
  • Posts: 15847
  • LXDE! Coffee, Bacon and Cheesecake!
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #90 on: March 31, 2011, 01:11:14 AM »
What do you think? >> python-mechanize description:
Quote
Stateful programmatic web browsing
Stateful programmatic web browsing, after Andy Lester's Perl module
WWW::Mechanize.

The library is layered: mechanize.Browser (stateful web browser),
mechanize.UserAgent (configurable URL opener), plus urllib2 handlers.

Features include: ftp:, http: and file: URL schemes, browser history,
high-level hyperlink and HTML form support, HTTP cookies, HTTP-EQUIV and
Refresh, Referer [sic] header, robots.txt, redirections, proxies, and
Basic and Digest HTTP authentication.  mechanize's response objects are
(lazily-) .seek()able and still work after .close().

Much of the code originally derived from Perl code by Gisle Aas
(libwww-perl), Johnny Lee (MSIE Cookie support) and last but not least
Andy Lester (WWW::Mechanize).  urllib2 was written by Jeremy Hylton.


Some more info:
http://www.ibm.com/developerworks/linux/library/l-python-mechanize-beautiful-soup/index.html
« Last Edit: March 31, 2011, 01:18:53 AM by Neal »

ongoto

  • Guest
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #91 on: March 31, 2011, 12:56:08 PM »
Moving right along.

Using APP_NAME= instead of name= .  It's a bit safer.

Added zoom-in/zoom-out feature in RtClick context menu to increase font size.  It's now persistent.  
Need an .rc file so we can fix/set certain attributes.

Code: [Select]
#!/usr/bin/env python
#
# [SNIPPET_NAME: Webkit Browser]
# [SNIPPET_CATEGORIES: Webkit, PyGTK]
# [SNIPPET_DESCRIPTION: Create a simple browser using the webkit API]
# [SNIPPET_AUTHOR: Andy Breiner <breinera@gmail.com>]
# [SNIPPET_LICENSE: GPL]
# [SNIPPET_DOCS: http://ardoris.wordpress.com/2009/04/26/a-browser-in-14-lines-using-python-and-webkit, http://www.aclevername.com/articles/python-webgui]

import pygtk
pygtk.require('2.0')
import gtk
import pango
#you need to import webkit and gobject, gobject is needed for threads
import webkit
import gobject

class Browser:
    DEFAULT_SITE = "http://www.pclinuxos.com"
    APP_NAME = "PCLinuxOS Hammer"
    DEFAULT_ZOOM = 1.3

    def delete_event(self, widget, event, data=None):
        return False

    def destroy(self, widget, data=None):
        gtk.main_quit()

    def __init__(self):
        gobject.threads_init()
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_resizable(True)
        self.window.set_default_size(1100, 950)
        self.window.connect("delete_event", self.delete_event)
        self.window.connect("destroy", self.destroy)

        #webkit.WebView allows us to embed a webkit browser
        #it takes care of going backwards/fowards/reloading
        #it even handles flash
        self.web_view = webkit.WebView()

        #add zoom to right click context menu
        settings = self.web_view.get_settings()
        settings.set_property("enable-developer-extras", True)
        # scale other content besides text as well if set True
        self.web_view.set_full_content_zoom(False)
        # make sure the items will be added in the end
        # hence the reason for the connect_after
        self.web_view.connect_after("populate-popup", self.populate_popup)

        self.web_view.open(self.DEFAULT_SITE)
        self.window.set_title('%s' % self.APP_NAME)
        toolbar = gtk.Toolbar()

        #create the back button and connect the action to
        #allow us to go backwards using webkit
        self.back_button = gtk.ToolButton(gtk.STOCK_GO_BACK)
        self.back_button.connect("clicked", self.go_back)

        #same idea for forward button
        self.forward_button = gtk.ToolButton(gtk.STOCK_GO_FORWARD)
        self.forward_button.connect("clicked", self.go_forward)

        #again for refresh
        refresh_button = gtk.ToolButton(gtk.STOCK_REFRESH)
        refresh_button.connect("clicked", self.refresh)

        #again for home
        home_button = gtk.ToolButton(gtk.STOCK_HOME)
        home_button.connect("clicked", self.go_home)

        #entry bar for typing in and display URLs, when they type in a site
        #and hit enter the on_active function is called
        self.url_bar = gtk.Entry()
        self.url_bar.connect("activate", self.on_active)
        entry_tool = gtk.ToolItem()
        entry_tool.set_expand(True)
        entry_tool.add(self.url_bar)
        self.url_bar.show()

        #anytime a site is loaded the update_buttons will be called
        self.web_view.connect("load_committed", self.update_buttons)

        scroll_window = gtk.ScrolledWindow(None, None)
        scroll_window.add(self.web_view)

        #add the buttons to the toolbar
        toolbar.add(self.back_button)
        toolbar.add(self.forward_button)
        toolbar.add(refresh_button)
        toolbar.add(home_button)
        toolbar.add(entry_tool)


        vbox = gtk.VBox(False, 0)
        vbox.pack_start(toolbar, False, True, 0)
        vbox.add(scroll_window)

        self.window.add(vbox)
        self.window.show_all()

    def populate_popup(self, view, menu):
        # zoom feature
        zoom_in = gtk.ImageMenuItem(gtk.STOCK_ZOOM_IN)
        zoom_in.connect('activate', zoom_in_cb, view)
        menu.append(zoom_in)

        zoom_out = gtk.ImageMenuItem(gtk.STOCK_ZOOM_OUT)
        zoom_out.connect('activate', zoom_out_cb, view)
        menu.append(zoom_out)

        menu.show_all()
        return False

    def on_active(self, widget, data=None):
        '''When the user enters an address in the bar, we check to make
           sure they added the http://, if not we add it for them.  Once
           the url is correct, we just ask webkit to open that site.'''
        url = self.url_bar.get_text()
        try:
            url.index("://")
        except:
            url = "http://"+url
        self.url_bar.set_text(url)
        self.web_view.open(url)

    def go_back(self, widget, data=None):
        '''Webkit will remember the links and this will allow us to go
           backwards.'''
        self.web_view.go_back()

    def go_forward(self, widget, data=None):
        '''Webkit will remember the links and this will allow us to go
           forwards.'''
        self.web_view.go_forward()

    def refresh(self, widget, data=None):
        '''Simple makes webkit reload the current back.'''
        self.web_view.reload()

    def go_home(self, widget, data=None):
        '''Returns to default site, Homepage'''
        self.web_view.open(self.DEFAULT_SITE)

    def update_buttons(self, widget, data=None):
        '''Gets the current url entry and puts that into the url bar.
           It then checks to see if we can go back, if we can it makes the
           back button clickable.  Then it does the same for the foward
           button.'''
        self.url_bar.set_text( widget.get_main_frame().get_uri() )
        self.back_button.set_sensitive(self.web_view.can_go_back())
        self.forward_button.set_sensitive(self.web_view.can_go_forward())
        self.web_view.set_zoom_level(self.DEFAULT_ZOOM)

    def main(self):
        gtk.main()

# context menu item callbacks
def zoom_in_cb(menu_item, web_view):
    """Zoom into the page"""
    web_view.zoom_in()
    Browser.DEFAULT_ZOOM = web_view.get_zoom_level()

def zoom_out_cb(menu_item, web_view):
    """Zoom out of the page"""
    web_view.zoom_out()
    Browser.DEFAULT_ZOOM = web_view.get_zoom_level()

if __name__ == "__main__":
    browser = Browser()
    browser.main()


It looks like tabbed browsing will double the size of this thing and require an almost complete rewrite from scratch.
« Last Edit: March 31, 2011, 07:52:54 PM by ongoto »

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6852
  • MLU
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #92 on: March 31, 2011, 01:02:55 PM »
Functions fine here ......  I see the Home button has been moved to the left of the location bar too ....  nice improvements.

Offline Hootiegibbon

  • Hero Member
  • *****
  • Posts: 4151
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #93 on: March 31, 2011, 01:46:58 PM »
Ongoto,

Posting this from my phone.......

We are hoping that we can get 'tabbed' working with this to avoid any rewrites it is an external tabbing app that embeds seperate instances into tabs

I think the ability to hide the toolbar would be good also needto see if it accepts cli argument http addreses (like being fed an url from an email)



Will post again after checking out tabbed

Neal, it launches ok just need to see how to embed it

Jase


I am Hootiegibbon, undisputed champion fo the typo

My .dotfiles

Offline Neal ManBear

  • Administrator
  • Super Villain
  • *****
  • Posts: 15847
  • LXDE! Coffee, Bacon and Cheesecake!
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #94 on: March 31, 2011, 01:47:50 PM »
Terminal feedack:
Quote
[neal@neal ~]$ hammer_ongoto
Traceback (most recent call last):
  File "/usr/bin/hammer_ongoto", line 152, in update_buttons
    self.go_home.set_sensitive(self.web_view.can_default_site())
AttributeError: 'function' object has no attribute 'set_sensitive'
** Message: console message:  @0: Unable to post message to http://googleads.g.doubleclick.net. Recipient has origin http://www.pclinuxos.com.Traceback (most recent call last):
  File "/usr/bin/ongoto", line 152, in update_buttons
    self.go_home.set_sensitive(self.web_view.can_default_site())
AttributeError: 'function' object has no attribute 'set_sensitive'
** Message: console message:  @0: Unable to post message to http://googleads.g.doubleclick.net. Recipient has origin http://www.pclinuxos.com.



Offline Hootiegibbon

  • Hero Member
  • *****
  • Posts: 4151
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #95 on: March 31, 2011, 02:49:58 PM »
« Last Edit: March 31, 2011, 02:52:16 PM by Hootiegibbon »


I am Hootiegibbon, undisputed champion fo the typo

My .dotfiles

Offline scoundrel

  • Administrator
  • Hero Member
  • *****
  • Posts: 4521
  • Philosophy= Bigger Hammer
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #96 on: March 31, 2011, 02:56:05 PM »
 ;D ;D
Please Donate Today..Or I Will Make You Wish You Had

ongoto

  • Guest
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #97 on: March 31, 2011, 03:04:38 PM »
Terminal feedack:
Quote
[neal@neal ~]$ hammer_ongoto
Traceback (most recent call last):
  File "/usr/bin/hammer_ongoto", line 152, in update_buttons
    self.go_home.set_sensitive(self.web_view.can_default_site())
AttributeError: 'function' object has no attribute 'set_sensitive'
** Message: console message:  @0: Unable to post message to http://googleads.g.doubleclick.net. Recipient has origin http://www.pclinuxos.com.Traceback (most recent call last):
  File "/usr/bin/ongoto", line 152, in update_buttons
    self.go_home.set_sensitive(self.web_view.can_default_site())
AttributeError: 'function' object has no attribute 'set_sensitive'
** Message: console message:  @0: Unable to post message to http://googleads.g.doubleclick.net. Recipient has origin http://www.pclinuxos.com.



Neal
You can delete line 152 (       self.go_home.set_sensitive(self.web_view.can_default_site()).  The home_button doesn't belong to webkit which is why the warning messages.

Offline Neal ManBear

  • Administrator
  • Super Villain
  • *****
  • Posts: 15847
  • LXDE! Coffee, Bacon and Cheesecake!
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #98 on: March 31, 2011, 03:08:42 PM »
Jase,
Over and under? How? ??? Can it be right/left?

Ongoto,
Done, thank you. :)

Offline Sproggy

  • Hero Member
  • *****
  • Posts: 1484
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #99 on: March 31, 2011, 03:32:32 PM »
well as i am probably the newest packager in the workshop ... i thought i would set myself the task of creating this initial package for this fantastic app

here ya go

http://dl.dropbox.com/u/5639762/pclinuxos-hammer-0.1-1Sproggy2011.i586.rpm


and the src rpm

http://dl.dropbox.com/u/5639762/pclinuxos-hammer-0.1-1Sproggy2011.src.rpm

works great lol

Kori ;D ;D ;D

Offline Hootiegibbon

  • Hero Member
  • *****
  • Posts: 4151
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #100 on: March 31, 2011, 03:42:13 PM »
Jase,
Over and under? How? ??? Can it be right/left?

Ongoto,
Done, thank you. :)


Neal,

Sorry to confuse - just showing how WMii handles 3hammers (all single instances 2x web pages and one using the embeded irc client)  with tabbed I think it needs to be specified at build - I am searching for something more tangible by way of a how to, but the folks over a suckless kinda expect this knowledge to be there alrady! lol

Sproggy,

Thanks and - how good would this be in ScrotWM (after we have this nailed i wanna sort out that xterm issue)
btw I propose that once this is done that you bee the Hammer janitor and maintain the package LOL (if Neal is ok with it)

Jase
« Last Edit: March 31, 2011, 03:43:48 PM by Hootiegibbon »


I am Hootiegibbon, undisputed champion fo the typo

My .dotfiles

Offline Neal ManBear

  • Administrator
  • Super Villain
  • *****
  • Posts: 15847
  • LXDE! Coffee, Bacon and Cheesecake!
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #101 on: March 31, 2011, 03:57:03 PM »
Okay by me, Jase.

Sprogster,
Look in dropbox at ~/SRPM/testing for the tabbed srpm. Try combining tabbed with hammer. This is to see if compiling/installing tabbed with hammer will make them work together.

Offline Sproggy

  • Hero Member
  • *****
  • Posts: 1484
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #102 on: March 31, 2011, 04:03:30 PM »
Okay by me, Jase.

Sprogster,
Look in dropbox at ~/SRPM/testing for the tabbed srpm. Try combining tabbed with hammer. This is to see if compiling/installing tabbed with hammer will make them work together.


Im gonna need some superior help here lolol

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6852
  • MLU
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #103 on: March 31, 2011, 04:06:49 PM »
well as i am probably the newest packager in the workshop ... i thought i would set myself the task of creating this initial package for this fantastic app

here ya go

http://dl.dropbox.com/u/5639762/pclinuxos-hammer-0.1-1Sproggy2011.i586.rpm


and the src rpm

http://dl.dropbox.com/u/5639762/pclinuxos-hammer-0.1-1Sproggy2011.src.rpm

works great lol

Kori ;D ;D ;D


It won't install here .....

Quote
pclinuxos-hammer:
 Depends: glibc (>= 2.12.1)
 Depends: libstdc++6 (>= 4.5.2)

Offline Sproggy

  • Hero Member
  • *****
  • Posts: 1484
Re: Lets create a PCLinuxOS webkit browser (python)
« Reply #104 on: March 31, 2011, 04:12:26 PM »

It won't install here .....

Quote
pclinuxos-hammer:
 Depends: glibc (>= 2.12.1)
 Depends: libstdc++6 (>= 4.5.2)

i am just following the bosses request to add those lines to all new packages ... you will have to install them manually ;D ;D ;D