Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/application.js

Issue 10233013: Crawler, second version (Closed)
Patch Set: Created April 12, 2013, 1:38 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/action_platform.js ('k') | lib/bootstrap_xpcom.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This Source Code is subject to the terms of the Mozilla Public License
3 * version 2.0 (the "License"). You can obtain a copy of the License at
4 * http://mozilla.org/MPL/2.0/.
5 */
6
7 Cu.import( "resource://gre/modules/FileUtils.jsm" );
8
9 let {Storage} = require( "storage" );
10 let {Instruction, Instruction_Set, Input_String, Input_File} = require( "instruc tion" );
11 let {Long_Task} = require( "task" );
12 let {Crawler} = require( "crawler" );
13 let {Logger} = require( "logger" );
14
15 /**
16 * @fileOverview The purpose of this module is to isolate the parameters needed to run a crawl from the means by which
17 * those parameters are collected by top-level interface code. There are two bas ic forms of interface: interactive and
18 * automated. The interactive interface uses a XUL dialog and starts a crawl whe n the user presses a button. The
19 * automated interface handles command line options and starts a crawl when the command line arrives.
20 *
21 * There are two kinds of arguments to the Application_Session constructor. The first kind are internal resources needed
22 * to perform the crawl. Notable here is the 'window' argument, which designates where to open the tabs for each crawled
23 * site. This comes easy in the interactive version, but a window must be create d for the automated one. The second kind
24 * of argument are the external parameters that the user manipulates, such as th e specification for the instructions.
25 *
26 * Internal.
27 * - window. The window in which to run the tabs.
28 * - progress. A progress monitor. Only useful for interactive modes.
29 * - output (transient). If required, output to a log window in YAML format.
30 * External.
31 * - instructions. Contains a list of all the sites to crawl. There are multipl e ways of obtaining this set, all of
32 * which start with a text stream which is then parsed and converted to an {I nstruction_Set} object. Mandatory.
33 * - Local input file. Parameter is a local file name. By convention local files are in YAML format.
34 * - Text string. Parameter is the text itself plus the name of one of the formats.
35 * - YAML format. Used for an interactive, multiline textbox. (Not impl emented)
36 * - JSON format. Used for accepting instructions from the command line . (Not implemented)
37 * - output (persistent). The recorded results of the crawl.
38 * - local file name. Optional for interactive use, since a log window migh t be adequate. Mandatory for automated
39 * use, otherwise what's the point?
40 * - format. Either YAML or JSON. Default to JSON.
41 * - leave_open. Leave the tabs open after crawling them. Only useful for inter action, say, in order to examine the DOM
42 * after a trial. Always false for automated use. Default is false for intera ctive use.
43 * - number_of_tabs. The number of simultaneous tabs. Built-in default if not p resent.
44 */
45
46 /**
47 * @param {Window} window
48 * @param {boolean} leave_open
49 * @param {number} n_tabs
50 * @param {number} time_limit
51 * @param {Function} progress
52 * @constructor
53 */
54 var Application_Session = function( window, leave_open, n_tabs, time_limit, prog ress )
55 {
56 this.runnable = true;
57 this.instructions = [];
58 this.outputs = [];
59 this.window = window;
60 this.leave_open = leave_open;
61 this.n_tabs = n_tabs;
62 this.time_limit = time_limit;
63 this.progress = progress;
64 };
65 exports.Application_Session = Application_Session;
66
67 Application_Session.prototype.run = function( finisher, catcher )
68 {
69 this.finisher = finisher;
70 this.catcher = catcher;
71 if ( !this.runnable )
72 {
73 this._run_catch( new Error( "Application_Session is not runnable" ) );
74 return;
75 }
76 this.runnable = false;
77
78 this.current_crawler = new Crawler(
79 this.instructions, this.outputs, this.window,
80 this.time_limit, this.leave_open, this.n_tabs
81 );
82
83
84 if ( this.progress )
85 {
86 /*
87 * Add an instance-specific notice member to the crawler's progress instance . This is cleaner than
88 * bothering with a subclass of the progress-notification class.
89 */
90 this.current_crawler.progress.notice = function( notice )
91 {
92 notice( this );
93 }.bind( this.current_crawler.progress, this.progress );
94 }
95 this.current_crawl = new Long_Task( this.current_crawler, this.instructions.si ze * 3 );
96 this.current_crawl.run( this._run_finally.bind( this ), this._run_catch.bind( this ) );
97 };
98
99 /**
100 *
101 * @param {*} ex
102 * Value to treat as a thrown exception. Treated as an opaque type.
103 * @private
104 */
105 Application_Session.prototype._run_catch = function( ex )
106 {
107 if ( this.catcher ) this.catcher( ex );
108 this._run_finally();
109 };
110
111 Application_Session.prototype._run_finally = function()
112 {
113 if ( this.finisher ) this.finisher();
114 };
115
116 /**
117 * Close the application session.
118 */
119 Application_Session.prototype.close = function()
120 {
121 if ( this.current_crawl )
122 {
123 this.current_crawl.close();
124 this.current_crawl = null;
125 }
126 if ( this.current_crawler )
127 {
128 this.current_crawler.close();
129 this.current_crawler = null;
130 }
131 };
132
133 /**
134 * Set an input string to specify the instruction set.
135 *
136 * @param {string} s
137 */
138 Application_Session.prototype.set_input_string = function( s )
139 {
140 this.instructions = new Instruction_Set.Parsed( new Input_String( s ) );
141 };
142
143 /**
144 * Set an input file from which to read the instruction set.
145 *
146 * @param {string} file_path
147 */
148 Application_Session.prototype.set_input_file = function( file_path )
149 {
150 var f = new FileUtils.File( file_path );
151 if ( !f.exists() )
152 {
153 throw new Error( "Input file does not exist. path = " + f.path );
154 }
155 if ( !f.isFile() )
156 {
157 throw new Error( "Input file path does not name a file. path = " + f.path );
158 }
159 if ( !f.isReadable() )
160 {
161 throw new Error( "Input file is not readable. path = " + f.path );
162 }
163 this.instructions = new Instruction_Set.Parsed( new Input_File( f ) );
164 };
165
166 /**
167 * Add a storage-encoding pair to the output list for the current session.
168 *
169 * @param storage
170 * @param {String} encode
171 * Either "JSON" or "YAML".
172 */
173 Application_Session.prototype.add_output = function( storage, encode )
174 {
175 this.outputs.push( { storage: storage, encode: encode } );
176 };
177
178 /**
179 * Add a file to the output list. Throws if it detects obvious problems with the arguments specifying a valid output,
180 * namely, if the directory path is not a writable directory or if the encoding is invalid.
181 *
182 * @param {string} directory_path
183 * Directory path for the output file.
184 * @param {string} base_name
185 * Base name for the output file.
186 * @param {boolean} append_timestamp
187 * Whether to append a timestamp to the base name, or not.
188 * @param {string} encode
189 * Either "JSON" or "YAML".
190 * @return {string}
191 * The constructed file name.
192 */
193 Application_Session.prototype.add_output_file = function( directory_path, base_n ame, append_timestamp, encode )
194 {
195 var file = Cc["@mozilla.org/file/local;1"].createInstance( Ci.nsILocalFile );
196 file.initWithPath( directory_path );
197 if ( !file.exists() )
198 {
199 throw new Error( "Output directory path does not exist. path = " + directory _path );
200 }
201 if ( !file.isDirectory() )
202 {
203 throw new Error( "Output directory path does not name a directory. path = " + directory_path );
204 }
205 if ( !file.isWritable() )
206 {
207 throw new Error( "Output directory is not writable. path = " + directory_pat h );
208 }
209 var file_name = ( base_name && base_name.length > 0 ) ? base_name : "crawl-res ults";
210 if ( append_timestamp )
211 {
212 file_name += filename_timestamp();
213 }
214 switch ( encode )
215 {
216 case "JSON":
217 file_name += ".json";
218 break;
219 case "YAML":
220 file_name += ".yaml";
221 break;
222 default:
223 throw new Error( "Invalid encoding = " + encode + ". Must be JSON or YAML. " );
224 }
225 file.append( file_name );
226 this.add_output( new Storage.Local_File( file ), encode );
227 return file.path;
228 };
229
230 function filename_timestamp()
231 {
232 var s = Logger.timestamp();
233 return "_" + s.substr( 0, 10 ) + "_" + s.substr( 11, 2 ) + "-" + s.substr( 14, 2 ) + "-" + s.substr( 17, 2 );
234 }
OLDNEW
« no previous file with comments | « lib/action_platform.js ('k') | lib/bootstrap_xpcom.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld