Commits

Geoffrey Sneddon committed 4ad4291

Allow processes to be enabled/disabled.

Comments (0)

Files changed (2)

 			# Create the generator
 			gen = generator.generator()
 			
+			# Remove the option we pass as an argument
+			processes = opts.processes
+			del opts.processes
+			
 			# Turn the options into a dict
 			kwargs = vars(opts)
 			
 			# Run the generator, and profile, or not, as the case may be
 			if kwargs["profile"]:
-				cProfile.runctx("gen.process(tree, **kwargs)", {}, {"gen": gen, "tree": tree, "kwargs": kwargs})
+				cProfile.runctx("gen.process(tree, processes, **kwargs)", {}, {"gen": gen, "tree": tree, "processes": processes, "kwargs": kwargs})
 			else:
-				gen.process(tree, **kwargs)
+				gen.process(tree, processes, **kwargs)
 			
 			# Serialize to XML
 			#if opts.xml:
 def getOptParser():
 	parser = OptionParser(usage = __doc__, version="%prog 1.0RC1-dev")
 	
+	parser.add_option("", "--enable", action="callback", callback=enable, type="string", dest="processes")
+	
+	parser.add_option("", "--disable", action="callback", callback=disable, type="string")
+	
 	#parser.add_option("", "", action="store_true",
-	#	default=False, dest="xml", help="Use an XML parser/serializer.")
+	#	dest="xml", help="Use an XML parser/serializer.")
 	
 	parser.add_option("", "--lxml.html", action="store_true",
-		default=False, dest="lxml_html", help="Use lxml's HTML parser/serializer.")
+		dest="lxml_html", help="Use lxml's HTML parser/serializer.")
 	
 	parser.add_option("", "--newline-char", action="store", type="string",
-		default="\n", dest="newline_char", help="Set the newline character/string used when creating new newlines. This should match the rest of the newlines in the document.")
+		dest="newline_char", help="Set the newline character/string used when creating new newlines. This should match the rest of the newlines in the document.")
 	
 	parser.add_option("", "--indent-char", action="store", type="string",
-		default="\t", dest="indent_char", help="Set the character/string used when creating indenting new blocks of (X)HTML. This should match the rest of the indentation in the document.")
+		dest="indent_char", help="Set the character/string used when creating indenting new blocks of (X)HTML. This should match the rest of the indentation in the document.")
 	
 	parser.add_option("", "--force-html4-id", action="store_true",
-		default=False, dest="force_html4_id", help="Force the ID generation algorithm to create HTML 4 compliant IDs regardless of the DOCTYPE.")
+		dest="force_html4_id", help="Force the ID generation algorithm to create HTML 4 compliant IDs regardless of the DOCTYPE.")
 	
 	parser.add_option("", "--min-depth", action="store", type="int",
 		default=2, dest="min_depth", help="Highest ranking header to number/insert into TOC.")
 		default=6, dest="max_depth", help="Lowest ranking header to number/insert into TOC.")
 	
 	parser.add_option("", "--allow-duplicate-dfns", action="store_true",
-		default=False, dest="allow_duplicate_dfns", help="Allow multiple definitions of terms when cross-referencing (the last instance of the term is used when referencing it).")
+		dest="allow_duplicate_dfns", help="Allow multiple definitions of terms when cross-referencing (the last instance of the term is used when referencing it).")
 	
 	parser.add_option("", "--w3c-compat", action="store_true",
-		default=False, dest="w3c_compat", help="Behave in a (mostly) compatible way to the W3C CSS WG's Postprocessor (this implies all of the other --w3c-compat options with the exception of --w3c-compat-crazy-substitution, as that is too crazy).")
+		dest="w3c_compat", help="Behave in a (mostly) compatible way to the W3C CSS WG's Postprocessor (this implies all of the other --w3c-compat options with the exception of --w3c-compat-crazy-substitution, as that is too crazy).")
 	
 	parser.add_option("", "--w3c-compat-xref-elements", action="store_true",
-		default=False, dest="w3c_compat_xref_elements", help="Uses the same list of elements to look for cross-references in as the W3C CSS WG's Postprocessor, even when the elements shouldn't semantically be used for cross-reference terms.")
+		dest="w3c_compat_xref_elements", help="Uses the same list of elements to look for cross-references in as the W3C CSS WG's Postprocessor, even when the elements shouldn't semantically be used for cross-reference terms.")
 	
 	parser.add_option("", "--w3c-compat-xref-a-placement", action="store_true",
-		default=False, dest="w3c_compat_xref_a_placement", help="When cross-referencing elements apart from span, put the a element inside the element instead of outside the element.")
+		dest="w3c_compat_xref_a_placement", help="When cross-referencing elements apart from span, put the a element inside the element instead of outside the element.")
 	
 	parser.add_option("", "--w3c-compat-xref-normalization", action="store_true",
-		default=False, dest="w3c_compat_xref_normalization", help="Only use ASCII letters, numbers, and spaces in comparison of cross-reference terms.")
+		dest="w3c_compat_xref_normalization", help="Only use ASCII letters, numbers, and spaces in comparison of cross-reference terms.")
 	
 	parser.add_option("", "--w3c-compat-class-toc", action="store_true",
-		default=False, dest="w3c_compat_class_toc", help="Add @class='toc' on every ol element in the table of contents (instead of only the root ol element).")
+		dest="w3c_compat_class_toc", help="Add @class='toc' on every ol element in the table of contents (instead of only the root ol element).")
 	
 	parser.add_option("", "--w3c-compat-substitutions", action="store_true",
-		default=False, dest="w3c_compat_substitutions", help="Do W3C specific substitutions.")
+		dest="w3c_compat_substitutions", help="Do W3C specific substitutions.")
 	
 	parser.add_option("", "--w3c-compat-crazy-substitutions", action="store_true",
-		default=False, dest="w3c_compat_crazy_substitutions", help="Do crazy W3C specific substitutions, which may cause unexpected behaviour (i.e., replacing random strings within the document with no special marker).")
+		dest="w3c_compat_crazy_substitutions", help="Do crazy W3C specific substitutions, which may cause unexpected behaviour (i.e., replacing random strings within the document with no special marker).")
 	
 	parser.add_option("", "--profile", action="store_true",
-		default=False, dest="profile", help=SUPPRESS_HELP)
+		dest="profile", help=SUPPRESS_HELP)
 	
 	parser.add_option("", "--inject-meta-charset", action="store_true",
-		default=False, dest="inject_meta_charset", help=SUPPRESS_HELP)
+		dest="inject_meta_charset", help=SUPPRESS_HELP)
 	
 	parser.add_option("", "--strip-whitespace", action="store_true",
-		default=False, dest="strip_whitespace", help=SUPPRESS_HELP)
+		dest="strip_whitespace", help=SUPPRESS_HELP)
 
 	parser.add_option("", "--omit-optional-tags", action="store_true",
-		default=False, dest="omit_optional_tags", help=SUPPRESS_HELP)
+		dest="omit_optional_tags", help=SUPPRESS_HELP)
 
 	parser.add_option("", "--quote-attr-values", action="store_true",
-		default=False, dest="quote_attr_values", help=SUPPRESS_HELP)
+		dest="quote_attr_values", help=SUPPRESS_HELP)
 
 	parser.add_option("", "--use-best-quote-char", action="store_true",
-		default=False, dest="use_best_quote_char",	help=SUPPRESS_HELP)
+		dest="use_best_quote_char",	help=SUPPRESS_HELP)
 
 	parser.add_option("", "--no-minimize-boolean-attributes",
 		action="store_false", default=True,
 		dest="minimize_boolean_attributes", help=SUPPRESS_HELP)
 
 	parser.add_option("", "--use-trailing-solidus", action="store_true",
-		default=False, dest="use_trailing_solidus", help=SUPPRESS_HELP)
+		dest="use_trailing_solidus", help=SUPPRESS_HELP)
 
 	parser.add_option("", "--space-before-trailing-solidus",
 		action="store_true", default=False,
 		dest="space_before_trailing_solidus", help=SUPPRESS_HELP)
 
 	parser.add_option("", "--escape-lt-in-attrs", action="store_true",
-		default=False, dest="escape_lt_in_attrs", help=SUPPRESS_HELP)
+		dest="escape_lt_in_attrs", help=SUPPRESS_HELP)
 
 	parser.add_option("", "--escape-rcdata", action="store_true",
-		default=False, dest="escape_rcdata", help=SUPPRESS_HELP)
+		dest="escape_rcdata", help=SUPPRESS_HELP)
+	
+	parser.set_defaults(
+		processes=set(["sub", "xref", "toc"]),
+		xml=False,
+		lxml_html=False,
+		newline_char=u"\n",
+		indent_char=u"\t",
+		force_html4_id=False,
+		min_depth=2,
+		max_depth=6,
+		allow_duplicate_dfns=False,
+		w3c_compat=False,
+		w3c_compat_xref_elements=False,
+		w3c_compat_xref_a_placement=False,
+		w3c_compat_xref_normalization=False,
+		w3c_compat_class_toc=False,
+		w3c_compat_substitutions=False,
+		w3c_compat_crazy_substitutions=False,
+		profile=False,
+		inject_meta_charset=False,
+		omit_optional_tags=False,
+		quote_attr_values=False,
+		use_best_quote_char=False,
+		minimize_boolean_attributes=False,
+		use_trailing_solidus=False,
+		space_before_trailing_solidus=False,
+		escape_lt_in_attrs=False,
+		escape_rcdata=False
+	)
 
 	return parser
 
+def enable(option, opt_str, value, parser, *args, **kwargs):
+	parser.values.processes.add(opt_str)
+
+def disable(option, opt_str, value, parser, *args, **kwargs):
+	parser.values.processes.discard(opt_str)
+
 if __name__ == "__main__":
 	main()

specGen/generator.py

 class generator(object):
 	""" This oversees all the actual work done """
 	
-	def process(self, tree, processes = [xref.xref, toc.toc, sub.sub], **kwargs):
+	def process(self, tree, processes, **kwargs):
 		""" Process the given tree. """
 		
 		# Find number of passes to do
 		for process in processes:
-			process(tree, **kwargs)
+			try:
+				process_module = getattr(__import__('processes', globals(), locals(), [process], -1), process)
+			except ImportError:
+				process_module = __import__(process, globals(), locals(), [], -1)
+			
+			getattr(process_module, process)(tree, **kwargs)