How to use the codalab.lib.formatting function in codalab

To help you get started, we’ve selected a few codalab examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github codalab / codalab-worksheets / codalab / lib / worksheet_util.py View on Github external
"""
    Generator that returns pretty-printed lines of text for the given worksheet.
    """
    lines = []
    for item in worksheet_info['items']:
        (bundle_info, subworksheet_info, value_obj, item_type) = item

        if item_type == TYPE_MARKUP:
            lines.append(value_obj)
        elif item_type == TYPE_DIRECTIVE:
            if len(value_obj) > 0 and value_obj[0] == DIRECTIVE_CHAR:
                # A comment directive
                lines.append('//' + ' '.join(value_obj[1:]))
            else:
                # A normal directive
                value = formatting.tokens_to_string(value_obj)
                value = (
                    DIRECTIVE_CHAR
                    + ('' if len(value) == 0 or value.startswith(DIRECTIVE_CHAR) else ' ')
                    + value
                )
                lines.append(value)
        elif item_type == TYPE_BUNDLE:
            if 'metadata' not in bundle_info:
                # This happens when we add bundles by uuid and don't actually make sure they exist
                # lines.append('ERROR: non-existent bundle %s' % bundle_info['uuid'])
                description = formatting.contents_str(None)
            else:
                metadata = bundle_info['metadata']
                # raise Exception(metadata)
                description = bundle_info['bundle_type']
                description += ' ' + metadata['name']
github codalab / codalab-worksheets / codalab / machines / remote_machine.py View on Github external
def __init__(self, config):
        self.verbose = config.get('verbose', 1)

        # Mandatory configuration
        self.dispatch_command = config['dispatch_command']
        self.default_docker_image = config['docker_image']
        def parse(to_value, field):
            return to_value(config[field]) if field in config else None
        self.default_request_time = parse(formatting.parse_duration, 'default_request_time')
        self.default_request_memory = parse(formatting.parse_size, 'default_request_memory')
        self.default_request_disk = parse(formatting.parse_size, 'default_request_disk')
        self.max_request_time = parse(formatting.parse_duration, 'max_request_time')
        self.max_request_memory = parse(formatting.parse_size, 'max_request_memory')
        self.max_request_disk = parse(formatting.parse_size, 'max_request_disk')

        self.default_request_cpus = config.get('request_cpus')
        self.default_request_gpus = config.get('request_gpus')
        self.default_request_queue = config.get('request_queue')
        self.default_request_priority = config.get('request_priority')
        self.default_request_network = config.get('request_network')
github codalab / codalab-worksheets / codalab / objects / user.py View on Github external
def check_quota(self, need_time=False, need_disk=False):
        if need_time:
            if self.time_used >= self.time_quota:
                raise UsageError(
                    'Out of time quota: %s'
                    % formatting.ratio_str(formatting.duration_str, self.time_used, self.time_quota)
                )
        if need_disk:
            if self.disk_used >= self.disk_quota:
                raise UsageError(
                    'Out of disk quota: %s'
                    % formatting.ratio_str(formatting.size_str, self.disk_used, self.disk_quota)
                )
github codalab / codalab-worksheets / codalab / lib / bundle_cli.py View on Github external
def _worksheet_description(self, worksheet_info):
        fields = [
            ('Worksheet', self.worksheet_str(worksheet_info)),
            ('Title', formatting.verbose_contents_str(worksheet_info['title'])),
            ('Tags', ' '.join(worksheet_info['tags'])),
            ('Owner', self.simple_user_str(worksheet_info['owner']) +
                (' [anonymous]' if worksheet_info['is_anonymous'] else '')),
            ('Permissions', group_permissions_str(worksheet_info['group_permissions']) +
                (' [frozen]' if worksheet_info['frozen'] else '')),
        ]
        return '\n'.join('### %s: %s' % (k, v) for k, v in fields)
github codalab / codalab-worksheets / codalab / rest / interpret.py View on Github external
block['bundle_info'][j] = None
                if not is_relevant_block:
                    interpreted_blocks['blocks'][i] = None

    worksheet_info['items'] = resolve_interpreted_blocks(interpreted_blocks['blocks'])
    worksheet_info['raw_to_block'] = interpreted_blocks['raw_to_block']
    worksheet_info['block_to_raw'] = interpreted_blocks['block_to_raw']

    for item in worksheet_info['items']:
        if item is None:
            continue
        if item['mode'] == 'table':
            for row_map in item['rows']:
                for k, v in row_map.items():
                    if v is None:
                        row_map[k] = formatting.contents_str(v)
        if 'bundle_info' in item:
            infos = []
            if isinstance(item['bundle_info'], list):
                infos = item['bundle_info']
            elif isinstance(item['bundle_info'], dict):
                infos = [item['bundle_info']]
            for bundle_info in infos:
                if bundle_info is None:
                    continue
                if 'bundle_type' not in bundle_info:
                    continue  # empty info: invalid bundle reference
                if isinstance(bundle_info, dict):
                    format_metadata(bundle_info.get('metadata'))
    if bundle_uuids:
        return {'items': worksheet_info['items']}
    return worksheet_info
github codalab / codalab-worksheets / codalab / lib / worksheet_util.py View on Github external
args += [bundle_type]
        # Dependencies
        for dep in deps:
            args.append(render_dep(dep, show_uuid=True))
        # Command
        if bundle_info['command']:
            args.append(formatting.quote(bundle_info['command']))
        # Add request arguments from metadata
        metadata = bundle_info['metadata']
        for key, value in metadata.items():
            if key.startswith('request_') and value:
                key = key.replace('_', '-')
                if isinstance(value, bool):
                    args.append('--' + key)
                else:
                    args.extend(['--' + key, formatting.quote(str(value))])
        return ' '.join(args)
    elif genpath == 'summary':

        def friendly_render_dep(dep):
            key = dep['child_path'] or dep['parent_name']
            friendly_parent_name = formatting.verbose_contents_str(dep['parent_name'])
            value = (
                key
                + '{'
                + (friendly_parent_name + ':' if key != dep['parent_name'] else '')
                + dep['parent_uuid'][0:4]
                + '}'
            )
            return key, value

        # Nice easy-to-ready description of how this bundle got created.
github codalab / codalab-worksheets / codalab / lib / codalab_manager.py View on Github external
def default_user_info(self):
        info = self.config['server'].get(
            'default_user_info', {'time_quota': '1y', 'disk_quota': '1t', 'parallel_run_quota': 3}
        )
        return {
            'time_quota': formatting.parse_duration(info['time_quota']),
            'disk_quota': formatting.parse_size(info['disk_quota']),
            'parallel_run_quota': info['parallel_run_quota'],
        }
github codalab / codalab-worksheets / codalab / server / rest_server.py View on Github external
def report_exception(self, exc):
        query = formatting.key_value_list(request.query.allitems())
        forms = formatting.key_value_list(
            self._censor_passwords(request.forms.allitems()) if request.json is None else [])
        body = formatting.verbose_pretty_json(request.json)
        local_vars = formatting.key_value_list(
            self._censor_passwords(server_util.exc_frame_locals().items()))
        aux_info = textwrap.dedent("""\
                    Query params:
                    {0}

                    Form params:
                    {1}

                    JSON body:
                    {2}

                    Local variables:
                    {3}""").format(query, forms, body, local_vars)

        if len(aux_info) > self.MAX_AUX_INFO_LENGTH:
github codalab / codalab-worksheets / codalab / server / rest_server.py View on Github external
def report_exception(self, exc):
        query = formatting.key_value_list(request.query.allitems())
        forms = formatting.key_value_list(
            self._censor_passwords(request.forms.allitems()) if request.json is None else [])
        body = formatting.verbose_pretty_json(request.json)
        local_vars = formatting.key_value_list(
            self._censor_passwords(server_util.exc_frame_locals().items()))
        aux_info = textwrap.dedent("""\
                    Query params:
                    {0}

                    Form params:
                    {1}

                    JSON body:
                    {2}

                    Local variables: