本文整理汇总了Python中salt._compat.ElementTree类的典型用法代码示例。如果您正苦于以下问题:Python ElementTree类的具体用法?Python ElementTree怎么用?Python ElementTree使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ElementTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_config
def get_config(instance=_DEFAULT_INSTANCE):
'''
Determine the configuration of the provided instance.
:param str instance: The name of the Tentacle instance.
:return: A dictionary containing the configuration data.
:rtype: dict
CLI Example:
.. code-block:: bash
salt '*' octopus_tentacle.get_config instance='Tentacle'
'''
ret = dict()
name_mapping = {'Octopus.Home': 'home_path',
'Octopus.Communications.Squid': 'squid',
'Tentacle.CertificateThumbprint': 'thumbprint',
'Tentacle.Communication.TrustedOctopusServers': 'servers',
'Tentacle.Deployment.ApplicationDirectory': 'app_path',
'Tentacle.Services.NoListen': 'comms',
'Tentacle.Services.PortNumber': 'port'}
config_path = get_config_path(instance)
if not os.path.isfile(config_path):
_LOG.error('Unable to get configuration file for instance: %s', instance)
return ret
with salt.utils.fopen(config_path, 'r') as fh_:
config = _parse_config(ElementTree.fromstring(fh_.read()))
for item in config:
# Skip keys that we aren't specifically looking for.
if item in name_mapping:
# Convert the NoListen value to a friendly value.
if name_mapping[item] == 'comms':
for comms_style in _COMMS_STYLES:
if config[item] == _COMMS_STYLES[comms_style]:
ret[name_mapping[item]] = comms_style
break
else:
ret[name_mapping[item]] = config[item]
return ret
示例2: query
def query(action=None, command=None, args=None, method="GET", data=None):
"""
Make a web call to a Parallels provider
"""
path = config.get_config_value("url", get_configured_provider(), __opts__, search_global=False)
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(
realm="Parallels Instance Manager",
uri=path,
user=config.get_config_value("user", get_configured_provider(), __opts__, search_global=False),
passwd=config.get_config_value("password", get_configured_provider(), __opts__, search_global=False),
)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
if action:
path += action
if command:
path += "/{0}".format(command)
if type(args) is not dict:
args = {}
kwargs = {"data": data}
if type(data) is str and "<?xml" in data:
kwargs["headers"] = {"Content-type": "application/xml"}
if args:
path += "?%s"
params = urllib.urlencode(args)
req = urllib2.Request(url=path % params, **kwargs)
else:
req = urllib2.Request(url=path, **kwargs)
req.get_method = lambda: method
log.debug("{0} {1}".format(method, req.get_full_url()))
if data:
log.debug(data)
try:
result = urllib2.urlopen(req)
log.debug("PARALLELS Response Status Code: {0}".format(result.getcode()))
if "content-length" in result.headers:
content = result.read()
result.close()
items = ET.fromstring(content)
return items
return {}
except urllib2.URLError as exc:
log.error("PARALLELS Response Status Code: {0} {1}".format(exc.code, exc.msg))
root = ET.fromstring(exc.read())
log.error(root)
return {"error": root}
示例3: create_node
def create_node(vm_):
"""
Build and submit the XML to create a node
"""
# Start the tree
content = ET.Element("ve")
# Name of the instance
name = ET.SubElement(content, "name")
name.text = vm_["name"]
# Description, defaults to name
desc = ET.SubElement(content, "description")
desc.text = config.get_config_value("desc", vm_, __opts__, default=vm_["name"], search_global=False)
# How many CPU cores, and how fast they are
cpu = ET.SubElement(content, "cpu")
cpu.attrib["number"] = config.get_config_value("cpu_number", vm_, __opts__, default="1", search_global=False)
cpu.attrib["power"] = config.get_config_value("cpu_power", vm_, __opts__, default="1000", search_global=False)
# How many megabytes of RAM
ram = ET.SubElement(content, "ram-size")
ram.text = config.get_config_value("ram", vm_, __opts__, default="256", search_global=False)
# Bandwidth available, in kbps
bandwidth = ET.SubElement(content, "bandwidth")
bandwidth.text = config.get_config_value("bandwidth", vm_, __opts__, default="100", search_global=False)
# How many public IPs will be assigned to this instance
ip_num = ET.SubElement(content, "no-of-public-ip")
ip_num.text = config.get_config_value("ip_num", vm_, __opts__, default="1", search_global=False)
# Size of the instance disk
disk = ET.SubElement(content, "ve-disk")
disk.attrib["local"] = "true"
disk.attrib["size"] = config.get_config_value("disk_size", vm_, __opts__, default="10", search_global=False)
# Attributes for the image
vm_image = config.get_config_value("image", vm_, __opts__, search_global=False)
image = show_image({"image": vm_image}, call="function")
platform = ET.SubElement(content, "platform")
template = ET.SubElement(platform, "template-info")
template.attrib["name"] = vm_image
os_info = ET.SubElement(platform, "os-info")
os_info.attrib["technology"] = image[vm_image]["technology"]
os_info.attrib["type"] = image[vm_image]["osType"]
# Username and password
admin = ET.SubElement(content, "admin")
admin.attrib["login"] = config.get_config_value("ssh_username", vm_, __opts__, default="root")
admin.attrib["password"] = config.get_config_value("password", vm_, __opts__, search_global=False)
data = ET.tostring(content, encoding="UTF-8")
salt.cloud.utils.fire_event(
"event", "requesting instance", "salt/cloud/{0}/requesting".format(vm_["name"]), {"kwargs": data}
)
node = query(action="ve", method="POST", data=data)
return node
示例4: test_gen_vol_xml_for_esxi
def test_gen_vol_xml_for_esxi(self):
xml_data = virt._gen_vol_xml('vmname', 'system', 8192, 'esxi')
root = ET.fromstring(xml_data)
self.assertEqual(root.find('name').text, 'vmname/system.vmdk')
self.assertEqual(root.find('key').text, 'vmname/system')
self.assertEqual(root.find('capacity').attrib['unit'], 'KiB')
self.assertEqual(root.find('capacity').text, str(8192 * 1024))
示例5: list_users
def list_users(order_by='id'):
'''
Show all users for this company.
CLI Example:
salt myminion bamboohr.list_users
By default, the return data will be keyed by ID. However, it can be ordered
by any other field. Keep in mind that if the field that is chosen contains
duplicate values (i.e., location is used, for a company which only has one
location), then each duplicate value will be overwritten by the previous.
Therefore, it is advisable to only sort by fields that are guaranteed to be
unique.
CLI Examples:
salt myminion bamboohr.list_users order_by=id
salt myminion bamboohr.list_users order_by=email
'''
ret = {}
status, result = _query(action='meta', command='users')
root = ET.fromstring(result)
users = root.getchildren()
for user in users:
user_id = None
user_ret = {}
for item in user.items():
user_ret[item[0]] = item[1]
if item[0] == 'id':
user_id = item[1]
for item in user.getchildren():
user_ret[item.tag] = item.text
ret[user_ret[order_by]] = user_ret
return ret
示例6: _get_error
def _get_error(error):
# Converts boto exception to string that can be used to output error.
error = '\n'.join(error.split('\n')[1:])
error = ET.fromstring(error)
code = error[0][1].text
message = error[0][2].text
return code, message
示例7: list_employees
def list_employees(order_by='id'):
'''
Show all employees for this company.
CLI Example:
salt myminion bamboohr.list_employees
By default, the return data will be keyed by ID. However, it can be ordered
by any other field. Keep in mind that if the field that is chosen contains
duplicate values (i.e., location is used, for a company which only has one
location), then each duplicate value will be overwritten by the previous.
Therefore, it is advisable to only sort by fields that are guaranteed to be
unique.
CLI Examples:
salt myminion bamboohr.list_employees order_by=id
salt myminion bamboohr.list_employees order_by=displayName
salt myminion bamboohr.list_employees order_by=workEmail
'''
ret = {}
status, result = _query(action='employees', command='directory')
root = ET.fromstring(result)
directory = root.getchildren()
for cat in directory:
if cat.tag != 'employees':
continue
for item in cat:
emp_id = item.items()[0][1]
emp_ret = {'id': emp_id}
for details in item.getchildren():
emp_ret[details.items()[0][1]] = details.text
ret[emp_ret[order_by]] = emp_ret
return ret
示例8: _get_artifact_metadata
def _get_artifact_metadata(artifactory_url, repository, group_id, artifact_id, headers):
metadata_xml = _get_artifact_metadata_xml(artifactory_url=artifactory_url, repository=repository, group_id=group_id, artifact_id=artifact_id, headers=headers)
root = ET.fromstring(metadata_xml)
assert group_id == root.find('groupId').text
assert artifact_id == root.find('artifactId').text
latest_version = root.find('versioning').find('latest').text
return {
'latest_version': latest_version
}
示例9: mksls
def mksls(src, dst=None):
'''
Convert an AutoYAST file to an SLS file
'''
with salt.utils.fopen(src, 'r') as fh_:
ps_opts = xml.to_dict(ET.fromstring(fh_.read()))
if dst is not None:
with salt.utils.fopen(dst, 'w') as fh_:
fh_.write(yaml.safe_dump(ps_opts, default_flow_style=False))
else:
return yaml.safe_dump(ps_opts, default_flow_style=False)
示例10: test_boot_default_dev
def test_boot_default_dev(self):
diskp = virt._disk_profile('default', 'kvm')
nicp = virt._nic_profile('default', 'kvm')
xml_data = virt._gen_xml(
'hello',
1,
512,
diskp,
nicp,
'kvm'
)
root = ET.fromstring(xml_data)
self.assertEqual(root.find('os/boot').attrib['dev'], 'hd')
示例11: _get_artifact_metadata
def _get_artifact_metadata(artifactory_url, repository, group_id, artifact_id, headers):
metadata_xml = _get_artifact_metadata_xml(
artifactory_url=artifactory_url,
repository=repository,
group_id=group_id,
artifact_id=artifact_id,
headers=headers,
)
root = ET.fromstring(metadata_xml)
assert group_id == root.find("groupId").text
assert artifact_id == root.find("artifactId").text
latest_version = root.find("versioning").find("latest").text
return {"latest_version": latest_version}
示例12: test_boot_multiple_devs
def test_boot_multiple_devs(self):
diskp = virt._disk_profile('default', 'kvm')
nicp = virt._nic_profile('default', 'kvm')
xml_data = virt._gen_xml(
'hello',
1,
512,
diskp,
nicp,
'kvm',
boot_dev='cdrom network'
)
root = ET.fromstring(xml_data)
devs = root.findall('.//boot')
self.assertTrue(len(devs) == 2)
示例13: test_controller_for_kvm
def test_controller_for_kvm(self):
diskp = virt._disk_profile('default', 'kvm')
nicp = virt._nic_profile('default', 'kvm')
xml_data = virt._gen_xml(
'hello',
1,
512,
diskp,
nicp,
'kvm'
)
root = ET.fromstring(xml_data)
controllers = root.findall('.//devices/controller')
# There should be no controller
self.assertTrue(len(controllers) == 0)
示例14: test_gen_xml_for_serial_console
def test_gen_xml_for_serial_console(self):
diskp = virt._disk_profile('default', 'kvm')
nicp = virt._nic_profile('default', 'kvm')
xml_data = virt._gen_xml(
'hello',
1,
512,
diskp,
nicp,
'kvm',
serial_type='pty',
console=True
)
root = ET.fromstring(xml_data)
self.assertEqual(root.find('devices/serial').attrib['type'], 'pty')
self.assertEqual(root.find('devices/console').attrib['type'], 'pty')
示例15: test_controller_for_esxi
def test_controller_for_esxi(self):
diskp = virt._disk_profile('default', 'esxi')
nicp = virt._nic_profile('default', 'esxi')
xml_data = virt._gen_xml(
'hello',
1,
512,
diskp,
nicp,
'esxi'
)
root = ET.fromstring(xml_data)
controllers = root.findall('.//devices/controller')
self.assertTrue(len(controllers) == 1)
controller = controllers[0]
self.assertEqual(controller.attrib['model'], 'lsilogic')