Re: Beagle filter for Gentoo ebuilds



(Keep forgetting to include the ML, argh )

On Monday 27 March 2006 13:19, you wrote:
> > 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?
>
> A new filter is instantiated each time.  So making them static is a good
> idea.

Done.

> >                 Regex METADATA_PATTERN = new
> > Regex("\\s*(?<key>([A-Z_]+))\\s*=\\s*\"(?<value>(.*))\"\\s*");
>
> Please put spaces after method names, but before parentheses.  Ie,
> "Regex (foo)".  Also, METADATA_PATTERN should probably be named
> metadata_pattern.  I'd suggest using "_pattern" for both EBUILD and
> PACKAGE too.

Done.

> See the HACKING file for the coding style standards.

Done.

> >                 public FilterEbuild ()
> >                 {
> >                         AddSupportedFlavor (FilterFlavor.NewFromExtension
> > (".ebuild")); SetVersion(2);
>
> This SetVersion() isn't necessary (when it's checked in upstream).

Removed. I was tested it out since I made the change from fixme:title to 
dc:title. I had to rebuild anyway.

> >                         String version =
> > match.Groups["version"].ToString(); if (version.Length > 0) {
> >                                 AddProperty (Beagle.Property.New
> > ("fixme:version", version)); }
>
> I think that Beagle.Property.NewKeyword() is better here.  The main
> difference is that New() gets stemmed, broken up on punctuation, etc.

Done.

> >                                 if (str.Length > 0) {
>
> Instead of adding a level of indentation for all the following code, I
> suggest doing:
>
>         if (str.Length == 0)
>                 continue;

Done.

> >                                                         if
> > (key.Equals("DESCRIPTION")) { AddProperty (Beagle.Property.New
> > ("dc:description", value)); }
> >                                                         else if
> > (key.Equals("LICENSE")) { AddProperty (Beagle.Property.New ("dc:rights",
> > value)); }

Removed unnecessary braces.

> Generally speaking, we don't put single lines inside "if" statements in
> brackets, but that's not a big deal.  More importantly, we put "else"
> and "else if" on the same line as the brackets.  Ie:
>
>         if (foo) {
>                 ;
>         } else if (bar) {
>                 ;
>         } else {
>                 ;
>         }

Done.

> >                                                         foreach (Match
> > theMatch in matches) {
>
> Please use "the_match" rather than "theMatch".

Done. Copied "theMatch" from one of the filters in the source tarball ;)

Attached source.

-- 
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 {
		static Regex metadata_pattern = new Regex ("\\s*(?<key>([A-Z_]+))\\s*=\\s*\"(?<value>(.*))\"\\s*");
		static Regex einfo_pattern = new Regex ("\\s*(einfo|ewarn)\\s+\"(?<message>(.*))\"\\s*");
		static Regex package_pattern = new Regex ("(?<name>([^0-9]+))-(?<version>(.+)).ebuild");

		public FilterEbuild () 
		{
			AddSupportedFlavor (FilterFlavor.NewFromExtension (".ebuild"));
		}

		override protected void DoOpen (FileInfo file) 
		{
			Match match = package_pattern.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.NewKeyword ("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)
					continue;

				// check for meta data
				MatchCollection matches;
				matches = metadata_pattern.Matches (str);
				if (matches.Count > 0) {
					foreach (Match the_match in matches) {
						String key = the_match.Groups ["key"].ToString ();
						String value = the_match.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_pattern.Matches (str);
					if (matches.Count > 0) {
						foreach (Match the_match in matches)
							AppendText (the_match.Groups ["message"].ToString ());
					}
				}
			}
			Finished ();
		}
	}
}

Attachment: pgpx4Jc2PwGVv.pgp
Description: PGP signature



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]