/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
namespace QuantConnect.Data.Auxiliary
{
///
/// Helper class for handling mapfile zip files
///
public static class MapFileZipHelper
{
///
/// Gets the mapfile zip filename for the specified date
///
public static string GetMapFileZipFileName(string market, DateTime date, SecurityType securityType)
{
return Path.Combine(Globals.DataFolder, MapFile.GetRelativeMapFilePath(market, securityType), $"map_files_{date:yyyyMMdd}.zip");
}
///
/// Reads the zip bytes as text and parses as MapFileRows to create MapFiles
///
public static IEnumerable ReadMapFileZip(Stream file, string market, SecurityType securityType)
{
if (file == null || file.Length == 0)
{
return Enumerable.Empty();
}
var result = from kvp in Compression.Unzip(file)
let filename = kvp.Key
where filename.EndsWith(".csv", StringComparison.InvariantCultureIgnoreCase)
let lines = kvp.Value.Where(line => !string.IsNullOrEmpty(line))
let mapFile = SafeRead(filename, lines, market, securityType)
select mapFile;
return result;
}
///
/// Parses the contents as a MapFile, if error returns a new empty map file
///
private static MapFile SafeRead(string filename, IEnumerable contents, string market, SecurityType securityType)
{
var permtick = Path.GetFileNameWithoutExtension(filename);
try
{
return new MapFile(permtick, contents.Select(s => MapFileRow.Parse(s, market, securityType)));
}
catch
{
return new MapFile(permtick, Enumerable.Empty());
}
}
}
}