def _convert_items_from_db(self, items): ''' Helper function. (bundle_uuid, subworksheet_uuid, value, type) -> (bundle_info, subworksheet_info, value_obj, type) ''' # Database only contains the uuid; need to expand to info. # We need to do to convert the bundle_uuids into bundle_info dicts. # However, we still make O(1) database calls because we use the # optimized batch_get_bundles multiget method. bundle_uuids = set( bundle_uuid for (bundle_uuid, subworksheet_uuid, value, type) in items if bundle_uuid is not None ) bundle_dict = self.get_bundle_infos(bundle_uuids) # Go through the items and substitute the components new_items = [] for (bundle_uuid, subworksheet_uuid, value, type) in items: bundle_info = bundle_dict.get(bundle_uuid, {'uuid': bundle_uuid}) if bundle_uuid else None if subworksheet_uuid: subworksheet_info = self.model.get_worksheet(subworksheet_uuid, fetch_items=False).to_dict() else: subworksheet_info = None value_obj = worksheet_util.string_to_tokens(value) if type == worksheet_util.TYPE_DIRECTIVE else value new_items.append((bundle_info, subworksheet_info, value_obj, type)) return new_items
def _convert_items_from_db(self, items): ''' Helper function. (bundle_uuid, subworksheet_uuid, value, type) -> (bundle_info, subworksheet_info, value_obj, type) ''' # Database only contains the uuid; need to expand to info. # We need to do to convert the bundle_uuids into bundle_info dicts. # However, we still make O(1) database calls because we use the # optimized batch_get_bundles multiget method. bundle_uuids = set( bundle_uuid for (bundle_uuid, subworksheet_uuid, value, type) in items if bundle_uuid is not None ) bundle_dict = self.get_bundle_infos(bundle_uuids) # Go through the items and substitute the components new_items = [] for (bundle_uuid, subworksheet_uuid, value, type) in items: bundle_info = bundle_dict.get(bundle_uuid, {'uuid': bundle_uuid}) if bundle_uuid else None if subworksheet_uuid: try: subworksheet_info = self.model.get_worksheet(subworksheet_uuid, fetch_items=False).to_dict() except UsageError, e: # If can't get the subworksheet, it's probably invalid, so just replace it with an error #type = worksheet_util.TYPE_MARKUP subworksheet_info = {'uuid': subworksheet_uuid} #value = 'ERROR: non-existent worksheet %s' % subworksheet_uuid else: subworksheet_info = None value_obj = worksheet_util.string_to_tokens(value) if type == worksheet_util.TYPE_DIRECTIVE else value new_items.append((bundle_info, subworksheet_info, value_obj, type))
def general_command(self, worksheet_uuid, command): cli = self._create_cli(worksheet_uuid) args = worksheet_util.string_to_tokens(command) def do_command(): from cStringIO import StringIO import sys real_stdout = sys.stdout sys.stdout = StringIO() stdout_str = None #real_stderr = sys.stderr #sys.stderr = StringIO() stderr_str = None exception = None try: cli.do_command(args) success = True except BaseException as e: # To capture SystemExit exception = e success = False stdout_str = sys.stdout.getvalue() sys.stdout.close() sys.stdout = real_stdout #stderr_str = sys.stderr.getvalue() #sys.stderr.close() #sys.stderr = real_stderr print '>>> general_command on worksheet %s: %s' % (worksheet_uuid, command) print stdout_str print stderr_str return {'stdout': stdout_str, 'stderr': stderr_str, 'exception': str(exception) if exception else None} return _call_with_retries(do_command)
def general_command(self, worksheet_uuid, command): cli = self._create_cli(worksheet_uuid) args = worksheet_util.string_to_tokens(command) def do_command(): from cStringIO import StringIO import sys real_stdout = sys.stdout sys.stdout = StringIO() stdout_str = None #real_stderr = sys.stderr #sys.stderr = StringIO() stderr_str = None exception = None try: cli.do_command(args) success = True except BaseException as e: # To capture SystemExit exception = e success = False stdout_str = sys.stdout.getvalue() sys.stdout.close() sys.stdout = real_stdout #stderr_str = sys.stderr.getvalue() #sys.stderr.close() #sys.stderr = real_stderr print '>>> general_command on worksheet %s: %s' % ( worksheet_uuid, command) print stdout_str print stderr_str return { 'stdout': stdout_str, 'stderr': stderr_str, 'exception': str(exception) if exception else None } return _call_with_retries(do_command)
def general_command(self, worksheet_uuid, command): cli = self._create_cli(worksheet_uuid) args = worksheet_util.string_to_tokens(command) def do_command(): from cStringIO import StringIO import sys real_stdout = sys.stdout sys.stdout = StringIO() stdout_str = None real_stderr = sys.stderr sys.stderr = StringIO() stderr_str = None exception = None structured_result = None try: structured_result = cli.do_command(args) success = True except SystemExit as e: pass # stderr will will tell the user the error except BaseException as e: exception = smart_str(e) success = False stdout_str = sys.stdout.getvalue() sys.stdout.close() sys.stdout = real_stdout stderr_str = sys.stderr.getvalue() sys.stderr.close() sys.stderr = real_stderr print '>>> general_command on worksheet %s: %s' % (worksheet_uuid, command) print stdout_str print stderr_str return {'structured_result': structured_result, 'stdout': stdout_str, 'stderr': stderr_str, 'exception': str(exception) if exception else None} return _call_with_retries(do_command)