def fixed_width_file_name(path, max_length=25): """ Return a fixed width file name of at most `max_length` characters extracted from the `path` string and usable for fixed width display. If the file_name is longer than `max_length`, it is truncated in the middle with using three dots "..." as an ellipsis and the extension is kept. For example: >>> short = fixed_width_file_name('0123456789012345678901234.c') >>> assert '0123456789...5678901234.c' == short """ if not path: return '' filename = fileutils.file_name(path) if len(filename) <= max_length: return filename base_name, extension = fileutils.splitext(filename) number_of_dots = 3 len_extension = len(extension) remaining_length = max_length - len_extension - number_of_dots if remaining_length < (len_extension + number_of_dots) or remaining_length < 5: return '' prefix_and_suffix_length = abs(remaining_length // 2) prefix = base_name[:prefix_and_suffix_length] ellipsis = number_of_dots * '.' suffix = base_name[-prefix_and_suffix_length:] return "{prefix}{ellipsis}{suffix}{extension}".format(**locals())
def fixed_width_file_name(path, max_length=25): """ Return a fixed width file name of at most `max_length` characters extracted from the `path` string and usable for fixed width display. If the file_name is longer than `max_length`, it is truncated in the middle with using three dots "..." as an ellipsis and the extension is kept. For example: >>> short = fixed_width_file_name('0123456789012345678901234.c') >>> assert '0123456789...5678901234.c' == short """ if not path: return '' # get the path as unicode for display! path = path_to_unicode(path) filename = fileutils.file_name(path) if len(filename) <= max_length: return filename base_name, extension = fileutils.splitext(filename) number_of_dots = 3 len_extension = len(extension) remaining_length = max_length - len_extension - number_of_dots if remaining_length < (len_extension + number_of_dots) or remaining_length < 5: return '' prefix_and_suffix_length = abs(remaining_length // 2) prefix = base_name[:prefix_and_suffix_length] ellipsis = number_of_dots * '.' suffix = base_name[-prefix_and_suffix_length:] return '{prefix}{ellipsis}{suffix}{extension}'.format(**locals())
def get_file_infos(location): """ Return a mapping of file information collected from the file or directory at `location`. """ from commoncode import fileutils from commoncode import filetype from commoncode.hash import multi_checksums from typecode import contenttype if on_linux: location = path_to_bytes(location) else: location = path_to_unicode(location) infos = OrderedDict() is_file = filetype.is_file(location) is_dir = filetype.is_dir(location) T = contenttype.get_type(location) infos['type'] = filetype.get_type(location, short=False) name = fileutils.file_name(location) if is_file: base_name, extension = fileutils.splitext(location) else: base_name = name extension = '' if on_linux: infos['name'] = path_to_unicode(name) infos['base_name'] = path_to_unicode(base_name) infos['extension'] = path_to_unicode(extension) else: infos['name'] = name infos['base_name'] = base_name infos['extension'] = extension infos['date'] = is_file and filetype.get_last_modified_date( location) or None infos['size'] = T.size infos.update(multi_checksums(location, ( 'sha1', 'md5', ))) infos['files_count'] = is_dir and filetype.get_file_count(location) or None infos['mime_type'] = is_file and T.mimetype_file or None infos['file_type'] = is_file and T.filetype_file or None infos['programming_language'] = is_file and T.programming_language or None infos['is_binary'] = bool(is_file and T.is_binary) infos['is_text'] = bool(is_file and T.is_text) infos['is_archive'] = bool(is_file and T.is_archive) infos['is_media'] = bool(is_file and T.is_media) infos['is_source'] = bool(is_file and T.is_source) infos['is_script'] = bool(is_file and T.is_script) return infos
def get_file_infos(location): """ Return a mapping of file information collected from the file or directory at `location`. """ from commoncode import fileutils from commoncode import filetype from commoncode.hash import multi_checksums from typecode import contenttype if on_linux: location = path_to_bytes(location) else: location = path_to_unicode(location) infos = OrderedDict() is_file = filetype.is_file(location) is_dir = filetype.is_dir(location) T = contenttype.get_type(location) infos['type'] = filetype.get_type(location, short=False) name = fileutils.file_name(location) if is_file: base_name, extension = fileutils.splitext(location) else: base_name = name extension = '' if on_linux: infos['name'] = path_to_unicode(name) infos['base_name'] = path_to_unicode(base_name) infos['extension'] = path_to_unicode(extension) else: infos['name'] = name infos['base_name'] = base_name infos['extension'] = extension infos['date'] = is_file and filetype.get_last_modified_date(location) or None infos['size'] = T.size infos.update(multi_checksums(location, ('sha1', 'md5',))) infos['files_count'] = is_dir and filetype.get_file_count(location) or None infos['mime_type'] = is_file and T.mimetype_file or None infos['file_type'] = is_file and T.filetype_file or None infos['programming_language'] = is_file and T.programming_language or None infos['is_binary'] = bool(is_file and T.is_binary) infos['is_text'] = bool(is_file and T.is_text) infos['is_archive'] = bool(is_file and T.is_archive) infos['is_media'] = bool(is_file and T.is_media) infos['is_source'] = bool(is_file and T.is_source) infos['is_script'] = bool(is_file and T.is_script) return infos
def fixed_width_file_name(path, max_length=25): """ Return a fixed width file name of at most `max_length` characters computed from the `path` string and usable for fixed width display. If the `path` file name is longer than `max_length`, the file name is truncated in the middle using three dots "..." as an ellipsis and the ext is kept. For example: >>> fwfn = fixed_width_file_name('0123456789012345678901234.c') >>> assert '0123456789...5678901234.c' == fwfn >>> fwfn = fixed_width_file_name('some/path/0123456789012345678901234.c') >>> assert '0123456789...5678901234.c' == fwfn >>> fwfn = fixed_width_file_name('some/sort.c') >>> assert 'sort.c' == fwfn >>> fwfn = fixed_width_file_name('some/123456', max_length=5) >>> assert '' == fwfn """ if not path: return '' # get the path as unicode for display! filename = file_name(path) if len(filename) <= max_length: return filename base_name, ext = splitext(filename) dots = 3 len_ext = len(ext) remaining_length = max_length - len_ext - dots if remaining_length < 5 or remaining_length < (len_ext + dots): return '' prefix_and_suffix_length = abs(remaining_length // 2) prefix = base_name[:prefix_and_suffix_length] ellipsis = dots * '.' suffix = base_name[-prefix_and_suffix_length:] return '{prefix}{ellipsis}{suffix}{ext}'.format(**locals())