-
Notifications
You must be signed in to change notification settings - Fork 338
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
fix for issue/217-Specifying-asterisk-as-query-causes-error #250
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,62 @@ | ||
import os | ||
import re | ||
import sys | ||
import locale | ||
import codecs | ||
import argparse | ||
from datetime import datetime, timedelta | ||
from dateutil.parser import parse | ||
from dateutil.tz import tzutc | ||
|
||
import boto3 | ||
from botocore.client import ClientError | ||
from botocore.compat import total_seconds | ||
from termcolor import colored | ||
|
||
from . import exceptions | ||
from .core import AWSLogs | ||
from ._version import __version__ | ||
|
||
|
||
def regex_str(s): | ||
"""Verifies that the s is a valid python regex | ||
if s is not a valid regex then an exception is raised""" | ||
try: | ||
re.compile(s) | ||
except Exception as e: | ||
raise exceptions.InvalidPythonRegularExpressionError('Log stream name pattern', s) | ||
|
||
return s | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Usually the return value/what is passed around after handling the CLI would be the compiled regex - it may also speed up the code if it the code using it is called more than once, as the cache use isn't free. |
||
|
||
|
||
def seconds_since_epoch(datetime_text): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Milliseconds since epoch? Good change 👍 |
||
"""Parse ``datetime_text`` into a seconds since epoch.""" | ||
|
||
if not datetime_text: | ||
return None | ||
|
||
ago_regexp = r'(\d+)\s?(m|minute|minutes|h|hour|hours|d|day|days|w|weeks|weeks)(?: ago)?' | ||
ago_match = re.match(ago_regexp, datetime_text) | ||
|
||
if ago_match: | ||
amount, unit = ago_match.groups() | ||
amount = int(amount) | ||
unit = {'m': 60, 'h': 3600, 'd': 86400, 'w': 604800}[unit[0]] | ||
date = datetime.utcnow() + timedelta(seconds=unit * amount * -1) | ||
else: | ||
try: | ||
date = parse(datetime_text) | ||
except ValueError: | ||
raise exceptions.UnknownDateError(datetime_text) | ||
|
||
if date.tzinfo: | ||
if date.utcoffset != 0: | ||
date = date.astimezone(tzutc()) | ||
date = date.replace(tzinfo=None) | ||
|
||
return int(total_seconds(date - datetime(1970, 1, 1))) * 1000 | ||
|
||
|
||
def main(argv=None): | ||
|
||
if sys.version_info < (3, 0): | ||
|
@@ -57,13 +101,13 @@ def add_common_arguments(parser): | |
|
||
def add_date_range_arguments(parser, default_start='5m'): | ||
parser.add_argument("-s", "--start", | ||
type=str, | ||
type=seconds_since_epoch, | ||
dest='start', | ||
default=default_start, | ||
help="Start time (default %(default)s)") | ||
|
||
parser.add_argument("-e", "--end", | ||
type=str, | ||
type=seconds_since_epoch, | ||
dest='end', | ||
help="End time") | ||
|
||
|
@@ -81,7 +125,7 @@ def add_date_range_arguments(parser, default_start='5m'): | |
help="log group name") | ||
|
||
get_parser.add_argument("log_stream_name", | ||
type=str, | ||
type=regex_str, | ||
default="ALL", | ||
nargs='?', | ||
help="log stream name") | ||
|
@@ -167,10 +211,9 @@ def add_date_range_arguments(parser, default_start='5m'): | |
type=str, | ||
help="log group name") | ||
|
||
# Parse input | ||
options, args = parser.parse_known_args(argv) | ||
|
||
try: | ||
# Parse input | ||
options, args = parser.parse_known_args(argv) | ||
logs = AWSLogs(**vars(options)) | ||
if not hasattr(options, 'func'): | ||
parser.print_help() | ||
|
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 doesn't mention how the regex is used. It is used in
.match(…)
, rather than say .fullmatch(…) (which I might prefer but have no strong opinion†) ,or.search(…)
(which it sounds like that's how people imagine it being used).† changing from
.match(…)
would be a breaking change (dropping the^
but still using.match(…)
is not as it acts exactly the same) so should be a major version change+in release notes