-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1723 from actonlang/revamp-stdin
Revamp stdin_install & add env actions
- Loading branch information
Showing
7 changed files
with
211 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Environment | ||
|
||
The environment of an Acton application is the outside world. Any useful application typically needs to interact with the environment in some way, like reading arguments or taking input from stdin and printing output. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Reading stdin input | ||
|
||
Read input from stdin by installing a handler for stdin data. The returned data is `str` | ||
```python | ||
actor main(env): | ||
def interact(input): | ||
print("Got some input:", input) | ||
|
||
env.stdin_install(interact) | ||
``` | ||
|
||
|
||
It is possible to specify the encoding and an on_error() callback which is invoked if there are problem with decoding the data. When encoding is not specified (default `None`), an attempt is made to discover the encoding by reading the `LANG` environment variable. If no encoding is discovered, the default is to use `utf-8`. | ||
|
||
```python | ||
actor main(env): | ||
def interact(input): | ||
print("Got some input:", input) | ||
|
||
def on_stdin_error(err, data): | ||
print("Some error with decoding the input data:", err) | ||
print("Raw bytes data:", data) | ||
|
||
env.stdin_install(on_stdin=interact, encoding="utf-8", on_error=on_stdin_error) | ||
``` | ||
|
||
You can read the raw data in `bytes` form by installing a bytes handler instead: | ||
|
||
```python | ||
actor main(env): | ||
def interact(bytes_input): | ||
# Note how the input might contain parts (some bytes) of a multi-byte | ||
# Unicode character in which case decoding will fail | ||
print("Got some input:", bytes_input.decode()) | ||
|
||
env.stdin_install(on_stdin_bytes=interact) | ||
``` | ||
|
||
This allows reading binary data and more explicit control over how to decode the data. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Environment variables | ||
|
||
It is possible to read, set and unset environment variables. The standard functions `env.getenv`, `env.setenv` and `env.unsetenv` all assume `str` input and output, which is a convenience based on the assumption that all data is encoded using UTF-8. POSIX systems really use binary encoding for both environment names and variables. To access the environment as bytes and handle decoding explicitly, use `env.getenvb`, `env.setenvb` and `env.unsetenvb`. | ||
|
||
Source: | ||
```python | ||
actor main(env): | ||
env_user = env.getenv("USER") | ||
if env_user is not None: | ||
print("User:", env_user) | ||
env.setenv("FOO", "bar") | ||
env.unsetenv("LANG") | ||
foo_env = env.getenv("FOO") | ||
if foo_env is not None: | ||
print("FOO:", foo_env) | ||
env.exit(0) | ||
``` | ||
|
||
Output: | ||
```sh | ||
User: myuser | ||
FOO: bar | ||
``` |