def zip_tree(rsc): list = zip_list(rsc) def get_icon(item): extension = item.split('.')[-1].lower() if extension in ['xml', 'txt', 'json']: return "file-text" if extension in ['csv', 'xls']: return "bar-chart" if extension in ['shp', 'geojson', 'kml', 'kmz']: return "globe" return "file" if not list: return None tree = OrderedDict() for compressed_file in list: if "/" not in compressed_file.filename: tree[compressed_file.filename] = {"title": compressed_file.filename, "file_size": (formatters.localised_filesize(compressed_file.file_size)), "children": [], "icon": get_icon(compressed_file.filename)} else: parts = compressed_file.filename.split("/") if parts[-1] != "": child = {"title": parts.pop(), "file_size": (formatters.localised_filesize(compressed_file.file_size)), "children": [], "icon": get_icon(compressed_file.filename)} parent = '/'.join(parts) if parent not in tree: tree[parent] = {"title": parent, "children": [], "icon": 'folder-open'} tree[parent]['children'].append(child) return tree.values()
def zip_tree(rsc): list = zip_list(rsc) def get_icon(item): extension = item.split('.')[-1].lower() if extension in ['xml', 'txt', 'json']: return "file-text" if extension in ['csv', 'xls']: return "bar-chart-o" if extension in ['shp', 'geojson', 'kml', 'kmz']: return "globe" return "file" if not list: return None tree = OrderedDict() for compressed_file in list: if "/" not in compressed_file.filename: tree[compressed_file.filename] = {"title": compressed_file.filename, "file_size": (formatters.localised_filesize(compressed_file.file_size)), "children": [], "icon": get_icon(compressed_file.filename)} else: parts = compressed_file.filename.split("/") if parts[-1] != "": child = {"title": re.sub(r'[^\x00-\x7f]',r'', parts.pop()), "file_size": (formatters.localised_filesize(compressed_file.file_size)), "children": [], "icon": get_icon(re.sub(r'[^\x00-\x7f]',r'',compressed_file.filename))} parent = '/'.join(parts) if parent not in tree: tree[parent] = {"title": parent, "children": [], "icon": 'folder-open'} tree[parent]['children'].append(child) return tree.values()
def format_resource_items(items): ''' Take a resource item list and format nicely with blacklisting etc. ''' blacklist = ['name', 'description', 'url', 'tracking_summary'] output = [] # regular expressions for detecting types in strings reg_ex_datetime = '^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{6})?$' reg_ex_int = '^-?\d{1,}$' reg_ex_float = '^-?\d{1,}\.\d{1,}$' for key, value in items: if not value or key in blacklist: continue # size is treated specially as we want to show in MiB etc if key == 'size': value = formatters.localised_filesize(int(value)) elif isinstance(value, basestring): # check if strings are actually datetime/number etc if re.search(reg_ex_datetime, value): datetime_ = date_str_to_datetime(value) value = formatters.localised_nice_date(datetime_) elif re.search(reg_ex_float, value): value = formatters.localised_number(float(value)) elif re.search(reg_ex_int, value): value = formatters.localised_number(int(value)) elif isinstance(value, int) or isinstance(value, float): value = formatters.localised_number(value) key = key.replace('_', ' ') output.append((key, value)) return sorted(output, key=lambda x: x[0])
def format_resource_items(items): ''' Take a resource item list and format nicely with blacklisting etc. ''' blacklist = ['name', 'description', 'url', 'tracking_summary', 'format', 'position', 'is_local_resource', 'datastore_active', 'on_same_domain', 'mimetype', 'state', 'url_type', 'has_views'] output = [] # regular expressions for detecting types in strings reg_ex_datetime = '^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{6})?$' reg_ex_int = '^-?\d{1,}$' reg_ex_float = '^-?\d{1,}\.\d{1,}$' for key, value in items: if not value or key in blacklist: continue # size is treated specially as we want to show in MiB etc if key == 'size': try: value = formatters.localised_filesize(int(value)) except ValueError: # Sometimes values that can't be converted to ints can sneak # into the db. In this case, just leave them as they are. pass elif isinstance(value, basestring): # check if strings are actually datetime/number etc if re.search(reg_ex_datetime, value): datetime_ = date_str_to_datetime(value) value = formatters.localised_nice_date(datetime_) elif re.search(reg_ex_float, value): value = formatters.localised_number(float(value)) elif re.search(reg_ex_int, value): value = formatters.localised_number(int(value)) elif ((isinstance(value, int) or isinstance(value, float)) and value not in (True, False)): value = formatters.localised_number(value) key = key.replace('_', ' ') output.append((key, value)) return sorted(output, key=lambda x: x[0])
def filesize_formatter(size): """Returns a localised unicode representation of a number in bytes, MiB etc :rtype: string """ try: return formatters.localised_filesize(int(size)) except (AttributeError, ValueError): # already formatted or unable to run formatter return size
def get_resource_file_size(rsc): if rsc.get('url_type') == 'upload': upload = uploader.ResourceUpload(rsc) value = None try: value = os.path.getsize(upload.get_path(rsc['id'])) value = formatters.localised_filesize(int(value)) except Exception: # Sometimes values that can't be converted to ints can sneak # into the db. In this case, just leave them as they are. pass return value return None
def format_resource_items(items): ''' Take a resource item list and format nicely with blacklisting etc. ''' blacklist = [ 'name', 'description', 'url', 'tracking_summary', 'format', 'position', 'is_local_resource' ] output = [] translations = [ _('created'), _('format'), _('has views'), _('last modified'), _('package id'), _('revision id'), _('state'), _('url type'), _('active'), _('upload'), _('position') ] # regular expressions for detecting types in strings reg_ex_datetime = '^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{6})?$' reg_ex_int = '^-?\d{1,}$' reg_ex_float = '^-?\d{1,}\.\d{1,}$' for key, value in items: if not value or key in blacklist: continue # size is treated specially as we want to show in MiB etc if key == 'size': try: value = formatters.localised_filesize(int(value)) except ValueError: # Sometimes values that can't be converted to ints can sneak # into the db. In this case, just leave them as they are. pass elif isinstance(value, basestring): # check if strings are actually datetime/number etc if re.search(reg_ex_datetime, value): datetime_ = date_str_to_datetime(value) value = formatters.localised_nice_date(datetime_) elif re.search(reg_ex_float, value): value = formatters.localised_number(float(value)) elif re.search(reg_ex_int, value): value = formatters.localised_number(int(value)) elif isinstance(value, int) or isinstance(value, float): value = formatters.localised_number(value) key = key.replace('_', ' ') key = _(key) output.append((key, value)) return sorted(output, key=lambda x: x[0])
def size_or_link(upload, size): '''Returns a string with a localised filesize From ckan/lib/formatters.py localised_filesize() ''' import ckan.lib.formatters as f if not size: return '' size = int(size) if upload and size > 0: return '(%s)' % f.localised_filesize(size) else: return ''
def size_or_link(upload, size): '''Returns a string with a localised filesize or an external link From ckan/lib/formatters.py localised_filesize() ''' import ckan.lib.formatters as f if not size: return '' size = int(size) if upload and size > 0: return f.localised_filesize(size) elif not upload: return 'external link' else: return ''
def format_resource_items(items): ''' Take a resource item list and format nicely with blacklisting etc. ''' blacklist = ['name', 'description', 'url', 'tracking_summary', 'format', 'position', 'is_local_resource', 'cache_url', 'can_be_previewed', 'hash', 'datastore_active', 'on_same_domain', 'mimetype', 'state', 'url_type', 'has_views', 'cache_last_updated', 'mimetype_inner', 'resource_type', '_authentication_token', 'ckan_url', 'datastore_contains_all_records_of_source_file', 'ignore_hash', 'original_url', 'resource_id', 'set_url_type', 'task_created', 'package_id', 'id', 'revision_id'] output = [] additioanl_fields = ['reference_number', 'demarcation', 'Language', 'spatial_coverage', 'geodetic'] # regular expressions for detecting types in strings reg_ex_datetime = '^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{6})?$' reg_ex_int = '^-?\d{1,}$' reg_ex_float = '^-?\d{1,}\.\d{1,}$' for key, value in items: if key in blacklist: continue # size is treated specially as we want to show in MiB etc if key == 'size': try: # in case of a url if value is None: continue value = formatters.localised_filesize(int(value)) except ValueError, e: # Sometimes values that can't be converted to ints can sneak # into the db. In this case, just leave them as they are. pass elif isinstance(value, basestring): try: # check if strings are actually datetime/number etc if re.search(reg_ex_datetime, value): datetime_ = date_str_to_datetime(value) value = formatters.localised_nice_date(datetime_) elif re.search(reg_ex_float, value): value = formatters.localised_number(float(value)) elif re.search(reg_ex_int, value) and key not in additioanl_fields: value = formatters.localised_number(int(value)) except ValueError, e: log.info(e.message) pass
def test_localized_filesize(size, expected): assert f.localised_filesize(size) == expected
def format_resource_filesize(size): import ckan.lib.formatters as formatters return formatters.localised_filesize(int(size))
def format_resource_filesize(size): """ Show a file size, formatted for humans. """ return formatters.localised_filesize(int(size))
def filesize_formatter(size): """Returns a localised unicode representation of a number in bytes, MiB etc :rtype: string """ return formatters.localised_filesize(int(size))