def push(ctx, format, filename): """Gets data from a file and pushes it into the service. """ if filename: with open(filename, 'rb') as f: data = f.read() else: data = sys.stdin.read() dataset = tablib.Dataset() setattr(dataset, format, data) parsed_data = dataset.dict # filter out empty lines parsed_data = filter(None, parsed_data) try: _add_changelogs(ctx.obj, parsed_data) with handle(VersionNotFoundError, show_warning_about_missing_version): _tag_versions(ctx.obj, parsed_data) except HTTPApiError as e: if e.response.status_code == 401: click.echo('Please provide valid OAuth token in AMCH_TOKEN environment variable') else: raise
def create_changelog(opts, namespace, name, source=None, downloader=None): require_authentication(opts) def handle_api_error(e): data = e.response.json() if 'Changelog with this Namespace and Name already exists' in data.get( '__all__', [''])[0]: signal(NamespaceNameAlreadyExists(namespace, name)) elif 'already exists' in data.get('source', [''])[0]: signal(SourceAlreadyExists(source)) signal(e) with handle(HTTPApiError, handle_api_error): data = dict(namespace=namespace, name=name) if source and not downloader or \ downloader and not source: signal( DownloaderAndSourceError( 'Both downloader and source are required')) if source and downloader: data['source'] = source data['downloader'] = 'downloader' return _post(opts, '/changelogs/', data=data)
def test_when_restart_not_found(self): def choose_bar(e): invoke_restart('some_strange_restart') with handle(TestError, choose_bar): self.assertRaises(RestartNotFoundError, self.blah) assert_that(self.messages, contains('Calling foo', 'FOO before signal'))
def log_analyzer2(path): """This procedure considers every line which can't be parsed as a line with ERROR level. """ with handle(MalformedLogEntryError, lambda (c): invoke_restart('reparse', 'ERROR: ' + c.text)): for filename in find_all_logs(path): analyze_log(filename)
def log_analyzer(path): """This procedure replaces every line which can't be parsed with special object MalformedLogEntry. """ with handle(MalformedLogEntryError, lambda (c): invoke_restart('use_value', MalformedLogEntry(c.text))): for filename in find_all_logs(path): analyze_log(filename)
def test_with_restarts(self): def choose_bar(e): invoke_restart('choose_bar') with handle(TestError, choose_bar): self.blah() assert_that( self.messages, contains('Calling foo', 'FOO before signal', 'Calling bar instead of failed foo', 'BAR'))
def test_when_restart_not_found(self): def choose_bar(e): invoke_restart('some_strange_restart') with handle(TestError, choose_bar): self.assertRaises(RestartNotFoundError, self.blah) assert_that(self.messages, contains( 'Calling foo', 'FOO before signal'))
def test_when_multiple_restarts(self): def handle_error_using_value(e): invoke_restart('use_value', -1) def handle_error_trying_value(e): invoke_restart('try_value', 5) def handle_error_continuing(e): invoke_restart('throw', e) def good_function(b): def use_value(value): return value def try_value(value): return good_function(value) def throw(e): raise e with restarts(use_value, try_value, throw) as call: return call(bad_function, 1000, b) def bad_function(a, b): return a / b result = good_function(10) assert result == 100 with handle(ZeroDivisionError, handle_error_using_value): result = good_function(0) assert result == -1 with handle(ZeroDivisionError, handle_error_trying_value): result = good_function(0) assert result == 200 with handle(ZeroDivisionError, handle_error_continuing): self.assertRaises(ZeroDivisionError, good_function, 0)
def test_with_handler(self): def continue_execution(e): self.log('Continuing execution') with handle(TestError, continue_execution): self.foo() self.bar() assert_that( self.messages, contains('FOO before signal', 'Continuing execution', 'FOO after signal', 'BAR'))
def test_with_handler_for_parent_exception_class(self): def continue_execution(e): self.log('Continuing execution') # in this case, it should work as well with handle(RuntimeError, continue_execution): self.foo() self.bar() assert_that( self.messages, contains('FOO before signal', 'Continuing execution', 'FOO after signal', 'BAR'))
def test_with_restarts(self): def choose_bar(e): invoke_restart('choose_bar') with handle(TestError, choose_bar): self.blah() assert_that(self.messages, contains( 'Calling foo', 'FOO before signal', 'Calling bar instead of failed foo', 'BAR'))
def test_with_handler(self): def continue_execution(e): self.log('Continuing execution') with handle(TestError, continue_execution): self.foo() self.bar() assert_that(self.messages, contains('FOO before signal', 'Continuing execution', 'FOO after signal', 'BAR'))
def tag(ctx, project, version, tag): """Marks some project's version with given tag. Usually, tag is a project name where tagged version is used. For example: amch tag python/django 1.8.10 allmychanges.com You can use command 'amch tags' to list all tags and corresponded projects. """ try: opts = ctx.obj project_params = parse_project_params(project) project_obj = get_changelogs(opts, **project_params) if not project_obj: click.echo(u'Project "{0}" not found.'.format( project)) project_obj = project_obj[0] def print_error(e): click.echo('ERROR: {0.message}'.format(e)) with handle(CLIError, print_error), \ handle(VersionNotFoundError, show_warning_about_missing_version): _tag_version(opts, project_obj['namespace'], project_obj['name'], version, tag) except ApiError as e: report_api_error(e)
def test_with_handler_for_parent_exception_class(self): def continue_execution(e): self.log('Continuing execution') # in this case, it should work as well with handle(RuntimeError, continue_execution): self.foo() self.bar() assert_that(self.messages, contains('FOO before signal', 'Continuing execution', 'FOO after signal', 'BAR'))
def test_when_restart_returns_value(self): def use_value(value): return value def handle_error(e): invoke_restart('use_value', 12345) def good_function(): with restart(use_value) as call: return call(bad_function) def bad_function(): signal(RuntimeError()) with handle(RuntimeError, handle_error): result = good_function() assert result == 12345