Class: Wukong::FilenamePattern

Inherits:
Object
  • Object
show all
Defined in:
lib/wukong/filename_pattern.rb

Constant Summary collapse

DEFAULT_PATTERN_STR =
":dest_dir/:handle_prefix/:handle/:date/:handle:timestamp-:pid-:hostname.tsv"
SAFE_CHARS =

Characters deemed safe in a filename;

'a-zA-Z0-9_\-\.\+\/'
RE_SAFE_FILENAME =
%r{[^#{SAFE_CHARS}]+}moxi

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, token_val_defaults = {}) ⇒ FilenamePattern

Returns a new instance of FilenamePattern.



10
11
12
13
# File 'lib/wukong/filename_pattern.rb', line 10

def initialize pattern, token_val_defaults={}
  self.pattern = pattern
  self.token_val_defaults    = token_val_defaults
end

Instance Attribute Details

#patternObject

the filename pattern, e.g. ‘ripd/:handle/:date/:handle+:timestamp-:pid-:hostname.tsv’



4
5
6
# File 'lib/wukong/filename_pattern.rb', line 4

def pattern
  @pattern
end

#token_val_defaultsObject

custom token replacements



6
7
8
# File 'lib/wukong/filename_pattern.rb', line 6

def token_val_defaults
  @token_val_defaults
end

Class Method Details

.sanitize(str) ⇒ Object



69
70
71
# File 'lib/wukong/filename_pattern.rb', line 69

def self.sanitize str
  str.gsub(RE_SAFE_FILENAME, '-')
end

Instance Method Details

#hostnameObject

Memoized: the hostname for the machine running this script.



58
59
60
# File 'lib/wukong/filename_pattern.rb', line 58

def hostname
  @hostname ||= ENV['HOSTNAME'] || `hostname`.chomp
end

#make(token_vals = {}) ⇒ Object

walk through pattern, replacing tokens (eg :time or :pid) with the corresponding value.

Don’t use ‘:’ in a pattern except to introduce a token and separate tokens with ‘-’, ‘+’ ‘/’ or ‘.’



22
23
24
25
26
27
# File 'lib/wukong/filename_pattern.rb', line 22

def make token_vals={}
  token_vals = token_val_defaults.merge token_vals
  token_vals[:timestamp] ||= Time.now.utc.strftime("%Y%m%d%H%M%S")
  val = pattern.gsub(/:(\w+)/){ replace($1, token_vals)  }
  val
end

#pidObject

Memoized: the Process ID for this invocation.



62
63
64
# File 'lib/wukong/filename_pattern.rb', line 62

def pid
  @pid      ||= Process.pid
end

#replace(token, token_vals) ⇒ Object

substitute for token



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/wukong/filename_pattern.rb', line 36

def replace token, token_vals
  token = token.to_sym
  return token_vals[token] if token_vals.include? token
  case token
  when :pid           then pid
  when :hostname      then hostname
  when :handle        then token_vals[:handle]
  when :handle_prefix then token_vals[:handle].to_s[0..5]
  when :timestamp     then token_vals[:timestamp]
  when :date          then token_vals[:timestamp][ 0..7]
  when :time          then token_vals[:timestamp][ 8..13]
  when :hour          then token_vals[:timestamp][ 8..9]
  when :h4            then "%0.2d" % (( token_vals[:timestamp][8..9].to_i / 4 ) * 4)
  when :min           then token_vals[:timestamp][10..11]
  when :sec           then token_vals[:timestamp][12..13]
  when :s10           then "%0.2d" % (( token_vals[:timestamp][12..13].to_i / 10 ) * 10)
  else
    raise "Don't know how to encode token #{token} #{token_vals[token]}"
  end
end

#to_s(token_vals = {}) ⇒ Object



29
30
31
# File 'lib/wukong/filename_pattern.rb', line 29

def to_s token_vals={}
  make token_vals
end