summaryrefslogtreecommitdiff
path: root/paramiko/config.py
diff options
context:
space:
mode:
authorJeff Forcier <jeff@bitprophet.org>2019-07-02 21:19:50 -0400
committerJeff Forcier <jeff@bitprophet.org>2019-08-26 20:48:25 -0400
commit77ea8aca1c78dcc1f00b675e3212d253018f3004 (patch)
tree7d306949b5c334387c713f98cb62875e5c3f1b1c /paramiko/config.py
parent5a56eed757c598301f1cc1f5a31e9d06aa081ac1 (diff)
downloadparamiko-77ea8aca1c78dcc1f00b675e3212d253018f3004.tar.gz
Add new SSHConfig constructors
Diffstat (limited to 'paramiko/config.py')
-rw-r--r--paramiko/config.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/paramiko/config.py b/paramiko/config.py
index af194452..f9ea02dc 100644
--- a/paramiko/config.py
+++ b/paramiko/config.py
@@ -49,9 +49,54 @@ class SSHConfig(object):
def __init__(self):
"""
Create a new OpenSSH config object.
+
+ Note: the newer alternate constructors `from_path`, `from_file` and
+ `from_text` are simpler to use, as they parse on instantiation. For
+ example, instead of::
+
+ config = SSHConfig()
+ config.parse(open("some-path.config")
+
+ you could::
+
+ config = SSHConfig.from_file(open("some-path.config"))
+ # Or more directly:
+ config = SSHConfig.from_path("some-path.config")
+ # Or if you have arbitrary ssh_config text from some other source:
+ config = SSHConfig.from_text("Host foo\\n\\tUser bar")
"""
self._config = []
+ @classmethod
+ def from_text(cls, text):
+ """
+ Create a new, parsed `SSHConfig` from ``text`` string.
+
+ .. versionadded:: 2.7
+ """
+ return cls.from_file(StringIO(text))
+
+ @classmethod
+ def from_path(cls, path):
+ """
+ Create a new, parsed `SSHConfig` from the file found at ``path``.
+
+ .. versionadded:: 2.7
+ """
+ with open(path) as flo:
+ return cls.from_file(flo)
+
+ @classmethod
+ def from_file(cls, flo):
+ """
+ Create a new, parsed `SSHConfig` from file-like object ``flo``.
+
+ .. versionadded:: 2.7
+ """
+ obj = cls()
+ obj.parse(flo)
+ return obj
+
def parse(self, file_obj):
"""
Read an OpenSSH config from the given file object.