def test_notify_parented(self): """Should notify through parent""" child = Response() parent = Response() parent.consume(child) child.notify('SUCCESS', 'Good Stuff', 'GO-CAULDRON') self.assertEqual(len(parent.messages), 1) m = parent.messages[0] self.assertEqual(m.code, 'GO-CAULDRON') self.assertEqual(m.kind, 'SUCCESS') self.assertEqual(m.message, 'Good Stuff')
def test_notify_parented(self): """Should notify through parent""" child = Response() parent = Response() parent.consume(child) child.notify('SUCCESS', 'Good Stuff', 'GO-CAULDRON') self.assertEqual(len(parent.messages), 1) m = parent.messages[0] self.assertEqual(m.code, 'GO-CAULDRON') self.assertEqual(m.kind, 'SUCCESS') self.assertEqual(m.message, 'Good Stuff')
def show_help(command_name: str = None, raw_args: str = '') -> Response: """ Prints the basic command help to the console """ response = Response() cmds = fetch() if command_name and command_name in cmds: parser, result = parse.get_parser(cmds[command_name], parse.explode_line(raw_args), dict()) if parser is not None: out = parser.format_help() return response.notify( kind='INFO', code='COMMAND_DESCRIPTION').kernel(commands=out).console( out, whitespace=1).response environ.log_header('Available Commands') response.consume(print_module_help()) return response.fail(code='NO_SUCH_COMMAND', message='Failed to show command help for "{}"'.format( command_name)).console( """ For more information on the various commands, enter help on the specific command: help [COMMAND] """, whitespace_bottom=1).response
def test_logging(self): """Should log messages to the log""" r = Response() r.notify( kind='TEST', code='TEST_MESSAGE', message='This is a test', ).console_header('Harold').console('Holly').console_raw('Handy') out = r.get_notification_log() self.assertGreater(out.find('Harold'), -1) self.assertGreater(out.find('Holly'), -1) self.assertGreater(out.find('Handy'), -1) r = Response.deserialize(r.serialize()) compare = r.get_notification_log() self.assertEqual(out, compare)
def sync_source_file(): """ """ r = Response() args = arguments.from_request() relative_path = args.get('relative_path') chunk = args.get('chunk') index = args.get('index', 0) sync_time = args.get('sync_time', -1) location = args.get('location', 'project') offset = args.get('offset', 0) if None in [relative_path, chunk]: return r.fail( code='INVALID_ARGS', message='Missing or invalid arguments' ).response.flask_serialize() project = cd.project.get_internal_project() if not project: return r.fail( code='NO_OPEN_PROJECT', message='No project is open. Unable to sync' ).response.flask_serialize() parts = relative_path.replace('\\', '/').strip('/').split('/') root_directory = project.source_directory if location == 'shared': root_directory = os.path.realpath(os.path.join( root_directory, '..', '__cauldron_shared_libs' )) file_path = os.path.join(root_directory, *parts) parent_directory = os.path.dirname(file_path) if not os.path.exists(parent_directory): os.makedirs(parent_directory) sync.io.write_file_chunk( file_path=file_path, packed_chunk=chunk, append=index > 0, offset=offset ) sync_status.update({}, time=sync_time) return r.notify( kind='SYNCED', code='SAVED_CHUNK', message='File chunk {} {}'.format(offset, file_path) ).console().response.flask_serialize()
def test_default_message_response(self, execute: MagicMock): """ should handle ResponseMessage returned from commander.execute """ r = Response() execute.return_value = r.notify() cmd = shell.CauldronShell() cmd.default('run something') self.assertEqual(cmd.last_response, r)
def test_default_message_response(self, execute: MagicMock): """ should handle ResponseMessage returned from commander.execute """ r = Response() execute.return_value = r.notify() cmd = shell.CauldronShell() cmd.default('run something') self.assertEqual(cmd.last_response, r)
def test_logging(self): """Should log messages to the log""" r = Response() r.notify( kind='TEST', code='TEST_MESSAGE', message='This is a test', ).console_header( 'Harold' ).console( 'Holly' ).console_raw( 'Handy' ) out = r.get_notification_log() self.assertGreater(out.find('Harold'), -1) self.assertGreater(out.find('Holly'), -1) self.assertGreater(out.find('Handy'), -1) r = Response.deserialize(r.serialize()) compare = r.get_notification_log() self.assertEqual(out, compare)
def sync_source_file(): """ """ r = Response() args = arguments.from_request() relative_path = args.get('relative_path') chunk = args.get('chunk') index = args.get('index', 0) sync_time = args.get('sync_time', -1) location = args.get('location', 'project') offset = args.get('offset', 0) if None in [relative_path, chunk]: return r.fail( code='INVALID_ARGS', message='Missing or invalid arguments').response.flask_serialize() project = cd.project.get_internal_project() if not project: return r.fail(code='NO_OPEN_PROJECT', message='No project is open. Unable to sync' ).response.flask_serialize() parts = relative_path.replace('\\', '/').strip('/').split('/') root_directory = project.source_directory if location == 'shared': root_directory = os.path.realpath( os.path.join(root_directory, '..', '__cauldron_shared_libs')) file_path = os.path.join(root_directory, *parts) parent_directory = os.path.dirname(file_path) if not os.path.exists(parent_directory): os.makedirs(parent_directory) sync.io.write_file_chunk(file_path=file_path, packed_chunk=chunk, append=index > 0, offset=offset) sync_status.update({}, time=sync_time) print('SAVED CHUNK:', offset, file_path) return r.notify(kind='SUCCESS', code='SAVED_CHUNK', message='Saved file chunk').response.flask_serialize()
def send( file_path: str, relative_path: str, file_kind: str = '', chunk_size: int = sync.io.DEFAULT_CHUNK_SIZE, remote_connection: 'environ.RemoteConnection' = None, newer_than: float = 0, progress_callback=None, sync_time: float = -1 ) -> Response: """ """ response = Response() sync_time = time.time() if sync_time < 0 else sync_time callback = ( progress_callback if progress_callback else (lambda x: x) ) modified_time = os.path.getmtime(file_path) if modified_time < newer_than: callback(response.notify( kind='SKIP', code='NOT_MODIFIED', message='No changes detected to "{}"'.format(relative_path), data=dict( file_path=file_path, relative_path=relative_path ) )) return response chunk_count = sync.io.get_file_chunk_count(file_path, chunk_size) chunks = sync.io.read_file_chunks(file_path, chunk_size) def get_progress(complete_count: int = 0) -> typing.Tuple[int, str]: """ """ if chunk_count < 2: return 0, '' progress_value = int(100 * complete_count / chunk_count) display = '({}%)'.format('{}'.format(progress_value).zfill(3)) return progress_value, display progress_display = get_progress(0)[-1] callback(response.notify( kind='SYNC', code='STARTED', message='{} "{}"'.format(progress_display, relative_path), data=dict( progress=0, file_path=file_path, relative_path=relative_path ) )) offset = 0 for index, chunk in enumerate(chunks): response = send_chunk( chunk=chunk, index=index, offset=offset, relative_path=relative_path, file_kind=file_kind, remote_connection=remote_connection, sync_time=sync_time ) offset += len(chunk) if response.failed: return response progress, progress_display = get_progress(index + 1) if chunk_count > 1 and (index + 1) < chunk_count: callback(response.notify( kind='SYNC', code='PROGRESS', message='{} "{}"'.format(progress_display, relative_path), data=dict( progress=0.01 * progress, chunk_count=chunk_count, file_path=file_path, relative_path=relative_path ) )) progress, progress_display = get_progress(chunk_count) callback(response.notify( kind='SYNC', code='DONE', message='{} "{}"'.format(progress_display, relative_path), data=dict( chunk_count=chunk_count, progress=progress, file_path=file_path, relative_path=relative_path ) )) return response
def send(file_path: str, relative_path: str, file_kind: str = '', chunk_size: int = sync.io.DEFAULT_CHUNK_SIZE, remote_connection: 'environ.RemoteConnection' = None, newer_than: float = 0, progress_callback=None, sync_time: float = -1) -> Response: """ """ response = Response() sync_time = time.time() if sync_time < 0 else sync_time callback = (progress_callback if progress_callback else (lambda x: x)) modified_time = os.path.getmtime(file_path) if modified_time < newer_than: callback( response.notify( kind='SKIP', code='NOT_MODIFIED', message='No changes detected to "{}"'.format(relative_path), data=dict(file_path=file_path, relative_path=relative_path))) return response chunk_count = sync.io.get_file_chunk_count(file_path, chunk_size) chunks = sync.io.read_file_chunks(file_path, chunk_size) def get_progress(complete_count: int = 0) -> typing.Tuple[int, str]: """ """ if chunk_count < 2: return 0, '' progress_value = int(100 * complete_count / chunk_count) display = '({}%)'.format('{}'.format(progress_value).zfill(3)) return progress_value, display progress_display = get_progress(0)[-1] callback( response.notify(kind='SYNC', code='STARTED', message='{} "{}"'.format(progress_display, relative_path), data=dict(progress=0, file_path=file_path, relative_path=relative_path))) for index, chunk in enumerate(chunks): response = send_chunk(chunk=chunk, index=index, relative_path=relative_path, file_kind=file_kind, remote_connection=remote_connection, sync_time=sync_time) if response.failed: return response progress, progress_display = get_progress(index + 1) if chunk_count > 1 and (index + 1) < chunk_count: callback( response.notify(kind='SYNC', code='PROGRESS', message='{} "{}"'.format( progress_display, relative_path), data=dict(progress=0.01 * progress, chunk_count=chunk_count, file_path=file_path, relative_path=relative_path))) progress, progress_display = get_progress(chunk_count) callback( response.notify(kind='SYNC', code='DONE', message='{} "{}"'.format(progress_display, relative_path), data=dict(chunk_count=chunk_count, progress=progress, file_path=file_path, relative_path=relative_path))) return response