pymensago/keycard.py
def load(self, path: str) -> RetVal:
		'''Loads a keycard from a file'''
		if not path:
			return RetVal(ErrBadValue, 'path may not be empty')
		
		if not os.path.exists(path):
			return RetVal(ErrNotFound)
		
		# Although we care very much about saving keycards with the Windows-style line endings,
		# we actually want the line endings to get stripped on load because the fields aren't
		# stored with line endings
		try:
			with open(path, 'r') as f:
				card_type = ''
				accumulator = list()
				line_index = 1
				entry_index = 1
				rawline = f.readline()

				while rawline:
					line = rawline.strip()
					if not line:
						line_index = line_index + 1
						continue
					
					if line == '----- BEGIN ENTRY -----':
						accumulator.clear()
					elif line == '----- END ENTRY -----':
						
						entry = None
						if card_type == 'User':
							entry = UserEntry()
						elif card_type == 'Organization':
							entry = OrgEntry()
						else:
							return RetVal(UnsupportedKeycardType,
									f'entry {entry_index} has invalid type')

						status = entry.set(b'\r\n'.join(accumulator))
						if status.error():
							status.info = f'keycard entry {entry_index}: {status.info}'
							return status
						self.entries.append(entry)
						entry_index = entry_index + 1
					else:
						parts = line.split(':', 1)
						if len(parts) != 2:
							return RetVal(ErrBadData, f'invalid line {line_index}')
						
						if parts[0] == 'Type':
							if card_type:
								if card_type != parts[1]:
									return RetVal(ErrBadData, 'entry type does not match keycard')
							else:
								card_type = parts[1]

						accumulator.append(line.encode())
					
					line_index = line_index + 1
					rawline = f.readline()

		
		except Exception as e:
			return RetVal.wrap_exception(e)




		return RetVal()
Loads a keycard from a file
Loads a keycard from a file
Loads a keycard from a file
def load(self, path: str) -> RetVal:
		if not path:
			return RetVal(ErrBadValue, 'path may not be empty')
		if not os.path.exists(path):
			return RetVal(ErrNotFound)
		try:
			with open(path, 'r') as f:
				card_type = ''
				accumulator = list()
				line_index = 1
				entry_index = 1
				rawline = f.readline()
				while rawline:
					line = rawline.strip()
					if not line:
						line_index = line_index + 1
						continue
					if line == '----- BEGIN ENTRY -----':
						accumulator.clear()
					elif line == '----- END ENTRY -----':
						entry = None
						if card_type == 'User':
							entry = UserEntry()
						elif card_type == 'Organization':
							entry = OrgEntry()
						else:
							return RetVal(UnsupportedKeycardType,
									f'entry {entry_index} has invalid type')
						status = entry.set(b'\r\n'.join(accumulator))
						if status.error():
							status.info = f'keycard entry {entry_index}: {status.info}'
							return status
						self.entries.append(entry)
						entry_index = entry_index + 1
					else:
						parts = line.split(':', 1)
						if len(parts) != 2:
							return RetVal(ErrBadData, f'invalid line {line_index}')
						if parts[0] == 'Type':
							if card_type:
								if card_type != parts[1]:
									return RetVal(ErrBadData, 'entry type does not match keycard')
							else:
								card_type = parts[1]
						accumulator.append(line.encode())
					line_index = line_index + 1
					rawline = f.readline()
		except Exception as e:
			return RetVal.wrap_exception(e)
		return RetVal()
def load ( self , path : str ) -> RetVal : if not path : return RetVal ( ErrBadValue , 'path may not be empty' ) if not os . path . exists ( path ) : return RetVal ( ErrNotFound ) try : with open ( path , 'r' ) as f : card_type = '' accumulator = list ( ) line_index = 1 entry_index = 1 rawline = f . readline ( ) while rawline : line = rawline . strip ( ) if not line : line_index = line_index + 1 continue if line == '----- BEGIN ENTRY -----' : accumulator . clear ( ) elif line == '----- END ENTRY -----' : entry = None if card_type == 'User' : entry = UserEntry ( ) elif card_type == 'Organization' : entry = OrgEntry ( ) else : return RetVal ( UnsupportedKeycardType , f'entry {entry_index} has invalid type' ) status = entry . set ( b'\r\n' . join ( accumulator ) ) if status . error ( ) : status . info = f'keycard entry {entry_index}: {status.info}' return status self . entries . append ( entry ) entry_index = entry_index + 1 else : parts = line . split ( ':' , 1 ) if len ( parts ) != 2 : return RetVal ( ErrBadData , f'invalid line {line_index}' ) if parts [ 0 ] == 'Type' : if card_type : if card_type != parts [ 1 ] : return RetVal ( ErrBadData , 'entry type does not match keycard' ) else : card_type = parts [ 1 ] accumulator . append ( line . encode ( ) ) line_index = line_index + 1 rawline = f . readline ( ) except Exception as e : return RetVal . wrap_exception ( e ) return RetVal ( )
Loads a keycard from a file
Loads a keycard from a file
'''Loads a keycard from a file''' # Although we care very much about saving keycards with the Windows-style line endings, # we actually want the line endings to get stripped on load because the fields aren't # stored with line endings
self
path
str