Skip to content

Commit

Permalink
binary: fix boolean environment variable parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
subnut committed Mar 29, 2023
1 parent 967be08 commit 6489f0a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
36 changes: 23 additions & 13 deletions binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,35 @@
from simple_websocket_server import WebSocket
from simple_websocket_server import WebSocketServer

BUILD_VERSION: str = "v0.5.0"
BUILD_VERSION: str = "v0.5.1"

WINDOWS: bool = os.name == "nt"
LOCALHOST: str = "127.0.0.1" if WINDOWS else "localhost"
SUPER_QUIET: bool = bool(os.environ.get("NVIM_GHOST_SUPER_QUIET", False))
SERVER_PORT: str = os.environ.get("GHOSTTEXT_SERVER_PORT", "4001")
FOCUSED_NVIM_ADDRESS = os.environ.get("NVIM_LISTEN_ADDRESS", None)
LOGGING_ENABLED: bool = False
VERBOSE_LOGGING: bool = bool(os.environ.get("NVIM_GHOST_VERBOSE_LOGGING"))
if os.environ.get("NVIM_GHOST_LOGGING_ENABLED") is not None:
if os.environ.get("NVIM_GHOST_LOGGING_ENABLED").isdigit():
LOGGING_ENABLED = bool(int(os.environ.get("NVIM_GHOST_LOGGING_ENABLED")))
else:
sys.exit("Invalid value of $NVIM_GHOST_LOGGING_ENABLED")


def envbool(envvar: str, default: str = False) -> bool:
val = os.environ.get(envvar)
if val is None:
return False
if val.isdigit():
val = int(val)
if val in (0, 1):
return bool(val)
raise ValueError(
f"The environment variable '{envvar}', if set, should be set to either 0 or 1"
)


AUTOEXIT: bool = envbool("NVIM_GHOST_AUTO_EXIT")
SUPER_QUIET: bool = envbool("NVIM_GHOST_SUPER_QUIET")
LOGGING_ENABLED: bool = envbool("NVIM_GHOST_LOGGING_ENABLED")
VERBOSE_LOGGING: bool = envbool("NVIM_GHOST_VERBOSE_LOGGING")

FOCUSED_NVIM_ADDRESS = os.environ.get("NVIM_LISTEN_ADDRESS", None)
NVIM_ADDRESSES = [FOCUSED_NVIM_ADDRESS] if FOCUSED_NVIM_ADDRESS is not None else []

SERVER_PORT: str = os.environ.get("GHOSTTEXT_SERVER_PORT", "4001")
if not SERVER_PORT.isdigit():
if FOCUSED_NVIM_ADDRESS is not None:
with pynvim.attach("socket", path=FOCUSED_NVIM_ADDRESS) as nvim_handle:
Expand All @@ -45,9 +58,6 @@
sys.exit("Port must be a number")
GHOST_PORT: int = int(SERVER_PORT)

AUTOEXIT: bool = bool(os.environ.get("NVIM_GHOST_AUTO_EXIT"))
NVIM_ADDRESSES = [FOCUSED_NVIM_ADDRESS] if FOCUSED_NVIM_ADDRESS is not None else []

# chdir to folder containing binary
# otherwise the logs are generated whereever the server was started from (i.e curdir)
# which..... isn't good. You'd have stdout.log and stderr.log files everywhere!
Expand Down
2 changes: 1 addition & 1 deletion binary_version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.5.0
v0.5.1

0 comments on commit 6489f0a

Please sign in to comment.