def test_categorise_file(mock_exists, basepath, kind, side_effect, suffixes) -> None: mock_exists.side_effect = side_effect for suffix in suffixes: fake_filepath = os.path.join(basepath, "walletfile") fake_filepath_full = fake_filepath + suffix ret = categorise_file(fake_filepath) assert ret.filename == "walletfile" assert ret.kind == kind assert ret.wallet_filepath == fake_filepath
def _get_recently_opened_wallets(self) -> Dict[str, Any]: results = [] for file_path in app_state.config.get('recently_open', []): if os.path.exists(file_path) and categorise_file( file_path) != StorageKind.HYBRID: results.append({ 'name': get_wallet_name_from_path(file_path), 'path': file_path, }) return results
def create_file_state(wallet_path: str) -> Optional[FileState]: if not os.path.exists(wallet_path): return None try: storage = WalletStorage(wallet_path) except Exception: logger.exception("problem looking at selected wallet '%s'", wallet_path) return None is_too_modern = False try: storage_info = categorise_file(wallet_path) if storage_info.kind == StorageKind.HYBRID: return None wallet_action = WalletAction.OPEN password_state = PasswordState.UNKNOWN if storage_info.kind == StorageKind.FILE: text_store = storage.get_text_store() try: text_store.attempt_load_data() except IOError: # IOError: storage.py:load_data() raises when selected file cannot be parsed. return None if storage.get("use_encryption"): # If there is a password and the wallet is not encrypted, then the private data # is encrypted. password_state = PasswordState.PASSWORDED elif text_store.is_encrypted(): # If there is a password and the wallet is encrypted, then the private data is # encrypted and the file is encrypted. password_state = PasswordState.PASSWORDED | PasswordState.ENCRYPTED else: # Neither the private data is encrypted or the file itself. password_state = PasswordState.NONE else: assert storage_info.kind == StorageKind.DATABASE password_state = PasswordState.PASSWORDED database_store = storage.get_database_store() is_too_modern = database_store.get("migration") > MIGRATION_CURRENT requires_upgrade = storage.requires_split( ) or storage.requires_upgrade() finally: storage.close() name = get_wallet_name_from_path(wallet_path) modification_time = os.path.getmtime(wallet_path) return FileState(name, wallet_path, wallet_action, storage_info.kind, password_state, requires_upgrade, modification_time, is_too_modern)
def _attempt_open_wallet(self, wallet_path: str, change_page: bool = False) -> bool: try: storage = WalletStorage(wallet_path) except Exception: logger.exception("problem looking at selected wallet '%s'", wallet_path) MessageBox.show_error( _("Unrecognised or unsupported wallet file.")) return False try: storage_info = categorise_file(wallet_path) if storage_info.kind == StorageKind.HYBRID: MessageBox.show_error( _("Unrecognised or unsupported wallet file.")) return False wallet_type = StorageKind.FILE if storage.is_legacy_format( ) else StorageKind.DATABASE if wallet_type == StorageKind.FILE: text_store = storage.get_text_store() text_store.attempt_load_data() if storage.get("use_encryption") or text_store.is_encrypted(): # If there is a password and the wallet is not encrypted, then the private data # is encrypted. If there is a password and the wallet is encrypted, then the # private data is encrypted and the file is encrypted. self._next_page_id = WalletPage.PREMIGRATION_PASSWORD_REQUEST else: # Neither the private data is encrypted or the file itself. self._next_page_id = WalletPage.PREMIGRATION_PASSWORD_ADDITION else: self._next_page_id = WalletPage.PREMIGRATION_PASSWORD_REQUEST finally: storage.close() self._force_completed = True wizard: WalletWizard = self.wizard() wizard.set_wallet_action(WalletAction.OPEN) wizard.set_wallet_type(wallet_type) wizard.set_wallet_path(wallet_path) if change_page: wizard.next() return True
def test_categorise_file(mock_exists, kind, side_effect) -> None: mock_exists.side_effect = side_effect fake_filepath = os.path.join("a", "b", "c", "walletfile") ret = categorise_file(fake_filepath) assert ret.filename == "walletfile" assert ret.kind == kind