Class: Typingpool::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/typingpool/config.rb,
lib/typingpool/config/root.rb

Overview

Hierarchical config object. Can be read from a YAML file and is often modified at runtime, for example in response to script flags.

Fields

All listed defaults are populated when you run tp-config.

Required

[transcripts] Unexpanded path to working directory for
              transcripts. This is where tp-make creates new
              transcript projects, and where other scripts like
              tp-assign, tp-review and tp-finish look for
              them. Default: On systems with a ~/Desktop (like OS
              X), ~/Desktop/Transcripts. Elsewhere,
              ~/transcripts.

amazon

[key]    An Amazon Web Services "Access Key ID." Default: none.
[secret] An Amazon Web Services "Secret Access Key." Default: none.
[bucket] The name of the "bucket" on Amazon S3 where your uploads
         will be stored. Not required if you specify SFTP config
         instead (see below). Default: Generated for you when you
         run tp-config.

Optional

[cache]     Unexpanded path to the cache file (pstore). Default:
            ~/.typingpool.cache
[templates] Unexpanded path to directory for user-created
            templates. Will be searched before looking in the
            template dir within the app. Default: 'templates' or
            'Templates' (OS X) dir inside the transcripts dir.

amazon

[url] Base URL to use when linking to files uploaded to S3. You
      may want to use this if you do custom domain mapping on
      S3. Default is https://$bucket.s3.amazonaws.com. MUST BE
      HTTPS URL PER AMAZON POLICY.

sftp

If you provide SFTP config, the specified SFTP server will be used to host remote mp3 and html files rather than Amazon S3. At minimum, you must provide a user, host, and URL. SFTP will work fine with public-key authentication (passwordless login). In fact, I’ve not bothered to add password support yet.

[user] SFTP username
[host] SFTP server
[path] Files will be uploaded into this path. Optional.
[url] Base URL to use when linking to files uploaded using the
       preceding config. MUST BE HTTPS URL PER AMAZON POLICY.

assign

Defaults for tp-assign.

[reward]   Pay per transcription chunk in U.S. dollars. Default: 0.75.
[deadline] Length of time a worker has to complete a
           transcription job after accepting it (HIT
           'AssignmentDurationInSeconds' in the Mechanical Turk
           API). For details on the format, see docs for
           Utility.timespec_to_seconds. Default: 3h.
[approval] Length of time before a submitted transcription job is
           automatically approved (HIT
           'AutoApprovalDelayInSeconds' in the Mechanical Turk
           API). For details on the format, see docs for
           Utility.timespec_to_seconds. Default: 1d.
[lifetime] Length of time before a transcription job is no longer
           available to be accepted (HIT 'LifetimeInSeconds' in
           the Mechanical Turk API). For details on the format,
           see docs for Utility.timespec_to_seconds. Default: 2d.
[qualify]  An array of qualifications with which to filter workers
           who may accept a transcript job. The first part of the
           qualification should be the string form of a key in
           RTurk::Qualifications.types (see
           https://github.com/mdp/rturk/blob/master/lib/rturk/builders/qualification_builder.rb
           ). The second part should be one of the following
           comparators: > >= < <= == != exists. The optional
           third part is a value. Default: ['approval_rate >=
           95'].
[keywords] An array of keywords with which to tag each
           transcription job. Default: ['transcription', 'audio',
           'mp3'].

API

Values are read via same-named methods and set via same-named equals methods, like so:

transcript_path = config.transcripts
config.transcripts = new_path

Nested sections are created simply by declaring a nested class (which should typically inherit from Config, even if nested several levels lower).

Fields can be assigned special behaviors:

class Config
  class Root < Config
    local_path_reader :transcripts
    class SFTP < Config
      never_ends_in_slash_reader :url
    end
  end
end

conf = Typingpool::Config.file
conf.transcripts = '~/Documents/Transcripts'
puts conf.transcripts #'/Volumes/Redsector/Users/chad/Documents/Transcripts'
conf.sftp.url = 'http://luvrecording.s3.amazonaws.com/'
puts conf.sftp.url #'http://luvrecording.s3.amazonaws.com'

Defined Under Namespace

Classes: Root

Constant Summary collapse

@@default_file =
"~/.typingpool"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Config

Returns a new instance of Config.



108
109
110
# File 'lib/typingpool/config.rb', line 108

def initialize(params)
  @param = params
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/typingpool/config.rb', line 217

def method_missing(meth, *args)
  equals_param = equals_method?(meth)
  if equals_param
    args.count == 1 or raise Error::Argument, "Wrong number of args (#{args.count} for 1)"
    return @param[equals_param] = args[0]
  end
  args.empty? or raise Error::Argument, "Too many args #{meth} #{args.join('|')}"
  value = @param[meth.to_s]
  if self.class.subklass?(meth.to_s) && value
    return self.class.subklass?(meth.to_s).new(value)
  end
  value
end

Class Method Details

.default_fileObject

Will always return ~/.typingpool unless you subclass. Will be handed to File.expand_path before use.



124
125
126
# File 'lib/typingpool/config.rb', line 124

def default_file
  @@default_file
end

.define_accessor(*syms, &block) ⇒ Object



181
182
183
184
185
186
187
188
189
# File 'lib/typingpool/config.rb', line 181

def define_accessor(*syms, &block)
  #explicit block passing to avoid sefault in 1.9.3-p362
  define_reader(*syms) do |value|
    block.call(value) if value
  end
  define_writer(*syms) do |value|
    block.call(value)
  end
end

.define_reader(*syms, &block) ⇒ Object



162
163
164
165
166
167
168
169
170
# File 'lib/typingpool/config.rb', line 162

def define_reader(*syms, &block)
  #explicit block passing to avoid sefault in 1.9.3-p362
  syms.each do |sym|
    define_method(sym) do
      value = @param[sym.to_s]
      block.call(value)
    end
  end
end

.define_writer(*syms, &block) ⇒ Object



172
173
174
175
176
177
178
179
# File 'lib/typingpool/config.rb', line 172

def define_writer(*syms, &block)
  #explicit block passing to avoid sefault in 1.9.3-p362
  syms.each do |sym|
    define_method("#{sym.to_s}=".to_sym) do |value|
      @param[sym.to_s] = block.call(value)
    end
  end
end

.file(path = File.expand_path(default_file)) ⇒ Object

Constructor.

Params

path

Fully expanded path to YAML file.

Returns

Config instance.



118
119
120
# File 'lib/typingpool/config.rb', line 118

def file(path=File.expand_path(default_file))
  Root.new(YAML.load(IO.read((path))))
end

.from_bundled_templateObject

Returns a new Config instance, empty except for the values included in lib/templates/config.yml



130
131
132
# File 'lib/typingpool/config.rb', line 130

def from_bundled_template
  file(File.join(Utility.lib_dir, 'templates', 'config.yml'))
end

.inherited(subklass) ⇒ Object



191
192
193
194
# File 'lib/typingpool/config.rb', line 191

def inherited(subklass)
  @@subklasses ||= {}
  @@subklasses[subklass.name.downcase] = subklass
end

.local_path_reader(*syms) ⇒ Object

Define a field in a Config subclass as a local path. Reads on that field will be filtered through File.expand_path.



138
139
140
141
142
# File 'lib/typingpool/config.rb', line 138

def local_path_reader(*syms)
  define_reader(*syms) do |value|
    File.expand_path(value) if value
  end
end

.never_ends_in_slash_reader(*syms) ⇒ Object

Define a field in a Config subclass as never ending in ‘/’. Useful for URLs and SFTP path specs. When a field is set to a value ending in ‘/’, the last character is stripped.



147
148
149
150
151
# File 'lib/typingpool/config.rb', line 147

def never_ends_in_slash_reader(*syms)
  define_reader(*syms) do |value|
    value.sub(/\/$/, '') if value
  end
end

.subklass?(param) ⇒ Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/typingpool/config.rb', line 196

def subklass?(param)
  @@subklasses["#{self.name.downcase}::#{param.downcase}"] 
end

.time_accessor(*syms) ⇒ Object

Define a field in a Config subclass as a time-length specification. For format details, see docs for Utility.timespec_to_seconds.



156
157
158
159
160
# File 'lib/typingpool/config.rb', line 156

def time_accessor(*syms)
  define_accessor(*syms) do |value|
    Utility.timespec_to_seconds(value) or raise Error::Argument::Format, "Can't convert '#{value}' to time"
  end
end

Instance Method Details

#[](key) ⇒ Object

Read the raw data for a field



208
209
210
# File 'lib/typingpool/config.rb', line 208

def [](key)
  @param[key]
end

#[]=(key, value) ⇒ Object

Set the raw data for a field



213
214
215
# File 'lib/typingpool/config.rb', line 213

def []=(key, value)
  @param[key] = value
end

#to_hashObject

All fields as raw key-value pairs. For nested subclasses, the value is another hash.



203
204
205
# File 'lib/typingpool/config.rb', line 203

def to_hash
  @param
end