Skip to content

Commit

Permalink
commands: remove content includign and after # to support commments
Browse files Browse the repository at this point in the history
Closes #17.
  • Loading branch information
eras committed Mar 8, 2023
1 parent b7a3854 commit 01145ab
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ You can use e.g. `screen`, `tmux` or `systemd` to arrange this process to run on
Note that by default you need to prefix commands with ```!```.

| command | description |
| --- | --- |
|---------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------|
| help | Show the list of commands supported. |
| authorize | Start the authorization flow. Works only in admin room (though you could only have one and same for control and admin). |
| authorize url | Last phase of the authorization flow. |
Expand Down Expand Up @@ -168,3 +168,5 @@ Note that by default you need to prefix commands with ```!```.
| charge schedule disable | Disable charging schedule. |
| heater seat n off/low/medium/high | Adjust seat heaters. Works only if AC is on. |
| heater steering off/high | Adjust steering wheel heater. Works only if AC is on. |
| command # comment | Run command; ignore # comment |
| # comment | Ignore message |
8 changes: 5 additions & 3 deletions teslabot/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from abc import ABC, abstractmethod
from typing import List, Callable, Coroutine, Any, TypeVar, Generic, Optional, Tuple, Mapping, Union, Awaitable
from .parser import Parser, ParseResult, ParseOK, ParseFail
from .utils import assert_some

from . import log

Expand All @@ -20,7 +21,7 @@ class CommandsException(Exception):
class ParseError(CommandsException):
pass

class InvocationParseError(ParseError):
class InvocationEmptyError(ParseError):
pass

@dataclass
Expand All @@ -45,13 +46,14 @@ class Invocation:

@staticmethod
def parse(message: str) -> "Invocation":
fields = [field for field in re.split(r" *", message) if field != ""]
command = assert_some(re.match(r"^[^#]*", message), "This should always match")[0] # extract non-comment part
fields = [field for field in re.split(r" *", command) if field != ""]
if len(fields):
logger.debug(f"Command: {fields}")
return Invocation(name=fields[0],
args=fields[1:])
else:
raise InvocationParseError()
raise InvocationEmptyError()

class Command(ABC, Generic[Context]):
name: str
Expand Down
2 changes: 2 additions & 0 deletions teslabot/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ async def process_message(self, command_context: CommandContext, message: str) -
await self.local_commands.invoke(command_context, invocation)
else:
await self.callback.command_callback(command_context, invocation)
except commands.InvocationEmptyError as exn:
logger.debug("Ignoring empty message (or completely commented)")
except commands.CommandParseError as exn:
logger.error(f"{command_context.txn}: Failed to parse command: {message}")
def format(word: str, highlight: bool) -> str:
Expand Down

0 comments on commit 01145ab

Please sign in to comment.