Exemplo n.º 1
0
	def __init__(self, id, address, port, document_root, language):
		"""
		Constructor for the class.

		@param id: The identifier of this server, must be a non-empty string.
		@param address: The web address (excluding the protocol, for the web server.
		@param port: The port of the server.
		@param document_root: The document root of the server.
		@param language: A language this server supports, like PHP5.
		"""

		if not Validator.validate(address, 'domain'):
			raise InvalidParameterError('address', 'Must be a valid domain name, excluding the protocol or path.')
		if not Validator.validate(port, 'port'):
			raise InvalidParameterError('port', 'Must be an integer value between 0 and 65535')
		if not Validator.validate(document_root, 'non_empty_string'):
			raise InvalidParameterError('document_root', 'Must be a non-empty string')
		if not Validator.validate(language, 'non_empty_string'):
			raise InvalidParameterError('language', 'Must be a non-empty string')

		self.__address = address
		self.__port = port
		self.__document_root = document_root
		self.__language = language

		Server.__init__(self, id, Server.REMOTE)
		WebClient.__init__(self)
Exemplo n.º 2
0
	def is_exists(self, path):
		"""
		Checks whether a file or directory exists at the specified path.

		@param path: The path to check.

		@rtype: Boolean
		@return: True on success, False on failure.
		"""

		if not Validator.validate(path, 'non_empty_string'):
			raise InvalidParameterError('path', 'should be a non-empty string')
		
		return os.path.exists(path)
Exemplo n.º 3
0
	def get_temp_name(self, parent, prefix = ""):
		"""
		Returns the path to a non-existing temporary file name in the directory parent. The file can be
		optionally prefixed with "prefix".
		
		@param parent: The parent directory to store the file at.
		@param prefix: A prefix to add to the file name.
		
		@rtype: string
		@return: The path of the file.
		
		@raise TemporaryFileError: If it cannot return such path.
		"""
		
		if not Validator.validate(parent, 'non_empty_string'):
			raise InvalidParameterError('parent', 'should be a non-empty string')
		if not Validator.validate(prefix, 'string'):
			raise InvalidParameterError('prefix', 'should be a string value')
		
		try:
			return self.get_platform().basename(os.tempnam(parent, prefix))
		except Exception, e:
			raise TemporaryFileError, str(e)
Exemplo n.º 4
0
	def download(cls, url, destination, replace = False, retry = 1):
		"""
		downloads a file from the web
		
		@param url: the url of the file to be downloaded
		@param destination: the destination where the file will be downloaded to
		@param replace: if set True then the downloaded file will replae any existinf file
		@param retry: number of retries
		
		@rtype: bool
		@return: True on success
		
		@raise FileDownloadError: if error during downloading
		"""
		if not Validator.validate(url, "url"):
			raise InvalidParameterError("url", "should be a non-empty string starts with 'http://'")
		if not Validator.validate(destination, "non_empty_string"):
			raise InvalidParameterError('destination', 'should be non-empty string')
		if not Validator.validate(replace, 'boolean'):
			raise InvalidParameterError('replace', 'should be a boolean value')
		if not Validator.validate(retry, 'integer'):
			raise InvalidParameterError('getting', 'should be an integer value')
		
		separator = Platform.get_current().get_separator()
		
		if not (re.match(r'/', destination) or re.match(r'[a-zA-Z]:\\', destination)):
			destination = destination.rstrip(separator)
		
		while retry:
			retry -= 1
			try:
				result = urllib2.urlopen(url, "")
				
				if os.path.exists(destination):
					if os.path.isdir(destination):
						if result.headers.has_key('content-disposition'):
							tmp = result.headers.dict['content-disposition']
							index = tmp.find('filename=')
							if index == -1:
								name = 'attachment'
							else:
								name = tmp[index+9: ]
								name = name.split(' ')[0]
						else:
							name = result.geturl()
							name = name.split('?')[0]
							name = name.rstrip('/')
							index = name.rfind('/')
							name = name[index+1:]
						
						destination = destination + separator + name
					
					else:
						if os.path.isfile(destination):
							if replace:
								os.remove(destination)
							else:
								raise FileExistsError, 'the destination file already exists'
						else:
							raise FileExistsError, 'destination is non-file, system cannot replace a non file'
					
				file = open(destination, "wb")
				
				buffer = result.read(4096)
				
				while len(buffer) != 0:
					file.write(buffer)
					buffer = result.read(4096)
				
				file.close()	
				
			except Exception, e:
				if retry == 0:
					raise FileDownloadError, str(e)
			
			else:
				break