Skip to content
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

Support CSV output where JSON is allowed #281

Merged
merged 1 commit into from
Jun 14, 2019

Conversation

davidag
Copy link
Contributor

@davidag davidag commented May 9, 2019

In response to #256, which seems like an interesting use case to me, I've started to implement CSV format support in the same places where JSON is currently allowed (log/report/aggregate).

Some things are still missing, but I've created the PR to get early feedback (as suggested in the guidelines :).

TODO

  • MutuallyExclusiveOption prints an invalid error message when specifying an option internal name, which is the case of -j/--json (i.e. format_json).
  • Decide whether --format should be added (as discussed on Support JSON output in report command #102).
    • After solving the previous point, it seems OK to have two different options for each of the two formats (i.e. json and csv), given that the plain format is the default and is hidden from the user.
  • Add tests
  • Document new command option(s) in cli.py
  • Add docstrings to new functions in utils.py

Cli options

  • Output formats for commands log, report and aggregate:
$ watson cmd --csv   # new: outputs a csv text as described below
$ watson cmd --json  # old: outputs a json in the same format as before
$ watson cmd --plain # new: this option is hidden in help/docs
                     # because it's the default output format
  • Handling mutually exclusive options:
$ watson cmd --csv --json
Error: The following options are mutually exclusive: `--json`, `--csv`

$ watson report --luna --week
Error: The following options are mutually exclusive: `--all`, `--luna`, `--year`, `--week`, `--month`, `--day`

CSV format

Design decisions

  • The generated output should contain, explicitly or implicitly, the same data available in the plain text format.
  • Python csv classes are instantiated with the default dialect, i.e. Excel.
  • Dates and times are formatted in a way that Excel can handle them (yyyy-MM-dd HH:mm:ss according to this link)

Examples

$ watson log --csv

id,start,stop,project,tags
b19d6b,2019-05-02 11:00:00,2019-05-02 13:02:34,project-2,
0ba222,2019-05-02 13:23:15,2019-05-02 14:31:25,project-2,
95c58c,2019-05-02 15:27:26,2019-05-02 20:47:36,project-2,
1f6adf,2019-05-06 15:37:38,2019-05-06 16:07:08,project-1,"tag-1, tag-2"
733381,2019-05-06 23:01:08,2019-05-06 23:26:37,project-3,"tag-3, tag-5"
e33ed7,2019-05-07 11:47:40,2019-05-07 14:06:50,project-3,"tag-3, tag-5"
1b2b89,2019-05-07 18:39:54,2019-05-07 18:57:59,project-4,"tag-5, tag-6, tag-7"
f9beb5,2019-05-07 19:14:14,2019-05-07 19:47:08,project-5,"tag-3, tag-5"
ed494a,2019-05-08 12:45:35,2019-05-08 13:00:37,project-4,"tag-5, tag-6, tag-7"
02da6f,2019-05-09 10:21:27,2019-05-09 13:19:22,project-3,"tag-3, tag-5"
45351e,2019-05-09 16:08:15,2019-05-09 19:23:15,project-3,"tag-3, tag-5"
4616a7,2019-05-09 19:13:15,2019-05-09 19:23:23,project-1,tag-1
8f4121,2019-05-09 19:23:44,2019-05-09 19:23:49,project-1,tag-2
980336,2019-05-09 19:30:16,2019-05-09 19:33:19,project-3,

$ watson report --csv

from,to,project,tag,time
2019-05-02 00:00:00,2019-05-09 23:59:59,project-1,,2383.0
2019-05-02 00:00:00,2019-05-09 23:59:59,project-1,tag-1,2378.0
2019-05-02 00:00:00,2019-05-09 23:59:59,project-1,tag-2,1775.0
2019-05-02 00:00:00,2019-05-09 23:59:59,project-2,,30654.0
2019-05-02 00:00:00,2019-05-09 23:59:59,project-3,,32437.0
2019-05-02 00:00:00,2019-05-09 23:59:59,project-3,tag-3,32254.0
2019-05-02 00:00:00,2019-05-09 23:59:59,project-3,tag-5,32254.0
2019-05-02 00:00:00,2019-05-09 23:59:59,project-4,,1987.0
2019-05-02 00:00:00,2019-05-09 23:59:59,project-4,tag-5,1987.0
2019-05-02 00:00:00,2019-05-09 23:59:59,project-4,tag-6,1987.0
2019-05-02 00:00:00,2019-05-09 23:59:59,project-4,tag-7,1987.0
2019-05-02 00:00:00,2019-05-09 23:59:59,project-5,,1974.0
2019-05-02 00:00:00,2019-05-09 23:59:59,project-5,tag-3,1974.0
2019-05-02 00:00:00,2019-05-09 23:59:59,project-5,tag-5,1974.0

$ watson aggregate --csv

from,to,project,tag,time
2019-05-02 00:00:00,2019-05-02 23:59:59,project-2,,30654.0
2019-05-06 00:00:00,2019-05-06 23:59:59,project-1,,1770.0
2019-05-06 00:00:00,2019-05-06 23:59:59,project-1,tag-1,1770.0
2019-05-06 00:00:00,2019-05-06 23:59:59,project-1,tag-2,1770.0
2019-05-06 00:00:00,2019-05-06 23:59:59,project-3,,1529.0
2019-05-06 00:00:00,2019-05-06 23:59:59,project-3,tag-3,1529.0
2019-05-06 00:00:00,2019-05-06 23:59:59,project-3,tag-5,1529.0
2019-05-07 00:00:00,2019-05-07 23:59:59,project-3,,8350.0
2019-05-07 00:00:00,2019-05-07 23:59:59,project-3,tag-3,8350.0
2019-05-07 00:00:00,2019-05-07 23:59:59,project-3,tag-5,8350.0
2019-05-07 00:00:00,2019-05-07 23:59:59,project-4,,1085.0
2019-05-07 00:00:00,2019-05-07 23:59:59,project-4,tag-5,1085.0
2019-05-07 00:00:00,2019-05-07 23:59:59,project-4,tag-6,1085.0
2019-05-07 00:00:00,2019-05-07 23:59:59,project-4,tag-7,1085.0
2019-05-07 00:00:00,2019-05-07 23:59:59,project-5,,1974.0
2019-05-07 00:00:00,2019-05-07 23:59:59,project-5,tag-3,1974.0
2019-05-07 00:00:00,2019-05-07 23:59:59,project-5,tag-5,1974.0
2019-05-08 00:00:00,2019-05-08 23:59:59,project-4,,902.0
2019-05-08 00:00:00,2019-05-08 23:59:59,project-4,tag-5,902.0
2019-05-08 00:00:00,2019-05-08 23:59:59,project-4,tag-6,902.0
2019-05-08 00:00:00,2019-05-08 23:59:59,project-4,tag-7,902.0
2019-05-09 00:00:00,2019-05-09 23:59:59,project-1,,613.0
2019-05-09 00:00:00,2019-05-09 23:59:59,project-1,tag-1,608.0
2019-05-09 00:00:00,2019-05-09 23:59:59,project-1,tag-2,5.0
2019-05-09 00:00:00,2019-05-09 23:59:59,project-3,,22558.0
2019-05-09 00:00:00,2019-05-09 23:59:59,project-3,tag-3,22375.0
2019-05-09 00:00:00,2019-05-09 23:59:59,project-3,tag-5,22375.0

@jmaupetit
Copy link
Contributor

jmaupetit commented May 17, 2019

Keep it up @davidag! 🙏

@davidag davidag changed the title WIP: Support CSV output where JSON is allowed Support CSV output where JSON is allowed May 24, 2019
@davidag
Copy link
Contributor Author

davidag commented May 25, 2019

@jmaupetit if you give me the okay, I can squash the commits to prepare the merge... or make changes if needed.

tests/test_utils.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@k4nar k4nar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nitpick, but LGTM otherwise 👍 .

@davidag
Copy link
Contributor Author

davidag commented Jun 4, 2019

@jmaupetit @k4nar I think there's something not working with Python 3.4, it looks related to Arrow compatibility:

ERROR: arrow requires Python '>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*' but the running Python is 3.4.6

Maybe Python 3.4 support should be dropped?

@k4nar
Copy link
Collaborator

k4nar commented Jun 4, 2019

@davidag : We should probably born arrow to <0.13.2 for users of python < 3.5 instead. Adding arrow<0.13.2;python_version<"3.5" to the install_requires part of the setup.py should do the trick.

@davidag
Copy link
Contributor Author

davidag commented Jun 4, 2019

@k4nar : I see how that can solve the issue, but I'm not sure I understand why Watson must still support Python 3.4, taking into account this message in travis:

DEPRECATION: Python 3.4 support has been deprecated. pip 19.1 will be the last one supporting it. Please upgrade your Python as Python 3.4 won't be maintained after March 2019 (cf PEP 429).

You can see other Python projects dropping support for Python 3.4 some months ago. For example, Pelican.

If you still want to support it, I suppose a different PR should be created.

@jmaupetit
Copy link
Contributor

Good point @davidag! I think we should drop support for python 3.4 in a new PR.

@davidag davidag force-pushed the csv-export branch 2 times, most recently from 847f97a to 9521d7a Compare June 13, 2019 18:14
@davidag
Copy link
Contributor Author

davidag commented Jun 13, 2019

@jmaupetit: Rebased, squashed and added a changelog entry. Ready to merge? 🎉

- New option --csv/-s in report, log and aggregate commands, mutually
exclusive with --json/-j.
- New hidden option --plain to handle the default output format in
report, aggregate and log commands (i.e. plain text). This option is
not intended to be used by the final user.
- Add support in MutuallyExclusiveOption for options with an internal
name different from the ones permitted in the command line.
@jmaupetit jmaupetit merged commit dc607b6 into jazzband:master Jun 14, 2019
@davidag davidag mentioned this pull request Aug 11, 2019
@jmaupetit jmaupetit mentioned this pull request Aug 26, 2019
@davidag davidag deleted the csv-export branch October 16, 2019 10:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants