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

Features/env, source and exec commands + basic unix pipelines #73

Merged
merged 29 commits into from
Aug 30, 2016

Conversation

fogine
Copy link
Contributor

@fogine fogine commented Aug 21, 2016

Solves #70

Adds new commands: env, source, exec
Adds output redirection feature:

> post query==value > /tmp/request.log # writes response to the file
> post query==value >> /tmp/request.log # appends response to the file ( or creates a new file if it does not exists)
> post query==value | tee [-a,--append] /tmp/request.log # appends or writes to the files and simultaneously prints the response to a console

Changed behavior of automatic context saving... It saves only current session to single file. It's like backup of a unsaved session.

Covered by tests
Readme file with starting guide updated... so it reflects the new features

fogine added 22 commits August 16, 2016 17:39
…ve the last environment state to a single file)

- Added handling of FileNotFoundError
…mand output logging to a console & writing to a file
@eliangcs
Copy link
Contributor

eliangcs commented Aug 22, 2016

@fogine This is a really awesome work! Thank you for putting the effort here. This PR will take me a while to do a full review. However, I have some initial thoughts here.

  1. Anything that comes after a pipe (for example, post | tee -a file.log) should be handed over to the native shell. Handling tee in the PEG grammar and ExecutionVisitor seems not flexible. I think we can implement the pipe feature in PR Add shell command integration #61 or so on, so I suggest to remove the pipe syntax for now.

  2. There should a more general implementation than doing if-else to determine your output redirection target. stdout and files alike, are all output streams, we could hold an output stream instance in ExecutionVisitor, and switch the output stream based on the user's command. Example code:

class ExecutionVisitor(NodeVisitor):

    def __init__(self, context, listener=None):
        # ...
        self.output = sys.stdout  # Defaults to stdout

    def visit_redir_file(self, node, children):
        path = ...  # Get destination file path from node
        self.output = open(path, 'w')
  1. Lint your code with flake8.

@fogine
Copy link
Contributor Author

fogine commented Aug 22, 2016

  1. Agreed. Just wanted to have something until shell commands are available. I'll remove the feature and look at the Add shell command integration #61 eventually.
  2. Cool. I haven't known this design applies in python to be honest, I haven't written that much code before in python. I'll change the design.
  3. my bad

I'll try to make the changes later today. Thank you for the feedback @eliangcs

import click


class Printer():
Copy link
Contributor Author

Choose a reason for hiding this comment

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

if it's not necessary to use the click library due to compatibility reasons, we can write directly to stdout.... I guess it's better this way...

Copy link
Contributor

@eliangcs eliangcs Aug 23, 2016

Choose a reason for hiding this comment

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

@fogine Instead of using sys.stdout directly, I think you should use click.echo_via_pager whenever you want to write to stdout. Sorry, it was my bad in the example code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@eliangcs , no problem, it's already using click.echo_via_pager, I was just stressing it out in case there would be better solution and I haven't known about it... never mind...

@eliangcs
Copy link
Contributor

eliangcs commented Aug 25, 2016

@fogine I got an error on the env command. Could you fix and add a test for it? Thanks!

http://localhost> env
TypeError: echo_via_pager() got an unexpected keyword argument 'nl'

Parse tree:
<Node called "env" matching "env">  <-- *** We were here. ***
    <RegexNode called "_" matching "">
    <Node matching "env">
    <RegexNode called "_" matching "">
    <Node matching "">

However, when I use env with a redirection, such as env > file.txt, it works as expected.

@@ -10,14 +10,17 @@

from http_prompt.context import Context
from http_prompt.execution import execute
from http_prompt.xdg import get_tmp_dir
Copy link
Contributor

@eliangcs eliangcs Aug 25, 2016

Choose a reason for hiding this comment

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

Python already has a tempfile module that can be used to manage temporary files and directories. So writing a get_tmp_dir in http_prompt.xdg is not really necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've noticed this recently and haven't had time to rewrite this yet... I will.

@fogine
Copy link
Contributor Author

fogine commented Aug 25, 2016

@eliangcs, what's the output of the httpie command when you get the error?
I don't have currently access to a computer. I can fix it at the beginning of next week.

@eliangcs
Copy link
Contributor

@fogine

http://localhost> httpie
TypeError: echo_via_pager() got an unexpected keyword argument 'nl'

Parse tree:
<Node called "immutation" matching "httpie">  <-- *** We were here. ***
    <Node called "preview" matching "httpie">
        <RegexNode called "_" matching "">
        <Node called "tool" matching "httpie">
            <Node matching "httpie">
        <RegexNode called "_" matching "">
        <Node matching "">
        <Node matching "">
        <Node matching "">
        <Node matching "">
        <RegexNode called "_" matching "">
http://localhost> env
TypeError: echo_via_pager() got an unexpected keyword argument 'nl'

Parse tree:
<Node called "env" matching "env">  <-- *** We were here. ***
    <RegexNode called "_" matching "">
    <Node matching "env">
    <RegexNode called "_" matching "">
    <Node matching "">

I think the error happens here: https://github.com/eliangcs/http-prompt/pull/73/files#diff-14f61a2e4ef220b7fe763cda57fec637R17, where click.echo_via_pager doesn't have a keyword argument named nl.

fogine added 2 commits August 29, 2016 08:58
…g` module so that the tests use the `TempAppDirTestCase` module
@eliangcs
Copy link
Contributor

@fogine Thanks for fixing the nl argument problem. I just pulled and tested it. When I submit a request using post or get command, the output is colorless. Could you check on that?

@fogine
Copy link
Contributor Author

fogine commented Aug 30, 2016

@eliangcs , fixed & test case added...

@eliangcs eliangcs merged commit b66f2a1 into httpie:master Aug 30, 2016
@eliangcs
Copy link
Contributor

Merged. Thanks for your contribution, @fogine! I'd like to refactor the code a bit, but I guess I can do that later.

@fogine
Copy link
Contributor Author

fogine commented Aug 30, 2016

I'd like to refactor the code a bit, but I guess I can do that later.

No problem. I'm interested in seeing how I can improve my possible future contributions.

Thank you for your support during the development, @eliangcs !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants