Twisted upgrade - now can't access site paths - python-3.x

I was running on Python2.6 and Twisted 15.0.0 with this:
from twisted.python import usage
from twisted.web import resource, server, static
from twisted.application import internet
class Options(usage.Options):
optParameters = [
]
def makeService(options):
# this variation works
root_resource = static.File('/tmp')
# this variation doesn't
root_resource = resource.Resource()
root_resource.putChild("here", static.File('/tmp'))
site = server.Site(root_resource)
web_svc = internet.TCPServer(8000, site)
return web_svc
But after upgrading to Python3.7 and twisted latest (18.7.0) I can't get anything at http://localhost:8000/here
No Such Resource
No such child resource.
Can't find any twisted docs or examples that say a different way to do it.
Additional:
It is starting up the service fine, else I wouldn't see the above.
For reproduction purposes, the twisted/plugins/my_plugin.py looks like:
from twisted.application.service import ServiceMaker
svc = ServiceMaker("TEST_ONE",
"svc",
"Service",
"tstsvc"
)
And executed with:
twist tstsvc

Mystery solved.
Of course it's Python3 here again with String handling.
root_resource.putChild(b"here", static.File('/tmp'))
Without the 'b' in front of the path, it never matched the url as typed.
Thought: Should the putChild() api throw an error if it's passed a str here? Seems bytes is always the right answer and the only thing that could match

Related

Awaiting transaction in the mempool

I am trying to follow Curve tutorial on Brownie. After installing Ganache and Brownie successfully I have tried to use the token template from https://github.com/brownie-mix by typing brownie bake token in the command line (while being in the project's folder). The structure of the token project got re-created in my directory. After typing brownie console I get response that brownie environment is ready. Then I try to deply token contract with Token.deploy("Test Token", "TST", 18, 1e21, {'from': accounts[0]}) and this command resulted with mesaage "Awaiting transaction in the mempool"- This message has been hanging on for over 30 minutes I am wondering how should I de-bug it? What can cause the situation where the token does not get deployed correctly?
maybe I can help you.
Let's go.
Your problem is with gas_price, in a recent eth version the gas is always necessary.
I resolved my problem with this way...
from brownie import accounts, config, SimpleStorage
from brownie.network import gas_price
from brownie.network.gas.strategies import LinearScalingStrategy
gas_strategy = LinearScalingStrategy("60 gwei", "70 gwei", 1.1)
gas_price(gas_strategy)
def deploy_simple_storage():
account = accounts[0]
simple_storage = SimpleStorage.deploy({
"from": account,
"gas_price": gas_strategy
})
# Transact
# Call
print(simple_storage)
def main():
deploy_simple_storage()
I don't know if need all this for your problem, but I needed. try import gas_price and call it after "from": account.
If someone have a better solution, tell me pls.
fonts:
https://eth-brownie.readthedocs.io/en/stable/config.html
https://github.com/batprem/pynft
As I encountered the same issue I have tried around and realized that when deploying to a live net a default gas price setting seems to be set meaning only ganache needs the above described variables and imports etc.
It actually seems to be working better when setting these variables ONLY for development nets and the "gas_price": gas_strategy in the contract deployment dictionary seems unnecessary for both.
I have used an "if on dev" check to set gas_price(gas_strategy) see below:
from brownie.network import gas_price
from brownie.network.gas.strategies import LinearScalingStrategy
gas_strategy = LinearScalingStrategy("60 gwei", "70 gwei", 1.1)
if network.show_active() == "development":
gas_price(gas_strategy)
(Solidity version 0.6.6 and this was just trial and error so no official fix)

How to create puppet external structured facts from script, such as python or bash?

I have seen the documentation, especially External Facts and Custom Facts.
I have the following yum_repos.rb:
require 'facter'
Facter.add('yum_repos') do
setcode '/usr/bin/python3 /opt/puppetlabs/puppet/cache/lib/facter/yum_repos.py'
end
I have the following yum_repos.py:
#!/usr/bin/python3
import configparser
import os
import json
result_dict={}
yum_repos_dir = '/etc/yum.repos.d'
for yum_repo_file in os.listdir(yum_repos_dir):
if not yum_repo_file.endswith(".repo"):
continue
yum_repo_name=yum_repo_file.replace(".repo","")
config = configparser.ConfigParser()
config.read(os.path.join(yum_repos_dir, yum_repo_file))
result_dict[yum_repo_name] = {}
for section in config.sections():
result_dict[yum_repo_name][section] = dict(config[section])
print(json.dumps(result_dict))
When I check facter, it all runs, but the yum_repos fact is a string. Why isn't it structured? I found somewhere on the internet, saying (for old versions of puppet) that stringify_facts must be set to false, so I tried that, but the behavior did not change. I believe that the default was changed in 4.0 to not stringify.
I am using puppet 6.24.0
I try to access the facts in a puppet class like this:
if $facts['yum_repos']['redhat']...['enabled'] != 1 {
...
}
And when I run puppet agent, I get the error message:
Error: Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: Evaluation Error: A substring operation does not accept a String as a character index. Expected an Integer
I never figured out why puppet is stringifying the fact, but I was able to workaround by explicitly calling parsejson(), like so:
if parsejson($facts['yum_repos'])['redhat']...['enabled'] != "1" {
...
}

How to use pystemd to control systemd timedated ntp service?

I'm working on a python app that needs to get the NTPSynchronized parameter from system-timedated. I'd also like to be able to start and stop the NTP service by using the SetNTP method.
To communicate with timedated over d-bus I have been using this as reference: https://www.freedesktop.org/wiki/Software/systemd/timedated/
I previously got this working with dbus-python, but have since learned that this library has been deprecated. I tried the dbus_next package, but that does not have support for Python 3.5, which I need.
I came across the pystemd package, but I am unsure if this can be used to do what I want. The only documentation I have been able to find is this example (https://github.com/facebookincubator/pystemd), but I can not figure out how to use this to work with system-timedated.
Here is the code I have that works with dbus-python:
import dbus
BUS_NAME = 'org.freedesktop.timedate1`
IFACE = 'org.freedesktop.timedate1`
bus = dbus.SystemBus()
timedate_obj = bus.get_object(BUS_NAME, '/org/freedesktop/timedate1')
# Get synchronization value
is_sync = timedate_obj.Get(BUS_NAME, 'NTPSynchronized', dbus_interface=dbus.PROPERTIES_IFACE)
# Turn off NTP
timedate_obj.SetNTP(False,False, dbus_interface=IFACE)
Here's what I have so far with pystemd, but I don't think I'm accessing it in the right way:
from pystemd.systemd1 import Unit
unit = Unit(b'systemd-timesyncd.service')
unit.load()
# Try to access properties
prop = unit.Properties
prop.NTPSynchronized
Running that I get:
Attribute Error: 'SDInterface' object has no attribute 'NTPSynchronized'
I have a feeling that either the service I entered is wrong, or the way I'm accessing properties is wrong, or even both are wrong.
Any help or advice is appreciated.
Looking at the source code, it appears that using the pystemd.systemd1 Unit object has a default destination of "org.freedesktop.systemd1" + the service name (https://github.com/facebookincubator/pystemd/blob/master/pystemd/systemd1/unit.py)
This is not what I want because I am trying to access "org.freedesktop.timedate1"
So instead I instantiated it's base class SDObject from pystemd.base (https://github.com/facebookincubator/pystemd/blob/master/pystemd/base.py)
The following code allowed me to get the sync status of NTP
from pystemd.base import SDObject
obj = SDObject(
destination=b'org.freedesktop.timedate1',
path=b'/org/freedesktop/timedate1',
bus=None,
_autoload=False
)
obj.load()
is_sync = obj.Properties.Get('org.freedesktop.timedate1','NTPSynchronized')
print(is_sync)
Not sure if this is what the library author intended, but hey it works!

How do I use tryNext() in Gremlin with Node.js?

The following example does not work for me in Node.js using the 'gremlin' 3.4.1 npm package:
g.V().has('person','name','bill').tryNext().orElseGet{g.addV('person').property('name','bill').next()}
I am getting a TypeError saying tryNext() is not a function. What am I doing wrong?
import {driver, structure} from 'gremlin';
import DriverRemoteConnection = driver.DriverRemoteConnection;
import Graph = structure.Graph;
const g = new Graph().traversal().withRemote(new DriverRemoteConnection('ws://localhost:8182/gremlin'));
console.log(g.V().toList()); <= working
Now using the line from above in that code will not work, but it does work using the Gremlin console.
Trying to call a function that doesn't exist, which appears to be as stated in the Gremlin docs, to wit:
tryNext() will return an Optional and thus, is a composite of hasNext()/next() (only supported for JVM languages).
http://tinkerpop.apache.org/docs/current/reference/#terminal-steps
Caveat: Never used TinkerPop, never used Gremlin. But I know how to use the web. Could be this is wrong, but the docs do seem fairly clear.
If tryNext() not supported as Dave mentioned.
You can rewrite your query to do the same with other gremlin steps:
g.V().has('person','name','bill').fold().coalesce(unfold(),g.addV('person').property('name','bill')).next()

Flask/Bokeh/Tornado6.0.2: Embedded bokeh servers no longer work after Tornado update

I have numerous Bokeh Server files in a directory say.. /dir/bokeh/, assume the bokeh servers are called bokeh1.py, bokeh2.py, bokeh3.py
The file structure is like so:
|--dir
|---flask.py
|---bokeh
|--bokeh1.py
|--bokeh2.py
I am deploying them all on flask like so:
files=[]
for file in os.listdir("/dir/bokeh/"):
if file.endswith('.py'):
file="bokeh/"+file
files.append(file)
argvs = {}
urls = []
for i in files:
argvs[i] = None
urls.append(i.split('\\')[-1].split('.')[0])
host = 'myhost.com'
apps = build_single_handler_applications(files, argvs)
bokeh_tornado = BokehTornado(apps, extra_websocket_origins=["myhost.com"])
bokeh_http = HTTPServer(bokeh_tornado)
sockets, port = bind_sockets("myhost.com", 0)
bokeh_http.add_sockets(sockets)
On update to Tornado 6.0.2, and deploying Flask, I get the Runtimerror There is no current event loop in thread Thread-1. On deeper research Tornado uses asyncio by default and imposes some restrictions. So I add asyncio.set_event_loop(asyncio.new_event_loop()) to the following.
def bk_worker():
asyncio.set_event_loop(asyncio.new_event_loop())####
server = BaseServer(IOLoop.current(), bokeh_tornado, bokeh_http)
server.start()
server.io_loop.start()
gc.collect()
from threading import Thread
Thread(target=bk_worker).start()
However, upon opening the bokeh server url through flask, the bokeh server selected (any of them) do not load and simply return a blank page. How can I circumvent this?
setting asyncio.set_event_loop_policy(AnyThreadEventLoopPolicy)) yields the same result.
edit:The previous code works with python 2/3, Tornado 4.5.3
I think this is a known Bokeh issue. The best way for now is to downgrade to Tornado 4.5.3.
pip install tornado==4.5.3

Resources