Lean  $LEAN_TAG$
RawFileProcessor.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
17 using System.Collections.Generic;
18 using System.IO;
19 using System.Linq;
20 using QuantConnect.Logging;
21 
22 namespace QuantConnect.ToolBox
23 {
24  /// <summary>
25  /// Processing harness used to read files in, parse them, and process them.
26  /// </summary>
27  public class RawFileProcessor : IDisposable
28  {
29  private DateTime? _start;
30  private readonly IStreamProvider _streamProvider;
31  private readonly IStreamParser _parser;
32  private readonly IDataProcessor[] _processors;
33 
34  /// <summary>
35  /// Gets or sets a name used for logging
36  /// </summary>
37  public string Name { get; set; }
38 
39  /// <summary>
40  /// Initializes a new instance of the <see cref="RawFileProcessor"/> class
41  /// </summary>
42  public RawFileProcessor(IStreamProvider streamProvider, IStreamParser parser, params IDataProcessor[] processors)
43  {
44  _streamProvider = streamProvider;
45  _parser = parser;
46  _processors = processors;
47  }
48 
49  /// <summary>
50  /// Runs the raw file processor on the specified files
51  /// </summary>
52  /// <param name="name">A name for the processor used for logging</param>
53  /// <param name="sources">The raw files to be processed</param>
54  /// <param name="streamProvider">Instance capable of reading the sources into a stream</param>
55  /// <param name="streamParser">Instance capable of parsing the provided stream</param>
56  /// <param name="processors">The data processors to process the parsed data</param>
57  /// <returns>True if the operation completed without error, otherwise false</returns>
58  public static bool Run(string name, IEnumerable<string> sources, IStreamProvider streamProvider, IStreamParser streamParser, params IDataProcessor[] processors)
59  {
60  using (var processor = new RawFileProcessor(streamProvider, streamParser, processors) { Name = name })
61  {
62  foreach (var zip in sources)
63  {
64  try
65  {
66  processor.Process(zip);
67  }
68  catch (Exception err)
69  {
70  Log.Error(err);
71  return false;
72  }
73  }
74  }
75  return true;
76  }
77 
78  /// <summary>
79  /// Perform processing on the specified source file
80  /// </summary>
81  /// <param name="source">The source file to be processed</param>
82  public void Process(string source)
83  {
84  _start = _start ?? DateTime.UtcNow;
85 
86  // process the source file
87  foreach (var stream in _streamProvider.Open(source))
88  {
89  using (stream)
90  {
91  foreach (var data in _parser.Parse(source, stream))
92  {
93  foreach (var processor in _processors)
94  {
95  processor.Process(data);
96  }
97  }
98  }
99  }
100 
101  Log.Trace("RawFileProcessor.Process({0}): Finished.", source);
102  _streamProvider.Close(source);
103  }
104 
105  /// <summary>
106  /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
107  /// </summary>
108  public void Dispose()
109  {
110  _streamProvider.Dispose();
111  _parser.Dispose();
112  foreach (var processor in _processors)
113  {
114  processor.Dispose();
115  }
116 
117  if (_start.HasValue)
118  {
119  var stop = DateTime.UtcNow;
120  Log.Trace("RawFileProcessor.Dispose({0}): Elapsed {1}", Name, stop - _start);
121  }
122  }
123  }
124 }