# Getting the context in which a function is running?

**URL:** <https://forum.inductiveautomation.com/t/getting-the-context-in-which-a-function-is-running/33977>\
**Category:** Ignition\
**Tags:** ignition80\
**Created:** [April 16, 2020, 12:28pm UTC](https://forum.inductiveautomation.com/t/getting-the-context-in-which-a-function-is-running/33977 "2020-04-16T12:28:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [April 16, 2020, 12:28pm UTC](https://forum.inductiveautomation.com/t/getting-the-context-in-which-a-function-is-running/33977/1 "2020-04-16T12:28:40Z")

</div>

Related to my question the other day - [Using importlib to run any function on the gateway?](http://forum.inductiveautomation.com/t/using-importlib-to-run-any-function-on-the-gateway/33915/10)

Now with forms, I like to run a validation on the client side so that the window doesn’t close and the user has a chance to fix their mistakes. And if that validation runs fine, then it sends a message to the gateway to run the actual create script.

However, I know it’s not enough to just validate client side, and I should run it on the gateway end as well to be safe in case someone is able to start running my modules in a way not intended.

But the validation function is the same no matter the scope what changes is the what it does at the end.

Is there a way to do something like the following:

```auto
def validate(...):
    if someCondition:
        raise ValueError("Something wrong")
    .....
   scope = system.someScriptHere()
   if scope == "client":
       system.util.sendMessage(...)
   elif scope == "gateway":
       return 1

```

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 16, 2020, 12:31pm UTC](https://forum.inductiveautomation.com/t/getting-the-context-in-which-a-function-is-running/33977/2 "2020-04-16T12:31:00Z")

</div>

> [@Best methods for checking script scope](http://forum.inductiveautomation.com/t/best-methods-for-checking-script-scope/30201/2):
>
> Try this: def getScope(): """ Returns: scope (int): 0=Gateway, 1=Client, 2=Designer """ try: if system.util.getSystemFlags() & system.util.CLIENT\_FLAG: location = 1 elif system.util.getSystemFlags() & system.util.DESIGNER\_FLAG: location = 2 except AttributeError: # system.util.getSystemFlags() does not exist in gateway scope location = 0 return location (Source: [Which scope is my code running in?](http://forum.inductiveautomation.com/t/which-scope-is-my-code-running-in/15540/3))

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 16, 2020, 1:02pm UTC](https://forum.inductiveautomation.com/t/getting-the-context-in-which-a-function-is-running/33977/3 "2020-04-16T13:02:15Z")

</div>

However you check for scope, consider doing it at the top level of an inherited script module, setting booleans like `isDesigner` and `isGateway`. Then just check the booleans everywhere else.

Something like this:

```auto
try:
    isDesigner = bool(system.util.getSystemFlags() & system.util.DESIGNER_FLAG)
    isGateway = False
except AttributeError:
    isDesigner = False
    isGateway = True

```

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [April 16, 2020, 1:14pm UTC](https://forum.inductiveautomation.com/t/getting-the-context-in-which-a-function-is-running/33977/4 "2020-04-16T13:14:02Z")

</div>

When you say top level of an inherited script what do you mean? I created a new script module `util` with one function `getScope` which is the above function. I was planning on using it by doing

```auto
import util
...
scope = util.getScope()

```

How would I utilize your method?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 16, 2020, 1:54pm UTC](https://forum.inductiveautomation.com/t/getting-the-context-in-which-a-function-is-running/33977/5 "2020-04-16T13:54:55Z")

</div>

Don’t import `util`–not necessary. If you use my example in the `util` script module, just do this in your validation script module:

```auto
def validate(...):
    if someCondition:
        raise ValueError("Something wrong")
    .....
   if util.isGateway:
       return 1
   else:
       return system.util.sendRequest(...)

```
