Merge pull request #3078 from kajojify/expanduser

Fix #3002. Auto-expanding for tilda.
This commit is contained in:
Aldo Cortesi 2018-04-26 21:02:10 +12:00 committed by GitHub
commit a4a48a96d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 10 additions and 5 deletions

View File

@ -1,6 +1,8 @@
import io import io
import csv import csv
import typing import typing
import os.path
from mitmproxy import command from mitmproxy import command
from mitmproxy import exceptions from mitmproxy import exceptions
from mitmproxy import flow from mitmproxy import flow
@ -87,7 +89,8 @@ class Cut:
append = False append = False
if path.startswith("+"): if path.startswith("+"):
append = True append = True
path = mitmproxy.types.Path(path[1:]) epath = os.path.expanduser(path[1:])
path = mitmproxy.types.Path(epath)
try: try:
if len(cuts) == 1 and len(flows) == 1: if len(cuts) == 1 and len(flows) == 1:
with open(path, "ab" if append else "wb") as fp: with open(path, "ab" if append else "wb") as fp:

View File

@ -10,7 +10,6 @@ The View:
""" """
import collections import collections
import typing import typing
import os
import blinker import blinker
import sortedcontainers import sortedcontainers
@ -359,9 +358,8 @@ class View(collections.Sequence):
""" """
Load flows into the view, without processing them with addons. Load flows into the view, without processing them with addons.
""" """
spath = os.path.expanduser(path)
try: try:
with open(spath, "rb") as f: with open(path, "rb") as f:
for i in io.FlowReader(f).stream(): for i in io.FlowReader(f).stream():
# Do this to get a new ID, so we can load the same file N times and # Do this to get a new ID, so we can load the same file N times and
# get new flows each time. It would be more efficient to just have a # get new flows each time. It would be more efficient to just have a

View File

@ -178,7 +178,7 @@ class _PathType(_BaseType):
return ret return ret
def parse(self, manager: _CommandBase, t: type, s: str) -> str: def parse(self, manager: _CommandBase, t: type, s: str) -> str:
return s return os.path.expanduser(s)
def is_valid(self, manager: _CommandBase, typ: typing.Any, val: typing.Any) -> bool: def is_valid(self, manager: _CommandBase, typ: typing.Any, val: typing.Any) -> bool:
return isinstance(val, str) return isinstance(val, str)

View File

@ -2,6 +2,7 @@ import pytest
import os import os
import typing import typing
import contextlib import contextlib
from unittest import mock
import mitmproxy.exceptions import mitmproxy.exceptions
import mitmproxy.types import mitmproxy.types
@ -68,7 +69,10 @@ def test_path(tdata):
b = mitmproxy.types._PathType() b = mitmproxy.types._PathType()
assert b.parse(tctx.master.commands, mitmproxy.types.Path, "/foo") == "/foo" assert b.parse(tctx.master.commands, mitmproxy.types.Path, "/foo") == "/foo"
assert b.parse(tctx.master.commands, mitmproxy.types.Path, "/bar") == "/bar" assert b.parse(tctx.master.commands, mitmproxy.types.Path, "/bar") == "/bar"
with mock.patch.dict("os.environ", {"HOME": "/home/test"}):
assert b.parse(tctx.master.commands, mitmproxy.types.Path, "~/mitm") == "/home/test/mitm"
assert b.is_valid(tctx.master.commands, mitmproxy.types.Path, "foo") is True assert b.is_valid(tctx.master.commands, mitmproxy.types.Path, "foo") is True
assert b.is_valid(tctx.master.commands, mitmproxy.types.Path, "~/mitm") is True
assert b.is_valid(tctx.master.commands, mitmproxy.types.Path, 3) is False assert b.is_valid(tctx.master.commands, mitmproxy.types.Path, 3) is False
def normPathOpts(prefix, match): def normPathOpts(prefix, match):