File: configuration.rb

package info (click to toggle)
ruby-mini-magick 4.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,588 kB
  • sloc: ruby: 1,859; sh: 7; makefile: 4
file content (198 lines) | stat: -rw-r--r-- 5,480 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
require 'mini_magick/utilities'
require 'logger'

module MiniMagick
  module Configuration

    ##
    # Set whether you want to use [ImageMagick](http://www.imagemagick.org) or
    # [GraphicsMagick](http://www.graphicsmagick.org).
    #
    # @return [Symbol] `:imagemagick`, `:imagemagick7`, or `:graphicsmagick`
    #
    attr_accessor :cli

    ##
    # If you don't have the CLI tools in your PATH, you can set the path to the
    # executables.
    #
    attr_writer :cli_path
    # @private (for backwards compatibility)
    attr_accessor :processor_path

    ##
    # Adds a prefix to the CLI command.
    # For example, you could use `firejail` to run all commands in a sandbox.
    # Can be a string, or an array of strings.
    # e.g. 'firejail', or ['firejail', '--force']
    #
    # @return [String]
    # @return [Array<String>]
    #
    attr_accessor :cli_prefix

    ##
    # If you don't want commands to take too long, you can set a timeout (in
    # seconds).
    #
    # @return [Integer]
    #
    attr_accessor :timeout
    ##
    # When get to `true`, it outputs each command to STDOUT in their shell
    # version.
    #
    # @return [Boolean]
    #
    attr_reader :debug
    ##
    # Logger for {#debug}, default is `MiniMagick::Logger.new(STDOUT)`, but
    # you can override it, for example if you want the logs to be written to
    # a file.
    #
    # @return [Logger]
    #
    attr_accessor :logger

    ##
    # If set to `true`, it will `identify` every newly created image, and raise
    # `MiniMagick::Invalid` if the image is not valid. Useful for validating
    # user input, although it adds a bit of overhead. Defaults to `true`.
    #
    # @return [Boolean]
    #
    attr_accessor :validate_on_create
    ##
    # If set to `true`, it will `identify` every image that gets written (with
    # {MiniMagick::Image#write}), and raise `MiniMagick::Invalid` if the image
    # is not valid. Useful for validating that processing was sucessful,
    # although it adds a bit of overhead. Defaults to `true`.
    #
    # @return [Boolean]
    #
    attr_accessor :validate_on_write

    ##
    # If set to `false`, it will not raise errors when ImageMagick returns
    # status code different than 0. Defaults to `true`.
    #
    # @return [Boolean]
    #
    attr_accessor :whiny

    ##
    # Instructs MiniMagick how to execute the shell commands. Available
    # APIs are "open3" (default) and "posix-spawn" (requires the "posix-spawn"
    # gem).
    #
    # @return [String]
    #
    attr_accessor :shell_api

    def self.extended(base)
      base.validate_on_create = true
      base.validate_on_write = true
      base.whiny = true
      base.shell_api = "open3"
      base.logger = Logger.new($stdout).tap { |l| l.level = Logger::INFO }
    end

    ##
    # @yield [self]
    # @example
    #   MiniMagick.configure do |config|
    #     config.cli = :graphicsmagick
    #     config.timeout = 5
    #   end
    #
    def configure
      yield self
    end

    CLI_DETECTION = {
      imagemagick7:   "magick",
      imagemagick:    "mogrify",
      graphicsmagick: "gm",
    }

    # @private (for backwards compatibility)
    def processor
      @processor ||= CLI_DETECTION.values.detect do |processor|
        MiniMagick::Utilities.which(processor)
      end
    end

    # @private (for backwards compatibility)
    def processor=(processor)
      @processor = processor.to_s

      unless CLI_DETECTION.value?(@processor)
        raise ArgumentError,
          "processor has to be set to either \"magick\", \"mogrify\" or \"gm\"" \
          ", was set to #{@processor.inspect}"
      end
    end

    ##
    # Get [ImageMagick](http://www.imagemagick.org) or
    # [GraphicsMagick](http://www.graphicsmagick.org).
    #
    # @return [Symbol] `:imagemagick` or `:graphicsmagick`
    #
    def cli
      if instance_variable_defined?("@cli")
        instance_variable_get("@cli")
      else
        cli = CLI_DETECTION.key(processor) or
          fail MiniMagick::Error, "You must have ImageMagick or GraphicsMagick installed"

        instance_variable_set("@cli", cli)
      end
    end

    ##
    # Set whether you want to use [ImageMagick](http://www.imagemagick.org) or
    # [GraphicsMagick](http://www.graphicsmagick.org).
    #
    def cli=(value)
      @cli = value

      if not CLI_DETECTION.key?(@cli)
        raise ArgumentError,
          "CLI has to be set to either :imagemagick, :imagemagick7 or :graphicsmagick" \
          ", was set to #{@cli.inspect}"
      end
    end

    ##
    # If you set the path of CLI tools, you can get the path of the
    # executables.
    #
    # @return [String]
    #
    def cli_path
      if instance_variable_defined?("@cli_path")
        instance_variable_get("@cli_path")
      else
        processor_path = instance_variable_get("@processor_path") if instance_variable_defined?("@processor_path")

        instance_variable_set("@cli_path", processor_path)
      end
    end

    ##
    # When set to `true`, it outputs each command to STDOUT in their shell
    # version.
    #
    def debug=(value)
      warn "MiniMagick.debug is deprecated and will be removed in MiniMagick 5. Use `MiniMagick.logger.level = Logger::DEBUG` instead."
      logger.level = value ? Logger::DEBUG : Logger::INFO
    end

    # Backwards compatibility
    def reload_tools
      warn "MiniMagick.reload_tools is deprecated because it is no longer necessary"
    end

  end
end