Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""":mod:`wand.version` --- Version data 

2~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

3 

4You can find the current version in the command line interface: 

5 

6.. sourcecode:: console 

7 

8 $ python -m wand.version 

9 0.0.0 

10 $ python -m wand.version --verbose 

11 Wand 0.0.0 

12 ImageMagick 6.7.7-6 2012-06-03 Q16 http://www.imagemagick.org 

13 $ python -m wand.version --config | grep CC | cut -d : -f 2 

14 gcc -std=gnu99 -std=gnu99 

15 $ python -m wand.version --fonts | grep Helvetica 

16 Helvetica 

17 Helvetica-Bold 

18 Helvetica-Light 

19 Helvetica-Narrow 

20 Helvetica-Oblique 

21 $ python -m wand.version --formats | grep CMYK 

22 CMYK 

23 CMYKA 

24 

25.. versionadded:: 0.2.0 

26 The command line interface. 

27 

28.. versionadded:: 0.2.2 

29 The ``--verbose``/``-v`` option which also prints ImageMagick library 

30 version for CLI. 

31 

32.. versionadded:: 0.4.1 

33 The ``--fonts``, ``--formats``, & ``--config`` option allows printing 

34 additional information about ImageMagick library. 

35 

36""" 

37from __future__ import print_function 

38 

39import ctypes 

40import datetime 

41import re 

42import sys 

43 

44try: 

45 from .api import libmagick, library 

46except ImportError: # pragma: no cover 

47 libmagick = None 

48from .compat import binary, string_type, text 

49 

50 

51__all__ = ('VERSION', 'VERSION_INFO', 'MAGICK_VERSION', 

52 'MAGICK_VERSION_DELEGATES', 'MAGICK_VERSION_FEATURES', 

53 'MAGICK_VERSION_INFO', 'MAGICK_VERSION_NUMBER', 

54 'MAGICK_RELEASE_DATE', 'MAGICK_RELEASE_DATE_STRING', 'MAGICK_HDRI', 

55 'QUANTUM_DEPTH', 'QUANTUM_RANGE', 'configure_options', 

56 'fonts', 'formats') 

57 

58#: (:class:`tuple`) The version tuple e.g. ``(0, 1, 2)``. 

59#: 

60#: .. versionchanged:: 0.1.9 

61#: Becomes :class:`tuple`. (It was string before.) 

62VERSION_INFO = (0, 6, 1) 

63 

64#: (:class:`basestring`) The version string e.g. ``'0.1.2'``. 

65#: 

66#: .. versionchanged:: 0.1.9 

67#: Becomes string. (It was :class:`tuple` before.) 

68VERSION = '{0}.{1}.{2}'.format(*VERSION_INFO) 

69 

70if libmagick: 

71 c_magick_version = ctypes.c_size_t() 

72 #: (:class:`basestring`) The version string of the linked ImageMagick 

73 #: library. The exactly same string to the result of 

74 #: :c:func:`GetMagickVersion` function. 

75 #: 

76 #: Example:: 

77 #: 

78 #: 'ImageMagick 6.7.7-6 2012-06-03 Q16 http://www.imagemagick.org' 

79 #: 

80 #: .. versionadded:: 0.2.1 

81 MAGICK_VERSION = text( 

82 libmagick.GetMagickVersion(ctypes.byref(c_magick_version)) 

83 ) 

84 

85 #: (:class:`numbers.Integral`) The version number of the linked 

86 #: ImageMagick library. 

87 #: 

88 #: .. versionadded:: 0.2.1 

89 MAGICK_VERSION_NUMBER = c_magick_version.value 

90 

91 _match = re.match(r'^ImageMagick\s+(\d+)\.(\d+)\.(\d+)(?:-(\d+))?', 

92 MAGICK_VERSION) 

93 

94 #: (:class:`basestring`) A string of all delegates enabled. 

95 #: This value is identical to what is returned by 

96 #: :c:func:`GetMagickDelegates` 

97 #: 

98 #: Set to empty string if the system uses an older version of 

99 #: ImageMagick-6, or does not support :c:func:`GetMagickDelegates`. 

100 #: 

101 #: .. versionadded:: 0.5.0 

102 if libmagick.GetMagickDelegates: # pragma: no cover 

103 MAGICK_VERSION_DELEGATES = text(libmagick.GetMagickDelegates()) 

104 else: # pragma: no cover 

105 MAGICK_VERSION_DELEGATES = "" 

106 

107 #: (:class:`basestring`) A string of all features enabled. 

108 #: This value is identical to what is returned by 

109 #: :c:func:`GetMagickFeatures` 

110 #: 

111 #: .. versionadded:: 0.5.0 

112 MAGICK_VERSION_FEATURES = text(libmagick.GetMagickFeatures()) 

113 

114 #: (:class:`tuple`) The version tuple e.g. ``(6, 7, 7, 6)`` of 

115 #: :const:`MAGICK_VERSION`. 

116 #: 

117 #: .. versionadded:: 0.2.1 

118 MAGICK_VERSION_INFO = tuple(int(v or 0) for v in _match.groups()) 

119 

120 #: (:class:`basestring`) The date string e.g. ``'2012-06-03'`` of 

121 #: :const:`MAGICK_RELEASE_DATE_STRING`. This value is the exactly same 

122 #: string to the result of :c:func:`GetMagickReleaseDate` function. 

123 #: 

124 #: .. versionadded:: 0.2.1 

125 MAGICK_RELEASE_DATE_STRING = text(libmagick.GetMagickReleaseDate()) 

126 

127 if MAGICK_RELEASE_DATE_STRING: 

128 _match = re.match(r'^(\d{4})-?(\d\d)-?(\d\d)$', 

129 MAGICK_RELEASE_DATE_STRING) 

130 #: (:class:`datetime.date`) The release date of the linked ImageMagick 

131 #: library. Equivalent to the result of :c:func:`GetMagickReleaseDate` 

132 #: function. 

133 #: 

134 #: .. versionadded:: 0.2.1 

135 MAGICK_RELEASE_DATE = datetime.date(*map(int, _match.groups())) 

136 

137 c_quantum_depth = ctypes.c_size_t() 

138 libmagick.GetMagickQuantumDepth(ctypes.byref(c_quantum_depth)) 

139 #: (:class:`numbers.Integral`) The quantum depth configuration of 

140 #: the linked ImageMagick library. One of 8, 16, 32, or 64. 

141 #: 

142 #: .. versionadded:: 0.3.0 

143 QUANTUM_DEPTH = c_quantum_depth.value 

144 

145 c_quantum_range = ctypes.c_size_t() 

146 libmagick.GetMagickQuantumRange(ctypes.byref(c_quantum_range)) 

147 #: (:class:`numbers.Integral`) The quantum range configuration of 

148 #: the linked ImageMagick library. 

149 #: 

150 #: .. versionadded:: 0.5.0 

151 QUANTUM_RANGE = c_quantum_range.value 

152 

153 #: (:class:`bool`) True if ImageMagick is compiled for High Dynamic 

154 #: Range Image. 

155 MAGICK_HDRI = 'HDRI' in MAGICK_VERSION_FEATURES 

156 

157 del c_magick_version, _match, c_quantum_depth, c_quantum_range 

158 

159 

160def configure_options(pattern='*'): 

161 """ 

162 Queries ImageMagick library for configurations options given at 

163 compile-time. 

164 

165 Example: Find where the ImageMagick documents are installed:: 

166 

167 >>> from wand.version import configure_options 

168 >>> configure_options('DOC*') 

169 {'DOCUMENTATION_PATH': '/usr/local/share/doc/ImageMagick-6'} 

170 

171 :param pattern: A term to filter queries against. Supports wildcard '*' 

172 characters. Default patterns '*' for all options. 

173 :type pattern: :class:`basestring` 

174 :returns: Directory of configuration options matching given pattern 

175 :rtype: :class:`collections.defaultdict` 

176 """ 

177 if not isinstance(pattern, string_type): 

178 raise TypeError('pattern must be a string, not ' + repr(pattern)) 

179 # We must force init environment to load user config paths. 

180 library.MagickWandGenesis() 

181 pattern_p = ctypes.create_string_buffer(binary(pattern)) 

182 config_count = ctypes.c_size_t(0) 

183 configs = {} 

184 configs_p = library.MagickQueryConfigureOptions(pattern_p, 

185 ctypes.byref(config_count)) 

186 cursor = 0 

187 while cursor < config_count.value: 

188 config = configs_p[cursor].value 

189 value = library.MagickQueryConfigureOption(config) 

190 configs[text(config)] = text(value.value) 

191 cursor += 1 

192 return configs 

193 

194 

195def fonts(pattern='*'): 

196 """ 

197 Queries ImageMagick library for available fonts. 

198 

199 Available fonts can be configured by defining `types.xml`, 

200 `type-ghostscript.xml`, or `type-windows.xml`. 

201 Use :func:`wand.version.configure_options` to locate system search path, 

202 and `resources <http://www.imagemagick.org/script/resources.php>`_ 

203 article for defining xml file. 

204 

205 Example: List all bold Helvetica fonts:: 

206 

207 >>> from wand.version import fonts 

208 >>> fonts('*Helvetica*Bold*') 

209 ['Helvetica-Bold', 'Helvetica-Bold-Oblique', 'Helvetica-BoldOblique', 

210 'Helvetica-Narrow-Bold', 'Helvetica-Narrow-BoldOblique'] 

211 

212 

213 :param pattern: A term to filter queries against. Supports wildcard '*' 

214 characters. Default patterns '*' for all options. 

215 :type pattern: :class:`basestring` 

216 :returns: Sequence of matching fonts 

217 :rtype: :class:`collections.Sequence` 

218 """ 

219 if not isinstance(pattern, string_type): 

220 raise TypeError('pattern must be a string, not ' + repr(pattern)) 

221 # We must force init environment to load user config paths. 

222 library.MagickWandGenesis() 

223 pattern_p = ctypes.create_string_buffer(binary(pattern)) 

224 number_fonts = ctypes.c_size_t(0) 

225 fonts = [] 

226 fonts_p = library.MagickQueryFonts(pattern_p, 

227 ctypes.byref(number_fonts)) 

228 cursor = 0 

229 while cursor < number_fonts.value: 

230 font = fonts_p[cursor].value 

231 fonts.append(text(font)) 

232 cursor += 1 

233 return fonts 

234 

235 

236def formats(pattern='*'): 

237 """ 

238 Queries ImageMagick library for supported formats. 

239 

240 Example: List supported PNG formats:: 

241 

242 >>> from wand.version import formats 

243 >>> formats('PNG*') 

244 ['PNG', 'PNG00', 'PNG8', 'PNG24', 'PNG32', 'PNG48', 'PNG64'] 

245 

246 

247 :param pattern: A term to filter formats against. Supports wildcards '*' 

248 characters. Default pattern '*' for all formats. 

249 :type pattern: :class:`basestring` 

250 :returns: Sequence of matching formats 

251 :rtype: :class:`collections.Sequence` 

252 """ 

253 if not isinstance(pattern, string_type): 

254 raise TypeError('pattern must be a string, not ' + repr(pattern)) 

255 # We must force init environment to load user config paths. 

256 library.MagickWandGenesis() 

257 pattern_p = ctypes.create_string_buffer(binary(pattern)) 

258 number_formats = ctypes.c_size_t(0) 

259 formats = [] 

260 formats_p = library.MagickQueryFormats(pattern_p, 

261 ctypes.byref(number_formats)) 

262 cursor = 0 

263 while cursor < number_formats.value: 

264 value = formats_p[cursor].value 

265 formats.append(text(value)) 

266 cursor += 1 

267 return formats 

268 

269 

270if __doc__ is not None: 

271 __doc__ = __doc__.replace('0.0.0', VERSION) 

272 

273del libmagick 

274 

275 

276if __name__ == '__main__': # pragma: no cover 

277 options = frozenset(sys.argv[1:]) 

278 if '-v' in options or '--verbose' in options: 

279 print('Wand', VERSION) 

280 try: 

281 print(MAGICK_VERSION) 

282 except NameError: 

283 pass 

284 elif '--fonts' in options: 

285 for font in fonts(): 

286 print(font) 

287 elif '--formats' in options: 

288 for supported_format in formats(): 

289 print(supported_format) 

290 elif '--config' in options: 

291 config_options = configure_options() 

292 for key in config_options: 

293 print('{:24s}: {}'.format(key, config_options[key])) 

294 else: 

295 print(VERSION)