public List <CCRecord> ReadFromStream(Stream stream, C3Configuration config, bool hasHeader = true) { using (var reader = new StreamReader(stream)) { return(this.ReadFromTextReader(reader, config, hasHeader)); } }
private List <CCRecord> ReadFromTextReader(TextReader reader, C3Configuration config, bool hasHeader = true) { var csv = new CsvReader(reader); csv.Configuration.RegisterClassMap(this.bankConfig.classMap); var defaultValueDict = config.columns.ToDictionary(k => k.columnName, v => v.defaultValue); try { var results = csv.GetRecords <CCRecord>() .Where(rec => !this.bankConfig.recordsToSkip.Contains(rec.Description)) .Select(rec => { rec.PredictedValues = config.columns.ToDictionary(key => key.columnName, val => defaultValueDict[val.columnName]); return(rec); }) .ToList(); return(results); } // here we convert the CsvHelper exception to a .NET exception so we don't need a CsvHelper reference in calling assemblies catch (BadDataException ex) { throw new FormatException(ex.Message); } }
static void Main(string[] args) { config = C3Configuration.LoadFromConfigurationManager(); var options = new Options(); if (CommandLine.Parser.Default.ParseArguments(args, options)) { if (options.Command == CommandType.IMPORT) { IRecordReader bankRecordReader = null; try { bankRecordReader = new CsvRecordReader(options.BankType); } catch (ApplicationException e) { Console.WriteLine($"Unable to create '{options.BankType}' record reader:\n{e.Message}"); Environment.Exit(-1); } if (!File.Exists(options.BaseFile)) { Console.WriteLine("Base file {0} does not exist.", options.BaseFile); } else if (!File.Exists(options.InputFile)) { Console.WriteLine("Input file {0} does not exist.", options.BaseFile); } else { try { Updater.ClassifyAndUpdate(options.BaseFile, options.InputFile, bankRecordReader, config); } catch (Exception e) { Console.WriteLine("Update failed: {0}", e.Message); } } } if (options.Command == CommandType.VIEW) { if (options.BaseFile == null) { Console.WriteLine("Base file must be specified."); return; } if (!File.Exists(options.BaseFile)) { Console.WriteLine($"Failed to open specified base file {options.BaseFile}"); return; } CCRecordSet records = CCRecordSet.FromFile(options.BaseFile, config); new Application().Run(new C3Window(records, options.BaseFile, config)); } } }
public C3Window(CCRecordSet ccRecordSet, string recordSetFileName, C3Configuration config) : this() { this.ccRecordSet = ccRecordSet; this.recordSetFileName = recordSetFileName; this.config = config; this.editDateTimeRange = Selectors.timeFilters[Consts.TIMEEXPR_LASTMONTH]; this.summaryDateTimeRange = Selectors.timeFilters[Consts.TIMEEXPR_LASTMONTH]; this.requiredHeaderNames = new HashSet <string>(this.ccRecordSet.RequiredHeaderNames); currentDataTable = ccRecordSet.ToDataTable(); UpdateEditDataGrid(); foreach (var header in this.ccRecordSet.RequiredHeaderNames) { var col = new DataGridTextColumn { Header = header, Binding = new Binding(header) { StringFormat = columnFormats[header] }, IsReadOnly = true }; EditTabDatagrid.Columns.Add(col); } foreach (var header in config.columns) { var col = new DataGridComboBoxColumn { Header = header.columnName, ItemsSource = header.validValues, SelectedValueBinding = new Binding(header.columnName) }; EditTabDatagrid.Columns.Add(col); } EditTimeFilterComboBox.ItemsSource = Selectors.timeFilters.Keys; EditTimeFilterComboBox.SelectedItem = Selectors.timeFilters.Keys.FirstOrDefault(); SummaryTimeFilterComboBox.ItemsSource = Selectors.timeFilters.Keys; SummaryTimeFilterComboBox.SelectedItem = Selectors.timeFilters.Keys.FirstOrDefault(); SummaryAggregateComboBox.ItemsSource = this.ccRecordSet.PredictedHeaderNames; SummaryAggregateComboBox.SelectedItem = this.ccRecordSet.PredictedHeaderNames.FirstOrDefault(); ReportGroupByComboBox.ItemsSource = this.ccRecordSet.PredictedHeaderNames; ReportGroupByComboBox.SelectedIndex = 0; ReportPeriodComboBox.ItemsSource = Selectors.periodSpecifiers.Keys; ReportPeriodComboBox.SelectedItem = Consts.PERIOD_SPECIFIER_MONTH; ReportAggregationComboBox.ItemsSource = Selectors.aggreations.Keys; ReportAggregationComboBox.SelectedItem = Consts.AGGREGATION_SUM; }
public void Init() { this.config = new C3Configuration() { columns = new C3PredictedColumn[] { new C3PredictedColumn() { columnName = "Category", classifierName = "NaiveBayesClassifier", defaultValue = "UNSPECIFIED", validValues = (new string[] { "CHAR", "CLOTHES", "COMM", "CULTURAL", "EATO", "EDU", "GIFTS", "GROC", "HOME", "MED", "NA", "TOYS", "TRANS", "TRAVEL", "UNSPECIFIED" }).ToHashSet(StringComparer.InvariantCultureIgnoreCase) }, new C3PredictedColumn() { columnName = "Owner", classifierName = "NaiveBayesClassifier", defaultValue = "UNSPECIFIED", validValues = (new string[] { "ALICE", "BOB", "BOTH", "UNSPECIFIED" }).ToHashSet(StringComparer.InvariantCultureIgnoreCase) } } }; }