当前位置: 首页>>代码示例>>Python>>正文


Python Monitor.disconnect方法代码示例

本文整理汇总了Python中monitor.Monitor.disconnect方法的典型用法代码示例。如果您正苦于以下问题:Python Monitor.disconnect方法的具体用法?Python Monitor.disconnect怎么用?Python Monitor.disconnect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在monitor.Monitor的用法示例。


在下文中一共展示了Monitor.disconnect方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Client

# 需要导入模块: from monitor import Monitor [as 别名]
# 或者: from monitor.Monitor import disconnect [as 别名]
class Client(object):
	"""Client that interacts with monitor on a higher level
	"""
	def __init__(self, config_filepath, machine_name, localhost_replace=None):
		"""Load configuration from json file 
		
		Args:
			config_filepath (str): Configuration file path
			machine_name (str): Virtual machine name
		"""
		self.config_filepath = config_filepath
		self.machine_name = machine_name
		self.localhost_replace = localhost_replace
		self.load_config()
		print("Limited QEMU Monitor Client for USB Switching")
		print("Type 'help' for a list of commands.")


	def load_config(self):
		"""Load configuration file 
		"""
		with open(self.config_filepath) as f:
			_dict = json.load(f)

		# Verify required keys are present
		rewrite = False
		required_keys = ("usb-devices", "virtual-machines")

		for key in required_keys:
			if not key in _dict.keys():
				_dict[key] = {}
				rewrite = True
				print("Element '%s' missing from config." % key)

		if rewrite:
			print("Adding required elements. Please modify them in your config.")
			print("Reload by typing 'reload' after you are finished.", "\n")
			with open(self.config_filepath, "w") as f:
				json.dump(_dict, f)
			return self.load_config()

		# Get useful info from config
		self.usb_devices_full = {
			k: v for k, v in _dict["usb-devices"].items()
				if v.get("action") != "disabled"
		}
		self.usb_devices = list(self.usb_devices_full.values())
		self.vm_names = list(_dict["virtual-machines"].keys())
		self.config = _dict["virtual-machines"].get(self.machine_name)

		if not self.config:
			return

		# Determine if we need to replace localhost from full config
		if "localhost-replace" in _dict.keys():
			self.localhost_replace = _dict["localhost-replace"]

		# Create monitor
		if "monitor" in self.config:
			host = self.config["monitor"]

			if self.localhost_replace:
				host = (host
					.replace("127.0.0.1", self.localhost_replace)
					.replace("localhost", self.localhost_replace))

			self.monitor = Monitor(host)


	def monitor_command(self, func):
		"""Quick monitor command: Connect, run, disconnect 
		
		Args:
			func (function): Callback function
		"""
		if not self.monitor.connect():
			print("Could not connect to monitor.")
			return
		result = func(self.monitor)
		self.monitor.disconnect()
		return result


	def device_names_to_ids(self, devices):
		"""Create list of devices by searching for 'devices' values and trying
		to find 'usb_devices_full' keys with them, otherwise use same value
		
		Args:
			devices (list): List of devices
		"""
		result = []

		for device in devices:
			name = self.usb_devices_full.get(device)
			result.append(name.get("id") if name else device)

		return result


	def parse_command(self, text):
#.........这里部分代码省略.........
开发者ID:thetarkus,项目名称:qemu-usb-device-manager,代码行数:103,代码来源:client.py


注:本文中的monitor.Monitor.disconnect方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。