<commit_before>import os
import requests
from bs4 import BeautifulSoup
from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

class global_arrays(object):
	allprobes = []
	allgroups = []
	alldevices = []
	allsensors = []

class baseconfig(object):
	def __str__(self):
		return("<Name: {name}, ID: {id}, Active: {active}>".format(name=self.name,id=self.id,active=self.active))
	def __repr__(self):
		return("<Name: {name}, ID: {id}, Active: {active}>".format(name=self.name,id=self.id,active=self.active))
	def set_config(self,host,port,user,passhash,protocol):
		self.confdata = (host,port,user,passhash,protocol)
	def unpack_config(self,confdata):
		self.host = confdata[0]
		self.port = confdata[1]
		self.user = confdata[2]
		self.passhash = confdata[3]
		self.protocol = confdata[4]
		self.confdata = confdata
		self.base_url = "{protocol}://{host}:{port}/api/".format(protocol=self.protocol,host=self.host,port=self.port)
		self.base_url_no_api = "{protocol}://{host}:{port}/".format(protocol=self.protocol,host=self.host,port=self.port)
		self.url_auth = "username={username}&passhash={passhash}".format(username=self.user,passhash=self.passhash)
	def clear_arrays(self):
		del self.allprobes[:]
		del self.allgroups[:]
		del self.alldevices[:]
		del self.allsensors[:]
	def delete(self,confirm=True):
		if self.type == "Root":
			return("You cannot delete the root object.")
		else:
			delete_url = "deleteobject.htm?id={objid}&approve=1}".format(objid=self.id)
			if confirm:
				response = ""
				while response.upper() not in ["Y","N"]:
					response = str(input("Would you like to continue?(Y/[N])  "))
					if response == "":
						response = "N"
				if response.upper() == "Y":
					req = self.get_request(url_string=delete_url)
			else:
				req = self.get_request(url_string=delete_url)
	def set_property(self,name,value):
		if self.type != "Channel":
			setprop_url = "setobjectproperty.htm?id={objid}&name={propname}&value={propval}".format(objid=self.id,propname=name,propval=value)
		else:
			setprop_url = "setobjectproperty.htm?id={objid}&subid={subid}&name={propname}&value={propval}".format(objid=self.sensorid,subid=self.objid,propname=name,propval=value)
		req = self.get_request(url_string=setprop_url)
		self.name = value
	def get_property(self,name):
		if self.type != "Channel":
			getprop_url = "getobjectproperty.htm?id={objid}&name={propname}&show=text".format(objid=self.id,propname=name)
		else:
			getprop_url = "getobjectproperty.htm?id={objid}&subid={subid}&name={propname}".format(objid=self.sensorid,subid=self.objid,propname=name)
		req = self.get_request(url_string=getprop_url)
		soup = BeautifulSoup(req.text,'lxml')
		if soup.result.text != "(Property not found)":
			setattr(self,name,soup.result.text)
			return(soup.result.text)
		else:
			raise(ResourceNotFound("No object property of name: {name}".format(name=name)))
	def set_interval(self,interval):
		'''note: you will still need to disable inheritance manually.
		Valid intervals are (seconds): 30, 60, 300, 600, 900, 1800, 3600, 14400, 21600, 43200, 86400'''
		self.set_property(name="interval",value=interval)
	def get_tree(self,root=''):
		#gets sensortree from prtg. If no rootid is provided returns entire tree
		tree_url = "table.xml?content=sensortree&output=xml&id={rootid}".format(rootid=root)
		req = self.get_request(url_string=tree_url)
		raw_data = req.text
		treesoup = BeautifulSoup(raw_data,"lxml")
		#returns the xml as a beautifulsoup object
		return(treesoup)
	def get_request(self,url_string,api=True):
		#global method for api calls. Provides errors for the 401 and 404 responses
		if api:
			url = "{base}{content}&{auth}".format(base=self.base_url,content=url_string,auth=self.url_auth)
		else:
			url = "{base}{content}&{auth}".format(base=self.base_url_no_api,content=url_string,auth=self.url_auth)
		req = requests.get(url,verify=False)
		if req.status_code == 200:
			return(req)
		elif req.status_code == 401:
			raise(AuthenticationError("PRTG authentication failed. Check credentials in config file"))
		elif req.status_code == 404:
			raise(ResourceNotFound("No resource at URL used: {0}".format(tree_url)))
	def rename(self,newname):
		rename_url = "rename.htm?id={objid}&value={name}".format(objid=self.id,name=newname)
		req = self.get_request(url_string=rename_url)
		self.name = newname
	def pause(self,duration=0,message=""):
		if duration > 0:
			pause_url = "pauseobjectfor.htm?id={objid}&duration={time}".format(objid=self.id,time=str(duration))
		else:
			pause_url = "pause.htm?id={objid}&action=0".format(objid=self.id)
		if message:
			pause_url += "&pausemsg={string}".format(string=message)
		req = self.get_request(url_string=pause_url)
		self.status = "Paused"
		self.active = "false"
		self.status_raw = "7"
	def resume(self):
		resume_url = "pause.htm?id={objid}&action=1".format(objid=self.id)<commit_msg>Raise error if object does not exist or no permission<commit_after>import os
import requests
from bs4 import BeautifulSoup
from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

class global_arrays(object):
	allprobes = []
	allgroups = []
	alldevices = []
	allsensors = []

class baseconfig(object):
	def __str__(self):
		return("<Name: {name}, ID: {id}, Active: {active}>".format(name=self.name,id=self.id,active=self.active))
	def __repr__(self):
		return("<Name: {name}, ID: {id}, Active: {active}>".format(name=self.name,id=self.id,active=self.active))
	def set_config(self,host,port,user,passhash,protocol):
		self.confdata = (host,port,user,passhash,protocol)
	def unpack_config(self,confdata):
		self.host = confdata[0]
		self.port = confdata[1]
		self.user = confdata[2]
		self.passhash = confdata[3]
		self.protocol = confdata[4]
		self.confdata = confdata
		self.base_url = "{protocol}://{host}:{port}/api/".format(protocol=self.protocol,host=self.host,port=self.port)
		self.base_url_no_api = "{protocol}://{host}:{port}/".format(protocol=self.protocol,host=self.host,port=self.port)
		self.url_auth = "username={username}&passhash={passhash}".format(username=self.user,passhash=self.passhash)
	def clear_arrays(self):
		del self.allprobes[:]
		del self.allgroups[:]
		del self.alldevices[:]
		del self.allsensors[:]
	def delete(self,confirm=True):
		if self.type == "Root":
			return("You cannot delete the root object.")
		else:
			delete_url = "deleteobject.htm?id={objid}&approve=1}".format(objid=self.id)
			if confirm:
				response = ""
				while response.upper() not in ["Y","N"]:
					response = str(input("Would you like to continue?(Y/[N])  "))
					if response == "":
						response = "N"
				if response.upper() == "Y":
					req = self.get_request(url_string=delete_url)
			else:
				req = self.get_request(url_string=delete_url)
	def set_property(self,name,value):
		if self.type != "Channel":
			setprop_url = "setobjectproperty.htm?id={objid}&name={propname}&value={propval}".format(objid=self.id,propname=name,propval=value)
		else:
			setprop_url = "setobjectproperty.htm?id={objid}&subid={subid}&name={propname}&value={propval}".format(objid=self.sensorid,subid=self.objid,propname=name,propval=value)
		req = self.get_request(url_string=setprop_url)
		self.name = value
	def get_property(self,name):
		if self.type != "Channel":
			getprop_url = "getobjectproperty.htm?id={objid}&name={propname}&show=text".format(objid=self.id,propname=name)
		else:
			getprop_url = "getobjectproperty.htm?id={objid}&subid={subid}&name={propname}".format(objid=self.sensorid,subid=self.objid,propname=name)
		req = self.get_request(url_string=getprop_url)
		soup = BeautifulSoup(req.text,'lxml')
		if soup.result.text != "(Property not found)":
			setattr(self,name,soup.result.text)
			return(soup.result.text)
		else:
			raise(ResourceNotFound("No object property of name: {name}".format(name=name)))
	def set_interval(self,interval):
		'''note: you will still need to disable inheritance manually.
		Valid intervals are (seconds): 30, 60, 300, 600, 900, 1800, 3600, 14400, 21600, 43200, 86400'''
		self.set_property(name="interval",value=interval)
	def get_tree(self,root=''):
		#gets sensortree from prtg. If no rootid is provided returns entire tree
		tree_url = "table.xml?content=sensortree&output=xml&id={rootid}".format(rootid=root)
		req = self.get_request(url_string=tree_url)
		raw_data = req.text
		treesoup = BeautifulSoup(raw_data,"lxml")
		#returns the xml as a beautifulsoup object
		if len(treesoup.sensortree.nodes) > 0:
			return(treesoup)
		else:
			raise(ResourceNotFound("No objects at ID: {id}".format(id=root)))
	def get_request(self,url_string,api=True):
		#global method for api calls. Provides errors for the 401 and 404 responses
		if api:
			url = "{base}{content}&{auth}".format(base=self.base_url,content=url_string,auth=self.url_auth)
		else:
			url = "{base}{content}&{auth}".format(base=self.base_url_no_api,content=url_string,auth=self.url_auth)
		req = requests.get(url,verify=False)
		if req.status_code == 200:
			return(req)
		elif req.status_code == 401:
			raise(AuthenticationError("PRTG authentication failed. Check credentials in config file"))
		elif req.status_code == 404:
			raise(ResourceNotFound("No resource at URL used: {0}".format(tree_url)))
	def rename(self,newname):
		rename_url = "rename.htm?id={objid}&value={name}".format(objid=self.id,name=newname)
		req = self.get_request(url_string=rename_url)
		self.name = newname
	def pause(self,duration=0,message=""):
		if duration > 0:
			pause_url = "pauseobjectfor.htm?id={objid}&duration={time}".format(objid=self.id,time=str(duration))
		else:
			pause_url = "pause.htm?id={objid}&action=0".format(objid=self.id)
		if message:
			pause_url += "&pausemsg={string}".format(string=message)
		req = self.get_request(url_string=pause_url)
		self.status = "Paused"
		self.active = "false"
		self.status_raw = "7"
	def resume(self):
		resume_url = "pause.htm?id={objid}&action=1".format(objid=self.id)
{'fraction_non_alphanumeric': 0.1572608738927043, 'fraction_numerical': 0.0142331044092764, 'mean_word_length': 5.982626824183461, 'pattern_counts': {'":': 12, '<': 7, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3028702', 'n_tokens_mistral': 3948, 'n_tokens_neox': 3564, 'n_words': 603}