Skip to content

Commit

Permalink
Review usage of arrow.get() for Arrow 0.15.0
Browse files Browse the repository at this point in the history
- Reduce the number of uses of arrow.get() related to spans in reports.
- Unify catched exceptions when calling arrow.get().
- Include 'Frame.updated_at' parsing inside try/catch just like
start/stop.
  • Loading branch information
davidag authored and jmaupetit committed Sep 18, 2019
1 parent 581a107 commit 297fd7f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Improve Arrow 0.15.0 support after changes in `arrow.get()` behavior (#296)

### Fixed

- Stylize prompt to create new project or tag (#310).
Expand Down
10 changes: 5 additions & 5 deletions watson/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def convert(self, value, param, ctx):
if value:
try:
date = arrow.get(value)
except arrow.parser.ParserError as e:
except (ValueError, TypeError) as e:
raise click.UsageError(str(e))
# When we parse a date, we want to parse it in the timezone
# expected by the user, so that midnight is midnight in the local
Expand Down Expand Up @@ -630,7 +630,7 @@ def _final_print(lines):
if aggregated:
_print(u'{} - {}'.format(
style('date', '{:ddd DD MMMM YYYY}'.format(
arrow.get(report['timespan']['from'])
report['timespan']['from']
)),
style('time', '{}'.format(format_timedelta(
datetime.timedelta(seconds=report['time'])
Expand All @@ -640,10 +640,10 @@ def _final_print(lines):
else:
_print(u'{} -> {}\n'.format(
style('date', '{:ddd DD MMMM YYYY}'.format(
arrow.get(report['timespan']['from'])
report['timespan']['from']
)),
style('date', '{:ddd DD MMMM YYYY}'.format(
arrow.get(report['timespan']['to'])
report['timespan']['to']
))
))

Expand Down Expand Up @@ -1244,7 +1244,7 @@ def edit(watson, confirm_new_project, confirm_new_tag, id):
# break out of while loop and continue execution of
# the edit function normally
break
except (ValueError, RuntimeError) as e:
except (ValueError, TypeError, RuntimeError) as e:
click.echo(u"Error while parsing inputted values: {}".format(e),
err=True)
except KeyError:
Expand Down
12 changes: 6 additions & 6 deletions watson/frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ def __new__(cls, start, stop, project, id, tags=None, updated_at=None,):

if not isinstance(stop, arrow.Arrow):
stop = arrow.get(stop)
except RuntimeError as e:

if updated_at is None:
updated_at = arrow.utcnow()
elif not isinstance(updated_at, arrow.Arrow):
updated_at = arrow.get(updated_at)
except (ValueError, TypeError) as e:
from .watson import WatsonError
raise WatsonError(u"Error converting date: {}".format(e))

start = start.to('local')
stop = stop.to('local')

if updated_at is None:
updated_at = arrow.utcnow()
elif not isinstance(updated_at, arrow.Arrow):
updated_at = arrow.get(updated_at)

if tags is None:
tags = []

Expand Down
8 changes: 2 additions & 6 deletions watson/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,8 @@ def flatten_report_for_csv(report):
of the report.
"""
result = []
datetime_from = arrow.get(
report['timespan']['from']
).format('YYYY-MM-DD HH:mm:ss')
datetime_to = arrow.get(
report['timespan']['to']
).format('YYYY-MM-DD HH:mm:ss')
datetime_from = report['timespan']['from'].format('YYYY-MM-DD HH:mm:ss')
datetime_to = report['timespan']['to'].format('YYYY-MM-DD HH:mm:ss')
for project in report['projects']:
result.append({
'from': datetime_from,
Expand Down
6 changes: 4 additions & 2 deletions watson/watson.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,11 @@ def _load_json_file(self, filename, type=dict):
)

def _parse_date(self, date):
"""Returns Arrow object from timestamp."""
return arrow.Arrow.utcfromtimestamp(date).to('local')

def _format_date(self, date):
"""Returns timestamp from string timestamp or Arrow object."""
if not isinstance(date, arrow.Arrow):
date = arrow.get(date)

Expand Down Expand Up @@ -494,8 +496,8 @@ def report(self, from_, to, current=None, projects=None, tags=None,

report = {
'timespan': {
'from': str(span.start),
'to': str(span.stop),
'from': span.start,
'to': span.stop,
},
'projects': []
}
Expand Down

0 comments on commit 297fd7f

Please sign in to comment.