Autokey: sporadic error on system.exec_command - python

I have a script that grabs the window title and writes out a debug statement based on whether the file title includes a certain string.
active_title = window.get_active_title()
counter = system.exec_command("date '+%s'")
newcounter = counter[-5:]
counter = newcounter
time.sleep(.4)
if '.php' in active_title:
output = "die('<pre>[" + str(counter) + "] ' . date('Y-m-d H:i:s'));"
time.sleep(.2)
clipboard.fill_clipboard(output)
time.sleep(.2)
keyboard.send_keys('<ctrl>+v')
if '.js' in active_title:
output = "console.log('[" + str(counter) + "] ' + Date.now())"
time.sleep(.2)
clipboard.fill_clipboard(output)
time.sleep(.2)
keyboard.send_keys('<ctrl>+v')
time.sleep(.2)
keyboard.send_keys("<ctrl>+s")
99% of the time it works without issue, but that 100th time it throws this error:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/autokey/service.py", line 485, in execute
exec(script.code, scope)
File "<string>", line 2, in <module>
File "/usr/lib/python3/dist-packages/autokey/scripting.py", line 1104, in get_active_title
return self.mediator.interface.get_window_title()
File "/usr/lib/python3/dist-packages/autokey/interface.py", line 1158, in get_window_title
return self.get_window_info(window, traverse).wm_title
File "/usr/lib/python3/dist-packages/autokey/interface.py", line 1074, in get_window_info
return self._get_window_info(window, traverse)
File "/usr/lib/python3/dist-packages/autokey/interface.py", line 1081, in _get_window_info
new_wm_class = self._try_get_window_class(window)
File "/usr/lib/python3/dist-packages/autokey/interface.py", line 1151, in _try_get_window_class
wm_class = window.get_wm_class()
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/xobject/drawable.py", line 660, in get_wm_class
d = self.get_full_property(Xatom.WM_CLASS, Xatom.STRING)
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/xobject/drawable.py", line 452, in get_full_property
prop = self.get_property(property, type, 0, sizehint)
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/xobject/drawable.py", line 435, in get_property
r = request.GetProperty(display = self.display,
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/protocol/rq.py", line 1481, in __init__
self.reply()
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/protocol/rq.py", line 1493, in reply
self._display.send_and_recv(request = self._serial)
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/protocol/display.py", line 556, in send_and_recv
gotreq = self.parse_response(request)
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/protocol/display.py", line 643, in parse_response
gotreq = self.parse_request_response(request) or gotreq
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/protocol/display.py", line 720, in parse_request_response
req = self.get_waiting_replyrequest()
File "/home/seamlyne/.local/lib/python3.9/site-packages/Xlib/protocol/display.py", line 847, in get_waiting_replyrequest
raise RuntimeError("Request reply to unknown request. Can't happen!")
RuntimeError: Request reply to unknown request. Can't happen!
Closing and restarting Autokey helps, but I'd like to avoid the error if at all possible. I have several scripts that get the window title, and this is the only script that has this issue. Is there something wrong with using system.exec_command?

Since you are invoking the clipboard, it helps to add to the start and the end
os.system("sleep .1; xsel -cb")
to clear it.

Related

How to read PDF from file storage object in pdf2image?

I am working with flask, where I am uploading a pdf file to convert it to an image and perform OCR using pytesseract.
However, pdf2image is not able to read the uploaded image.
I tried searching on the internet but I could not find anything.
I tried passing the file storage object directly, but am getting an error, my code looks like this:
log_file = request.files.get('pdf')
images = convert_from_path(log_file)
text = ""
for img in images:
im = img
ocr_dict = pytesseract.image_to_data(im, lang='eng', output_type=Output.DICT)
text += " ".join(ocr_dict['text'])
cleaned_text = clean_text(txt=text)
which gives this error,
**TypeError: expected str, bytes or os.PathLike object, not FileStorage**
I also tried doing,
log_file = request.files.get('pdf')
images = convert_from_path(log_file.read())
text = ""
for img in images:
im = img
ocr_dict = pytesseract.image_to_data(im, lang='eng', output_type=Output.DICT)
text += " ".join(ocr_dict['text'])
cleaned_text = clean_text(txt=text)
which gives error:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/pdf2image/pdf2image.py", line 458, in pdfinfo_from_path
proc = Popen(command, env=env, stdout=PIPE, stderr=PIPE)
File "/usr/lib/python3.8/subprocess.py", line 858, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/lib/python3.8/subprocess.py", line 1639, in _execute_child
self.pid = _posixsubprocess.fork_exec(
ValueError: embedded null byte
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 1516, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.8/dist-packages/flask/app.py", line 1502, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/usr/local/lib/python3.8/dist-packages/flask_restful/__init__.py", line 467, in wrapper
resp = resource(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/flask/views.py", line 84, in view
return current_app.ensure_sync(self.dispatch_request)(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/flask_restful/__init__.py", line 582, in dispatch_request
resp = meth(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/flask_httpauth.py", line 172, in decorated
return self.ensure_sync(f)(*args, **kwargs)
File "/home/ubuntu/Credit_Scoring/API_Script/temp2.py", line 38, in post
json_text = coi_ocr.get_coi_ocr_text()
File "/home/ubuntu/Credit_Scoring/API_Script/ocr_script/certificate_of_incorporation/coi_ocr_script_pdf.py", line 51, in get_coi_ocr_text
text1 = self.extract_text_from_COI()
File "/home/ubuntu/Credit_Scoring/API_Script/ocr_script/certificate_of_incorporation/coi_ocr_script_pdf.py", line 16, in extract_text_from_COI
images = convert_from_path(self.fl)
File "/usr/local/lib/python3.8/dist-packages/pdf2image/pdf2image.py", line 98, in convert_from_path
page_count = pdfinfo_from_path(pdf_path, userpw, poppler_path=poppler_path)["Pages"]
File "/usr/local/lib/python3.8/dist-packages/pdf2image/pdf2image.py", line 489, in pdfinfo_from_path
"Unable to get page count.\n%s" % err.decode("utf8", "ignore")
UnboundLocalError: local variable 'err' referenced before assignment
Okay, it turns out I need to pass convert_from_bytes instead of convert_from_path.

Testlink api python

I'm using TestLink-API-Python-client to work with Testlink application API. So, I need to get all testcases for the TestSuite with execution_type = 1 or execution_type = 2 to calculate the quantity of automated and manual tests (when execution_type = 1 it's manual tests, when execution_type = 2, it's automated tests).
For this purpose I used next method from the library getTestCasesForTestSuite(testsuite["id"], True, "full", getkeywords=True) as the result I got issue:
Traceback (most recent call last):
File "C:/Users/user1/PycharmProjects/script_for_testlink/script_for_testlink.py", line 871, in <module>
result = getAutomatedTestcasesForProject(TESTLINK_API_URL, TESTLINK_API_DEVKEY, project_in_testlink)
File "C:/Users/user1/PycharmProjects/script_for_testlink/script_for_testlink.py", line 112, in getAutomatedTestcasesForProject
testcases_for_testsuites = testlink.getTestCasesForTestSuite(testsuite["id"], True, "full", getkeywords=True)
File "C:\Users\user1\TAF\venv\lib\site-packages\testlink\testlinkdecorators.py", line 140, in wrapperReplaceTLResponseError
response = methodAPI(self, *argsPositional, **argsOptional)
File "C:\Users\user1\TAF\venv\lib\site-packages\testlink\testlinkdecorators.py", line 112, in wrapperAddDevKey
return methodAPI(self, *argsPositional, **argsOptional)
File "C:\Users\user1\TAF\venv\lib\site-packages\testlink\testlinkdecorators.py", line 100, in wrapperWithArgs
*argsPositional, **argsOptional)
File "C:\Users\user1\TAF\venv\lib\site-packages\testlink\testlinkapigeneric.py", line 1517, in callServerWithPosArgs
response = self._callServer(methodNameAPI, argsOptional)
File "C:\Users\user1\TAF\venv\lib\site-packages\testlink\testlinkapigeneric.py", line 1992, in _callServer
response = getattr(self.server.tl, methodNameAPI)(argsAPI)
File "c:\users\user1\appdata\local\programs\python\python37\Lib\xmlrpc\client.py", line 1117, in __call__
return self.__send(self.__name, args)
File "c:\users\user1\appdata\local\programs\python\python37\Lib\xmlrpc\client.py", line 1457, in __request
verbose=self.__verbose
File "c:\users\user1\appdata\local\programs\python\python37\Lib\xmlrpc\client.py", line 1159, in request
return self.single_request(host, handler, request_body, verbose)
File "c:\users\user1\appdata\local\programs\python\python37\Lib\xmlrpc\client.py", line 1175, in single_request
return self.parse_response(resp)
File "c:\users\user1\appdata\local\programs\python\python37\Lib\xmlrpc\client.py", line 1345, in parse_response
p.close()
File "c:\users\user1\appdata\local\programs\python\python37\Lib\xmlrpc\client.py", line 453, in close
parser.Parse(b"", True) # end of data
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 5186, column 0
How can I get testcases with field execution_type without issues?

Logging exception error upgrading to python 3.4 from python 2.7

My application was running well but as I upgraded it to python version 3.4 . I am getting the error shown bellow. Looking at the error message I am not exactly able to debug the problem
Error:
--- Logging error ---
Traceback (most recent call last):
File "/usr/lib64/python3.4/logging/__init__.py", line 978, in emit
msg = self.format(record)
File "/usr/lib64/python3.4/logging/__init__.py", line 828, in format
return fmt.format(record)
File "/usr/lib64/python3.4/logging/__init__.py", line 573, in format
record.exc_text = self.formatException(record.exc_info)
File "/usr/lib64/python3.4/logging/__init__.py", line 523, in formatException
traceback.print_exception(ei[0], ei[1], tb, None, sio)
File "/usr/lib64/python3.4/traceback.py", line 169, in print_exception
for line in _format_exception_iter(etype, value, tb, limit, chain):
File "/usr/lib64/python3.4/traceback.py", line 146, in _format_exception_iter
for value, tb in values:
File "/usr/lib64/python3.4/traceback.py", line 125, in _iter_chain
context = exc.__context__
AttributeError: 'NoneType' object has no attribute '__context__'
Call stack:
File "main.py", line 253, in <module>
main()
File "main.py", line 144, in main
plugininfo.plugin_object.run(cons.MAIN_CONFIG_PATH, outputpath, finallogs, plugininfo.name, args.inventory)
File "/home/sjoshi/python3.4/check-acess/plugins/plugin_etc_passwd/plugin_etc_passwd.py", line 107, in run
self.result = phelper.executeCommand(runcommand)
File "/home/sjoshi/python3.4/check-acess/lib/sshplugin/sshplugin.py", line 86, in executeCommand
logging.exception("ErrorCode:%d %s", cmdout.returncode, output)
Message: 'ErrorCode:%d %s'
Arguments: (255, b'')
No Result found or all users filtered sbx32 /etc/passwd
Permission denied (publickey).
--- Logging error ---
Traceback (most recent call last):
File "/usr/lib64/python3.4/logging/__init__.py", line 978, in emit
msg = self.format(record)
File "/usr/lib64/python3.4/logging/__init__.py", line 828, in format
return fmt.format(record)
File "/usr/lib64/python3.4/logging/__init__.py", line 573, in format
record.exc_text = self.formatException(record.exc_info)
File "/usr/lib64/python3.4/logging/__init__.py", line 523, in formatException
traceback.print_exception(ei[0], ei[1], tb, None, sio)
File "/usr/lib64/python3.4/traceback.py", line 169, in print_exception
for line in _format_exception_iter(etype, value, tb, limit, chain):
File "/usr/lib64/python3.4/traceback.py", line 146, in _format_exception_iter
for value, tb in values:
File "/usr/lib64/python3.4/traceback.py", line 125, in _iter_chain
context = exc.__context__
AttributeError: 'NoneType' object has no attribute '__context__'
Call stack:
File "main.py", line 253, in <module>
main()
File "main.py", line 144, in main
plugininfo.plugin_object.run(cons.MAIN_CONFIG_PATH, outputpath, finallogs, plugininfo.name, args.inventory)
File "/home/sjoshi/python3.4/check-acess/plugins/plugin_etc_passwd/plugin_etc_passwd.py", line 107, in run
self.result = phelper.executeCommand(runcommand)
File "/home/sjoshi/python3.4/check-acess/lib/sshplugin/sshplugin.py", line 86, in executeCommand
logging.exception("ErrorCode:%d %s", cmdout.returncode, output)
Message: 'ErrorCode:%d %s'
Arguments: (255, b'')
sshplugin.py
import os
import subprocess as subp
import logging
import lib.exceptions.errors as error
import lib.inventory.bashhostlist as hostname
class SSHPlugin:
"""
Helper class for the SSH based plugins
"""
def makeCommand(self, user, host, filepath, sudo, timeout, attempt, changetols=None):
"""
-Two types of commands with or without ssh depends on hostname
-Other depends on parameter set or unset
-ConnectionAttempts and ConnectTimeout to occur must be SSH
-last arg(changetols): This is to checkif the plugin type is authorized_keys. Because in that case we need ls not cat
"""
command=""
if changetols is None:
command = ["cat", filepath]
else:
command=["ls", filepath]
if host.lower() == 'localhost':
if sudo.lower() == 'yes':
command.insert(0, 'sudo')
else:
command.insert(0, "ssh")
if timeout and not attempt:
command.insert(1, '-o')
command.insert(
2, "ConnectTimeout={timeout}".format(timeout=timeout))
command.insert(3, user+"#"+host)
if sudo.lower() == 'yes':
command.insert(4, 'sudo')
elif timeout and attempt:
command.insert(1, '-o')
command.insert(
2, "ConnectTimeout={timeout}".format(timeout=timeout))
command.insert(3, '-o')
command.insert(
4, "ConnectionAttempts={att}".format(att=attempt))
command.insert(5, user+"#"+host)
if sudo.lower() == 'yes':
command.insert(6, 'sudo')
elif attempt and not timeout:
command.insert(1, '-o')
command.insert(
2, "ConnectionAttempts={att}".format(att=attempt))
command.insert(3, user+"#"+host)
if sudo.lower() == 'yes':
command.insert(4, 'sudo')
else:
command.insert(1, user+"#"+host)
if sudo.lower() == 'yes':
command.insert(2, 'sudo')
return command
def executeCommand(self, command, filtercommand=None):
"""
Multuple Popen used to execute the process,Command followed by filter if any
if no filter than just a single commad run else
The output of first will act as the input to the filtering command
"""
if filtercommand is None:
cmdout = subp.Popen(command, stdout=subp.PIPE)
output, err = cmdout.communicate()
if cmdout.returncode is 0:
logging.info("Result success,status code %d", cmdout.returncode)
return output
else:
logging.exception("ErrorCode:%d %s %s", cmdout.returncode, output,err)
return False
else:
cmdout = subp.Popen(command, stdout=subp.PIPE)
filtered = subp.Popen(filtercommand, stdin=cmdout.stdout, stdout=subp.PIPE)
output, err = filtered.communicate()
if filtered.returncode is 0:
logging.info("Result success,status code %d", filtered.returncode)
return output
else:
logging.exception("ErrorCode:%d %s", filtered.returncode, output)
return False
why is the logging.exception throwing errors. Is it the problem with the logging module. I tried to replace the logging.exception with logging.info instead but that didn't work.

Async query throws AssertionError first time (AppEngine, NDB)

When I use fetch_async() on a query it crashes with AssertionError the first time it is run. If I run it again immediately, it's fine.
Eg.
With model:
class User(ndb.Model):
user = ndb.UserProperty()
name = ndb.StringProperty()
penname = ndb.StringProperty()
first_login = ndb.DateTimeProperty(auto_now_add=True)
contact = ndb.BooleanProperty()
The following works straight away, returning an empty list:
users = User.query(User.penname == "asdf").fetch()
But this crashes:
future = User.query(User.penname == "asdf").fetch_async()
users = future.get_result()
With:
Traceback (most recent call last):
File "/opt/google-appengine-python/google/appengine/ext/admin/__init__.py", line 320, in post
exec(compiled_code, globals())
File "<string>", line 6, in <module>
File "/opt/google-appengine-python/google/appengine/ext/ndb/tasklets.py", line 320, in get_result
self.check_success()
File "/opt/google-appengine-python/google/appengine/ext/ndb/tasklets.py", line 357, in _help_tasklet_along
value = gen.throw(exc.__class__, exc, tb)
File "/opt/google-appengine-python/google/appengine/ext/ndb/query.py", line 887, in _run_to_list
batch = yield rpc
File "/opt/google-appengine-python/google/appengine/ext/ndb/tasklets.py", line 435, in _on_rpc_completion
result = rpc.get_result()
File "/opt/google-appengine-python/google/appengine/api/apiproxy_stub_map.py", line 592, in get_result
return self.__get_result_hook(self)
File "/opt/google-appengine-python/google/appengine/datastore/datastore_query.py", line 2386, in __query_result_hook
self._batch_shared.conn.check_rpc_success(rpc)
File "/opt/google-appengine-python/google/appengine/datastore/datastore_rpc.py", line 1191, in check_rpc_success
rpc.check_success()
File "/opt/google-appengine-python/google/appengine/api/apiproxy_stub_map.py", line 558, in check_success
self.__rpc.CheckSuccess()
File "/opt/google-appengine-python/google/appengine/api/apiproxy_rpc.py", line 156, in _WaitImpl
self.request, self.response)
File "/opt/google-appengine-python/google/appengine/api/datastore_file_stub.py", line 568, in MakeSyncCall
response)
File "/opt/google-appengine-python/google/appengine/api/apiproxy_stub.py", line 87, in MakeSyncCall
method(request, response)
File "/opt/google-appengine-python/google/appengine/datastore/datastore_stub_util.py", line 2367, in UpdateIndexesWrapper
self._UpdateIndexes()
File "/opt/google-appengine-python/google/appengine/datastore/datastore_stub_util.py", line 2656, in _UpdateIndexes
self._index_yaml_updater.UpdateIndexYaml()
File "/opt/google-appengine-python/google/appengine/datastore/datastore_stub_index.py", line 244, in UpdateIndexYaml
all_indexes, manual_indexes)
File "/opt/google-appengine-python/google/appengine/datastore/datastore_stub_index.py", line 85, in GenerateIndexFromHistory
required, kind, ancestor, props, num_eq_filters = datastore_index.CompositeIndexForQuery(query)
File "/opt/google-appengine-python/google/appengine/datastore/datastore_index.py", line 424, in CompositeIndexForQuery
assert filter.property(0).name() == ineq_property
AssertionError
But if I run it again immediately, for example by doing:
future = User.query(User.penname == "asdf").fetch_async()
try:
users = future.get_result()
except:
users = future.get_result()
It works.
This has been cropping up all over the place and I'm struggling to pin down the root cause.
The solution was to upgrade to the 1.7.1 SDK.

Python: Code crashes randomly when checking e-mail

This code:
import imaplib, re
import os
import time
conn = imaplib.IMAP4_SSL("imap.gmail.com", 993)
conn.login("ddd#ddd.com", "ddd")
while(True):
unreadCount = re.search("UNSEEN (\d+)", conn.status("INBOX", "(UNSEEN)")[1][0]).group(1)
print unreadCount
if int(unreadCount) > 20:
os.system('heroku restart --app warm-beach-203')
#os.system('ls')
#print "Restarting server...."
time.sleep(60)
Output:
0
1
0
0
0
0
0
4
Traceback (most recent call last):
File "gmail.py", line 10, in <module>
unreadCount = re.search("UNSEEN (\d+)", conn.status("INBOX", "(UNSEEN)")[1][0]).group(1)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/imaplib.py", line 704, in status
typ, dat = self._simple_command(name, mailbox, names)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/imaplib.py", line 1059, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/imaplib.py", line 889, in _command_complete
typ, data = self._get_tagged_response(tag)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/imaplib.py", line 990, in _get_tagged_response
self._get_response()
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/imaplib.py", line 907, in _get_response
resp = self._get_line()
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/imaplib.py", line 1000, in _get_line
line = self.readline()
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/imaplib.py", line 1170, in readline
char = self.sslobj.read(1)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ssl.py", line 136, in read
return self._sslobj.read(len)
socket.error: [Errno 60] Operation timed out
I dont understand why it crashes randomly.
The final error line is descriptive: the connection is timing out before the IMAP operation completes. It could be simply that Gmail is running slowly. If that's the case, you could try extending the timeout like so:
import socket
socket.setdefaulttimeout(15) # In seconds
where 15 is the value you've experimentally determined works well for you.

Categories

Resources