20. filetype
— Common file type specifications¶
This module defines the Filetype
class, which holds the basic
information about one of the common file types used by pyFormex. The user
can define more file types if needed. All file types are collected in
a single dict Filetypes
using a simple mnemonic string as key.
Furthermore this module defines some functions to help in the use of
file types, e.g. in dialogs.
20.1. Classes defined in module filetype¶
- class filetype.Filetype(key, *args, **kargs)[source]¶
A class for holding file types and the related filename patterns.
- Parameters:
key (str) – A short and unique mnemonic string, by preference lower case, that wil be used as lookup key for this Filetype definition in the global
Filetypes
collection.text (str) – A textual description of the file type.
*suffixes (sequence of str) – All remaining parameters are file suffixes that should be used to filter files that are supposed to be of this type. Any number of suffixes is allowed. If None are provided, all files will match the file type.
Examples
>>> for key in Filetype.dict: print(f"{key} = '{Filetype[key]}'") all = 'All files (*)' ccx = 'CalCuliX files (*.dat *.inp)' dcm = 'DICOM images (*.dcm)' dxf = 'AutoCAD DXF files (*.dxf)' dxftext = 'Converted AutoCAD files (*.dxftext)' flavia = 'flavia results (*.flavia.msh *.flavia.res)' gts = 'GTS files (*.gts)' gz = 'Compressed files (*.gz *.bz2)' html = 'Web pages (*.html)' icon = 'Icons (*.xpm)' img = 'Images (*.png *.jpg *.jpeg *.eps *.gif *.bmp)' inp = 'Abaqus or CalCuliX input files (*.inp)' neu = 'Gambit Neutral files (*.neu)' obj = 'Wavefront OBJ files (*.obj)' off = 'Geomview object files (*.off)' pgf = 'pyFormex geometry files (*.pgf)' ply = 'Stanford Polygon File Format files (*.ply)' png = 'PNG images (*.png)' poly = 'Polygon files (*.off *.obj *.ply)' postproc = 'Postproc scripts (*.post.py)' project = 'pyFormex Projects (*.pzf *.hdf5 *.pyf)' pyformex = 'pyFormex scripts (*.py)' pyf = 'pyFormex projects (*.pyf)' python = 'Python files (*.py)' pzf = 'pyFormex zip files (*.pzf)' smesh = 'Tetgen surface mesh files (*.smesh)' stl = 'STL files (*.stl)' stlb = 'Binary STL files (*.stl)' surface = 'Surface models (*.gts *.stl *.off *.obj *.ply)' tetsurf = 'Tetgen surface (*.smesh)' tetgen = 'Tetgen files (*.poly *.smesh *.ele *.face *.edge *.node *.neigh)' text = 'Text files (*.txt *.out *.rst *.md *.py *.html)' video = 'Video (*.mp4)' vtk = 'VTK types (*.vtk *.vtp)' vtp = 'vtkPolyData file (*.vtp)' geometry = 'Geometry files (*.gts *.inp *.neu *.obj *.off *.pgf *.ply *.pzf *.stl *.vtk *.vtp)'
- suffixes(compr=False)[source]¶
Return a list of file suffixes for the Filetype.
- Parameters:
compr (bool) – If True, the file suffixes for compressed files of this type are automatically added.
Examples
>>> Filetype['pgf'].suffixes() ['pgf'] >>> Filetype['pgf'].suffixes(compr=True) ['pgf', 'pgf.gz', 'pgf.bz2'] >>> Filetype['all'].suffixes() []
- patterns(compr=False)[source]¶
Return a list with the file patterns matching the Filetype.
- Parameters:
compr (bool) – If True, the file suffixes for compressed files of this type are automatically added.
Examples
>>> Filetype['pgf'].patterns() ['*.pgf'] >>> Filetype['pgf'].patterns(compr=True) ['*.pgf', '*.pgf.gz', '*.pgf.bz2'] >>> Filetype['all'].patterns() ['*']
- desc(compr=False)[source]¶
Create a filetype descriptor compatible with Qt Widgets.
- Parameters:
compr (bool) – If True, the file patterns for compressed files are automatically added.
- Returns:
str – A string that can be directly used in the Qt File Dialog widgets to filter the selectable files. This string has the format
file type text (*.ext1 *.ext2)
.
Examples
>>> Filetype['img'].desc() 'Images (*.png *.jpg *.jpeg *.eps *.gif *.bmp)' >>> fileDescriptor('inp') 'Abaqus or CalCuliX input files (*.inp)' >>> fileDescriptor('doc') 'DOC files (*.doc)' >>> fileDescriptor('*.inp') '*.inp' >>> fileDescriptor('pgf',compr=True) 'pyFormex geometry files (*.pgf *.pgf.gz *.pgf.bz2)'
20.2. Functions defined in module filetype¶
- filetype.fileDescriptor(ftype, compr=False)[source]¶
Return a description of the specified file type(s).
The description is in a format understood by the Qt QFileDialog widget.
- Parameters:
ftype (str or list of str) – The file type (or types) for which a description is requested. The case of the string(s) is ignored: it is converted to lower case.
- Returns:
str or list of str – The file description(s) corresponding with the specified file type(s). The return value(s) depend(s) on the value of the input string(s) in the the following way (see Examples below):
if it is a key in the
file_description
dict, the corresponding value is returned;if it is a string of only alphanumerical characters: it is interpreted as a file extension and the corresponding return value is
FTYPE files (*.ftype)
;any other string is returned as as: this allows the user to compose his filters himself.
Examples
>>> fileDescriptor('img') 'Images (*.png *.jpg *.jpeg *.eps *.gif *.bmp)' >>> fileDescriptor(['stl','all']) ['STL files (*.stl)', 'All files (*)'] >>> fileDescriptor('inp') 'Abaqus or CalCuliX input files (*.inp)' >>> fileDescriptor('doc') 'DOC files (*.doc)' >>> fileDescriptor('Video (*.mp4 *.ogv)') 'Video (*.mp4 *.ogv)' >>> fileDescriptor('pgf',compr=True) 'pyFormex geometry files (*.pgf *.pgf.gz *.pgf.bz2)'
- filetype.fileTypesFromFilter(fdesc)[source]¶
Extract the filetypes from a file type descriptor.
A file type descriptor is a string consisting of an initial part followed by a second part enclosed in parentheses. The second part is a space separated list of glob patterns. An example file descriptor is
file type text (\*.ext1 \*.ext2)
. This is the format as returned byFiletype.desc()
.- Parameters:
- Returns:
desc (str) – The file type description text.
ext (list of str) – A list of the matching extensions (without dot) for this type. An empty string means that any extension is accepted.
Examples
>>> fileTypesFromFilter(Filetype['img'].desc()) ['png', 'jpg', 'jpeg', 'eps', 'gif', 'bmp'] >>> fileTypesFromFilter(Filetype['pgf'].desc(compr=True)) ['pgf', 'pgf.gz', 'pgf.bz2'] >>> fileTypesFromFilter('* *.png') ['', 'png'] >>> fileTypesFromFilter('Images (*.png *.jpg *.jpeg)') ['png', 'jpg', 'jpeg']
- filetype.setFiletypeFromFilter(filename, fdesc)[source]¶
Make sure a filename has an acceptable suffix.
- Parameters:
- Returns:
Path – If filename had a suffix included in accept, returns the input filename unaltered. Else returns the filename with a dot and the first suffix from accepted appeded to it.
Examples
>>> setFiletypeFromFilter('image01.jpg', 'Images (*.png *.jpg *.jpeg)') Path('image01.jpg') >>> setFiletypeFromFilter('image01', 'Images (*.png *.jpg *.jpeg)') Path('image01.png')