On Friday 24 March 2006 12:32, Pat Double wrote: > I've written a filter for Gentoo Linux ebuilds. The main goal is to extract > information like the title, version, description, etc. It was really easy > to write. I'd appreciate comments (I'm a Java developer, so I'm sure there > are some bad things I've done with C# here ;). Any chance this could get > included in the beagle distribution? How do I go about that, file a bug? Here's a better version, I'll attach this time. It handles bash line continuation and I've changed from using "fixme:title" to "dc:title". Question, does the filter get instantiated once per beagle process, or for every file? I am wondering if I should make the Regex fields static? -- Pat Double, pat patdouble com "In the beginning God created the heaven and the earth."
// // FilterEbuild.cs // // Copyright (C) 2006 Pat Double <pat patdouble com> // // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. // using System; using System.IO; using System.Text.RegularExpressions; using Beagle.Daemon; namespace Beagle.Filters { public class FilterEbuild : Beagle.Daemon.Filter { Regex METADATA_PATTERN = new Regex("\\s*(?<key>([A-Z_]+))\\s*=\\s*\"(?<value>(.*))\"\\s*"); Regex EINFO = new Regex("\\s*(einfo|ewarn)\\s+\"(?<message>(.*))\"\\s*"); Regex PACKAGE = new Regex("(?<name>([^0-9]+))-(?<version>(.+)).ebuild"); public FilterEbuild () { AddSupportedFlavor (FilterFlavor.NewFromExtension (".ebuild")); SetVersion(2); } override protected void DoOpen (FileInfo file) { Match match = PACKAGE.Match(file.Name); String pkgname = match.Groups["name"].ToString(); if (pkgname.Length > 0) { AddProperty (Beagle.Property.New ("dc:title", pkgname)); } String version = match.Groups["version"].ToString(); if (version.Length > 0) { AddProperty (Beagle.Property.New ("fixme:version", version)); } StreamReader reader = new StreamReader(new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read)); string str = null; while ((str = reader.ReadLine ()) != null) { // Skip comments if (str.StartsWith("#")) continue; // Handle line continuation string str2 = null; while (str.Trim().EndsWith("\\") && ((str2 = reader.ReadLine ()) != null)) { str = str.Trim(); if (str.Length == 1) str = str2; else str = str.Substring(0, str.Length - 1) + " " + str2.Trim(); } if (str.Length > 0) { // check for meta data MatchCollection matches; matches = METADATA_PATTERN.Matches (str); if (matches.Count > 0) { foreach (Match theMatch in matches) { String key = theMatch.Groups ["key"].ToString (); String value = theMatch.Groups ["value"].ToString (); if (key.Equals("DESCRIPTION")) { AddProperty (Beagle.Property.New ("dc:description", value)); } else if (key.Equals("LICENSE")) { AddProperty (Beagle.Property.New ("dc:rights", value)); } else if (key.Equals("HOMEPAGE")) { AddProperty (Beagle.Property.New ("dc:source", value)); } } } else { // check for einfo/ewarn matches = EINFO.Matches (str); if (matches.Count > 0) { foreach (Match theMatch in matches) { AppendText(theMatch.Groups ["message"].ToString ()); } } } } } Finished(); } } }
Attachment:
pgpnkTHtmZg50.pgp
Description: PGP signature