pymensago/serverconn.py
def replace(conn: ServerConnection, oldfile: str, localpath: str, newpath: str, tempname='',
	offset=-1, hashstr=''):
	'''Uploads a local file to the server to replace an existing one.
	
	Parameters:
		oldfile: (str) Mensago path of the file to replace
		localpath: (str) local path of the new file to upload
		newpath: (str) Mensago path of the folder for the replacement file
		tempname: (str) Resume only. Name of the temporary file returned by the server
			in the initial upload attempt.
		offset: (int) Resume only. Location in the file to continue uploading at.
		hashstr: CryptoString-formatted string containing the hash of the file
	
	Returns:
		sent: (int) Only when interrupted. Number of bytes sent to the server.
		tempname: (str) Only when interrupted. Name of the server-side temporary file
			needed when resuming
		filename: (str) Only on success. Mensago path of the replacement file
	'''

	try:	
		filesize = os.path.getsize(localpath)
	except OSError as e:
		return RetVal(ErrBadValue, str(e))
	except Exception as e:
		return RetVal().wrap_exception(e)
	
	if offset >= 0 and offset > filesize:
		return RetVal(ErrBadValue, 'bad offset')
	
	if (offset >= 0 and not tempname) or (offset < 0 and tempname):
		return RetVal(ErrBadValue, 'both tempname and offset must both be set')
	
	if not newpath:
		return RetVal(ErrBadValue, 'empty server path')
	
	if not hashstr:
		status = hashfile(localpath)
		if status.error():
			return status
		hashstr = status['hash']
	
	request = {
		'Action': 'REPLACE',
		'Data': {
			'OldPath': oldfile,
			'NewPath': newpath,
			'Size': str(filesize),
			'Hash': hashstr,
			'Path': newpath
		}
	}

	if offset >= 0:
		request['Data']['Offset'] = str(offset)
	
	if tempname:
		request['Data']['TempName'] = tempname
	
	status = conn.send_message(request)
	if status.error():
		return status

	response = conn.read_response(server_response)
	if response['Code'] != 100:
		return wrap_server_error(response)
	
	totalsent = 0
	try:
		handle = open(localpath, 'rb')
	except Exception as e:
		return RetVal(ErrFilesystemError, e)

	if offset > 0:
		handle.seek(offset)

	try:
		filedata = handle.read(io.DEFAULT_BUFFER_SIZE)
	except Exception as e:
		handle.close()
		return RetVal(ErrFilesystemError, e).set_values({
			'sent': totalsent,
			'tempname': response['TempName']
		})
	while filedata:
		try:
			sent_size = conn.socket.send(filedata)
		except Exception as e:
			handle.close()
			return RetVal(ErrNetworkError, e).set_values({
				'sent': totalsent,
				'tempname': response['TempName']
			})
		totalsent = totalsent + sent_size

		try:
			filedata = handle.read(io.DEFAULT_BUFFER_SIZE)
		except Exception as e:
			handle.close()
			return RetVal(ErrFilesystemError, e).set_values({
				'sent': totalsent,
				'tempname': response['TempName']
			})
	handle.close()

	response = conn.read_response(server_response)
	if response['Code'] != 200:
		return wrap_server_error(response)

	return RetVal().set_value("filename", response['Data']['FileName'])
Uploads a local file to the server to replace an existing one.
	
	Parameters:
		oldfile: (str) Mensago path of the file to replace
		localpath: (str) local path of the new file to upload
		newpath: (str) Mensago path of the folder for the replacement file
		tempname: (str) Resume only. Name of the temporary file returned by the server
			in the initial upload attempt.
		offset: (int) Resume only. Location in the file to continue uploading at.
		hashstr: CryptoString-formatted string containing the hash of the file
	
	Returns:
		sent: (int) Only when interrupted. Number of bytes sent to the server.
		tempname: (str) Only when interrupted. Name of the server-side temporary file
			needed when resuming
		filename: (str) Only on success. Mensago path of the replacement file
	
Uploads a local file to the server to replace an existing one.
Uploads a local file to the server to replace an existing one .
def replace(conn: ServerConnection, oldfile: str, localpath: str, newpath: str, tempname='',
	offset=-1, hashstr=''):
	try:	
		filesize = os.path.getsize(localpath)
	except OSError as e:
		return RetVal(ErrBadValue, str(e))
	except Exception as e:
		return RetVal().wrap_exception(e)
	if offset >= 0 and offset > filesize:
		return RetVal(ErrBadValue, 'bad offset')
	if (offset >= 0 and not tempname) or (offset < 0 and tempname):
		return RetVal(ErrBadValue, 'both tempname and offset must both be set')
	if not newpath:
		return RetVal(ErrBadValue, 'empty server path')
	if not hashstr:
		status = hashfile(localpath)
		if status.error():
			return status
		hashstr = status['hash']
	request = {
		'Action': 'REPLACE',
		'Data': {
			'OldPath': oldfile,
			'NewPath': newpath,
			'Size': str(filesize),
			'Hash': hashstr,
			'Path': newpath
		}
	}
	if offset >= 0:
		request['Data']['Offset'] = str(offset)
	if tempname:
		request['Data']['TempName'] = tempname
	status = conn.send_message(request)
	if status.error():
		return status
	response = conn.read_response(server_response)
	if response['Code'] != 100:
		return wrap_server_error(response)
	totalsent = 0
	try:
		handle = open(localpath, 'rb')
	except Exception as e:
		return RetVal(ErrFilesystemError, e)
	if offset > 0:
		handle.seek(offset)
	try:
		filedata = handle.read(io.DEFAULT_BUFFER_SIZE)
	except Exception as e:
		handle.close()
		return RetVal(ErrFilesystemError, e).set_values({
			'sent': totalsent,
			'tempname': response['TempName']
		})
	while filedata:
		try:
			sent_size = conn.socket.send(filedata)
		except Exception as e:
			handle.close()
			return RetVal(ErrNetworkError, e).set_values({
				'sent': totalsent,
				'tempname': response['TempName']
			})
		totalsent = totalsent + sent_size
		try:
			filedata = handle.read(io.DEFAULT_BUFFER_SIZE)
		except Exception as e:
			handle.close()
			return RetVal(ErrFilesystemError, e).set_values({
				'sent': totalsent,
				'tempname': response['TempName']
			})
	handle.close()
	response = conn.read_response(server_response)
	if response['Code'] != 200:
		return wrap_server_error(response)
	return RetVal().set_value("filename", response['Data']['FileName'])
def replace ( conn : ServerConnection , oldfile : str , localpath : str , newpath : str , tempname = '' , offset = - 1 , hashstr = '' ) : try : filesize = os . path . getsize ( localpath ) except OSError as e : return RetVal ( ErrBadValue , str ( e ) ) except Exception as e : return RetVal ( ) . wrap_exception ( e ) if offset >= 0 and offset > filesize : return RetVal ( ErrBadValue , 'bad offset' ) if ( offset >= 0 and not tempname ) or ( offset < 0 and tempname ) : return RetVal ( ErrBadValue , 'both tempname and offset must both be set' ) if not newpath : return RetVal ( ErrBadValue , 'empty server path' ) if not hashstr : status = hashfile ( localpath ) if status . error ( ) : return status hashstr = status [ 'hash' ] request = { 'Action' : 'REPLACE' , 'Data' : { 'OldPath' : oldfile , 'NewPath' : newpath , 'Size' : str ( filesize ) , 'Hash' : hashstr , 'Path' : newpath } } if offset >= 0 : request [ 'Data' ] [ 'Offset' ] = str ( offset ) if tempname : request [ 'Data' ] [ 'TempName' ] = tempname status = conn . send_message ( request ) if status . error ( ) : return status response = conn . read_response ( server_response ) if response [ 'Code' ] != 100 : return wrap_server_error ( response ) totalsent = 0 try : handle = open ( localpath , 'rb' ) except Exception as e : return RetVal ( ErrFilesystemError , e ) if offset > 0 : handle . seek ( offset ) try : filedata = handle . read ( io . DEFAULT_BUFFER_SIZE ) except Exception as e : handle . close ( ) return RetVal ( ErrFilesystemError , e ) . set_values ( { 'sent' : totalsent , 'tempname' : response [ 'TempName' ] } ) while filedata : try : sent_size = conn . socket . send ( filedata ) except Exception as e : handle . close ( ) return RetVal ( ErrNetworkError , e ) . set_values ( { 'sent' : totalsent , 'tempname' : response [ 'TempName' ] } ) totalsent = totalsent + sent_size try : filedata = handle . read ( io . DEFAULT_BUFFER_SIZE ) except Exception as e : handle . close ( ) return RetVal ( ErrFilesystemError , e ) . set_values ( { 'sent' : totalsent , 'tempname' : response [ 'TempName' ] } ) handle . close ( ) response = conn . read_response ( server_response ) if response [ 'Code' ] != 200 : return wrap_server_error ( response ) return RetVal ( ) . set_value ( "filename" , response [ 'Data' ] [ 'FileName' ] )
Uploads a local file to the server to replace an existing one.
Uploads a local file to the server to replace an existing one .
'''Uploads a local file to the server to replace an existing one.
	
	Parameters:
		oldfile: (str) Mensago path of the file to replace
		localpath: (str) local path of the new file to upload
		newpath: (str) Mensago path of the folder for the replacement file
		tempname: (str) Resume only. Name of the temporary file returned by the server
			in the initial upload attempt.
		offset: (int) Resume only. Location in the file to continue uploading at.
		hashstr: CryptoString-formatted string containing the hash of the file
	
	Returns:
		sent: (int) Only when interrupted. Number of bytes sent to the server.
		tempname: (str) Only when interrupted. Name of the server-side temporary file
			needed when resuming
		filename: (str) Only on success. Mensago path of the replacement file
	'''
(int) Only when interrupted. Number of bytes sent to the server.
tempname: (str) Only when interrupted. Name of the server-side temporary file
needed when resuming
filename: (str) Only on success. Mensago path of the replacement file
( int ) Only when interrupted . Number of bytes sent to the server . tempname : ( str ) Only when interrupted . Name of the server - side temporary file needed when resuming filename : ( str ) Only on success . Mensago path of the replacement file
sent
conn
ServerConnection
oldfile
str
(str) Mensago path of the file to replace
( str ) Mensago path of the file to replace
localpath
str
(str) local path of the new file to upload
( str ) local path of the new file to upload
newpath
str
(str) Mensago path of the folder for the replacement file
( str ) Mensago path of the folder for the replacement file
tempname
(str) Resume only. Name of the temporary file returned by the server
in the initial upload attempt.
( str ) Resume only . Name of the temporary file returned by the server in the initial upload attempt .
offset
(int) Resume only. Location in the file to continue uploading at.
( int ) Resume only . Location in the file to continue uploading at .
hashstr
CryptoString-formatted string containing the hash of the file
CryptoString - formatted string containing the hash of the file