Skip to content

io -- Read/write mesh or data files

read(name, type=None, **opts)

Load a mesh from a file, guessing its file type

Source code in madcad/io.py
25
26
27
28
29
30
31
32
def read(name: str, type=None, **opts) -> Mesh:
	''' Load a mesh from a file, guessing its file type '''
	type = filetype(name, type)
	reader = globals().get(type+'_read')
	if reader:
		return reader(name, **opts)
	else:
		raise FileFormatError('no read function available for format '+type)

write(mesh, name, type=None, **opts)

Write a mesh to a file, guessing its file type

Source code in madcad/io.py
34
35
36
37
38
39
40
41
def write(mesh: Mesh, name: str, type=None, **opts):
	''' Write a mesh to a file, guessing its file type '''
	type = filetype(name, type)
	writer = globals().get(type+'_write')
	if writer:
		return writer(mesh, name, **opts)
	else:
		raise FileFormatError('no write function available for format '+type)

cache(filename, create=None, name=None, storage=None, **opts)

Small cachefile system, it allows to dump objects to files and to get them when needed.

It's particularly useful when working with other processes. The cached files are reloaded only when the cache files are newer than the memory cache data.

If specified, create() is called to provide the data, in case it doesn't exist in memory neither as file. If specified, name is the cache name used to index the file it defaults to the filename. If specified, storage is the dictionary used to storage cache data, defaults to io.caches.

Source code in madcad/io.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def cache(filename: str, create: callable=None, name=None, storage=None, **opts) -> Mesh:
	''' Small cachefile system, it allows to dump objects to files and to get them when needed.

		It's particularly useful when working with other processes. The cached files are reloaded only when the cache files are newer than the memory cache data.

		If specified, create() is called to provide the data, in case it doesn't exist in memory neither as file.
		If specified, name is the cache name used to index the file it defaults to the `filename`.
		If specified, storage is the dictionary used to storage cache data, defaults to io.caches.
	'''
	if not storage:	storage = caches
	if not name:	name = filename

	# create the cache file if it doesn't exist
	if os.path.exists(filename):
		cachedate = storage[name][0] if name in storage else -inf
		filedate = os.path.getmtime(filename)
		if cachedate < filedate:
			storage[name] = (filedate, read(filename, **opts))
	# load reload the file content if it's newer that the data in memory
	else:
		if name in storage:	obj = storage[name][1]
		elif create:		obj = create()
		else:
			raise IOError("the cache file doesn't exist")
		write(obj, filename, **opts)
		storage[name] = (os.path.getmtime(filename), obj)
	return storage[name][1]

FileFormatError

Bases: Exception