Class: Originals::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/originals/tool.rb

Class Method Summary collapse

Class Method Details

._parse_id(str) ⇒ Object

helpers



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/originals/tool.rb', line 179

def self._parse_id( str )
  if str.gsub( /[ #()№º._-]/, '' ) =~ /
                    ^(no|n|id)?
                    (?<id>[0-9]+)$
                     /ix
    id = Regexp.last_match[:id].to_i(10)   ## note: add base 10 (for 008 or such)
    id
  else
     nil   # retur nil if no match
  end
end

.main(args = ARGV) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/originals/tool.rb', line 7

def self.main( args=ARGV )
  puts "==> welcome to the fab(ricate) tool with args:"
  pp args

  options = { }
  parser = OptionParser.new do |opts|

    opts.on("--zoom NUM", "-z", Integer,
            "Zoom factor x2, x4, x8, etc. (default: 1)") do |num|
        options[ :zoom]  = num
    end

    opts.on("--background STRING", "--bg STRING", "-b", String,
            "Background (default: transparent|0x0)") do |str|
        options[ :background] = str
    end

    opts.on("--name STRING", "-n", String,
            "Base name (default: punk|phunk|marilyn|etc.)") do |str|
        options[ :name] = str
    end

    opts.on("--id NUM", "-i", Integer,
            "Unique identifier (default: none)") do |str|
        options[ :id] = str
    end

    opts.on( "--require STRING", "-r", String, "Require image library / gem (default: none)") do |str|
        ## todo/check: allow multiple requires - why? why not?
        ##   or use comma to separate??
        options[ :gems ] = str
    end

    opts.on("-h", "--help", "Prints this help") do
      puts opts
      exit
    end
  end

  parser.parse!( args )
  puts "options:"
  pp options

  puts "args:"
  pp args

  if args.size < 1
    puts "!! ERROR - no art series name found - use <name> <attribute>..."
    puts ""
    exit
  end


  if options[:gems]
      gems = options[:gems].split(',')
      gems.each do |gem|
         puts "==> auto-require >#{gem}<..."
         require gem
      end
  end


  name       = args[0]   ## todo/check - use collection_name/slug or such?


  if ['ls', 'list'].include?( name )
     ## list all known image generator(s)
     Original.factory.list
     exit
  end



  attributes = args[1..-1]


  ## normalize name of series
  ##   e.g.   Shiba Inu  => shibainu  etc.
  key = Factory.normalize_key( name )

  ## check for readymade via series id
  ##    allow some "literal" variants e.g.
  ##      punk no.1 or no.001
  ##      punk #1   or #001
  ##  etc.
  ##    and some more in the future - why? why not?

  id = nil

  img =   if attributes.size == 1 && id=_parse_id( attributes[0] )
              ## check for known collection names
              ## (mostly singular to plural)  - keep? why? why not?

              ## add support for "virtual" phunks  - move "upstream" to artbase - why? why not?
              if ['phunk', 'phunks'].include?( key )
                Artbase::Image.get( 'punks', id ).mirror
              elsif ['morephunk', 'morephunks'].include?( key )
                Artbase::Image.get( 'morepunks', id ).mirror
              elsif ['bwphunk', 'bwphunks'].include?( key )
                Artbase::Image.get( 'bwpunks', id ).mirror
              elsif ['readymadephunk', 'readymadephunks'].include?( key )
                Artbase::Image.get( 'readymadepunks', id ).mirror
              elsif ['intlphunk', 'intlphunks'].include?( key )
                Artbase::Image.get( 'intlpunks', id ).mirror
              elsif ['phoonbird', 'phoonbirds',
                     'moonbhird', 'moonbhirds',
                     'bhird', 'bhirds'].include?( key )
                Artbase::Image.get( 'moonbirds', id ).mirror
              elsif ['moonbirdphunk', 'moonbirdphunks',
                     'birdphunk', 'birdphunks'].include?( key )
                Artbase::Image.get( 'moonbirdpunks', id ).mirror
              else
                collection = Artbase::COLLECTIONS[ key ] || key
                Artbase::Image.get( collection, id )
              end
           else
              Original::Image.fabricate( key, *attributes )
           end

  if img.nil?
    puts "!! ERROR - don't know how to fabricate >#{name}<; sorry"
    exit 1
  end


  ## note: keep adding background as a separate step for now
  if options[ :background]
     ##  hack: for now for artbase collections
     ##          try to remove background if any
     ##         keep? why? why not?
     img = img.transparent    if id

     ## note: support multiple backgrounds
     ##       via + for now e.g.
     ##         blue+rainbow1
     ##   add more separators - why? why not?
     backgrounds = options[ :background].split( '+' )
     backgrounds = backgrounds.map {|background| background.strip }

     img = img.background( *backgrounds )
  end


  basename = String.new('')   ## note: start with our own string buffer
                              ##    otherwise assigned first string gets modified!!
  basename +=   options[:name] ? options[:name] : key

  basename += if options[:id]
                options[:id]
              elsif id
                id.to_s
              else
                '' # add nothing
              end


  path =  if options[:zoom]
             img = img.zoom( options[:zoom] )
             "./#{basename}@#{options[:zoom]}x.png"
          else
             "./#{basename}.png"
          end

  puts "  saving original #{name} (#{img.width}x#{img.height}) to >#{path}<..."
  img.save( path )

  puts "bye"
end