old_contents: import json
  from openpassword.agile_keychain.encryption_key import EncryptionKey
  from openpassword.exceptions import EncryptionKeyNotFoundException
  
  
  class EncryptionKeyRepository:
      def __init__(self, path):
          self._path = path
  
      def key_for_security_level(self, security_level):
          key_file_path = self._resolve_key_file_path()
          keys = self._load_key_data(key_file_path)
  
          try:
              identifier = keys[security_level]
          except KeyError:
              raise EncryptionKeyNotFoundException("Unknown security level: {0}".format(security_level))
  
          for key in keys["list"]:
              if key["identifier"] == identifier:
                  return EncryptionKey(key)
  
      def _resolve_key_file_path(self):
          return self._path + '/data/default/encryptionKeys.js'
  
      def _load_key_data(self, path):
          try:
              file = open(path)
          except IOError:
              raise InvalidUuidException("Invalid path: %s" % path)
  
          data = json.load(file)
          file.close()
  
          return data
  
new_contents: import json
  from openpassword.agile_keychain.encryption_key import EncryptionKey
  from openpassword.exceptions import EncryptionKeyNotFoundException
  
  
  class EncryptionKeyRepository:
      def __init__(self, path):
          self._path = path
          self._keys = None
  
      def key_for_security_level(self, security_level):
          self._load_keys()
  
          key_id = self._resolve_key_id_for_security_level(security_level)
          return self._get_key_by_id(key_id)
  
      def _load_keys(self):
          key_file_path = self._resolve_key_file_path()
          self.keys = self._load_key_data(key_file_path)
  
      def _resolve_key_file_path(self):
          return self._path + '/data/default/encryptionKeys.js'
  
      def _load_key_data(self, path):
          try:
              file = open(path)
          except IOError:
              raise InvalidUuidException("Invalid path: %s" % path)
  
          data = json.load(file)
          file.close()
  
          return data
  
      def _resolve_key_id_for_security_level(self, security_level):
          try:
              return self.keys[security_level]
          except KeyError:
              raise EncryptionKeyNotFoundException("Unknown security level: {0}".format(security_level))
  
      def _get_key_by_id(self, key_id):
          for key in self.keys["list"]:
              if key["identifier"] == key_id:
                  return EncryptionKey(key)
  
subject: Refactor key_for_security_level method