lib/xdg.rb in xdg-0.3.0 vs lib/xdg.rb in xdg-0.4.0

- old
+ new

@@ -1,11 +1,10 @@ # :Title: XDG # :Author: &trans; # :Copyright: (c)2008 Tiger Ops # :License: GPLv3 - # = XDG Base Directory Standard # # This provides a conveient library for conforming to the # XDG Base Directory Standard. # @@ -30,124 +29,173 @@ # # This module returns paths as strings. module XDG - def self.home + extend self #module_function + + def home ENV['HOME'] || File.expand_path('~') end # Location of user's personal config directory. - def self.config_home + def config_home File.expand_path( - ENV['XDG_CONFIG_HOME'] || File.join(xdg_home, '.config') + ENV['XDG_CONFIG_HOME'] || File.join(home, '.config') ) end # List of user's shared config directories. - def self.config_dirs + def config_dirs dirs = ENV['XDG_CONFIG_DIRS'].to_s.split(/[:;]/) if dirs.empty? dirs = %w{/etc/xdg} end dirs.collect{ |d| File.expand_path(d) } end # Location of user's personal data directory. - def self.data_home + def data_home File.expand_path( - ENV['XDG_DATA_HOME'] || File.join(xdg_home, '.local', 'share') + ENV['XDG_DATA_HOME'] || File.join(home, '.local', 'share') ) end # List of user's shared data directores. - def self.data_dirs + def data_dirs dirs = ENV['XDG_DATA_DIRS'].split(/[:;]/) if dirs.empty? dirs = %w{/usr/local/share/ /usr/share/} end dirs.collect{ |d| File.expand_path(d) } end # Location of user's personal cache directory. - def self.cache_home + def cache_home File.expand_path( - ENV['XDG_CACHE_HOME'] || File.join(xdg_home, '.cache') + ENV['XDG_CACHE_HOME'] || File.join(home, '.cache') ) end # Find a file or directory in data dirs. - def self.data_file(file) - find = nil - [xdg_data_home, *xdg_data_dirs].each do |dir| - path = File.join(dir,file) - break find = path if File.exist?(path) + def data_find(*glob_and_flags) + data_glob(*glob_and_flags).first + end + + def data_glob(*glob_and_flags) + glob, flags = *glob_and_flags.partition{ |e| String===e } + flag = flags.inject(0) do |m, f| + if Symbol === f + m + File::const_get("FNM_#{f.to_s.upcase}") + else + m + f.to_i + end end + find = [] + [data_home, *data_dirs].each do |dir| + path = File.join(dir, *glob) + find.concat(Dir.glob(path, flag)) + end find end - # Find a file or directory in config dirs. - def self.config_file(file) - find = nil - [xdg_config_home, *xdg_config_dirs].each do |dir| - path = File.join(dir,file) - break find = path if File.exist?(path) + # Return the fist matching file or directory + # from the config locations. + # + # See +config_glog+. + def config_find(*glob_and_flags) + config_glob(*glob_and_flags).first + end + + # Return array of matching files or directories + # in any of the config locations. + # + # This starts with the user's home directory + # and then searches system directories. + # + # String parameters are joined into a pathname + # while Integers and Symbols treated as flags. + # + # For example, the following are equivalent: + # + # XDG.config_glob('sow/plugins', File::FNM_CASEFOLD) + # + # XDG.config_glob('sow', 'plugins', :casefold) + # + def config_glob(*glob_and_flags) + glob, flags = *glob_and_flags.partition{ |e| String===e } + flag = flags.inject(0) do |m, f| + if Symbol === f + m + File::const_get("FNM_#{f.to_s.upcase}") + else + m + f.to_i + end end + find = [] + [config_home, *config_dirs].each do |dir| + path = File.join(dir, *glob) + find.concat(Dir.glob(path, flag)) + end find end - # Find a file or directory in the user cache. - def self.cache_file(file) - path = File.join(xdg_cache_home,file) - File.exist?(path) ? path : nil + # Return the fist matching file or directory + # in the cache directory. + # + # See +cache_glog+. + def cache_find(*glob_and_flags) + cache_glob(*glob_and_flags).first end + # Return array of matching files or directories + # from the cache directory. + # + # String parameters are joined into a pathname + # while Integers and Symbols treated as flags. + # + # For example, the following are equivalent: + # + # XDG.cache_glob('sow/tmp', File::FNM_CASEFOLD) + # + # XDG.cache_glob('sow', 'tmp', :casefold) + # + def cache_glob(*glob_and_flags) + glob, flags = *glob_and_flags.partition{ |e| String===e } + flag = flags.inject(0) do |m, f| + if Symbol === f + m + File::const_get("FNM_#{f.to_s.upcase}") + else + m + f.to_i + end + end + path = File.join(cache_home,*glob) + Dir.glob(path, flag) + end + #-- # The following are not strictly XDG spec, - # but are useful in the same respect. + # but are useful in a similar respect. #++ # Location of working config directory. - def self.config_work + def config_work File.expand_path( - #ENV['XDG_CONFIG_WORK'] || File.join(Dir.pwd, '.config') File.join(Dir.pwd, '.config') ) end # Location of working data directory. - def self.data_work + def data_work File.expand_path( - #ENV['XDG_DATA_WORK'] || File.join(Dir.pwd, '.share') File.join(Dir.pwd, '.share') ) end # Location of working cache directory. - def self.cache_work + def cache_work File.expand_path( - #ENV['XDG_CACHE_WORK'] || File.join(Dir.pwd, '.cache') File.join(Dir.pwd, '.cache') ) end - - ############### - module_function - ############### - - def xdg_home ; XDG.home ; end - def xdg_config_home ; XDG.config_home ; end - def xdg_config_dirs ; XDG.config_dirs ; end - def xdg_data_home ; XDG.data_home ; end - def xdg_data_dirs ; XDG.data_dirs ; end - def xdg_cache_home ; XDG.cache_home ; end - - def xdg_data_file(file) ; XDG.data_file(file) ; end - def xdg_config_file(file) ; XDG.config_file(file) ; end - def xdg_cache_file(file) ; XDG.cache_file(file) ; end - - def xdg_config_work ; XDG.config_work ; end - def xdg_data_work ; XDG.data_work ; end - def xdg_cache_work ; XDG.cache_work ; end end # module XDG