def read(file_path, **kwargs): """ Read a file path, find the format parser based on the file extension. """ if isinstance(file_path, (str, unicode)) is False: msg = 'file path must be a string, got %r' raise TypeError(msg % type(file_path)) if os.path.isfile(file_path) is False: msg = 'file path does not exist; %r' raise OSError(msg % file_path) file_format_class = None mgr = fmtmgr.get_format_manager() for fmt in mgr.get_formats(): attr = getattr(fmt, 'file_exts', None) if attr is None: continue if not isinstance(fmt.file_exts, list): continue for ext in fmt.file_exts: if file_path.endswith(ext): file_format_class = fmt break if file_format_class is None: msg = 'No file formats found for file path: %r' raise RuntimeError(msg % file_path) file_format_obj = file_format_class() file_info, mkr_data_list = file_format_obj.parse(file_path, **kwargs) return file_info, mkr_data_list
def get_file_filter(): """ Construct a string to be given to QFileDialog as a file filter. :return: String of file filters, separated by ';;' characters. :rtype: str """ file_fmt_names = [] file_exts = [] fmt_mgr = formatmanager.get_format_manager() fmts = fmt_mgr.get_formats() for fmt in fmts: file_fmt_names.append(fmt.name) file_exts += fmt.file_exts file_fmt_names = sorted(file_fmt_names) file_exts = sorted(file_exts) extensions_str = '' for file_ext in file_exts: extensions_str += '*' + file_ext + ' ' file_filter = 'Marker Files (%s);;' % extensions_str for name in file_fmt_names: name = name + ';;' file_filter += name file_filter += 'All Files (*.*);;' return file_filter
def get_file_path_format(text): """ Look up the Format from the file path. :param text: File path text. :returns: Format for the file path, or None if not found. :rtype: None or Format """ format_ = None if isinstance(text, (str, unicode)) is False: return format_ if os.path.isfile(text) is False: return format_ fmt_mgr = formatmanager.get_format_manager() fmts = fmt_mgr.get_formats() ext_to_fmt = {} for fmt in fmts: for ext in fmt.file_exts: ext_to_fmt[ext] = fmt for ext, fmt in ext_to_fmt.items(): if text.endswith(ext): format_ = fmt break return format_
def _get_file_path_formats(text): """ Look up the possible Formats for the file path. :param text: File path text. :returns: List of formats for the file path. :rtype: [Format, ..] """ formats = [] if isinstance(text, pycompat.TEXT_TYPE) is False: return formats if os.path.isfile(text) is False: return formats fmt_mgr = formatmanager.get_format_manager() fmts = fmt_mgr.get_formats() ext_to_fmt = {} for fmt in fmts: for ext in fmt.file_exts: ext_to_fmt[ext] = fmt for ext, fmt in ext_to_fmt.items(): if text.endswith(ext): formats.append(fmt) break return formats
def read(file_path, **kwargs): """ Read a file path, find the format parser based on the file extension. """ if isinstance(file_path, pycompat.TEXT_TYPE) is False: msg = 'file path must be a string, got %r' raise TypeError(msg % type(file_path)) if os.path.isfile(file_path) is False: msg = 'file path does not exist; %r' raise OSError(msg % file_path) file_format_classes = [] mgr = fmtmgr.get_format_manager() for fmt in mgr.get_formats(): attr = getattr(fmt, 'file_exts', None) if attr is None: continue if not isinstance(fmt.file_exts, list): continue for ext in fmt.file_exts: if file_path.endswith(ext): file_format_classes.append(fmt) if len(file_format_classes) == 0: msg = 'No file formats found for file path: %r' raise RuntimeError(msg % file_path) file_info = None mkr_data_list = [] for file_format_class in file_format_classes: file_format_obj = file_format_class() try: contents = file_format_obj.parse(file_path, **kwargs) except (interface.ParserError, OSError): contents = (None, []) file_info, mkr_data_list = contents if (file_info and (isinstance(mkr_data_list, list) and len(mkr_data_list) > 0)): break return file_info, mkr_data_list
file_exts = ['.uv'] args = [] def parse(self, file_path, **kwargs): """ Decodes a file path into a list of MarkerData. kwargs is not used. :param file_path: The file path to parse. :type file_path: str :param kwargs: There are no custom keyword arguments used. :return: List of MarkerData """ version = determine_format_version(file_path) if version == const.UV_TRACK_FORMAT_VERSION_1: mkr_data_list = parse_v1(file_path) elif version == const.UV_TRACK_FORMAT_VERSION_2: mkr_data_list = parse_v2(file_path) else: msg = 'Could not determine format version for UV Track file.' raise interface.ParserError(msg) return mkr_data_list # Register the File Format mgr = fmtmgr.get_format_manager() mgr.register_format(LoaderUVTrack)