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

(#92) Enable shell-like syntax in extra_args from file #93

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Mainframe3270/x3270.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import re
import shlex
import socket
import time
from datetime import timedelta
Expand Down Expand Up @@ -64,12 +65,13 @@ def open_connection(
`extra_args` accepts either a list or a path to a file containing [https://x3270.miraheze.org/wiki/Category:Command-line_options|x3270 command line options].

Entries in the argfile can be on one line or multiple lines. Lines starting with "#" are considered comments.
Arguments containing whitespace must be enclosed in single or double quotes.

| # example_argfile_oneline.txt
| -accepthostname myhost.com

| # example_argfile_multiline.txt
| -accepthostname myhost.com
| -xrm "wc3270.acceptHostname: myhost.com"
| # this is a comment
| -charset french
| -port 992
Expand Down Expand Up @@ -117,7 +119,7 @@ def _process_args(self, args) -> list:
for line in file:
if line.lstrip().startswith("#"):
continue
for arg in line.replace("\n", "").rstrip().split(" "):
for arg in shlex.split(line):
processed_args.append(arg)
return processed_args

Expand Down
2 changes: 1 addition & 1 deletion utest/x3270/resources/argfile_oneline.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
-charset german
-charset german -xrm "*acceptHostname: myhost.com" -xrm '*blankFill: true'
26 changes: 20 additions & 6 deletions utest/x3270/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,19 @@ def test_open_connection_with_extra_args_oneline(mocker: MockerFixture):
"Mainframe3270.py3270.Emulator.__init__", return_value=None
)
mocker.patch("Mainframe3270.py3270.Emulator.connect")
extra_args = os.path.join(CURDIR, "resources/argfile_oneline.txt")
extra_args = os.path.join(CURDIR, "resources", "argfile_oneline.txt")

under_test = x3270(**X3270_DEFAULT_ARGS)
under_test.open_connection("myhost", extra_args=extra_args)

args_from_file = ["-charset", "german"]
args_from_file = [
"-charset",
"german",
"-xrm",
"*acceptHostname: myhost.com",
"-xrm",
"*blankFill: true",
]
m_emulator.assert_called_with(True, 30.0, args_from_file)


Expand All @@ -87,12 +94,19 @@ def test_open_connection_none_windows_extra_args_oneline(
"Mainframe3270.py3270.Emulator.__init__", return_value=None
)
mocker.patch("Mainframe3270.py3270.Emulator.connect")
extra_args = os.path.join(CURDIR, "resources/argfile_oneline.txt")
extra_args = os.path.join(CURDIR, "resources", "argfile_oneline.txt")

under_test = x3270(**X3270_DEFAULT_ARGS)
under_test.open_connection("myhost", extra_args=extra_args)

args_from_file = ["-charset", "german"]
args_from_file = [
"-charset",
"german",
"-xrm",
"*acceptHostname: myhost.com",
"-xrm",
"*blankFill: true",
]
m_emulator.assert_called_with(True, 30.0, args_from_file)


Expand All @@ -102,7 +116,7 @@ def test_open_connection_with_extra_args_multiline(mocker: MockerFixture):
"Mainframe3270.py3270.Emulator.__init__", return_value=None
)
mocker.patch("Mainframe3270.py3270.Emulator.connect")
extra_args = os.path.join(CURDIR, "resources/argfile_multiline.txt")
extra_args = os.path.join(CURDIR, "resources", "argfile_multiline.txt")

under_test = x3270(**X3270_DEFAULT_ARGS)
under_test.open_connection("myhost", extra_args=extra_args)
Expand All @@ -117,7 +131,7 @@ def test_open_connection_with_extra_args_multiline_comments(mocker: MockerFixtur
"Mainframe3270.py3270.Emulator.__init__", return_value=None
)
mocker.patch("Mainframe3270.py3270.Emulator.connect")
extra_args = os.path.join(CURDIR, "resources/argfile_multiline_comments.txt")
extra_args = os.path.join(CURDIR, "resources", "argfile_multiline_comments.txt")

under_test = x3270(**X3270_DEFAULT_ARGS)
under_test.open_connection("myhost", extra_args=extra_args)
Expand Down