Incorrect context when trying to reload text - blender

in order to develop my script in an external IDE, I'm trying to automatically reload a text in blender when it is modified from outside of Blender. I think I've got the idea of how it works, but there must be one tiny detail that completely blocks me from achieving my goal.
My script is the following:
import bpy
import sys
ctx = bpy.context.copy()
for area in ctx['screen'].areas:
if area.type == 'TEXT_EDITOR':
textEditor = area
for text in bpy.data.texts:
if text.name == 'main.py':
textEditor.spaces[0].text = text
bpy.ops.text.reload()
Output:
>>> exec(bpy.data.texts['reload.py'].as_string())
Traceback (most recent call last):
File "<blender_console>", line 1, in <module>
File "<string>", line 13, in <module>
File "/Applications/Blender.app/Contents/Resources/3.3/scripts/modules/bpy/ops.py", line 113, in __call__
ret = _op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.text.reload.poll() failed, context is incorrect
I cannot get what part of the context is correct. I found no solution on the Internet, even the doc recommends reading the source code to understand what might be the cause of this error, but I couldn't find it anyway in the source code.
Can someone tell me what I am missing here please?

Related

GNU Radio OOT block: AttributeError: module 'twoTypes' has no attribute 'passthrough_cc'

What I'm trying to do:
I am trying to write an OOT block for GNU radio that accept complex or byte values and just pass through. (My final target is obviously do some processing of the incoming stream, but it had so many errors, I had to go back to the basics)
The error:
I receive this error at runtime
traceback (most recent call last):
File "/home/maisun/Desktop/asdf.py", line 193, in <module>
main()
File "/home/maisun/Desktop/asdf.py", line 171, in main
tb = top_block_cls()
File "/home/maisun/Desktop/asdf.py", line 82, in __init__
self.twoTypes_passthrough_0 = twoTypes.passthrough_cc(0)
AttributeError: module 'twoTypes' has no attribute 'passthrough_cc'
what I've tried:
I have looked into the source of GR itself and tried to correct my yaml files, my header files. To the best of my knowledge, I have defined 'passthrough_cc' in the twoTypes.h header file as,
typedef passthrough<std::uint8_t> passthrough_bb;
typedef passthrough<gr_complex> passthrough_cc;
Obviosly i'm still doing something wrong here.
my questions:
I have 2 questions.
First, how can I correct the CPP code and the python module? so that I can call passthrough_cc without errors.
second, I am more comfortable in C language. So sometimes GR codes are very confusing to me. GR wiki has some nice guides available. But does anyone know if there is any guide/blog post that kind of discusses the workflow of GR? For example, when I trace my codes, I start from main() and follow the flow. with GR I always get lost.
full code:
https://github.com/maisunmonowar/gr-twoTypes

Odoo 12: 'report.label.report_label' AttributeError

I am using a third-party module in Odoo to do mass label printing (https://www.odoo.com/apps/modules/12.0/label/) and despite the fact that the module claims to be compatible with version 12, I am getting server errors when trying to run the pdf rendering:
Odoo Server Error
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/odoo/addons/web/controllers/main.py", line 1677, in report_download
response = self.report_routes(reportname, converter=converter, **dict(data))
File "/usr/lib/python3/dist-packages/odoo/http.py", line 517, in response_wrap
response = f(*args, **kw)
File "/usr/lib/python3/dist-packages/odoo/addons/web/controllers/main.py", line 1614, in report_routes
pdf = report.with_context(context).render_qweb_pdf(docids, data=data)[0]
File "/usr/lib/python3/dist-packages/odoo/addons/base/models/ir_actions_report.py", line 677, in render_qweb_pdf
html = self.with_context(context).render_qweb_html(res_ids, data=data)[0]
File "/usr/lib/python3/dist-packages/odoo/addons/base/models/ir_actions_report.py", line 710, in render_qweb_html
data = self._get_rendering_context(docids, data)
File "/usr/lib/python3/dist-packages/odoo/addons/base/models/ir_actions_report.py", line 723, in _get_rendering_context
data.update(report_model._get_report_values(docids, data=data))
AttributeError: 'report.label.report_label' object has no attribute '_get_report_values'
Screenshot:
It may be an error related to the change of some Odoo version (or not, I don’t really know).
Does anyone know if this attribute exist? I haven’t been able to find this information in the Odoo documentation (it doesn’t seem very complete regarding these topic).
Here are some screenshots of the configurations I’m using:
Thank you for your help!
From the traceback you shared on first screenshot, the problem seems to be with report_model variable which is reference to report.label.report_label object, defined in label/report/dunamic_model.py file which contains the method get_report_values. But from odoo 12 community code, ir.actions.report is looking for _get_report_values, the mismatch between this two method name is actually causing the problem.

Error using pymtp and libmtp

I am new to programming on python and i am trying to create an application which uses pyMTP to communicate to an MTP device. However im getting this error and cant figure out the fix for it.
$ python c:/Users/Atul/Desktop/mtp.py
None
Traceback (most recent call last):
File "c:/Users/Atul/Desktop/mtp.py", line 2, in <module>
import pymtp
File "C:\Users\Atul\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pymtp.py", line 42, in <module>
_libmtp = ctypes.CDLL(_module_path)
File "C:\Users\Atul\AppData\Local\Programs\Python\Python36-32\lib\ctypes\__init__.py", line 348, in __init__
self._handle = _dlopen(self._name, mode)
TypeError: LoadLibrary() argument 1 must be str, not None
Think it is not a program, but a library, you have to import. Like once in python, import mtp (or import pymtp, i don't know anymore). Please be aware pymtp is about 5 years old and does only run on python 2. I have been working on a python 3 version but it's not really complete because i found testing MTP software is extremely time consuming because the mtp interface is very, very slow at startup. I suggest to look at gMTP to transfer you files.

Using fileinput.input() to read gzip files

I'm using fileinput to read some large data:
import gzip
import fileinput
f=gzip.open('/scratch/try.fastq.gz','r')
for line in fileinput.input(f):
print line
However I got errors like:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/share/lib/python2.6/fileinput.py", line 253, in next
line = self.readline()
File "/share/lib/python2.6/fileinput.py", line 345, in readline
self._file = open(self._filename, self._mode)
IOError: [Errno 2] No such file or directory: '#HWI-ST150_0129:2:1:13466:2247#0/1\n'
Cannot fileinput take file object as input? Then how to use fileinput to deal with gzip file?
thx
Nope, the first argument to fileinput.input should be a list of filenames. What you want can be achieved with
for line in gzip.open('/scratch/try.fastq.gz')
print line
fileinput exists to support the idiom where a program reads from a list of files, probably supplied on the command line, or standard input if no files have been specified. If you still want to use it, even though it's useless in your example, you should do
for line in fileinput(['/scratch/try.fastq.gz'], openhook=gzip.open):
print line
As other sources have said, the value for openhook must be a function, but that doesn't mean you can't call a function to return a function. For example, if you want to support multiple different types of incoming files you could write something like this:
import fileinput
import gzip
def get_open_handler(compressed):
if deciding_data:
# mode comes in as 'r' by defualt, but that means binary to `gzip`
return lambda file_name, mode: gzip.open(file_name, mode='rt')
else:
# the default mode of 'r' means text for `open`
return open
# get args here
for line in fileinput.input(args.files, openhook=get_open_handler(args.compressed))
print(line)
As you can see, we are calling a function from openhook, but that function returns another function. In this case, we are fixing the mode of gzip.open, but we can do anything we want, including using functools.partial to bind some values to a function so that when the default filename and mode get passed to the function assigned to openhook, the function will do what you want.

How to rewrite a specific frame in a traceback?

In python you can compile() string to be executed faster with exec(). But as soon as i use it, we lost information when an exception happen in the exec.
For example, here is a code snippet that calling a unknown method (for demo purposes):
code = 'my_unknown_method()'
bytecode = compile(code, '<string>', 'exec')
And later, i'm calling exec on that bytecode:
exec bytecode
The traceback showed is:
Traceback (most recent call last):
File "test.py", line 3, in <module>
exec bytecode
File "<string>", line 1, in <module>
NameError: name 'my_unknown_method' is not defined
The "exec()" frame is now obscure. I would like to have a better exception like:
Traceback (most recent call last):
File "test.py", line 3, in <module>
exec "my_unknown_method()"
File "<string>", line 1, in <module>
NameError: name 'my_unknown_method' is not defined
Any thoughts ?
Notes:
I don't want to use the second argument of compile (the filename)
I have tested to play with inspect and modify f_code on the frame, but it's readonly attribute.
EDIT: after looking more to sys.excepthook, i've seen that in python source code/traceback.c, when python want to show the line content, they are fopen() directly the file if found. No hook available at all to display our own content. The only way would be to create in real fake filename on disk ? Anyone ?
EDIT2: i checked some jinja2 debug code, and they are rewriting traceback too, but not for content. Would i need a custom except hook ? My concerns with it is since it's not in the traceback itself, if an user/module/whatever take the exception, the traceback will not contain valuable information.
You should have a look at this talk by Armin Ronacher. He's the author of Jinja2 and in this talk he explained, how he manipulates stack traces in Jinja2. If I remember correctly, he's using ctypes, to manipulate the data structures on the C level of Python. The talk was in my opinion the best talk of the whole Europython 2011 by the way.
After a deep search, it's not possible, CPython is using its own API to determine where the file is etc, and it cannot be patched in pure Python.
How about something like this:
import sys
def mkexec(code_str):
bc = compile(code, '<string>', 'exec')
def run():
try:
exec bc
except: # Yes I know a bare except
t, v, tb = sys.exc_info()
raise MyUsefullException("%s raised %s" % (code_str, v))
return run
exe = mkexec("some_unknown_something()")
exe()