Lean  $LEAN_TAG$
ZipEntryNameSubscriptionDataSourceReader.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 QuantConnect.Data;
19 using System.Collections.Generic;
21 
23 {
24  /// <summary>
25  /// Provides an implementation of <see cref="ISubscriptionDataSourceReader"/> that reads zip entry names
26  /// </summary>
28  {
29  private readonly IDataCacheProvider _dataProvider;
30  private readonly SubscriptionDataConfig _config;
31  private readonly DateTime _date;
32  private readonly bool _isLiveMode;
33  private readonly BaseData _factory;
34 
35  /// <summary>
36  /// Initializes a new instance of the <see cref="ZipEntryNameSubscriptionDataSourceReader"/> class
37  /// </summary>
38  /// <param name="dataProvider">Used to fetch data</param>
39  /// <param name="config">The subscription's configuration</param>
40  /// <param name="date">The date this factory was produced to read data for</param>
41  /// <param name="isLiveMode">True if we're in live mode, false for backtesting</param>
42  public ZipEntryNameSubscriptionDataSourceReader(IDataCacheProvider dataProvider, SubscriptionDataConfig config, DateTime date, bool isLiveMode)
43  : base(dataProvider, isLiveMode, null)
44  {
45  _date = date;
46  _config = config;
47  _isLiveMode = isLiveMode;
48  _dataProvider = dataProvider;
49  _factory = config.GetBaseDataInstance();
50  }
51 
52  /// <summary>
53  /// Reads the specified <paramref name="source"/>
54  /// </summary>
55  /// <param name="source">The source to be read</param>
56  /// <returns>An <see cref="IEnumerable{BaseData}"/> that contains the data in the source</returns>
57  public override IEnumerable<BaseData> Read(SubscriptionDataSource source)
58  {
59  var fileName = source.Source;
60 
61  if (source.TransportMedium == SubscriptionTransportMedium.RemoteFile)
62  {
63  using var reader = CreateStreamReader(source) as RemoteFileSubscriptionStreamReader;
64  if (reader != null)
65  {
66  fileName = reader.LocalFileName;
67  }
68  }
69 
70  List<string> entryNames;
71  try
72  {
73  entryNames = _dataProvider.GetZipEntries(fileName);
74  }
75  catch (Exception err)
76  {
77  OnInvalidSource(source, err);
78  yield break;
79  }
80 
81  foreach (var entryFileName in entryNames)
82  {
83  var instance = _factory.Reader(_config, entryFileName, _date, _isLiveMode);
84  if (instance != null && instance.EndTime != default(DateTime))
85  {
86  yield return instance;
87  }
88  }
89  }
90 
91  /// <summary>
92  /// Event invocator for the <see cref="BaseSubscriptionDataSourceReader.InvalidSource"/> event
93  /// </summary>
94  /// <param name="source">The <see cref="SubscriptionDataSource"/> that was invalid</param>
95  /// <param name="exception">The exception if one was raised, otherwise null</param>
96  private void OnInvalidSource(SubscriptionDataSource source, Exception exception)
97  {
98  OnInvalidSource(source, exception);
99  }
100  }
101 }