| 
									
										
										
										
											2012-04-03 20:47:39 +02:00
										 |  |  | # -*- coding: utf-8 -*- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | Format management. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Creating new formats | 
					
						
							|  |  |  | -------------------- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | A new format named 'foo-bar' corresponds to Python module | 
					
						
							|  |  |  | 'tracetool/format/foo_bar.py'. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | A format module should provide a docstring, whose first non-empty line will be | 
					
						
							|  |  |  | considered its short description. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | All formats must generate their contents through the 'tracetool.out' routine. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Format functions | 
					
						
							|  |  |  | ---------------- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-23 20:37:40 +01:00
										 |  |  | ======== ================================================================== | 
					
						
							| 
									
										
										
										
											2012-04-03 20:47:39 +02:00
										 |  |  | Function Description | 
					
						
							| 
									
										
										
										
											2014-02-23 20:37:40 +01:00
										 |  |  | ======== ================================================================== | 
					
						
							|  |  |  | generate Called to generate a format-specific file. | 
					
						
							|  |  |  | ======== ================================================================== | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-03 20:47:39 +02:00
										 |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | __author__     = "Lluís Vilanova <vilanova@ac.upc.edu>" | 
					
						
							| 
									
										
										
										
											2014-02-23 20:37:24 +01:00
										 |  |  | __copyright__  = "Copyright 2012-2014, Lluís Vilanova <vilanova@ac.upc.edu>" | 
					
						
							| 
									
										
										
										
											2012-04-03 20:47:39 +02:00
										 |  |  | __license__    = "GPL version 2 or (at your option) any later version" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | __maintainer__ = "Stefan Hajnoczi" | 
					
						
							| 
									
										
										
										
											2020-05-11 10:28:16 +02:00
										 |  |  | __email__      = "stefanha@redhat.com" | 
					
						
							| 
									
										
										
										
											2012-04-03 20:47:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-30 12:00:23 +01:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2012-04-03 20:47:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import tracetool | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def get_list(): | 
					
						
							|  |  |  |     """Get a list of (name, description) pairs.""" | 
					
						
							|  |  |  |     res = [] | 
					
						
							| 
									
										
										
										
											2012-04-30 12:00:23 +01:00
										 |  |  |     modnames = [] | 
					
						
							|  |  |  |     for filename in os.listdir(tracetool.format.__path__[0]): | 
					
						
							|  |  |  |         if filename.endswith('.py') and filename != '__init__.py': | 
					
						
							|  |  |  |             modnames.append(filename.rsplit('.', 1)[0]) | 
					
						
							| 
									
										
										
										
											2014-02-23 20:37:24 +01:00
										 |  |  |     for modname in sorted(modnames): | 
					
						
							| 
									
										
										
										
											2012-04-03 20:47:39 +02:00
										 |  |  |         module = tracetool.try_import("tracetool.format." + modname) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # just in case; should never fail unless non-module files are put there | 
					
						
							|  |  |  |         if not module[0]: | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         module = module[1] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         doc = module.__doc__ | 
					
						
							|  |  |  |         if doc is None: | 
					
						
							|  |  |  |             doc = "" | 
					
						
							|  |  |  |         doc = doc.strip().split("\n")[0] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         name = modname.replace("_", "-") | 
					
						
							|  |  |  |         res.append((name, doc)) | 
					
						
							|  |  |  |     return res | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def exists(name): | 
					
						
							|  |  |  |     """Return whether the given format exists.""" | 
					
						
							|  |  |  |     if len(name) == 0: | 
					
						
							|  |  |  |         return False | 
					
						
							|  |  |  |     name = name.replace("-", "_") | 
					
						
							|  |  |  |     return tracetool.try_import("tracetool.format." + name)[1] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-04 14:35:59 +01:00
										 |  |  | def generate(events, format, backend, group): | 
					
						
							| 
									
										
										
										
											2014-02-23 20:37:40 +01:00
										 |  |  |     if not exists(format): | 
					
						
							|  |  |  |         raise ValueError("unknown format: %s" % format) | 
					
						
							|  |  |  |     format = format.replace("-", "_") | 
					
						
							|  |  |  |     func = tracetool.try_import("tracetool.format." + format, | 
					
						
							|  |  |  |                                 "generate")[1] | 
					
						
							|  |  |  |     if func is None: | 
					
						
							|  |  |  |         raise AttributeError("format has no 'generate': %s" % format) | 
					
						
							| 
									
										
										
										
											2016-10-04 14:35:59 +01:00
										 |  |  |     func(events, backend, group) |