pygtk + webkit : AttributeError: Resource instance has no attribute '_uri' - python

I am writing a script (headless browser) that loads a website and logs the time the resources of the website need to be loaded.
#!/usr/bin/env python
import gtk
import time
import webkit
import gobject
class Resource():
__signals = [
'content-length-received',
'load-failed',
'load-finished',
'response-received'
]
_start = time.time()
_durations = {}
def __init__(self, webkit_resource):
self._webkit_resource = webkit_resource
# literally copy the uri
self._uri = "".join([c for c in self._webkit_resource.get_uri()])
self._connect_callbacks()
#property
def uri(self):
return self._uri
def _connect_callbacks(self):
for s_id in self.__signals:
self._webkit_resource.connect(s_id, self._on_callback, s_id)
def _on_callback(self, *args):
self._durations[args[-1]] = time.time() - self._start
print "==>", self._uri, "==>", args[-1], "==>", self._durations[args[-1]]
class Browser(webkit.WebView):
def __init__(self):
webkit.WebView.__init__(self)
gobject.threads_init()
self.connect('resource-request-starting',
self._on_resource_request_starting)
def _on_resource_request_starting(self, view, frame, resource, request, response):
c_resource = Resource(resource)
if __name__ == "__main__":
browser = Browser()
browser.open("http://amazon.com/")
gtk.main()
The code runs fine but then suddenly I get the following weird Tracebacks.
==> http://amazon.com/ ==> content-length-received ==> 0.98512005806
==> http://www.amazon.com/ ==> content-length-received ==> 0.985180854797
==> http://z-ecx.images-amazon.com/images/G/01/browser-scripts/us-site-wide-css-beacon/site-wide-5659237078._V1_.css ==> content-length-received ==> 0.986252069473
==> http://z-ecx.images-amazon.com/images/G/01/browser-scripts/us-site-wide-css-beacon/site-wide-5659237078._V1_.css ==> content-length-received ==> 0.987208843231
==> http://z-ecx.images-amazon.com/images/G/01/browser-scripts/us-site-wide-css-beacon/site-wide-5659237078._V1_.css ==> load-finished ==> 0.999194860458
==>
Traceback (most recent call last):
File "test.py", line 37, in _on_callback
print "==>", self._uri, "==>", args[-1], "==>", self._durations[args[-1]]
AttributeError: Resource instance has no attribute '_uri'
==>
Traceback (most recent call last):
File "test.py", line 37, in _on_callback
print "==>", self._uri, "==>", args[-1], "==>", self._durations[args[-1]]
AttributeError: Resource instance has no attribute '_uri'
==>
I did set self._uri. I actually copy each char one by one.
Is it being deleted somewhere?

Related

Object not callable in python class in flask application

This is my first flask application and i tried my best to get it running. Nevertheless i stuck into one error.
I tried to create a python flask app but stuck into an error.
here is my code
flask.py
from test_displayclass import Bartender
class MyFlask:
bartender = Bartender()
def __init__(self):
#self.bartender = Bartender()
self.bartender.test()
from flask import Flask
app = Flask(__name__)
my_flask = MyFlask()
#app.route("/Test")
def Test():
return my_flask.test.APIfunction
if __name__ == "__main__":
app.run(debug=True,port=9999)
test_displayclass.py
import adafruit_ssd1306
import busio
from board import SCL, SDA
from PIL import Image, ImageDraw, ImageFont
class Display():
def __init__(self):
i2c = busio.I2C(SCL, SDA)
self.oled = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3C)
self.oled.fill(0)
self.oled.show()
def drawImage(self, image):
self.oled(image)
self.oled.show()
class Bartender():
def __init__(self):
self.oled = Display()
def test(self):
image = Image.new("1", (20, 20))
draw = ImageDraw.Draw(image)
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 25)
self.len = len("e")
draw.text(
(0, 40 - 2 // 2),
"e",
font=font,
fill=255,
)
Error is:
Traceback (most recent call last):
File "/home/pi/Smart-Bartender/bartender_flask_new_test.py", line 13, in <module>
my_flask = MyFlask()
File "/home/pi/Smart-Bartender/bartender_flask_new_test.py", line 8, in __init__
self.bartender.test()
File "/home/pi/Smart-Bartender/test_displayclass.py", line 61, in test
self.oled.drawImage(image)
File "/home/pi/Smart-Bartender/test_displayclass.py", line 33, in drawImage
self.oled(image)
TypeError: 'SSD1306_I2C' object is not callable
Can you advice me how to do it the correct waY?
Need of taken the function self.oled.image(image)

Run all unittests from seperate py file

I have multiple py files with unit tests specified for specific module.
I want to import these unittests to seperate py file and run them from there, but I have trouble getting it working.
example of one of the unittest:
from webdriver_manager.chrome import ChromeDriverManager
from to_import import acceptConsent, URL_poznavacky, URL_poznavacky_vikendy, URL_poznavacky_rodiny, URL_pobocky
import time
from selenium import webdriver
import unittest
class TestPobocky_D(unittest.TestCase):
def setup_method(self, method):
self.driver = webdriver.Chrome(ChromeDriverManager().install())
self.vars = {}
def teardown_method(self, method):
self.driver.quit()
def test_pobocky_D(self):
self.driver.get(URL_pobocky)
acceptConsent(self.driver)
self.driver.maximize_window()
time.sleep(2)
mapa = self.driver.find_element_by_xpath("//*[#class='leaflet-pane leaflet-tile-pane']") ## jen jeden element, no need to call find_elementS
mapaDisplayed = mapa.is_displayed()
assert mapaDisplayed == True
mapaKolecka = self.driver.find_elements_by_xpath("//*[#class='leaflet-marker-icon marker-cluster marker-cluster-medium leaflet-zoom-animated leaflet-interactive']")
y=0
for _ in mapaKolecka:
mapaKoleckaDisplayed = mapaKolecka[y].is_displayed()
y=y+1
print("mapa kolecka")
assert mapaKoleckaDisplayed == True
pobockaBoxiky = self.driver.find_elements_by_xpath("//*[#class='f_branch-header f_anchor']")
x=0
for _ in pobockaBoxiky:
pobockaBoxikyDisplay = pobockaBoxiky[x].is_displayed()
print("boxiky")
assert pobockaBoxikyDisplay == True
x=x+1
basicInfo = self.driver.find_elements_by_xpath("//*[#class='f_branch-basicInfo']")
a=0
for _ in basicInfo:
basicInfoDisplay = basicInfo[a].is_displayed()
print("basic info ")
assert basicInfoDisplay == True
a=a+1
I want to import the whole class TestPobocky_D to new file and run it in seperate py file. I tried following:
import unittest
from pobocky import TestPobocky_D
TestPobocky_D(unittest.TestCase)
that just gives me this error
"Traceback (most recent call last):
File "C:/Users/KDK/Desktop/Automation_Local_Deploy_PyCharm/starter_local.py", line 4, in <module>
TestPobocky_D(unittest.TestCase)
File "C:\Users\KDK\anaconda3\lib\unittest\case.py", line 433, in __init__
testMethod = getattr(self, methodName)
TypeError: getattr(): attribute name must be string"
Anyone who can help me with this please ? Or point me to the right direction how to go about this.
Thanks in advance for every response
Can you please try as below?
starttest.py
import unittest
from pobocky import TestPobocky_D
if __name__ == '__main__':
unittest.main(argv=[''],verbosity=2, exit=False)
Run the command "python -m unittest starttest.py"

D-Bus method GetAll not found?

As far as I'm aware, org.freedesktop.DBus.Properties.GetAll should work to get properties of an interface. For some reason, this doesn't seem to work on org.freedesktop.NetworkManager.Connections.Active. Any suggestions on how to make this code work?
The code:
import dbus
from gi.repository import GObject
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)
system_bus = dbus.SystemBus()
loop = GObject.MainLoop()
def handle_nm_change(o):
if 'ActiveConnections' in o:
# This is a connection change, let's see if it's in our SSID list
# First, get the ActiveConnection that changed:
for c in o['ActiveConnections']:
# Get the active connection
dev = system_bus.get_object('org.freedesktop.NetworkManager', c)
# Attempt to get the properties of the connection.
devprops_iface = dbus.Interface(dev, dbus_interface='org.freedesktop.DBus.Properties')
devprops = devprops_iface.GetAll('org.freedesktop.NetworkManager.Connection.Active')
# if not devprops['Default']:
# ii = input('Device not default: ' + c)
# if ii == 'n':
# exit(0)
appath = devprops['SpecificObject']
if appath.startswith('/org/freedesktop/NetworkManager/AccessPoint'):
ap = system_bus.get_object('org.freedesktop.NetworkManager', appath)
ssid = ap.Get('org.freedesktop.NetworkManager.AccessPoint', 'Ssid',
dbus_interface=dbus.PROPERTIES_IFACE
)
print(ssid)
if __name__ == '__main__':
system_bus.add_signal_receiver(handle_nm_change,
'PropertiesChanged',
'org.freedesktop.NetworkManager'
)
loop.run()
The error:
ERROR:dbus.connection:Exception in handler for D-Bus signal:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/dbus/connection.py", line 230, in maybe_handle_message
self._handler(*args, **kwargs)
File "so-mockup.py", line 18, in handle_nm_change
devprops = devprops_iface.GetAll('org.freedesktop.NetworkManager.Connection.Active')
File "/usr/lib/python3/dist-packages/dbus/proxies.py", line 70, in __call__
return self._proxy_method(*args, **keywords)
File "/usr/lib/python3/dist-packages/dbus/proxies.py", line 145, in __call__
**keywords)
File "/usr/lib/python3/dist-packages/dbus/connection.py", line 651, in call_blocking
message, timeout)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.UnknownMethod: Method "GetAll" with signature "s" on interface "org.freedesktop.DBus.Properties" doesn't exist
Signal 'PropertiesChanged' is sent also when a connection is deactivated. Then the object path for the "deactivated" connection does not exist anymore. That's why you are receiving the UnknownMethod exception.
Before getting properties of the ActiveConnection make sure it still exists.
Try the changes below:
# Get ActiveConnection upon receiving a PropertiesChanged signal
nm = system_bus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager')
nm_iface = dbus.Interface(nm, dbus_interface='org.freedesktop.DBus.Properties')
nms = nm_iface.Get('org.freedesktop.NetworkManager', 'ActiveConnections')
# Check current active connections
for ac in nms:
print("ActiveConnection: "+ac)
# This is a connection change, let's see if it's in our SSID list
# First, get the ActiveConnection that changed:
for c in o['ActiveConnections']:
# Do whatever if c is in "ActiveConnections"
if c in nms:

itertools aparent bug with pygraphviz and return chain to flask

I am finding a possible bug with chaining several methods in a return statement in flask. I wanted to see if anyone had any suggestions on how to get around this problem.
Here is my code:
#!/usr/bin/env python
import flask
import time
from itertools import chain
from pygraphviz import AGraph
class TestClass(object):
def __init__(self):
G = ''
def worker(self):
a='1234'
b=a + '45\n'
yield b
time.sleep(3)
yield a
def worker2(self):
time.sleep(3)
c = '\n9876'
yield c
def graph(self):
G = AGraph(overlap='false')
tc = TestClass()
app = flask.Flask(__name__)
#app.route('/')
def test_method_get_stuff():
return flask.render_template('index.html')
#app.route('/', methods=['POST'])
def test_method_post_stuff():
def test_method_sub_function():
return chain(tc.worker(), tc.worker2(),tc.graph())
return flask.Response(test_method_sub_function(),mimetype= 'text/plain')
app.run(debug=True)
Once a post is requested the tc.worker() and tc.worker2() generators return correctly but once it tries to perform tc.graph() it errors out with the below error.
* Running on http://127.0.0.1:5000/
* Restarting with reloader
127.0.0.1 - - [17/Aug/2014 18:23:17] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [17/Aug/2014 18:23:18] "POST / HTTP/1.1" 200 -
Debugging middleware caught exception in streamed response at a point where response headers were already sent.
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/werkzeug/wsgi.py", line 691, in __next__
return self._next()
File "/usr/lib/python2.7/site-packages/werkzeug/wrappers.py", line 81, in _iter_encoded
for item in iterable:
TypeError: 'NoneType' object is not iterable

Python Attribute Error: object has no attribute 'self'

I have a problem in class inheritance in Python; maybe it's not related to inheritance, but I have no other idea. I'm working with selenium web-driver. Here's the code I use:
from selenium import webdriver
class UIInterface(object):
def __init__(self):
self.driver = webdriver.Ie()
self.driver.get('URL')
def parentMethod(self, itemClassName = ''):
allElements = self.getAllElements()
return [el for el in allElements if el.get_attribute('type') == 'checkbox']
def getAllElements(self):
return self.driver.find_elements_by_tag_name('input')
class InterfaceChild(UIInterface):
def __init__(self):
super(InterfaceChild, self).__init__()
def childMethod(self):
returnedList = self.parentMethod(itemClassName = 'SomeClassName')
for item in returnedList:
print item.get_attribute('innerHTML')
This code gives me an error for line returnedList = self.parentMethod(itemClassName = 'SomeClassName'); the error description is this:
(<type 'exceptions.AttributeError'>, AttributeError("'InterfaceChild' object has no attribute 'self'",), <traceback object at 0x000000000418E5C8>)
I thought it might be related to inheritance, so I tried to put parentMethod and getAllElements in the class InterfaceChild; same exception raised. Any idea about this??
EDIT 1:
This is the main method for making instance of classes:
if __name__ == '__main__':
ieInterface = InterfaceChild()
ieInterface.childMethod()
This is the complete stack-trace:
Traceback (most recent call last):
File "C:\Program Files\Eclipse\eclipse-jee-helios-win32\eclipse-jee-helios-win32\plugins\org.python.pydev_2.8.2.2013090511\pysrc\pydevd.py", line 1446, in <module>
debugger.run(setup['file'], None, None)
File "C:\Program Files\Eclipse\eclipse-jee-helios-win32\eclipse-jee-helios-win32\plugins\org.python.pydev_2.8.2.2013090511\pysrc\pydevd.py", line 1092, in run
pydev_imports.execfile(file, globals, locals) #execute the script
File "D:\workspace\testCode.py", line 133, in main
ieInterface.childMethod()
File "D:\workspace\testCode.py", line 33, in childMethod
returnedList = self.parentMethod(itemClassName = 'SomeClassName')
File "D:\workspace\testCode.py", line 45, in parentMethod
allElements = self.getAllElements()
AttributeError: 'InterfaceChild' object has no attribute 'self'
I installed selenium with pip under Python 2.7 and changed the code to use Chrome driver[1] instead and changed the checker to make sure there would be some input tags being found. full code below. I'm running the code on Mac OS X. it works just fine.
So I guess the issue here is not the code. (Windows is not a friend of Python, perhaps :).
from selenium import webdriver
class UIInterface(object):
def __init__(self):
self.driver = webdriver.Chrome()
self.driver.get('http://duckduckgo.com')
def parentMethod(self, itemClassName = ''):
allElements = self.getAllElements()
result = [el for el in allElements]
print('===', result)
return result
def getAllElements(self):
return self.driver.find_elements_by_tag_name('input')
class InterfaceChild(UIInterface):
def __init__(self):
super(InterfaceChild, self).__init__()
def childMethod(self):
returnedList = self.parentMethod(itemClassName = 'SomeClassName')
for item in returnedList:
print item.get_attribute('innerHTML')
if __name__ == '__main__':
ieInterface = InterfaceChild()
ieInterface.childMethod()
The output looks like:
('===', [<selenium.webdriver.remote.webelement.WebElement object at 0x10e145cd0>, <selenium.webdriver.remote.webelement.WebElement object at 0x10e145d10>, <selenium.webdriver.remote.webelement.WebElement object at 0x10e145d50>])
[1] http://chromedriver.storage.googleapis.com/index.html?path=2.9/

Categories