Fabric - Python 3 - What is context and what does it have to contain and why do I need to pass it?
Asked Answered
M

1

5

This is my fabric code:

from fabric import Connection, task

server = Connection(host="[email protected]:22", connect_kwargs={"password": "mypassword"})

@task
def dostuff(somethingmustbehere):
    server.run("uname -a")

This code works just fine. When I execute fab dostuff it does what I want it to do.

When I remove somethingmustbehere however I get this error message:

    raise TypeError("Tasks must have an initial Context argument!")
TypeError: Tasks must have an initial Context argument!

I never defined somethingmustbehere anywhere in my code. I just put it in and the error is gone and everything works. But why? What is this variable? Why do I need it? Why is it so important? And if it is so important why can it just be empty? I am really lost here. Yes it works, but I cannot run code that I don't understand. It drives me insane. :-)

Please be aware that I'm talking about the Python 3(!) version of Fabric! The Fabric version is 2.4.0

Minda answered 15/10, 2018 at 18:42 Comment(2)
Which version of Fabric are you using? (The version number of Fabric itself, not the Python version)Exemplification
Hey I'm using 2.4.0 I also added this to the original post so it is more clear to others as well.Minda
G
6

To be able to run a @task you need a context argument. Fabric uses invoke task() which expects to see a context object. Normally we name the variable c or ctx (which I always use to make it more clear). I don't prefer using c because I use it normally for connection

Check this line on github from invoke package repo, you will see that it raises an exception when the context argument is not present, but it doesn't explain why!

To know more about Context object, what it 's and why we need it, you can read the following on the site of pyinvoke:

Aside: what exactly is this ‘context’ arg anyway? A common problem task runners face is transmission of “global” data - values loaded from configuration files or other configuration vectors, given via CLI flags, generated in ‘setup’ tasks, etc.

Some libraries (such as Fabric 1.x) implement this via module-level attributes, which makes testing difficult and error prone, limits concurrency, and increases implementation complexity.

Invoke encapsulates state in explicit Context objects, handed to tasks when they execute . The context is the primary API endpoint, offering methods which honor the current state (such as Context.run) as well as access to that state itself.

Check these both links :

To be honest, I wasted a lot of time figuring out what context is and why my code wouldn't run without it. But at some point I just gave up and started using to make my code run without errors.

Gamophyllous answered 17/10, 2018 at 7:46 Comment(4)
"To be honest, I wasted a lot of time figuring out what context is and why my code wouldn't run without it. But at some point I just gave up and started using to make my code run without errors." Haha, my experience exactly. Thanks for your answer!!Minda
Sorry, what do we need to do to make this thing work? The docs are not very useful.Rainbow
@Rainbow excuse me, I didn't get your question! What do you mean exactly?Gamophyllous
Like, where does the context come from?Rainbow

© 2022 - 2024 — McMap. All rights reserved.