Commits

Michael Granger  committed 8b1c8ec

Loggability cleanup

  • Participants
  • Parent commits d9968c6

Comments (0)

Files changed (10)

 
 begin
 	require 'loggability'
+	require 'linguistics'
+
 	Loggability.level = :debug
 	Loggability.format_with( :color )
 
-	require 'linguistics'
-	Linguistics.use( :en )
+	# Linguistics.use( :en )
 	# Linguistics.use( :en, monkeypatch: true )
 rescue Exception => err
 	$stderr.puts "Linguistics failed to load: %p: %s" % [ err.class, err.message ]

File lib/linguistics.rb

 
 
 	require 'linguistics/monkeypatches'
-	require 'linguistics/mixins'
 	require 'linguistics/iso639'
 	require 'linguistics/inflector'
 
 	def self::register_language( language, mod )
 		language_entry = LANGUAGE_CODES[ language.to_sym ] or
 			raise "Unknown ISO639-2 language code '#{language}'"
-		Linguistics.log.info "Registering %s for language %p" % [ mod, language_entry ]
+		self.log.info "Registering %s for language %p" % [ mod, language_entry ]
 
 		language_entry[:codes].each do |lang|
 			self.languages[ lang.to_sym ] = mod
 
 		# Load in plugins for the language
 		Gem.find_files( "linguistics/#{language}/**.rb" ).each do |extension|
-			Linguistics.log.debug "  trying to load #{language_entry[:eng_name]} extension %p" % [ extension ]
+			extension.sub!( %r{.*/linguistics/}, 'linguistics/' )
+			self.log.debug "  trying to load #{language_entry[:eng_name]} extension %p" % [ extension ]
 			begin
 				require extension
 			rescue LoadError => err
-				Linguistics.log.debug "    failed (%s): %s %s" %
+				self.log.debug "    failed (%s): %s %s" %
 					[ err.class.name, err.message, err.backtrace.first ]
 			else
-				Linguistics.log.debug "    success."
+				self.log.debug "    success."
 			end
 		end
 
 	def self::load_language( lang )
 		unless mod = self.languages[ lang.to_sym ]
 
-			Linguistics.log.debug "Trying to load language %p" % [ lang ]
+			self.log.debug "Trying to load language %p" % [ lang ]
 			language = LANGUAGE_CODES[ lang.to_sym ] or
 				raise "Unknown ISO639-2 language code '#{lang}'"
-			Linguistics.log.debug "  got language code %p" % [ language ]
+			self.log.debug "  got language code %p" % [ language ]
 
 			# Sort all the codes for the specified language, trying the 2-letter
 			# versions first in alphabetical order, then the 3-letter ones
 
 				begin
 					require "linguistics/#{code}"
-					Linguistics.log.debug "  loaded linguistics/#{code}!"
+					self.log.debug "  loaded linguistics/#{code}!"
 					mod = self.languages[ lang.to_sym ]
-					Linguistics.log.debug "  set mod to %p" % [ mod ]
+					self.log.debug "  set mod to %p" % [ mod ]
 					break
 				rescue LoadError => err
-					Linguistics.log.error "  require of linguistics/#{code} failed: #{err.message}"
+					self.log.error "  require of linguistics/#{code} failed: #{err.message}"
 					msgs << "Tried 'linguistics/#{code}': #{err.message}\n"
 				end
 			end
 		classes = Array(config[:classes]) if config[:classes] 
 		classes ||= DEFAULT_EXT_CLASSES
 
-		Linguistics.log.debug "Extending %d classes with %d language modules." %
+		self.log.debug "Extending %d classes with %d language modules." %
 			[ classes.length, languages.length ]
 
 		# Mix the language module for each requested language into each
 		# specified class
 		classes.each do |klass|
-			Linguistics.log.debug "  extending %p" % [ klass ]
+			self.log.debug "  extending %p" % [ klass ]
 			languages.each do |lang|
 				mod = load_language( lang ) or
 					raise LoadError, "failed to load a language extension for %p" % [ lang ]
-				Linguistics.log.debug "    using %s language module: %p" % [ lang, mod ]
+				self.log.debug "    using %s language module: %p" % [ lang, mod ]
 
 				if config[:monkeypatch]
 					klass.send( :include, mod )
 				else
 					inflector = make_inflector_mixin( lang, mod )
-					Linguistics.log.debug "    made an inflector mixin: %p" % [ inflector ]
+					self.log.debug "    made an inflector mixin: %p" % [ inflector ]
 					klass.send( :include, inflector )
 				end
 			end
 			raise "Unknown ISO639-2 language code '#{lang}'"
 
 		unless mixin = self.inflector_mixins[ mod ]
-			Linguistics.log.debug "Making an inflector mixin for %p" % [ mod ]
+			self.log.debug "Making an inflector mixin for %p" % [ mod ]
 
 			bibcode, alpha2code, termcode = *language[:codes]
 			inflector = Class.new( Linguistics::Inflector ) { include(mod) }
-			Linguistics.log.debug "  created inflector class %p for [%p, %p, %p]" %
+			self.log.debug "  created inflector class %p for [%p, %p, %p]" %
 				[ inflector, bibcode, termcode, alpha2code ]
 
 			mixin = Module.new do

File lib/linguistics/en.rb

 	### Register an English-language extension.
 	def self::register_extension( mod )
 		MODULES.push( mod )
-		Linguistics.log.debug "Registered English extension %p" % [ mod ]
+		self.log.debug "Registered English extension %p" % [ mod ]
 
 		include( mod )
 		mod.extend( Loggability )
 
 		if mod.const_defined?( :SingletonMethods )
 			smod = mod.const_get(:SingletonMethods)
-			Linguistics.log.debug "  and its singleton methods %p" % [ smod ]
+			self.log.debug "  and its singleton methods %p" % [ smod ]
 			extend( smod )
 
 			ivars = mod.instance_variables
-			Linguistics.log.debug "  and instance variables %p" % [ ivars ]
+			self.log.debug "  and instance variables %p" % [ ivars ]
 			ivars.each do |ivar|
 				instance_variable_set( ivar, mod.instance_variable_get(ivar) )
 			end

File lib/linguistics/en/conjugation.rb

 require 'linguistics/en' unless defined?( Linguistics::EN )
 
 # This file contains functions for conjugating verbs.
-# 
+#
 # == Version
 #
 #  $Id$
-# 
+#
 # == Authors
-# 
+#
 # * Robert Berry <berrydigital@gmail.com>
 # * Michael Granger <ged@FaerieMUD.org>
-# 
+#
 # == Copyright
-#    
-# Based on MorphAdorner (http://morphadorner.northwestern.edu/), which is 
+#
+# Based on MorphAdorner (http://morphadorner.northwestern.edu/), which is
 # licensed under the following terms:
 #
 # Copyright © 2006-2009 by Northwestern University. All rights reserved.
-# 
+#
 # Developed by:
 # Academic and Research Technologies
 # Northwestern University
 # http://www.it.northwestern.edu/about/departments/at/
-# 
+#
 # Permission is hereby granted, free of charge, to any person obtaining a copy
 # of this software and associated documentation files (the "Software"), to deal
 # with 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:
-# 
+#
 #   Redistributions of source code must retain the above copyright notice,
 #   this list of conditions and the following disclaimers.
-# 
+#
 #   Redistributions in binary form must reproduce the above copyright notice,
 #   this list of conditions and the following disclaimers in the documentation
 #   and/or other materials provided with the distribution.
-# 
+#
 #   Neither the names of Academic and Research Technologies, Northwestern
 #   University, nor the names of its contributors may be used to endorse or
 #   promote products derived from this Software without specific prior written
 #   permission.
-# 
+#
 # 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
 # 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 WITH
 # THE SOFTWARE.
-# 
+#
 module Linguistics::EN::Conjugation
 	extend Loggability
 
 
 
 
-	### Conjugate the receiving string as an infinitive with the specified +tense+ 
+	### Conjugate the receiving string as an infinitive with the specified +tense+
 	### and +person+.
 	def conjugate( tense, person=nil )
 		self.log.debug "Conjugating %p in %s tense, %s person" %
 	#######
 
 	### Conjugate the specified +verb+ in the present tense in the given +person+. The
-	### only +person+ that is 
+	### only +person+ that is
 	def conjugate_present( verb, person=:third_person_singular )
 		return verb unless person == :third_person_singular
 
 	alias_method :conjugate_past_participle, :conjugate_past
 
 
-	#### If the given +verb+ is irregular in the given +person_ and 
+	#### If the given +verb+ is irregular in the given +person_ and
 	def get_irregular( verb, person, tense )
 		self.log.debug "  looking for irregular verb: %p, %p person, %p tense" %
 			[ verb, person, tense ]
 
 		data.each_line do |line|
 			if line =~ /^(#|\s*$)/ # Skip comments and blank lines
-				self.log.debug "  skipping line: %p" % [ line ]
+				# self.log.debug "  skipping line: %p" % [ line ]
 				next
 			end
 
 			infinitive, person, tense, conjugation = line.chomp.split( /\s+/, 4 )
-			self.log.debug "  line split into: %p" %
-				[[ infinitive, person, tense, conjugation ]]
+			# self.log.debug "  line split into: %p" %
+			#	[[ infinitive, person, tense, conjugation ]]
 
 			raise "malformed line: %p" % [ line ] unless infinitive && person && tense && conjugation
 
 __END__
 #
 # Irregular verbs (from MorphAdorner's irregularverbs.txt)
-# 
+#
 arise	*	past	arose
 arise	*	past_participle	arisen
 awake	*	past	awoke

File lib/linguistics/en/numbers.rb

 	###   word groups instead of a String.
 	def numwords( hashargs={} )
 		num = self.to_s
-		Linguistics.log.debug "Turning %p into number words..." % [ num ]
+		self.log.debug "Turning %p into number words..." % [ num ]
 		config = NUMWORD_DEFAULTS.merge( hashargs )
 		raise "Bad chunking option: #{config[:group]}" unless
 			config[:group].between?( 0, 3 )
 		# Wordify each chunk, pushing arrays into the parts array
 		chunks.each_with_index do |chunk,section|
 			chunk.gsub!( /\D+/, '' )
-			Linguistics.log.debug "  working on chunk %p (section %d)" % [ chunk, section ]
+			self.log.debug "  working on chunk %p (section %d)" % [ chunk, section ]
 
 			# If there's nothing in this chunk of the number, set it to zero
 			# unless it's the whole-number part, in which case just push an
 			# second or succeeding part of a non-group number
 			unless config[:group].zero? && section.nonzero?
 				parts.push number_to_words( chunk, config )
-				Linguistics.log.debug "  added %p" % [ parts.last ]
+				self.log.debug "  added %p" % [ parts.last ]
 			else
 				parts.push number_to_words( chunk, config.merge(:group => 1) )
-				Linguistics.log.debug "  added %p" % [ parts.last ]
+				self.log.debug "  added %p" % [ parts.last ]
 			end
 		end
 
-		Linguistics.log.debug "Parts => %p" % [ parts ]
+		self.log.debug "Parts => %p" % [ parts ]
 
 		# Turn the last word of the whole-number part back into an ordinal if
 		# the original number came in that way.
 
 		else
 			number = self.to_s
-			Linguistics.log.debug "Making an ordinal out of a non-Integer (%p)" % [ number ]
+			self.log.debug "Making an ordinal out of a non-Integer (%p)" % [ number ]
 			return number.sub( /(#{ORDINAL_SUFFIXES})\Z/ ) { ORDINALS[$1] }
 		end
 	end
 		# Scan the string, and call the word-chunk function that deals with
 		# chunks of the found number of digits.
 		return number.to_s.scan( re ).collect do |digits|
-			Linguistics.log.debug "   digits = %p" % [ digits ]
+			self.log.debug "   digits = %p" % [ digits ]
 			numerals = digits.flatten.compact.collect {|i| i.to_i}
-			Linguistics.log.debug "   numerals = %p" % [ numerals ]
+			self.log.debug "   numerals = %p" % [ numerals ]
 
 			fn = NUMBER_TO_WORDS_FUNCTIONS[ numerals.length ]
 			self.log.debug "  number to word function is #%d: %p" % [ numerals.length, fn ]
 		phrase.sub!( /\A\s*0+/, '' )
 		chunks = []
 		mill = 0
-		Linguistics.log.debug "Making standard word groups out of %p" % [ phrase ]
+		self.log.debug "Making standard word groups out of %p" % [ phrase ]
 
 		# Match backward from the end of the digits in the string, turning
 		# chunks of three, of two, and of one into words.

File lib/linguistics/en/wordnet.rb

 #   # Test to be sure the WordNet module loaded okay.
 #   Linguistics::EN.has_wordnet?
 #   # => true
-#  
+#
 #   # Fetch the default synset for the word "balance"
 #   "balance".en.synset
 #   # => #<WordNet::Synset:0x40376844 balance (noun): "a state of equilibrium"
 #    (derivations: 3, antonyms: 1, hypernyms: 1, hyponyms: 3)>
-#  
+#
 #   # Fetch the synset for the first verb sense of "balance"
 #   "balance".en.synset( :verb )
 #   # => #<WordNet::Synset:0x4033f448 balance, equilibrate, equilibrize, equilibrise
 #   (verb): "bring into balance or equilibrium; "She has to balance work and her
 #   domestic duties"; "balance the two weights"" (derivations: 7, antonyms: 1,
 #   verbGroups: 2, hypernyms: 1, hyponyms: 5)>
-#  
+#
 #   # Fetch the second noun sense
 #   "balance".en.synset( 2, :noun )
 #   # => #<WordNet::Synset:0x404ebb24 balance (noun): "a scale for weighing; depends
 #   on pull of gravity" (hypernyms: 1, hyponyms: 5)>
-#  
+#
 #   # Fetch the second noun sense's hypernyms (more-general words, like a superclass)
 #   "balance".en.synset( 2, :noun ).hypernyms
 #   # => [#<WordNet::Synset:0x404e5620 scale, weighing machine (noun): "a measuring
 #   instrument for weighing; shows amount of mass" (derivations: 2, hypernyms: 1,
 #   hyponyms: 2)>]
-#  
+#
 #   # A simpler way of doing the same thing:
 #   "balance".en.hypernyms( 2, :noun )
 #   # => [#<WordNet::Synset:0x404e5620 scale, weighing machine (noun): "a measuring
 #   instrument for weighing; shows amount of mass" (derivations: 2, hypernyms: 1,
 #   hyponyms: 2)>]
-#  
+#
 #   # Fetch the first hypernym's hypernyms
 #   "balance".en.synset( 2, :noun ).hypernyms.first.hypernyms
 #   # => [#<WordNet::Synset:0x404c60b8 measuring instrument, measuring system,
 #   measuring device (noun): "instrument that shows the extent or amount or quantity
 #   or degree of something" (hypernyms: 1, hyponyms: 83)>]
-#  
+#
 #   # Find the synset to which both the second noun sense of "balance" and the
 #   # default sense of "shovel" belong.
 #   ("balance".en.synset( 2, :noun ) | "shovel".en.synset)
 #   # => #<WordNet::Synset:0x40473da4 instrumentality, instrumentation (noun): "an
 #   artifact (or system of artifacts) that is instrumental in accomplishing some
 #   end" (derivations: 1, hypernyms: 1, hyponyms: 13)>
-#  
+#
 #   # Fetch just the words for the other kinds of "instruments"
 #   "instrument".en.hyponyms.collect {|synset| synset.words}.flatten
 #   # => ["analyzer", "analyser", "cautery", "cauterant", "drafting instrument",
 	# Load WordNet if possible, saving the error that occurs if anything goes wrong.
 	begin
 		require 'wordnet'
-		WordNet.logger = Linguistics.logger
 		@has_wordnet = true
 	rescue LoadError => err
 		@error = err

File lib/linguistics/inflector.rb

 #!/usr/bin/ruby
 # coding: utf-8
 
+require 'loggability'
 require 'linguistics' unless defined?( Linguistics )
 
 # A facade object that acts as the extension point for linguistic modules
 # for an object that has been extended with a Linguistics language
 # the first time the language is used.
 class Linguistics::Inflector
-	include ::Linguistics::Loggable
+	extend Loggability
+
+
+	# Loggability API -- log to the linguistics logger
+	log_to :linguistics
 
 
 	### Create a new inflector for +obj+.

File lib/linguistics/mixins.rb

-#!/usr/bin/ruby
-
-require 'rbconfig'
-require 'erb'
-require 'etc'
-require 'logger'
-
-require 'linguistics' unless defined?( Linguistics )
-
-
-#--
-# A collection of mixins shared between Linguistics modules.
-#
-module Linguistics # :nodoc:
-
-	### Add logging to a Linguistics class. Including classes get #log and #log_debug methods.
-	module Loggable
-
-		LEVEL = {
-			:debug => Logger::DEBUG,
-			:info  => Logger::INFO,
-			:warn  => Logger::WARN,
-			:error => Logger::ERROR,
-			:fatal => Logger::FATAL,
-		  }
-
-		### A logging proxy class that wraps calls to the logger into calls that include
-		### the name of the calling class.
-		class ClassNameProxy # :nodoc:
-
-			### Create a new proxy for the given +klass+.
-			def initialize( klass, force_debug=false )
-				@classname   = klass.name
-				@force_debug = force_debug
-			end
-
-			### Delegate calls the global logger with the class name as the 'progname' 
-			### argument.
-			def method_missing( sym, msg=nil, &block )
-				return super unless LEVEL.key?( sym )
-				sym = :debug if @force_debug
-				Linguistics.logger.add( LEVEL[sym], msg, @classname, &block )
-			end
-		end # ClassNameProxy
-
-		#########
-		protected
-		#########
-
-		### Copy constructor -- clear the original's log proxy.
-		def initialize_copy( original )
-			@log_proxy = @log_debug_proxy = nil
-			super
-		end
-
-		### Return the proxied logger.
-		def log
-			@log_proxy ||= ClassNameProxy.new( self.class )
-		end
-
-		### Return a proxied "debug" logger that ignores other level specification.
-		def log_debug
-			@log_debug_proxy ||= ClassNameProxy.new( self.class, true )
-		end
-	end # module Loggable
-
-end # module Linguistics
-
-# vim: set nosta noet ts=4 sw=4:
-

File spec/linguistics/mixins_spec.rb

-#!/usr/bin/env spec -cfs
-
-BEGIN {
-	require 'pathname'
-	basedir = Pathname.new( __FILE__ ).dirname.parent.parent
-
-	libdir = basedir + "lib"
-
-	$LOAD_PATH.unshift( basedir.to_s ) unless $LOAD_PATH.include?( basedir.to_s )
-	$LOAD_PATH.unshift( libdir.to_s ) unless $LOAD_PATH.include?( libdir.to_s )
-}
-
-require 'rspec'
-require 'spec/lib/helpers'
-
-require 'linguistics'
-require 'linguistics/mixins'
-
-describe Linguistics::Loggable, "mixed into a class" do
-	before(:each) do
-		@logfile = StringIO.new('')
-		Linguistics.logger = Logger.new( @logfile )
-
-		@test_class = Class.new do
-			include Linguistics::Loggable
-
-			def log_test_message( level, msg )
-				self.log.send( level, msg )
-			end
-
-			def logdebug_test_message( msg )
-				self.log_debug.debug( msg )
-			end
-		end
-		@obj = @test_class.new
-	end
-
-
-	it "is able to output to the log via its #log method" do
-		@obj.log_test_message( :debug, "debugging message" )
-		@logfile.rewind
-		@logfile.read.should =~ /debugging message/
-	end
-
-	it "is able to output to the log via its #log_debug method" do
-		@obj.logdebug_test_message( "sexydrownwatch" )
-		@logfile.rewind
-		@logfile.read.should =~ /sexydrownwatch/
-	end
-end

File spec/linguistics_spec.rb

 		end
 
 		it "raise an error for valid languages that don't have any linguistic functions to load" do
-			Linguistics.logger.level = Logger::FATAL
 			expect {
 				Linguistics.use( :ja )
 			}.to raise_error( LoadError, /failed to load a language extension/i )