-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
93 change regex command #94
Conversation
Codecov Report
@@ Coverage Diff @@
## master #94 +/- ##
==========================================
+ Coverage 98.58% 98.62% +0.04%
==========================================
Files 69 69
Lines 2117 2181 +64
==========================================
+ Hits 2087 2151 +64
Misses 30 30
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reckon we should merge #91 first to forgo the complexities of performing str<->bytes
conversion for regex
@@ -12,16 +20,17 @@ class Command(Protocol): | |||
|
|||
#: A flag which indicates whether calling of the method should trigger an interrupt | |||
interrupt: bool | |||
# format: Optional[str] = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# format: Optional[str] = None |
|
||
@abstractmethod | ||
def parse(self, data: bytes) -> Optional[Sequence[AnyStr]]: | ||
def parse(self, data: AnyStr) -> Optional[Tuple[Sequence[AnyStr], int, int, int]]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels like Tuple[Sequence[AnyStr], int, int, int]
should be given a NewType
declaration or even a dataclass
message_end, | ||
) = parse_result | ||
if match_end != message_end: | ||
# We want the whole (formatted) message to match a command |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documentation of the whole / complete would be good to have in the class docstring
# can we use signature instead to more cleverly do conversions? i.e. if | ||
# type hints aren't available just pass on with no conversion? Otherwise | ||
# we can get unhelpful error messages. | ||
# Also, should we deal with str/bytes conversions seperately? | ||
# Create a mapping function that does conversion rather than argtype(arg)? | ||
# It would take args and inspect.Parameters as parameters? | ||
# Is type hints enough? - doesn't know about non-hinted vars |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is definitely worth investigating at some point. My current thoughts are:
- The case of no type hints should be accounted for, either by raising an error or dumping the raw
AnyStr
on the user - Due to the complexity of
bytes<->string
it makes most sense to dump the rawAnyStr
on the user and let them deal with it, the complexity of passing through encodings / decodings seems unnecessary when the user can do it in their endpoint. - A mapping function would be nice if we could come up with one that satisfied users needs across the board, but I frankly don't see that happening in a way that's better than using the default casts of the types
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think discussions like this are better had in new issues than in code comments/PRs. Can we remove the code in the comment and make a new issue?
@@ -36,24 +36,50 @@ def __call__(self, func: Callable) -> Callable: | |||
setattr(func, "__command__", self) | |||
return func | |||
|
|||
def parse(self, data: bytes) -> Optional[Sequence[AnyStr]]: | |||
def parse(self, data: AnyStr) -> Optional[Tuple[Sequence[AnyStr], int, int, int]]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be drastically simplified by removing all encoding / decoding if we merge #91 first
Closing this as not planned. When it becomes desired again raise a new issue, linked to this #177. |
Requires #89
Fixes #92 and #93
Required for #90
RegexCommand
to parse from data of typeAnyStr
rather than justbytes
.RegexCommand.parse
now also returns the start and end indices of the match within the message, as well as the length of the message matched against. This gives enough information forMultiCommandInterprerter
to strip off a matched command and forCommandInterpreter
to check for a full match.CommandInterpreter.handle
logic updated to use the new return signature.RegexCommand
andCommandInterpreter