當前位置: 首頁>>代碼示例>>Python>>正文


Python FS.cat方法代碼示例

本文整理匯總了Python中jnpr.junos.utils.fs.FS.cat方法的典型用法代碼示例。如果您正苦於以下問題:Python FS.cat方法的具體用法?Python FS.cat怎麽用?Python FS.cat使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在jnpr.junos.utils.fs.FS的用法示例。


在下文中一共展示了FS.cat方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: facts_domain

# 需要導入模塊: from jnpr.junos.utils.fs import FS [as 別名]
# 或者: from jnpr.junos.utils.fs.FS import cat [as 別名]
def facts_domain(junos, facts):
    """
    The following facts are required:
        facts['hostname']

    The following facts are assigned:
        facts['domain']
        facts['fqdn']
    """

    try:
        domain_filter_xml = E('configuration', E('system', E('domain-name')))
        domain = junos.rpc.get_config(filter_xml=domain_filter_xml,
                                      options=INHERIT)
        domain_name = domain.xpath('.//domain-name')
        if len(domain_name) > 0:
            facts['domain'] = domain_name[0].text
            facts['fqdn'] = facts['hostname'] + '.' + facts['domain']
            return
    except RpcError:
        pass

    fs = FS(junos)
    file_content = fs.cat('/etc/resolv.conf') or fs.cat('/var/etc/resolv.conf')
    words = file_content.split() if file_content is not None else ''
    if 'domain' not in words:
        facts['domain'] = None
        facts['fqdn'] = facts['hostname']
    else:
        idx = words.index('domain') + 1
        facts['domain'] = words[idx]
        facts['fqdn'] = facts['hostname'] + '.' + facts['domain']
開發者ID:GIC-de,項目名稱:py-junos-eznc,代碼行數:34,代碼來源:domain.py

示例2: get_facts

# 需要導入模塊: from jnpr.junos.utils.fs import FS [as 別名]
# 或者: from jnpr.junos.utils.fs.FS import cat [as 別名]
def get_facts(device):
    """
    Gathers domain facts from the configuration or /etc/resolv.conf.
    """
    domain_config = """
        <configuration>
            <system>
                <domain-name/>
             </system>
        </configuration>
    """
    domain = None
    fqdn = None
    # Try to read the domain-name from the config.
    # This might fail due to lack of permissions.
    try:
        rsp = device.rpc.get_config(filter_xml=etree.XML(domain_config),
                                    options={'database': 'committed',
                                             'inherit': 'inherit',
                                             'commit-scripts': 'apply', })
        domain = rsp.findtext('.//domain-name')
    # Ignore if user can't view the configuration.
    except PermissionError:
        pass

    # Try to read the domain from the resolv.conf file. This only requires
    # view permissions.
    if domain is None:
        fs = FS(device)
        file_content = (fs.cat('/etc/resolv.conf') or
                        fs.cat('/var/etc/resolv.conf'))
        words = file_content.split() if file_content is not None else []
        if 'domain' in words:
            idx = words.index('domain') + 1
            domain = words[idx]

    # Set the fqdn
    fqdn = device.facts['hostname']
    if fqdn is not None and domain is not None:
        fqdn = fqdn + '.' + domain

    return {'domain': domain,
            'fqdn': fqdn, }
開發者ID:GIC-de,項目名稱:py-junos-eznc,代碼行數:45,代碼來源:domain.py

示例3: facts_domain

# 需要導入模塊: from jnpr.junos.utils.fs import FS [as 別名]
# 或者: from jnpr.junos.utils.fs.FS import cat [as 別名]
def facts_domain(junos, facts):
    """
    The following facts are required:
        facts['hostname']

    The following facts are assigned:
        facts['domain']
        facts['fqdn']
    """
    fs = FS(junos)
    words = fs.cat('/etc/resolv.conf').split()
    if 'domain' not in words:
        facts['domain'] = None
        facts['fqdn'] = facts['hostname']
    else:
        idx = words.index('domain') + 1
        facts['domain'] = words[idx]
        facts['fqdn'] = facts['hostname'] + '.' + facts['domain']
開發者ID:RobWC,項目名稱:py-junos-eznc,代碼行數:20,代碼來源:domain.py

示例4: TestFS

# 需要導入模塊: from jnpr.junos.utils.fs import FS [as 別名]
# 或者: from jnpr.junos.utils.fs.FS import cat [as 別名]
class TestFS(unittest.TestCase):
    @patch("ncclient.manager.connect")
    def setUp(self, mock_connect):
        mock_connect.side_effect = self._mock_manager
        self.dev = Device(host="1.1.1.1", user="rick", password="password123", gather_facts=False)
        self.dev.open()
        self.fs = FS(self.dev)

    def test_cat_wrong_path_return_none(self):
        path = "test/report"
        self.assertEqual(self.fs.cat(path), None)

    def test_cat(self):
        self.fs._dev.rpc.file_show = MagicMock(side_effect=self._mock_manager)
        path = "test/cat.txt"
        self.assertTrue("testing cat functionality" in self.fs.cat(path))
        self.fs._dev.rpc.file_show.assert_called_with(filename="test/cat.txt")

    def test_cwd(self):
        self.fs._dev.rpc.set_cli_working_directory = MagicMock(side_effect=self._mock_manager)
        folder = "change/directory"
        self.assertEqual("change/directory", self.fs.cwd(folder))
        self.fs._dev.rpc.set_cli_working_directory.assert_called_with(directory="change/directory")

    @patch("jnpr.junos.Device.execute")
    def test_pwd(self, mock_execute):
        mock_execute.side_effect = MagicMock(side_effect=self._mock_manager)
        self.fs.pwd()
        self.assertEqual(self.fs.pwd(), "/cf/var/home/rick")

    def test_checksum_return_none(self):
        path = "test/report"
        self.assertEqual(self.fs.checksum(path), None)

    def test_checksum_unknown_calc(self):
        path = "test/report"
        self.assertRaises(ValueError, self.fs.checksum, path=path, calc="abc")

    def test_checksum_return_rsp(self):
        self.fs.dev.rpc.get_sha256_checksum_information = MagicMock(side_effect=self._mock_manager)
        path = "test/checksum"
        self.assertEqual(self.fs.checksum(path, "sha256"), "xxxx")
        self.fs.dev.rpc.get_sha256_checksum_information.assert_called_with(path="test/checksum")

    def test_stat_calling___decode_file(self):
        path = "test/stat/decode_file"
        self.fs.dev.rpc.file_list = MagicMock(side_effect=self._mock_manager)
        self.assertEqual(
            self.fs.stat(path),
            {
                "owner": "pqr",
                "path": "/var/abc.sh",
                "permissions": 755,
                "permissions_text": "-rwxr-xr-x",
                "size": 2,
                "ts_date": "Mar 13 06:54",
                "ts_epoc": "1394693680",
                "type": "file",
            },
        )

    def test_stat_calling___decode_dir(self):
        path = "test/stat/decode_dir"
        self.fs.dev.rpc.file_list = MagicMock(side_effect=self._mock_manager)
        self.assertEqual(self.fs.stat(path), {"path": "/var", "type": "dir", "file_count": 1, "size": 2})

    def test_stat_return_none(self):
        path = "test/abc"
        self.fs.dev.rpc.file_list = MagicMock()
        self.fs.dev.rpc.file_list.find.return_value = "output"
        self.assertEqual(self.fs.stat(path), None)

    def test_ls_calling___decode_file(self):
        path = "test/stat/decode_file"
        self.fs.dev.rpc.file_list = MagicMock(side_effect=self._mock_manager)
        self.assertEqual(
            self.fs.ls(path),
            {
                "owner": "pqr",
                "path": "/var/abc.sh",
                "permissions": 755,
                "permissions_text": "-rwxr-xr-x",
                "size": 2,
                "ts_date": "Mar 13 06:54",
                "ts_epoc": "1394693680",
                "type": "file",
            },
        )

    def test_ls_calling___decode_dir(self):
        path = "test/stat/decode_dir"
        self.fs.dev.rpc.file_list = MagicMock(side_effect=self._mock_manager)
        self.assertEqual(
            self.fs.ls(path),
            {
                "files": {
                    "abc": {
                        "permissions_text": "drwxr-xr-x",
                        "ts_date": "Feb 17 15:30",
                        "ts_epoc": "1392651039",
#.........這裏部分代碼省略.........
開發者ID:pklimai,項目名稱:py-junos-eznc,代碼行數:103,代碼來源:test_fs.py

示例5: TestFS

# 需要導入模塊: from jnpr.junos.utils.fs import FS [as 別名]
# 或者: from jnpr.junos.utils.fs.FS import cat [as 別名]
class TestFS(unittest.TestCase):

    @patch('ncclient.manager.connect')
    def setUp(self, mock_connect):
        mock_connect.side_effect = self._mock_manager
        self.dev = Device(host='1.1.1.1', user='rick', password='password123',
                          gather_facts=False)
        self.dev.open()
        self.fs = FS(self.dev)

    @patch('jnpr.junos.device.warnings')
    def test_cat_wrong_path_return_none(self, mock_warnings):
        path = 'test/report'
        self.assertEqual(self.fs.cat(path), None)

    def test_cat(self):
        self.fs._dev.rpc.file_show = MagicMock(side_effect=self._mock_manager)
        path = 'test/cat.txt'
        self.assertTrue('testing cat functionality' in self.fs.cat(path))
        self.fs._dev.rpc.file_show.assert_called_with(filename='test/cat.txt')

    def test_cwd(self):
        self.fs._dev.rpc.set_cli_working_directory = MagicMock(
            side_effect=self._mock_manager)
        folder = 'change/directory'
        self.assertEqual('change/directory', self.fs.cwd(folder))
        self.fs._dev.rpc.set_cli_working_directory.\
            assert_called_with(directory='change/directory')

    @patch('jnpr.junos.Device.execute')
    def test_pwd(self, mock_execute):
        mock_execute.side_effect = MagicMock(side_effect=self._mock_manager)
        self.fs.pwd()
        self.assertEqual(self.fs.pwd(), '/cf/var/home/rick')

    @patch('jnpr.junos.device.warnings')
    def test_checksum_return_none(self, mock_warnings):
        path = 'test/report'
        self.assertEqual(self.fs.checksum(path), None)

    def test_checksum_unknown_calc(self):
        path = 'test/report'
        self.assertRaises(ValueError, self.fs.checksum, path=path, calc='abc')

    def test_checksum_return_rsp(self):
        self.fs.dev.rpc.get_sha256_checksum_information = \
            MagicMock(side_effect=self._mock_manager)
        path = 'test/checksum'
        self.assertEqual(self.fs.checksum(path, 'sha256'), 'xxxx')
        self.fs.dev.rpc.get_sha256_checksum_information.\
            assert_called_with(path='test/checksum')

    def test_stat_calling___decode_file(self):
        path = 'test/stat/decode_file'
        self.fs.dev.rpc.file_list = \
            MagicMock(side_effect=self._mock_manager)
        self.assertEqual(self.fs.stat(path),
                         {'owner': 'pqr', 'path': '/var/abc.sh',
                          'permissions': 755,
                          'permissions_text': '-rwxr-xr-x', 'size': 2,
                          'ts_date': 'Mar 13 06:54',
                          'ts_epoc': '1394693680',
                          'type': 'file'})

    def test_stat_calling___decode_dir(self):
        path = 'test/stat/decode_dir'
        self.fs.dev.rpc.file_list = \
            MagicMock(side_effect=self._mock_manager)
        self.assertEqual(self.fs.stat(path),
                         {'path': '/var', 'type': 'dir', 'file_count': 1,
                          'size': 2})

    def test_stat_return_none(self):
        path = 'test/abc'
        self.fs.dev.rpc.file_list = MagicMock()
        self.fs.dev.rpc.file_list.find.return_value = 'output'
        self.assertEqual(self.fs.stat(path), None)

    def test_ls_calling___decode_file(self):
        path = 'test/stat/decode_file'
        self.fs.dev.rpc.file_list = \
            MagicMock(side_effect=self._mock_manager)
        self.assertEqual(self.fs.ls(path),
                         {'owner': 'pqr', 'path': '/var/abc.sh',
                          'permissions': 755,
                          'permissions_text': '-rwxr-xr-x', 'size': 2,
                          'ts_date': 'Mar 13 06:54',
                          'ts_epoc': '1394693680',
                          'type': 'file'})

    def test_ls_calling___decode_dir(self):
        path = 'test/stat/decode_dir'
        self.fs.dev.rpc.file_list = \
            MagicMock(side_effect=self._mock_manager)
        self.assertEqual(self.fs.ls(path),
                         {'files':
                          {'abc': {'permissions_text': 'drwxr-xr-x',
                                   'ts_date': 'Feb 17 15:30',
                                   'ts_epoc': '1392651039',
                                   'owner': 'root', 'path': 'abc',
#.........這裏部分代碼省略.........
開發者ID:ydnath,項目名稱:py-junos-eznc,代碼行數:103,代碼來源:test_fs.py


注:本文中的jnpr.junos.utils.fs.FS.cat方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。