CommandWrap 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,18 @@
1
+ A set of utility classes to extract meta data from different file types
2
+
3
+ INSTALL
4
+ -------
5
+
6
+ gem install fileutils
7
+
8
+ REQUIREMENTS
9
+ ------------
10
+
11
+ For full functionality the following programs must be installed:
12
+ - openoffice
13
+ - openoffice python bindings
14
+ - pdftk
15
+ - zip
16
+ - imagemagick
17
+ - Xvfb
18
+ - libqt-webkit, libqt4-svg
data/bin/CutyCapt ADDED
Binary file
@@ -0,0 +1,231 @@
1
+ #
2
+ # PyODConverter (Python OpenDocument Converter) v1.1 - 2009-11-14
3
+ #
4
+ # This script converts a document from one office format to another by
5
+ # connecting to an OpenOffice.org instance via Python-UNO bridge.
6
+ #
7
+ # Copyright (C) 2008-2009 Mirko Nasato <mirko@artofsolving.com>
8
+ # Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl-2.1.html
9
+ # - or any later version.
10
+ #
11
+ DEFAULT_OPENOFFICE_PORT = 8100
12
+
13
+ import uno
14
+ from os.path import abspath, isfile, splitext
15
+ from com.sun.star.beans import PropertyValue
16
+ from com.sun.star.task import ErrorCodeIOException
17
+ from com.sun.star.connection import NoConnectException
18
+
19
+ FAMILY_TEXT = "Text"
20
+ FAMILY_WEB = "Web"
21
+ FAMILY_SPREADSHEET = "Spreadsheet"
22
+ FAMILY_PRESENTATION = "Presentation"
23
+ FAMILY_DRAWING = "Drawing"
24
+
25
+ #---------------------#
26
+ # Configuration Start #
27
+ #---------------------#
28
+
29
+ # see http://wiki.services.openoffice.org/wiki/Framework/Article/Filter
30
+
31
+ # most formats are auto-detected; only those requiring options are defined here
32
+ IMPORT_FILTER_MAP = {
33
+ "txt": {
34
+ "FilterName": "Text (encoded)",
35
+ "FilterOptions": "utf8"
36
+ },
37
+ "csv": {
38
+ "FilterName": "Text - txt - csv (StarCalc)",
39
+ "FilterOptions": "44,34,0"
40
+ }
41
+ }
42
+
43
+ EXPORT_FILTER_MAP = {
44
+ "pdf": {
45
+ FAMILY_TEXT: { "FilterName": "writer_pdf_Export" },
46
+ FAMILY_WEB: { "FilterName": "writer_web_pdf_Export" },
47
+ FAMILY_SPREADSHEET: { "FilterName": "calc_pdf_Export" },
48
+ FAMILY_PRESENTATION: { "FilterName": "impress_pdf_Export" },
49
+ FAMILY_DRAWING: { "FilterName": "draw_pdf_Export" }
50
+ },
51
+ "html": {
52
+ FAMILY_TEXT: { "FilterName": "HTML (StarWriter)" },
53
+ FAMILY_SPREADSHEET: { "FilterName": "HTML (StarCalc)" },
54
+ FAMILY_PRESENTATION: { "FilterName": "impress_html_Export" }
55
+ },
56
+ "odt": {
57
+ FAMILY_TEXT: { "FilterName": "writer8" },
58
+ FAMILY_WEB: { "FilterName": "writerweb8_writer" }
59
+ },
60
+ "doc": {
61
+ FAMILY_TEXT: { "FilterName": "MS Word 97" }
62
+ },
63
+ "rtf": {
64
+ FAMILY_TEXT: { "FilterName": "Rich Text Format" }
65
+ },
66
+ "txt": {
67
+ FAMILY_TEXT: {
68
+ "FilterName": "Text",
69
+ "FilterOptions": "utf8"
70
+ }
71
+ },
72
+ "ods": {
73
+ FAMILY_SPREADSHEET: { "FilterName": "calc8" }
74
+ },
75
+ "xls": {
76
+ FAMILY_SPREADSHEET: { "FilterName": "MS Excel 97" }
77
+ },
78
+ "csv": {
79
+ FAMILY_SPREADSHEET: {
80
+ "FilterName": "Text - txt - csv (StarCalc)",
81
+ "FilterOptions": "44,34,0"
82
+ }
83
+ },
84
+ "odp": {
85
+ FAMILY_PRESENTATION: { "FilterName": "impress8" }
86
+ },
87
+ "ppt": {
88
+ FAMILY_PRESENTATION: { "FilterName": "MS PowerPoint 97" }
89
+ },
90
+ "swf": {
91
+ FAMILY_DRAWING: { "FilterName": "draw_flash_Export" },
92
+ FAMILY_PRESENTATION: { "FilterName": "impress_flash_Export" }
93
+ }
94
+ }
95
+
96
+ PAGE_STYLE_OVERRIDE_PROPERTIES = {
97
+ FAMILY_SPREADSHEET: {
98
+ #--- Scale options: uncomment 1 of the 3 ---
99
+ # a) 'Reduce / enlarge printout': 'Scaling factor'
100
+ "PageScale": 100,
101
+ # b) 'Fit print range(s) to width / height': 'Width in pages' and 'Height in pages'
102
+ #"ScaleToPagesX": 1, "ScaleToPagesY": 1000,
103
+ # c) 'Fit print range(s) on number of pages': 'Fit print range(s) on number of pages'
104
+ #"ScaleToPages": 1,
105
+ "PrintGrid": False
106
+ }
107
+ }
108
+
109
+ #-------------------#
110
+ # Configuration End #
111
+ #-------------------#
112
+
113
+ class DocumentConversionException(Exception):
114
+
115
+ def __init__(self, message):
116
+ self.message = message
117
+
118
+ def __str__(self):
119
+ return self.message
120
+
121
+
122
+ class DocumentConverter:
123
+
124
+ def __init__(self, port=DEFAULT_OPENOFFICE_PORT):
125
+ localContext = uno.getComponentContext()
126
+ resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext)
127
+ try:
128
+ context = resolver.resolve("uno:socket,host=localhost,port=%s;urp;StarOffice.ComponentContext" % port)
129
+ except NoConnectException:
130
+ raise DocumentConversionException, "failed to connect to OpenOffice.org on port %s" % port
131
+ self.desktop = context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context)
132
+
133
+ def convert(self, inputFile, outputFile):
134
+
135
+ inputUrl = self._toFileUrl(inputFile)
136
+ outputUrl = self._toFileUrl(outputFile)
137
+
138
+ loadProperties = { "Hidden": True }
139
+ inputExt = self._getFileExt(inputFile)
140
+ if IMPORT_FILTER_MAP.has_key(inputExt):
141
+ loadProperties.update(IMPORT_FILTER_MAP[inputExt])
142
+
143
+ document = self.desktop.loadComponentFromURL(inputUrl, "_blank", 0, self._toProperties(loadProperties))
144
+ try:
145
+ document.refresh()
146
+ except AttributeError:
147
+ pass
148
+
149
+ family = self._detectFamily(document)
150
+ self._overridePageStyleProperties(document, family)
151
+
152
+ outputExt = self._getFileExt(outputFile)
153
+ storeProperties = self._getStoreProperties(document, outputExt)
154
+
155
+ try:
156
+ document.storeToURL(outputUrl, self._toProperties(storeProperties))
157
+ finally:
158
+ document.close(True)
159
+
160
+ def _overridePageStyleProperties(self, document, family):
161
+ if PAGE_STYLE_OVERRIDE_PROPERTIES.has_key(family):
162
+ properties = PAGE_STYLE_OVERRIDE_PROPERTIES[family]
163
+ pageStyles = document.getStyleFamilies().getByName('PageStyles')
164
+ for styleName in pageStyles.getElementNames():
165
+ pageStyle = pageStyles.getByName(styleName)
166
+ for name, value in properties.items():
167
+ pageStyle.setPropertyValue(name, value)
168
+
169
+ def _getStoreProperties(self, document, outputExt):
170
+ family = self._detectFamily(document)
171
+ try:
172
+ propertiesByFamily = EXPORT_FILTER_MAP[outputExt]
173
+ except KeyError:
174
+ raise DocumentConversionException, "unknown output format: '%s'" % outputExt
175
+ try:
176
+ return propertiesByFamily[family]
177
+ except KeyError:
178
+ raise DocumentConversionException, "unsupported conversion: from '%s' to '%s'" % (family, outputExt)
179
+
180
+ def _detectFamily(self, document):
181
+ if document.supportsService("com.sun.star.text.WebDocument"):
182
+ return FAMILY_WEB
183
+ if document.supportsService("com.sun.star.text.GenericTextDocument"):
184
+ # must be TextDocument or GlobalDocument
185
+ return FAMILY_TEXT
186
+ if document.supportsService("com.sun.star.sheet.SpreadsheetDocument"):
187
+ return FAMILY_SPREADSHEET
188
+ if document.supportsService("com.sun.star.presentation.PresentationDocument"):
189
+ return FAMILY_PRESENTATION
190
+ if document.supportsService("com.sun.star.drawing.DrawingDocument"):
191
+ return FAMILY_DRAWING
192
+ raise DocumentConversionException, "unknown document family: %s" % document
193
+
194
+ def _getFileExt(self, path):
195
+ ext = splitext(path)[1]
196
+ if ext is not None:
197
+ return ext[1:].lower()
198
+
199
+ def _toFileUrl(self, path):
200
+ return uno.systemPathToFileUrl(abspath(path))
201
+
202
+ def _toProperties(self, dict):
203
+ props = []
204
+ for key in dict:
205
+ prop = PropertyValue()
206
+ prop.Name = key
207
+ prop.Value = dict[key]
208
+ props.append(prop)
209
+ return tuple(props)
210
+
211
+
212
+ if __name__ == "__main__":
213
+ from sys import argv, exit
214
+
215
+ if len(argv) < 3:
216
+ print "USAGE: python %s <input-file> <output-file>" % argv[0]
217
+ exit(255)
218
+ if not isfile(argv[1]):
219
+ print "no such input file: %s" % argv[1]
220
+ exit(1)
221
+
222
+ try:
223
+ converter = DocumentConverter(argv[3])
224
+ converter.convert(argv[1], argv[2])
225
+ except DocumentConversionException, exception:
226
+ print "ERROR! " + str(exception)
227
+ exit(1)
228
+ except ErrorCodeIOException, exception:
229
+ print "ERROR! ErrorCodeIOException %d" % exception.ErrCode
230
+ exit(1)
231
+
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require File.dirname(__FILE__) + "/../lib/command_wrap"
3
+
4
+ CommandWrap::OpenOffice::Server.restart
@@ -0,0 +1,118 @@
1
+ require 'RMagick'
2
+
3
+ module CommandWrap
4
+
5
+ # Creates a screenshot from the given url
6
+ # Uses CutyCapt (cutycapt.sourceforge.net)
7
+ def self.capture (url, target)
8
+ command = CommandWrap::Config::Xvfb.command(File.dirname(__FILE__) + "/../bin/CutyCapt --min-width=1024 --min-height=768 --url=#{url} --out=#{target}")
9
+ `#{command}`
10
+ end
11
+
12
+ # Sources consists of paths followed by the filename that must be used in the zip
13
+ def self.zip (target, *sources)
14
+ targetdir = "#{CommandWrap::Config.tmp_dir}/zip"
15
+ id = 1
16
+ while File.exists?(targetdir)
17
+ targetdir = "#{CommandWrap::Config.tmp_dir}/zip#{id}"
18
+ id += 1
19
+ end
20
+ FileUtils.mkdir(targetdir)
21
+
22
+ path = ''
23
+ sources.each do |value|
24
+ if path == ''
25
+ path = value
26
+ else
27
+ FileUtils.copy(path, "#{targetdir}/#{value}")
28
+ path = ''
29
+ end
30
+ end
31
+
32
+ `#{CommandWrap::Config.zip} -j #{target} #{targetdir}/*`
33
+
34
+ FileUtils.rm_rf(targetdir)
35
+ end
36
+
37
+ def self.extension (filename)
38
+ return '' unless filename.include?('.')
39
+ filename.split('.').last
40
+ end
41
+
42
+ def self.preview (source, target, width, height)
43
+ extension = self.extension(source).upcase
44
+
45
+ # Image ?
46
+ formats = Magick.formats
47
+ if formats.key?(extension) && formats[extension].include?('r')
48
+ begin
49
+ CommandWrap::Image.scale(source, target, width, height)
50
+ return true
51
+ rescue
52
+ return false
53
+ end
54
+ end
55
+
56
+ tmppdf = self.temp('pdf')
57
+ tmppng = self.temp('png')
58
+ begin
59
+ # Create a pdf of the document
60
+ CommandWrap::OpenOffice.convert(source, tmppdf)
61
+ # Create a screenshot of first page of the generated pdf
62
+ CommandWrap::Pdf.preview(tmppdf, tmppng)
63
+ # Scale it down to thumb
64
+ CommandWrap::Image.scale(tmppng, target, width, height)
65
+ return true
66
+ rescue
67
+
68
+ ensure
69
+ # Cleanup
70
+ File.delete(tmppdf) if File.exists?(tmppdf) && File.writable?(tmppdf)
71
+ File.delete(tmppng) if File.exists?(tmppng) && File.writable?(tmppng)
72
+ end
73
+
74
+ nil
75
+ end
76
+
77
+ # Generates a temp filepath for the given extension
78
+ def self.temp (extension)
79
+ path = "#{CommandWrap::Config.tmp_dir}/tmp.#{extension}"
80
+ id = 1
81
+ while File.exists?(path)
82
+ path = "#{CommandWrap::Config.tmp_dir}/tmp.#{id}.#{extension}"
83
+ id += 1
84
+ end
85
+
86
+ path
87
+ end
88
+
89
+ # Tries to convert content of file to plaintext or html
90
+ def self.index (path)
91
+ extension = CommandWrap.extension(path).downcase
92
+
93
+ if extension == 'txt'
94
+ return IO.read(path)
95
+ end
96
+
97
+ if extension == 'html' || extension == 'htm'
98
+ return IO.read(path)
99
+ end
100
+
101
+ tmp = self.temp('html')
102
+ begin
103
+ CommandWrap::OpenOffice.convert(path, tmp)
104
+ return IO.read(tmp) if File.exists?(tmp)
105
+ rescue
106
+ ensure
107
+ File.delete(tmp) if File.exists?(tmp) && File.writable?(tmp)
108
+ end
109
+
110
+ ''
111
+ end
112
+
113
+ autoload :Image, File.dirname(__FILE__) + "/command_wrap/image"
114
+ autoload :OpenOffice, File.dirname(__FILE__) + "/command_wrap/open_office"
115
+ autoload :Config, File.dirname(__FILE__) + "/command_wrap/config"
116
+ autoload :Pdf, File.dirname(__FILE__) + "/command_wrap/pdf"
117
+
118
+ end
@@ -0,0 +1,34 @@
1
+ module CommandWrap
2
+
3
+ module Config
4
+
5
+ def self.tmp_dir
6
+ @tmp_dir ||= '/tmp'
7
+ end
8
+
9
+ def self.tmp_dir= (tmp_dir)
10
+ @tmp_dir = tmp_dir
11
+ end
12
+
13
+ def self.pdftk
14
+ @pdftk ||= 'pdftk'
15
+ end
16
+
17
+ def self.pdftk= (pdftk)
18
+ @pdftk = pdftk
19
+ end
20
+
21
+ def self.zip
22
+ @zip ||= 'zip'
23
+ end
24
+
25
+ def self.zip= (zip)
26
+ @zip = zip
27
+ end
28
+
29
+ autoload :OpenOffice, File.dirname(__FILE__) + "/config/open_office"
30
+ autoload :Xvfb, File.dirname(__FILE__) + "/config/xvfb"
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,79 @@
1
+ module CommandWrap::Config
2
+
3
+ module OpenOffice
4
+
5
+ def self.command
6
+ "#{executable} #{parsed_params}"
7
+ end
8
+
9
+ def self.parsed_params
10
+ params.gsub('[port]', port.to_s).gsub('[host]', host)
11
+ end
12
+
13
+ def self.executable
14
+ @openoffice ||= "soffice"
15
+ end
16
+
17
+ def self.executable= (openoffice)
18
+ @openoffice = openoffice
19
+ end
20
+
21
+ def self.port
22
+ @port ||= 8000
23
+ end
24
+
25
+ def self.port= (port)
26
+ @port = port
27
+ end
28
+
29
+ def self.host
30
+ @host ||= "127.0.0.1"
31
+ end
32
+
33
+ def self.host= (host)
34
+ @host = host
35
+ end
36
+
37
+ def self.params
38
+ @params ||= '-headless -display :30 -accept="socket,host=[host],port=[port];urp;" -nofirststartwizard'
39
+ end
40
+
41
+ def self.params= (params)
42
+ @params = params
43
+ end
44
+
45
+ def self.xvfb
46
+ @xvfb ||= 'Xvfb :30 -screen 0 1024x768x24'
47
+ end
48
+
49
+ def self.xvfb= (xvfb)
50
+ @xvfb = xvfb
51
+ end
52
+
53
+ def self.stop_xvfb
54
+ @stop_xvfb ||= 'killall -9 Xvfb'
55
+ end
56
+
57
+ def self.stop_xvfb= (stop_xvfb)
58
+ @stop_xvfb = stop_xvfb
59
+ end
60
+
61
+ def self.stop
62
+ @stop ||= 'killall -9 soffice.bin'
63
+ end
64
+
65
+ def self.stop= (stop)
66
+ @stop = stop
67
+ end
68
+
69
+ def self.python
70
+ @python ||= 'python'
71
+ end
72
+
73
+ def self.python= (python)
74
+ @python = python
75
+ end
76
+
77
+ end
78
+
79
+ end
@@ -0,0 +1,27 @@
1
+ module CommandWrap::Config
2
+
3
+ module Xvfb
4
+
5
+ def self.executable
6
+ @executable ||= "xvfb-run"
7
+ end
8
+
9
+ def self.executable= (executeable)
10
+ @executable = executeable
11
+ end
12
+
13
+ def self.params
14
+ @params ||= '--server-args="-screen 0, 1024x768x24"'
15
+ end
16
+
17
+ def self.params= (params)
18
+ @params = params
19
+ end
20
+
21
+ def self.command (subcommand)
22
+ "#{executable} #{params} #{subcommand}"
23
+ end
24
+
25
+ end
26
+
27
+ end
@@ -0,0 +1,38 @@
1
+ require 'RMagick'
2
+
3
+ module CommandWrap
4
+
5
+ module Image
6
+
7
+ def self.dimensions (path)
8
+ img = Magick::Image.read(path)[0]
9
+ { :width => img.columns, :height => img.rows }
10
+ end
11
+
12
+ def self.scale (source, target, width, height = nil)
13
+ height = width unless height
14
+
15
+ # Scale source
16
+ simg = Magick::Image.read(source)[0]
17
+
18
+ if simg.columns > width && simg.rows > height
19
+ simg.resize_to_fit!(width, height)
20
+ end
21
+
22
+ # Create transparent image
23
+ timg = Magick::Image.new(width, height)
24
+ d = Magick::Draw.new
25
+ d.fill('white')
26
+ d.draw(timg)
27
+ timg = timg.transparent('white')
28
+
29
+ # Insert thumb
30
+ timg.composite!(simg, Magick::CenterGravity, Magick::OverCompositeOp)
31
+
32
+ # Save result
33
+ timg.write(target)
34
+ end
35
+
36
+ end
37
+
38
+ end
@@ -0,0 +1,40 @@
1
+ module CommandWrap
2
+
3
+ module OpenOffice
4
+
5
+ autoload :Server, File.dirname(__FILE__) + "/open_office/server"
6
+
7
+ # Converts the given source to target
8
+ #
9
+ # Possible parameters:
10
+ # source (path to source file) + target (path to target file)
11
+ # content (content of source file) + source extension (extension of source file) + target extension (extension of target file)
12
+ def self.convert (*args)
13
+ if args.length == 2
14
+ source = args[0]
15
+ target = args[1]
16
+ elsif args.length == 3
17
+ source = CommandWrap.temp(args[1])
18
+ target = CommandWrap.temp(args[2])
19
+ # Save Content
20
+ File.open(source, 'w') do |file|
21
+ file.write args[0]
22
+ end
23
+ else
24
+ raise ArgumentError.new('wrong number of arguments')
25
+ end
26
+ command = File.dirname(__FILE__) + "/../../bin/DocumentConverter.py"
27
+ result = `#{CommandWrap::Config::OpenOffice.python} #{command} #{source} #{target} #{CommandWrap::Config::OpenOffice.port}`
28
+ raise result unless result.strip == ''
29
+
30
+ if args.length == 3
31
+ result = IO.read(target)
32
+ File.delete(source) if File.writable?(source)
33
+ File.delete(target) if File.writable?(target)
34
+ result
35
+ end
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,29 @@
1
+ module CommandWrap::OpenOffice
2
+
3
+ module Server
4
+
5
+ def self.start
6
+ pid1 = fork do
7
+ exec FileUtils::Config::OpenOffice.xvfb
8
+ end
9
+ sleep 5 # 5 seconden wachten tot xvfb draait
10
+ pid2 = fork do
11
+ exec FileUtils::Config::OpenOffice.command
12
+ end
13
+ end
14
+
15
+ def self.stop
16
+ `#{FileUtils::Config::OpenOffice.stop_xvfb}`
17
+ sleep 5
18
+ `#{FileUtils::Config::OpenOffice.stop}`
19
+ end
20
+
21
+ def self.restart
22
+ stop
23
+ sleep 5
24
+ start
25
+ end
26
+
27
+ end
28
+
29
+ end
@@ -0,0 +1,47 @@
1
+ require 'RMagick'
2
+
3
+ module CommandWrap
4
+
5
+ module Pdf
6
+
7
+ def self.metas (path)
8
+ metas = {}
9
+
10
+ key = ''
11
+ `#{CommandWrap::Config.pdftk} #{path} dump_data`.gsub("\r\n", "\n").gsub("\r", "\n").split("\n").each do |line|
12
+ parts = line.split(':')
13
+ parts[1] = parts[1].gsub('&#0;', '')
14
+ if parts[0] == 'InfoValue'
15
+ if key != ''
16
+ metas[key] = parts[1].strip
17
+ key = ''
18
+ end
19
+ elsif parts[0] == 'InfoKey'
20
+ key = parts[1].strip
21
+ else
22
+ metas[parts[0].strip] = parts[1].strip
23
+ end
24
+ end
25
+
26
+ metas
27
+ end
28
+
29
+ def self.pages (path)
30
+ metas(path)['NumberOfPages'].to_i
31
+ end
32
+
33
+ # Generates an image of a pdf page
34
+ # Page starts with 0
35
+ def self.preview (source, target, page = 0)
36
+ pdf = Magick::ImageList.new(source)[page]
37
+ pdf.write target
38
+ end
39
+
40
+ # Merges the given pdfs into a single pdf
41
+ def self.merge (target, *sources)
42
+ `#{CommandWrap::Config.pdftk} #{sources.join(' ')} cat output #{target}`
43
+ end
44
+
45
+ end
46
+
47
+ end
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1 @@
1
+ TEST.HTM
@@ -0,0 +1 @@
1
+ TESTING
Binary file
@@ -0,0 +1 @@
1
+ test
@@ -0,0 +1,3 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require File.dirname(__FILE__) + "/../lib/command_wrap"
@@ -0,0 +1,94 @@
1
+ require File.dirname(__FILE__) + "/../test_helper"
2
+
3
+ class CommandWrapTest < Test::Unit::TestCase
4
+
5
+ def test_capture
6
+ assert_nothing_raised do
7
+ path = File.dirname(__FILE__) + "/../../google.png"
8
+ File.delete(path) if File.exists?(path)
9
+ CommandWrap.capture('http://www.google.be', path)
10
+ assert File.exists?(path)
11
+ end
12
+ end
13
+
14
+ def test_zip
15
+ target = File.dirname(__FILE__) + "/../../result.zip"
16
+ source1 = File.dirname(__FILE__) + "/../helpers/2pages.pdf"
17
+ source2 = File.dirname(__FILE__) + "/../helpers/test.odt"
18
+
19
+ File.delete(target) if File.exists?(target)
20
+
21
+ assert_nothing_raised do
22
+ CommandWrap.zip(target, source1, 'doc.pdf', source2, 'doc.odt')
23
+ assert File.exists?(target)
24
+ end
25
+ end
26
+
27
+ def test_extension
28
+ assert_equal 'exe', CommandWrap.extension('test.exe')
29
+ end
30
+
31
+ def test_extension_none
32
+ assert_equal '', CommandWrap.extension('test')
33
+ end
34
+
35
+ def test_preview
36
+ target = File.dirname(__FILE__) + "/../../result.png"
37
+
38
+ assert_nothing_raised do
39
+ source = File.dirname(__FILE__) + "/../../README"
40
+
41
+ assert_nil CommandWrap.preview(source, target, 100, 100)
42
+
43
+ # Image
44
+ source = File.dirname(__FILE__) + "/../helpers/scale.jpg"
45
+
46
+ File.delete(target) if File.exists?(target)
47
+
48
+ assert CommandWrap.preview(source, target, 100, 100), 'Creation of image preview'
49
+ assert File.exists?(target), 'File exists? preview of image'
50
+
51
+ # Document
52
+ source = File.dirname(__FILE__) + "/../helpers/test.odt"
53
+
54
+ File.delete(target) if File.exists?(target)
55
+
56
+ assert CommandWrap.preview(source, target, 100, 100), 'Creation of odt preview'
57
+ assert File.exists?(target), 'File exists? preview of odt'
58
+ end
59
+ end
60
+
61
+ def test_temppath
62
+ assert_nothing_raised do
63
+ path1 = CommandWrap.temp('jpg')
64
+ assert_equal "#{CommandWrap::Config.tmp_dir}/tmp.jpg", path1
65
+
66
+ FileUtils.touch(path1)
67
+
68
+ path2 = CommandWrap.temp('jpg')
69
+ assert_equal "#{CommandWrap::Config.tmp_dir}/tmp.1.jpg", path2
70
+
71
+ File.delete(path1)
72
+ end
73
+ end
74
+
75
+ def test_index
76
+ assert_nothing_raised do
77
+ assert_equal '', CommandWrap.index(File.dirname(__FILE__) + "/../helpers/scale.jpg")
78
+ assert_equal 'test', CommandWrap.index(File.dirname(__FILE__) + "/../helpers/test.txt")
79
+ assert CommandWrap.index(File.dirname(__FILE__) + "/../helpers/test.odt").include?('TEST'), 'TEST in odt'
80
+ assert_equal 'TESTING', CommandWrap.index(File.dirname(__FILE__) + "/../helpers/test.html")
81
+ assert_equal 'TEST.HTM', CommandWrap.index(File.dirname(__FILE__) + "/../helpers/test.htm")
82
+ end
83
+ end
84
+
85
+ def test_preview_mpg
86
+ source = File.dirname(__FILE__) + "/../helpers/test.mpg"
87
+ target = CommandWrap.temp('png')
88
+ File.delete(target) if File.exists?(target)
89
+ assert_nothing_raised do
90
+ assert_equal false, CommandWrap.preview(source, target, 160, 200)
91
+ end
92
+ end
93
+
94
+ end
@@ -0,0 +1,108 @@
1
+ require File.dirname(__FILE__) + "/../test_helper"
2
+
3
+ class ConfigOpenOfficeTest < Test::Unit::TestCase
4
+
5
+ def test_exists
6
+ assert_nothing_raised do
7
+ CommandWrap::Config::OpenOffice
8
+ end
9
+ end
10
+
11
+ def setup
12
+ @params = {
13
+ :executable => CommandWrap::Config::OpenOffice.executable,
14
+ :host => CommandWrap::Config::OpenOffice.host,
15
+ :port => CommandWrap::Config::OpenOffice.port,
16
+ :params => CommandWrap::Config::OpenOffice.params,
17
+ :xvfb => CommandWrap::Config::OpenOffice.xvfb,
18
+ :stop_xvfb => CommandWrap::Config::OpenOffice.stop_xvfb,
19
+ :stop => CommandWrap::Config::OpenOffice.stop,
20
+ :python => CommandWrap::Config::OpenOffice.python
21
+ }
22
+ end
23
+
24
+ # Reset of config
25
+ def teardown
26
+ CommandWrap::Config::OpenOffice.executable = @params[:executable]
27
+ CommandWrap::Config::OpenOffice.host = @params[:host]
28
+ CommandWrap::Config::OpenOffice.port = @params[:port]
29
+ CommandWrap::Config::OpenOffice.params = @params[:params]
30
+ CommandWrap::Config::OpenOffice.xvfb = @params[:xvfb]
31
+ CommandWrap::Config::OpenOffice.stop_xvfb = @params[:stop_xvfb]
32
+ CommandWrap::Config::OpenOffice.stop = @params[:stop]
33
+ CommandWrap::Config::OpenOffice.python = @params[:python]
34
+ end
35
+
36
+ def test_config_openoffice_executable
37
+ assert_kind_of String, CommandWrap::Config::OpenOffice.executable
38
+ assert_equal 'soffice', CommandWrap::Config::OpenOffice.executable
39
+ CommandWrap::Config::OpenOffice.executable = 'soffice.exe'
40
+ assert_equal 'soffice.exe', CommandWrap::Config::OpenOffice.executable
41
+ CommandWrap::Config::OpenOffice.executable = 'soffice'
42
+ end
43
+
44
+ def test_config_openoffice_port
45
+ assert_kind_of Integer, CommandWrap::Config::OpenOffice.port
46
+ assert_equal 8000, CommandWrap::Config::OpenOffice.port
47
+ CommandWrap::Config::OpenOffice.port = 8020
48
+ assert_equal 8020, CommandWrap::Config::OpenOffice.port
49
+ CommandWrap::Config::OpenOffice.port = 8000
50
+ end
51
+
52
+ def test_config_openoffice_host
53
+ assert_kind_of String, CommandWrap::Config::OpenOffice.host
54
+ assert_equal '127.0.0.1', CommandWrap::Config::OpenOffice.host
55
+ CommandWrap::Config::OpenOffice.host = 'localhost'
56
+ assert_equal 'localhost', CommandWrap::Config::OpenOffice.host
57
+ CommandWrap::Config::OpenOffice.host = '127.0.0.1'
58
+ end
59
+
60
+ def test_config_openoffice_params
61
+ assert_kind_of String, CommandWrap::Config::OpenOffice.params
62
+ assert_equal '-headless -display :30 -accept="socket,host=[host],port=[port];urp;" -nofirststartwizard', CommandWrap::Config::OpenOffice.params
63
+ CommandWrap::Config::OpenOffice.params = '-headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard'
64
+ assert_equal '-headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard', CommandWrap::Config::OpenOffice.params
65
+ CommandWrap::Config::OpenOffice.params = '-headless -accept="socket,host=[host],port=[port];urp;" -nofirststartwizard'
66
+ end
67
+
68
+ def test_config_openoffice_command
69
+ assert_kind_of String, CommandWrap::Config::OpenOffice.command
70
+ assert_equal 'soffice -headless -display :30 -accept="socket,host=127.0.0.1,port=8000;urp;" -nofirststartwizard', CommandWrap::Config::OpenOffice.command
71
+
72
+ CommandWrap::Config::OpenOffice.executable = 'soffice.exe'
73
+ CommandWrap::Config::OpenOffice.host = 'localhost'
74
+ CommandWrap::Config::OpenOffice.port = 8080
75
+ CommandWrap::Config::OpenOffice.params = '-headless -display :30 -accept="socket,host=[host],port=[port];urp;"'
76
+
77
+ assert_equal 'soffice.exe -headless -display :30 -accept="socket,host=localhost,port=8080;urp;"', CommandWrap::Config::OpenOffice.command
78
+ end
79
+
80
+ def test_config_openoffice_xvfb
81
+ assert_kind_of String, CommandWrap::Config::OpenOffice.xvfb
82
+ assert_equal 'Xvfb :30 -screen 0 1024x768x24', CommandWrap::Config::OpenOffice.xvfb
83
+ CommandWrap::Config::OpenOffice.xvfb = 'Xvfb'
84
+ assert_equal 'Xvfb', CommandWrap::Config::OpenOffice.xvfb
85
+ end
86
+
87
+ def test_config_openoffice_stop_xvfb
88
+ assert_kind_of String, CommandWrap::Config::OpenOffice.stop_xvfb
89
+ assert_equal 'killall -9 Xvfb', CommandWrap::Config::OpenOffice.stop_xvfb
90
+ CommandWrap::Config::OpenOffice.stop_xvfb = 'echo "dummy"'
91
+ assert_equal 'echo "dummy"', CommandWrap::Config::OpenOffice.stop_xvfb
92
+ end
93
+
94
+ def test_config_openoffice_stop
95
+ assert_kind_of String, CommandWrap::Config::OpenOffice.stop
96
+ assert_equal 'killall -9 soffice.bin', CommandWrap::Config::OpenOffice.stop
97
+ CommandWrap::Config::OpenOffice.stop = 'echo "dummy"'
98
+ assert_equal 'echo "dummy"', CommandWrap::Config::OpenOffice.stop
99
+ end
100
+
101
+ def test_config_openoffice_python
102
+ assert_kind_of String, CommandWrap::Config::OpenOffice.python
103
+ assert_equal 'python', CommandWrap::Config::OpenOffice.python
104
+ CommandWrap::Config::OpenOffice.python = '/usr/bin/python'
105
+ assert_equal '/usr/bin/python', CommandWrap::Config::OpenOffice.python
106
+ end
107
+
108
+ end
@@ -0,0 +1,36 @@
1
+ require File.dirname(__FILE__) + "/../test_helper"
2
+
3
+ class ConfigTest < Test::Unit::TestCase
4
+
5
+ def test_exists
6
+ assert_nothing_raised do
7
+ CommandWrap::Config
8
+ end
9
+ end
10
+
11
+ def test_tmp_dir
12
+ tmp_dir = '/tmp'
13
+ assert_equal tmp_dir, CommandWrap::Config.tmp_dir
14
+ CommandWrap::Config.tmp_dir = '/home/test/tmp'
15
+ assert_equal '/home/test/tmp', CommandWrap::Config.tmp_dir
16
+ CommandWrap::Config.tmp_dir = tmp_dir
17
+ end
18
+
19
+ def test_pdftk
20
+ pdftk = 'pdftk'
21
+ assert_equal pdftk, CommandWrap::Config.pdftk
22
+ assert_equal 'pdftk', CommandWrap::Config.pdftk
23
+ CommandWrap::Config.pdftk = '/usr/bin/pdftk'
24
+ assert_equal '/usr/bin/pdftk', CommandWrap::Config.pdftk
25
+ CommandWrap::Config.pdftk = pdftk
26
+ end
27
+
28
+ def test_zip
29
+ zip = 'zip'
30
+ assert_equal 'zip', CommandWrap::Config.zip
31
+ CommandWrap::Config.zip = '/usr/bin/zip'
32
+ assert_equal '/usr/bin/zip', CommandWrap::Config.zip
33
+ CommandWrap::Config.zip = zip
34
+ end
35
+
36
+ end
@@ -0,0 +1,34 @@
1
+ require File.dirname(__FILE__) + "/../test_helper"
2
+
3
+ class ConfigXvfbTest < Test::Unit::TestCase
4
+
5
+ def test_exists
6
+ assert_nothing_raised do
7
+ CommandWrap::Config::Xvfb
8
+ end
9
+ end
10
+
11
+ def teardown
12
+ CommandWrap::Config::Xvfb.executable = 'xvfb-run'
13
+ CommandWrap::Config::Xvfb.params = '--server-args="-screen 0, 1024x768x24"'
14
+ end
15
+
16
+ def test_config_xvbf_executable
17
+ assert_kind_of String, CommandWrap::Config::Xvfb.executable
18
+ assert_equal 'xvfb-run', CommandWrap::Config::Xvfb.executable
19
+ CommandWrap::Config::Xvfb.executable = '/usr/bin/xvfb-run'
20
+ assert_equal '/usr/bin/xvfb-run', CommandWrap::Config::Xvfb.executable
21
+ end
22
+
23
+ def test_params
24
+ assert_kind_of String, CommandWrap::Config::Xvfb.params
25
+ assert_equal '--server-args="-screen 0, 1024x768x24"', CommandWrap::Config::Xvfb.params
26
+ CommandWrap::Config::Xvfb.params = 'dummy'
27
+ assert_equal 'dummy', CommandWrap::Config::Xvfb.params
28
+ end
29
+
30
+ def test_command
31
+ assert_equal 'xvfb-run --server-args="-screen 0, 1024x768x24" echo "test"', CommandWrap::Config::Xvfb.command('echo "test"')
32
+ end
33
+
34
+ end
@@ -0,0 +1,53 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class ImageTest < Test::Unit::TestCase
4
+
5
+ def test_exists
6
+ assert_nothing_raised do
7
+ CommandWrap::Image
8
+ end
9
+ end
10
+
11
+ def test_dimensions
12
+ dim = { :width => 150, :height => 200 }
13
+
14
+ assert_equal dim, CommandWrap::Image.dimensions(File.dirname(__FILE__) + "/../helpers/img150x200.jpg")
15
+ end
16
+
17
+ def test_scale
18
+ dim = { :width => 100, :height => 100 }
19
+ source = File.dirname(__FILE__) + "/../helpers/scale.jpg"
20
+ target = File.dirname(__FILE__) + "/../../target.png"
21
+
22
+ File.delete(target) if File.exists?(target)
23
+
24
+ CommandWrap::Image.scale(source, target, 100)
25
+ assert File.exists?(target)
26
+ assert_equal dim, CommandWrap::Image.dimensions(target)
27
+ end
28
+
29
+ def test_scale_width_and_height
30
+ dim = { :width => 100, :height => 150 }
31
+ source = File.dirname(__FILE__) + "/../helpers/scale.jpg"
32
+ target = File.dirname(__FILE__) + "/../../target2.png"
33
+
34
+ File.delete(target) if File.exists?(target)
35
+
36
+ CommandWrap::Image.scale(source, target, 100, 150)
37
+ assert File.exists?(target)
38
+ assert_equal dim, CommandWrap::Image.dimensions(target)
39
+ end
40
+
41
+ def test_scale_width_and_height_big
42
+ dim = { :width => 1000, :height => 1500 }
43
+ source = File.dirname(__FILE__) + "/../helpers/scale.jpg"
44
+ target = File.dirname(__FILE__) + "/../../target3.png"
45
+
46
+ File.delete(target) if File.exists?(target)
47
+
48
+ CommandWrap::Image.scale(source, target, 1000, 1500)
49
+ assert File.exists?(target)
50
+ assert_equal dim, CommandWrap::Image.dimensions(target)
51
+ end
52
+
53
+ end
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + "/../test_helper"
2
+
3
+ class OpenOfficeServerTest < Test::Unit::TestCase
4
+
5
+ def test_exists
6
+ assert_nothing_raised do
7
+ CommandWrap::OpenOffice::Server
8
+ end
9
+ end
10
+
11
+ end
@@ -0,0 +1,27 @@
1
+ require File.dirname(__FILE__) + "/../test_helper"
2
+
3
+ class OpenOfficeTest < Test::Unit::TestCase
4
+
5
+ def test_exists
6
+ assert_nothing_raised do
7
+ CommandWrap::OpenOffice
8
+ end
9
+ end
10
+
11
+ def test_convert
12
+ assert_nothing_raised do
13
+ path = CommandWrap.temp('pdf')
14
+ CommandWrap::OpenOffice.convert File.dirname(__FILE__) + "/../helpers/test.odt", path
15
+ assert File.exists?(path), 'Pdf bestaat niet'
16
+ File.delete(path) if File.writable?(path)
17
+ end
18
+ end
19
+
20
+ def test_convert_stream
21
+ assert_nothing_raised do
22
+ content = IO.read(File.dirname(__FILE__) + "/../helpers/test.odt")
23
+ assert_kind_of String, CommandWrap::OpenOffice.convert(content, 'odt', 'pdf')
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,74 @@
1
+ require File.dirname(__FILE__) + "/../test_helper"
2
+
3
+ class PdfTest < Test::Unit::TestCase
4
+
5
+ TEST_PDF = File.dirname(__FILE__) + "/../helpers/2pages.pdf"
6
+
7
+ def test_exists
8
+ assert_nothing_raised do
9
+ CommandWrap::Pdf
10
+ end
11
+ end
12
+
13
+ def test_metas
14
+ assert_nothing_raised do
15
+ metas = CommandWrap::Pdf.metas(TEST_PDF)
16
+ assert_kind_of Hash, metas
17
+ assert_equal 'OpenOffice.org 3.2', metas['Producer']
18
+ end
19
+ end
20
+
21
+ def test_pages
22
+ assert_equal 2, CommandWrap::Pdf.pages(TEST_PDF)
23
+ end
24
+
25
+ def test_screenshot
26
+ target1 = File.dirname(__FILE__) + "/../../pdf1.jpg"
27
+ target2 = File.dirname(__FILE__) + "/../../pdf2.jpg"
28
+ target3 = File.dirname(__FILE__) + "/../../pdf3.jpg"
29
+
30
+ [target1, target2, target2].each do |target|
31
+ File.delete(target) if File.exists?(target)
32
+ end
33
+
34
+ assert_nothing_raised do
35
+ CommandWrap::Pdf.preview(TEST_PDF, target1)
36
+ assert File.exists?(target1)
37
+ CommandWrap::Pdf.preview(TEST_PDF, target2, 0)
38
+ assert File.exists?(target2)
39
+ CommandWrap::Pdf.preview(TEST_PDF, target3, 1)
40
+ assert File.exists?(target3)
41
+ assert_equal IO.read(target1), IO.read(target2)
42
+ end
43
+ end
44
+
45
+ def test_merge
46
+ source1 = File.dirname(__FILE__) + "/../helpers/2pages.pdf"
47
+ source2 = File.dirname(__FILE__) + "/../helpers/pdf2.pdf"
48
+ target = File.dirname(__FILE__) + "/../../result.pdf"
49
+
50
+ File.delete(target) if File.exists?(target)
51
+
52
+ assert_nothing_raised do
53
+ CommandWrap::Pdf.merge(target, source1, source2)
54
+ assert File.exists?(target)
55
+ assert_equal 3, CommandWrap::Pdf.pages(target)
56
+ end
57
+ end
58
+
59
+ def test_merge_3
60
+ source1 = File.dirname(__FILE__) + "/../helpers/2pages.pdf"
61
+ source2 = File.dirname(__FILE__) + "/../helpers/pdf2.pdf"
62
+ source3 = File.dirname(__FILE__) + "/../helpers/pdf3.pdf"
63
+ target = File.dirname(__FILE__) + "/../../result.pdf"
64
+
65
+ File.delete(target) if File.exists?(target)
66
+
67
+ assert_nothing_raised do
68
+ CommandWrap::Pdf.merge(target, source1, source2, source3)
69
+ assert File.exists?(target)
70
+ assert_equal 4, CommandWrap::Pdf.pages(target)
71
+ end
72
+ end
73
+
74
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: CommandWrap
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ version: "0.1"
10
+ platform: ruby
11
+ authors:
12
+ - Stefaan Colman
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-02-22 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rmagick
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 57
29
+ segments:
30
+ - 2
31
+ - 13
32
+ - 1
33
+ version: 2.13.1
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ description: A set of utility classes to extract meta data from different file types
37
+ email:
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - lib/command_wrap/config.rb
46
+ - lib/command_wrap/open_office.rb
47
+ - lib/command_wrap/config/open_office.rb
48
+ - lib/command_wrap/config/xvfb.rb
49
+ - lib/command_wrap/open_office/server.rb
50
+ - lib/command_wrap/image.rb
51
+ - lib/command_wrap/pdf.rb
52
+ - lib/command_wrap.rb
53
+ - tests/unit/command_wrap_test.rb
54
+ - tests/unit/pdf_test.rb
55
+ - tests/unit/config_test.rb
56
+ - tests/unit/open_office_test.rb
57
+ - tests/unit/config_xvfb_test.rb
58
+ - tests/unit/image_test.rb
59
+ - tests/unit/config_open_office_test.rb
60
+ - tests/unit/open_office_server_test.rb
61
+ - tests/helpers/test.odt
62
+ - tests/helpers/2pages.pdf
63
+ - tests/helpers/scale.jpg
64
+ - tests/helpers/pdf3.pdf
65
+ - tests/helpers/test.htm
66
+ - tests/helpers/test.html
67
+ - tests/helpers/test.txt
68
+ - tests/helpers/img150x200.jpg
69
+ - tests/helpers/error.mpg
70
+ - tests/helpers/pdf2.pdf
71
+ - tests/test_helper.rb
72
+ - README
73
+ - bin/CutyCapt
74
+ - bin/openoffice_server.rb
75
+ - bin/DocumentConverter.py
76
+ has_rdoc: true
77
+ homepage:
78
+ licenses: []
79
+
80
+ post_install_message:
81
+ rdoc_options: []
82
+
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ hash: 3
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ requirements: []
104
+
105
+ rubyforge_project:
106
+ rubygems_version: 1.5.2
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: Extracting meta data from file
110
+ test_files:
111
+ - tests/unit/command_wrap_test.rb
112
+ - tests/unit/pdf_test.rb
113
+ - tests/unit/config_test.rb
114
+ - tests/unit/open_office_test.rb
115
+ - tests/unit/config_xvfb_test.rb
116
+ - tests/unit/image_test.rb
117
+ - tests/unit/config_open_office_test.rb
118
+ - tests/unit/open_office_server_test.rb
119
+ - tests/test_helper.rb