[Debtags-commits] [svn] r1549 - in tagcoll/trunk: . tools

Enrico Zini enrico at costa.debian.org
Thu Feb 9 15:45:40 UTC 2006


Author: enrico
Date: Thu Feb  9 15:45:39 2006
New Revision: 1549

Modified:
   tagcoll/trunk/   (props changed)
   tagcoll/trunk/configure.ac
   tagcoll/trunk/tools/tagcoll.cc
Log:
 r7192 at viaza:  enrico | 2006-02-09 16:45:01 +0100
 Refactored tagcoll to parse commandline arguments as one would expect, allowing for multiple input files, stdin input default and all around cleaner code
 Added grep option


Modified: tagcoll/trunk/configure.ac
==============================================================================
--- tagcoll/trunk/configure.ac	(original)
+++ tagcoll/trunk/configure.ac	Thu Feb  9 15:45:39 2006
@@ -18,7 +18,7 @@
 
 dnl Add options for local configuration
 
-dnl Ti use subdirs
+dnl To use subdirs
 AC_PROG_MAKE_SET
 
 AC_ISC_POSIX

Modified: tagcoll/trunk/tools/tagcoll.cc
==============================================================================
--- tagcoll/trunk/tools/tagcoll.cc	(original)
+++ tagcoll/trunk/tools/tagcoll.cc	Thu Feb  9 15:45:39 2006
@@ -307,78 +307,30 @@
 	}
 };
 
-enum valid_command { COPY, DIFF, RELATED, IMPLICATIONS, HIERARCHY, CLEANHIERARCHY, REVERSE, FINDSPECIALS };
+enum valid_command { COPY, DIFF, RELATED, IMPLICATIONS, HIERARCHY, CLEANHIERARCHY, REVERSE, FINDSPECIALS, GREP };
 
-int main(int argc, const char* argv[])
+class Reader
 {
-	try {
-		CommandlineParserWithCommand opts(APPNAME, "[options] [command] [file1|-] [file2|-]",
-				"Perform various operations on a tagged collection\n\n"
-				"Commands are:\n"
-				"  copy         output the collection\n"
-				"  reverse      \"reverse\" the collection, outputting one with items\n"
-				"               associated to tags\n"
-				"  diff         output a tag patch file with the differences between two files\n"
-				"  related      print a list of items related to the given one\n"
-				"  implications compute a list of tag implications\n"
-				"  hierarchy    build a smart hierarchy with the collection data\n"
-				"  cleanhierarchy build a cleaned smart hierarchy with the collection data\n"
-				"  findspecials generate a smart hierarchy and print, for each toplevel tag,\n"
-				"               what are the items that make it toplevel instead of going below\n"
-				"               another tag.\n");
-
-		/*
-		opts.add("hierarchy", 's', "smart-hierarchy", "build a smart hierarchy");
-		opts.add("implications", 'm', "show-implications", "output a list of tag implications");
-		opts.add("copy", 'c', "copy", "output the collection");
-		opts.add("diff", 'd', "diff", "output a tag patch file with the differences between two files (requires two file arguments)");
-		*/
-
-		opts.add("derived", 'e', "derived-tags-from", "use an external list of derived tags", "file");
-		opts.add("extimpl", 'i', "implications-from", "use an external list of implications", "file");
-		opts.add("rename", 'r', "rename-from", "rename tags using the given mapping list", "file");
-		opts.add("patch", 'p', "patch-with", "apply patches from the given tag patch file", "file");
-
-		opts.add("expanded", 'x', "expanded-output", "produce full (and redundant) output data instead of compact");
-		opts.add("groupitems", 'g', "group-items", "group items with the same tagset in the output collection");
-		opts.add("distance", 'd', "distance", "set the maximum distance to use for the \"related\" command (defaults to 0)", "num");
-		opts.add("flatten", 0, "flatten-threshold", "set the number of total items below which a branch is flattened when using the \"hierarchy\" command (defaults to 0, meaning \"don't flatten\")", "num");
-		opts.add("filter", 'f', "filter", "filter out the tags with cardinality less than the given value (defaults to not filter; currently only works when building hierarchies)", "num");
-		opts.add("untagged-tag", 0, "untagged-tag", "set item name to use for associating untagged items when using the \"reverse\" command.  If not specified, untagged items are not included in the output.", "tag");
-		opts.add("remove-unfaceted", 0, "remove-unfaceted", "while parsing, remove all tags with no facet part.");
-		opts.add("remove-tags", 0, "remove-tags", "while parsing, remove all tags matching the given tag expression.", "expression");
-		
-		//opts.add("verbose", 'v', "verbose", "enable verbose output");
-		//opts.add("debug", 0, "debug", "enable debugging output (including verbose output)");
-
-		opts.addCommand("copy", (int)COPY);
-		opts.addCommand("reverse", (int)REVERSE);
-		opts.addCommand("diff", (int)DIFF);
-		opts.addCommand("related", (int)RELATED);
-		opts.addCommand("implications", (int)IMPLICATIONS);
-		opts.addCommand("hierarchy", (int)HIERARCHY);
-		opts.addCommand("cleanhierarchy", (int)CLEANHIERARCHY);
-		opts.addCommand("findspecials", (int)FINDSPECIALS);
-
-		// Process the commandline
-		valid_command cmd = (valid_command)opts.parse(argc, argv);
-
-		CommandlineArgs args(argc, argv);
-		
-		string item;
-		if (cmd == RELATED)
-			item = args.next();
-		string file1 = args.next();
-
-		//fprintf(stderr, "argc: %d file: %.*s\n", argc, PFSTR(file));
-
-		// Prepare the input filter chain
-		FilterChain<string, string> filters;
-		Substitute<string, string> substitutions;
-		PatchList<string, string> patches;
-		Implications<string> implications;
-		DerivedTags derivedTags;
-
+	// Prepare the input filter chain
+	FilterChain<string, string> filters;
+	Substitute<string, string> substitutions;
+	PatchList<string, string> patches;
+	Implications<string> implications;
+	DerivedTags derivedTags;
+
+	AddImplied<string, string> addImplied;
+	RemoveImplied<string, string> removeImplied;
+	AddDerived<string> addDerived;
+	RemoveDerived<string> removeDerived;
+	UnfacetedRemover<string> unfacetedRemover;
+	FilterTagsByExpression<string, string> filterByExpression;
+
+public:
+	Reader(CommandlineParserWithCommand& opts, valid_command cmd)
+		: addImplied(implications), removeImplied(implications),
+		  addDerived(derivedTags), removeDerived(derivedTags),
+		  unfacetedRemover("::"), filterByExpression("")
+	{
 		if (opts.get("rename").defined())
 		{
 			readCollection(opts.get("rename").stringVal(), substitutions.substitutions());
@@ -404,11 +356,6 @@
 		bool hasImpl = opts.get("extimpl").defined();
 		bool hasDerv = opts.get("derived").defined();
 
-		AddImplied<string, string> addImplied(implications);
-		RemoveImplied<string, string> removeImplied(implications);
-		AddDerived<string> addDerived(derivedTags);
-		RemoveDerived<string> removeDerived(derivedTags);
-
 		if (compressOutput)
 		{
 			if (hasDerv)
@@ -436,28 +383,98 @@
 			}
 		}
 
-		UnfacetedRemover<string> unfacetedRemover("::");
 		if (opts.get("remove-unfaceted").defined())
 			filters.appendFilter(unfacetedRemover);
 
-		FilterTagsByExpression<string, string> filterByExpression("");
 		if (opts.get("remove-tags").defined())
 		{
 			filterByExpression.setExpression(not Expression(opts.get("remove-tags").stringVal())); 
 			filters.appendFilter(filterByExpression);
 		}
+	}
+	void output(const string& file, Consumer<string, string>& cons)
+	{
+		filters.setConsumer(cons);
+		readCollection(file, filters);
+	}
+	void output(CommandlineArgs args, Consumer<string, string>& cons)
+	{
+		if (args.hasNext())
+			while (args.hasNext())
+				output(args.next(), cons);
+		else
+			output("-", cons);
+	}
+};
 
-		InputMerger<string, string> merger;
-		filters.setConsumer(merger);
-		readCollection(file1, filters);
+int main(int argc, const char* argv[])
+{
+	try {
+		CommandlineParserWithCommand opts(APPNAME, "[options] [command] [file1|-] [file2|-]",
+				"Perform various operations on a tagged collection\n\n"
+				"Commands are:\n"
+				"  copy or cat  output the collection\n"
+				"  reverse      \"reverse\" the collection, outputting one with items\n"
+				"               associated to tags\n"
+				"  diff         output a tag patch file with the differences between two files\n"
+				"  related      print a list of items related to the given one\n"
+				"  implications compute a list of tag implications\n"
+				"  hierarchy    build a smart hierarchy with the collection data\n"
+				"  cleanhierarchy build a cleaned smart hierarchy with the collection data\n"
+				"  findspecials generate a smart hierarchy and print, for each toplevel tag,\n"
+				"               what are the items that make it toplevel instead of going below\n"
+				"               another tag.\n"
+				"  grep <expr>  output the collection of tags that match the given tag expression\n");
+
+		/*
+		opts.add("hierarchy", 's', "smart-hierarchy", "build a smart hierarchy");
+		opts.add("implications", 'm', "show-implications", "output a list of tag implications");
+		opts.add("copy", 'c', "copy", "output the collection");
+		opts.add("diff", 'd', "diff", "output a tag patch file with the differences between two files (requires two file arguments)");
+		*/
+
+		opts.add("derived", 'e', "derived-tags-from", "use an external list of derived tags", "file");
+		opts.add("extimpl", 'i', "implications-from", "use an external list of implications", "file");
+		opts.add("rename", 'r', "rename-from", "rename tags using the given mapping list", "file");
+		opts.add("patch", 'p', "patch-with", "apply patches from the given tag patch file", "file");
+
+		opts.add("expanded", 'x', "expanded-output", "produce full (and redundant) output data instead of compact");
+		opts.add("groupitems", 'g', "group-items", "group items with the same tagset in the output collection");
+		opts.add("distance", 'd', "distance", "set the maximum distance to use for the \"related\" command (defaults to 0)", "num");
+		opts.add("flatten", 0, "flatten-threshold", "set the number of total items below which a branch is flattened when using the \"hierarchy\" command (defaults to 0, meaning \"don't flatten\")", "num");
+		opts.add("filter", 'f', "filter", "filter out the tags with cardinality less than the given value (defaults to not filter; currently only works when building hierarchies)", "num");
+		opts.add("untagged-tag", 0, "untagged-tag", "set item name to use for associating untagged items when using the \"reverse\" command.  If not specified, untagged items are not included in the output.", "tag");
+		opts.add("remove-unfaceted", 0, "remove-unfaceted", "while parsing, remove all tags with no facet part.");
+		opts.add("remove-tags", 0, "remove-tags", "while parsing, remove all tags matching the given tag expression.", "expression");
+		
+		//opts.add("verbose", 'v', "verbose", "enable verbose output");
+		//opts.add("debug", 0, "debug", "enable debugging output (including verbose output)");
 
+		opts.addCommand("copy", (int)COPY);
+		opts.addCommand("cat", (int)COPY);
+		opts.addCommand("reverse", (int)REVERSE);
+		opts.addCommand("diff", (int)DIFF);
+		opts.addCommand("related", (int)RELATED);
+		opts.addCommand("implications", (int)IMPLICATIONS);
+		opts.addCommand("hierarchy", (int)HIERARCHY);
+		opts.addCommand("cleanhierarchy", (int)CLEANHIERARCHY);
+		opts.addCommand("findspecials", (int)FINDSPECIALS);
+		opts.addCommand("grep", (int)GREP);
+
+		// Process the commandline
+		valid_command cmd = (valid_command)opts.parse(argc, argv);
+
+		CommandlineArgs args(argc, argv);
+
+		Reader reader(opts, cmd);
+		
 		// Perform the correct operation
 		switch (cmd)
 		{
 			case IMPLICATIONS:
 			{
 				CardinalityStore<string, string> coll;
-				merger.output(coll);
+				reader.output(args, coll);
 
 				Implications<string> newImpls;
 
@@ -488,7 +505,7 @@
 					flattenThreshold = opts.get("flatten").intVal();
 
 				CardinalityStore<string, string> coll;
-				merger.output(coll);
+				reader.output(args, coll);
 
 				if (opts.get("filter").defined())
 					coll.removeTagsWithCardinalityLessThan(opts.get("filter").intVal());
@@ -506,7 +523,7 @@
 					flattenThreshold = opts.get("flatten").intVal();
 
 				CardinalityStore<string, string> coll;
-				merger.output(coll);
+				reader.output(args, coll);
 
 				if (opts.get("filter").defined())
 					coll.removeTagsWithCardinalityLessThan(opts.get("filter").intVal());
@@ -518,12 +535,14 @@
 			}
 			case DIFF:
 			{
+				InputMerger<string, string> merger1;
+				reader.output(args.next(), merger1);
+
 				InputMerger<string, string> merger2;
-				filters.setConsumer(merger2);
-				readCollection(args.next(), filters);
+				reader.output(args.next(), merger2);
 
 				PatchList<string, string> newpatches;
-				newpatches.addPatch(merger, merger2);
+				newpatches.addPatch(merger1, merger2);
 
 				Converter<string, string> conv;
 				TextFormat<string, string>::outputPatch(conv, conv, newpatches, stdout);
@@ -531,6 +550,10 @@
 			}
 			case RELATED:
 			{
+				string item = args.next();
+				InputMerger<string, string> merger;
+				reader.output(args, merger);
+
 				int maxdist = 0;
 				if (opts.get("distance").defined())
 					maxdist = opts.get("distance").intVal();
@@ -594,7 +617,7 @@
 			case REVERSE:
 			{
 				ItemGrouper<string, string> reverser;
-				merger.output(reverser);
+				reader.output(args, reverser);
 
 				/*
 				if (opts.get("untagged-tag").defined())
@@ -618,10 +641,10 @@
 				if (opts.get("groupitems").defined())
 				{
 					ItemGrouper<string, string> grouper;
-					merger.output(grouper);
+					reader.output(args, grouper);
 					grouper.output(writer);
 				} else
-					merger.output(writer);
+					reader.output(args, writer);
 				break;
 			}
 			case FINDSPECIALS:
@@ -631,7 +654,7 @@
 					flattenThreshold = opts.get("flatten").intVal();
 
 				CardinalityStore<string, string> coll;
-				merger.output(coll);
+				reader.output(args, coll);
 
 				if (opts.get("filter").defined())
 					coll.removeTagsWithCardinalityLessThan(opts.get("filter").intVal());
@@ -653,7 +676,7 @@
 						for (OpSet<string>::const_iterator j = items.begin();
 								j != items.end(); j++)
 						{
-							OpSet<string> tags = merger.getTags(*j) ^ seen;
+							OpSet<string> tags = coll.getTags(*j) ^ seen;
 							if (tags.empty())
 								newItems += *j;
 						}
@@ -671,6 +694,24 @@
 
 				break;
 			}
+			case GREP:
+			{
+				FilterItemsByExpression<string, string> filter(args.next());
+
+				Converter<string, string> conv;
+				TextFormat<string, string> writer(conv, conv, stdout);
+				if (opts.get("groupitems").defined())
+				{
+					ItemGrouper<string, string> grouper;
+					filter.setConsumer(grouper);
+					reader.output(args, filter);
+					grouper.output(writer);
+				} else {
+					filter.setConsumer(writer);
+					reader.output(args, filter);
+				}
+				break;
+			}
 		}
 
 		return 0;



More information about the Debtags-commits mailing list