public void OpenCalledTwice_NoExceptionThrown(CSVReaderWriterForAnnotation.Mode mode, string inputFilePath) { var readerWriter = new CsvReaderWriter(); readerWriter.Open(inputFilePath, mode); readerWriter.Open(inputFilePath, mode); readerWriter.Close(); }
private static void WritePayslipsToCsv(List <Payslip> payslips, string filename) { var lines = new List <string>() { "name,pay period,gross income,income tax,net income,super" }; lines.AddRange(payslips.Select(payslip => CsvFormatter.CreateCsvLineFromPayslip(payslip))); CsvReaderWriter.WriteLines(filename, lines); }
public void NonUsefulReadMethodKeepsOldBehaviour() { // check old implementation var csvReaderWriterOld = new CSVReaderWriterForAnnotation(); AssertReturnsTrueOrThrowsException(csvReaderWriterOld); // check new implementation var csvReaderWriterNew = new CsvReaderWriter(); AssertReturnsTrueOrThrowsException(csvReaderWriterNew); }
public void ReadsAllLines() { // check old implementation var csvReaderWriterOld = new CSVReaderWriterForAnnotation(); AssertReadsAllLines(csvReaderWriterOld); // check new implementation var csvReaderWriterNew = new CsvReaderWriter(); AssertReadsAllLines(csvReaderWriterNew); }
public void WritesColumns() { // check old implementation var csvReaderWriterOld = new CSVReaderWriterForAnnotation(); AssertWritesColumns(csvReaderWriterOld); // check new implementation var csvReaderWriterNew = new CsvReaderWriter(); AssertWritesColumns(csvReaderWriterNew); }
private void ParseButton_Click(object sender, EventArgs e) { switch (ParseButton.Text) { case "Parse": CsvReaderWriter crw = new CsvReaderWriter(FilePath_textBox.Text, requester.GetResultPath()); parsing_progressBar.Value = 0; parsing_progressBar.Maximum = crw.getMax(); for (int i = 0; i < Streams_numericUpDown.Value; i++) { Thread t = new Thread(delegate () { while (true) { string source; lock (crw) { source = crw.getNextSource(); } if (source == null) { MessageBox.Show("Done!"); break; } ParsedResult parsedResult = requester.Parse(source); Progress_Label.Invoke(new Action(() => { parsing_progressBar.Value++; Progress_Label.Text = parsing_progressBar.Value.ToString() + "/" + parsing_progressBar.Maximum.ToString(); TimeLeft_label.Text = getLeftTime(); })); lock (crw) { crw.insertParsed(parsedResult); } Thread.Sleep((int)Time_Out_numericUpDown.Value * 1000); } }); t.Start(); threads.Add(t); } break; case "Pause": foreach (var item in threads) { if (item.IsAlive) item. } break; case "Continue": break; default: break; } }
public void Process(string inputFile) { var reader = new CsvReaderWriter(); reader.Open(inputFile, CSVReaderWriterForAnnotation.Mode.Read); string column1, column2; while (reader.Read(out column1, out column2)) { _mailShot.SendMailShot(column1, column2); } reader.Close(); }
public static List <Payslip> MakePayslip(string csvFileName) { var allPayslips = new List <Payslip>(); var lines = CsvReaderWriter.ReadLines(csvFileName); for (var i = 1; i < lines.Length; i++) { var line = lines[i]; var employee = CsvFormatter.ExtractEmployee(line); employee.Country = Config.Australia; // TODO: Remove this hardcoded value once we have config input var payPeriod = CsvFormatter.ExtractPayPeriod(line); var payslip = CreatePayslipObject(employee, payPeriod); allPayslips.Add(payslip); } return(allPayslips); }
// Import data from a CSV file. Display instructions and error messages as needed. private async void MenuItemImportFromCsv_Click(object sender, RoutedEventArgs e) { if (this.State.SuppressCsvImportPrompt == false) { // Tell the user how importing CSV files work. Give them the opportunity to abort. if (Dialogs.MenuFileHowImportingCSVWorksDialog(this) == false) { return; } } string csvFileName = Path.GetFileNameWithoutExtension(this.DataHandler.FileDatabase.FileName) + Constant.File.CsvFileExtension; if (Dialogs.TryGetFileFromUserUsingOpenFileDialog( "Select a .csv file to merge into the current image set", Path.Combine(this.DataHandler.FileDatabase.FolderPath, csvFileName), String.Format("Comma separated value files (*{0})|*{0}", Constant.File.CsvFileExtension), Constant.File.CsvFileExtension, out string csvFilePath) == false) { return; } // Create a backup database file if (FileBackup.TryCreateBackup(this.FolderPath, this.DataHandler.FileDatabase.FileName)) { this.StatusBar.SetMessage("Backup of data file made."); } else { this.StatusBar.SetMessage("No data file backup was made."); } try { // Show the Busy indicator this.BusyCancelIndicator.IsBusy = true; Tuple <bool, List <string> > resultAndImportErrors; resultAndImportErrors = await CsvReaderWriter.TryImportFromCsv(csvFilePath, this.DataHandler.FileDatabase).ConfigureAwait(true); this.BusyCancelIndicator.IsBusy = false; if (resultAndImportErrors.Item1 == false) { // Can't import CSV File Dialogs.MenuFileCantImportCSVFileDialog(this, Path.GetFileName(csvFilePath), resultAndImportErrors.Item2); } else { // Importing done. Dialogs.MenuFileCSVFileImportedDialog(this, Path.GetFileName(csvFilePath)); // Reload the data this.BusyCancelIndicator.IsBusy = true; await this.FilesSelectAndShowAsync().ConfigureAwait(true); this.BusyCancelIndicator.IsBusy = false; this.StatusBar.SetMessage("CSV file imported."); } } catch (Exception exception) { // Can't import the .csv file Dialogs.MenuFileCantImportCSVFileDialog(this, Path.GetFileName(csvFilePath), exception.Message); } }
// Export data for this image set as a .csv file // Export data for this image set as a .csv file and preview in Excel private void MenuItemExportCsv_Click(object sender, RoutedEventArgs e) { if (this.State.SuppressSelectedCsvExportPrompt == false && this.DataHandler.FileDatabase.ImageSet.FileSelection != FileSelectionEnum.All) { // Export data for this image set as a.csv file, but confirm, as only a subset will be exported since a selection is active if (Dialogs.MenuFileExportCSVOnSelectionDialog(this) == false) { return; } } // Generate the file names/path string csvFileName = Path.GetFileNameWithoutExtension(this.DataHandler.FileDatabase.FileName) + ".csv"; string csvFilePath = Path.Combine(this.FolderPath, csvFileName); // Backup the csv file if it exists, as the export will overwrite it. if (FileBackup.TryCreateBackup(this.FolderPath, csvFileName)) { this.StatusBar.SetMessage("Backup of csv file made."); } else { this.StatusBar.SetMessage("No csv file backup was made."); } try { CsvReaderWriter.ExportToCsv(this.DataHandler.FileDatabase, csvFilePath, this.excludeDateTimeAndUTCOffsetWhenExporting); } catch (IOException exception) { // Can't write the spreadsheet file Dialogs.MenuFileCantWriteSpreadsheetFileDialog(this, csvFilePath, exception.GetType().FullName, exception.Message); return; } MenuItem mi = (MenuItem)sender; if (mi == this.MenuItemExportAsCsvAndPreview) { // Show the file in excel // Create a process that will try to show the file ProcessStartInfo processStartInfo = new ProcessStartInfo { UseShellExecute = true, RedirectStandardOutput = false, FileName = csvFilePath }; if (ProcessExecution.TryProcessStart(processStartInfo) == false) { // Can't open excel Dialogs.MenuFileCantOpenExcelDialog(this, csvFilePath); return; } } else if (this.State.SuppressCsvExportDialog == false) { Dialogs.MenuFileCSVDataExportedDialog(this, csvFileName); } this.StatusBar.SetMessage("Data exported to " + csvFileName); }
// Export data for this image set as a .csv file // Export data for this image set as a .csv file and preview in Excel private async void MenuItemExportCsv_Click(object sender, RoutedEventArgs e) { if (this.State.SuppressSelectedCsvExportPrompt == false && this.DataHandler.FileDatabase.ImageSet.FileSelection != FileSelectionEnum.All) { // Export data for this image set as a.csv file, but confirm, as only a subset will be exported since a selection is active if (Dialogs.MenuFileExportCSVOnSelectionDialog(this) == false) { return; } } // Generate the candidate file name/path string csvFileName = Path.GetFileNameWithoutExtension(this.DataHandler.FileDatabase.FileName) + ".csv"; // Get the selected filepath from the user if (false == Dialogs.TryGetFileFromUserUsingSaveFileDialog( "Export and save your data as a CSV file", csvFileName, String.Format("CSV files (*{0})|*{0}", Constant.File.CsvFileExtension), Constant.File.CsvFileExtension, out string selectedCSVFilePath)) { // Abort, as file selection is cancelled this.StatusBar.SetMessage("Csv file export cancelled."); return; } if (File.Exists(selectedCSVFilePath) && new System.IO.FileInfo(selectedCSVFilePath).Attributes.HasFlag(System.IO.FileAttributes.ReadOnly)) { // The file exists but its read only... Dialogs.FileCantOpen(GlobalReferences.MainWindow, selectedCSVFilePath, true); this.StatusBar.SetMessage("Csv file export cancelled."); return; } // Backup the csv file if it exists, as the export will overwrite it. if (FileBackup.TryCreateBackup(this.FolderPath, selectedCSVFilePath)) { this.StatusBar.SetMessage("Backup of csv file made."); } else { this.StatusBar.SetMessage("No csv file backup was made."); } try { // Show the Busy indicator this.BusyCancelIndicator.IsBusy = true; if (false == await CsvReaderWriter.ExportToCsv(this.DataHandler.FileDatabase, selectedCSVFilePath, this.State.CSVDateTimeOptions, this.State.CSVInsertSpaceBeforeDates)) { Dialogs.FileCantOpen(GlobalReferences.MainWindow, selectedCSVFilePath, true); return; } // Hide the Busy indicator this.BusyCancelIndicator.IsBusy = false; } catch (Exception exception) { // Can't write the spreadsheet file Dialogs.MenuFileCantWriteSpreadsheetFileDialog(this, selectedCSVFilePath, exception.GetType().FullName, exception.Message); return; } MenuItem mi = (MenuItem)sender; if (mi == this.MenuItemExportAsCsvAndPreview) { // Show the file in excel // Create a process that will try to show the file ProcessStartInfo processStartInfo = new ProcessStartInfo { UseShellExecute = true, RedirectStandardOutput = false, FileName = selectedCSVFilePath }; if (ProcessExecution.TryProcessStart(processStartInfo) == false) { // Can't open excel Dialogs.MenuFileCantOpenExcelDialog(this, selectedCSVFilePath); return; } } else if (this.State.SuppressCsvExportDialog == false) { Dialogs.MenuFileCSVDataExportedDialog(this, selectedCSVFilePath); } this.StatusBar.SetMessage("Data exported to " + selectedCSVFilePath); }
private void ParseButton_Click(object sender, EventArgs e) { switch (ParseButton.Text) { case "Start": bool write_headers = true; if (radioButton3.Checked) { if ((!Da_pa_checkbox.Checked && !Cf_tf_checkbox.Checked)) { MessageBox.Show("Выберете хоть одну метрику!"); return; } write_headers = false; } isparsedtoken = true; crw = new CsvReaderWriter(ParseFilePath_textBox.Text, parserequester.GetResultPath(), Da_pa_checkbox.Checked, Cf_tf_checkbox.Checked, write_headers, false); parsing_progressBar.Value = 0; parsing_progressBar.Maximum = crw.getMax(); for (int i = 0; i < Streams_numericUpDown.Value; i++) { Thread t = new Thread(delegate() { while (true) { if (isparsedtoken) { string source; lock (crw) { source = crw.getNextSource(); } if (source == null) { CancelButton.Invoke(new Action(() => { ParseButton.Text = "Start"; isparsedtoken = false; })); break; } ParsedResult parsedResult = parserequester.Parse(source); if (Da_pa_checkbox.Checked || Cf_tf_checkbox.Checked) { parserequester.getDA_PA(source, parsedResult); } Progress_Label.Invoke(new Action(() => { parsing_progressBar.Value++; Progress_Label.Text = parsing_progressBar.Value.ToString() + "/" + parsing_progressBar.Maximum.ToString(); TimeLeft_label.Text = getLeftTime((int)Time_Out_numericUpDown.Value, (int)Streams_numericUpDown.Value); })); Thread.Sleep((int)Time_Out_numericUpDown.Value * 1000); lock (crw) { crw.insertParsed(parsedResult); } } } }); t.Start(); threads.Add(t); } ParseButton.Text = "Pause"; CancelButton.Enabled = true; break; case "Pause": isparsedtoken = false; ParseButton.Text = "Continue"; break; case "Continue": isparsedtoken = true; ParseButton.Text = "Pause"; break; default: break; } }
private void Whoisstart_button_Click_1(object sender, EventArgs e) { switch (WhoisStart_button.Text) { case "Start": iswhoistoken = true; whoisreader = new CsvReaderWriter(Whois_textBox.Text, whoisrequester.GetResultPath(), false, false, false, true); Whois_progressBar.Value = 0; Whois_progressBar.Maximum = whoisreader.getMax(); for (int i = 0; i < WhoisStreams_numericUpDown.Value; i++) { Thread t = new Thread(delegate() { while (true) { if (iswhoistoken) { string source; lock (whoisreader) { source = whoisreader.getNextSource(); } if (source == null) { WhoisCancel_button.Invoke(new Action(() => { WhoisStart_button.Text = "Start"; iswhoistoken = false; WhoisCancel_button.Enabled = false; })); break; } ParsedResult parsedResult = whoisrequester.Parse(source); WhoisCountLeft_label.Invoke(new Action(() => { Whois_progressBar.Value++; WhoisCountLeft_label.Text = Whois_progressBar.Value.ToString() + "/" + Whois_progressBar.Maximum.ToString(); WhoisTimeLeft_label.Text = getWhoisLeftTime((int)Whois_TimeOut_numericUpDown.Value, (int)WhoisStreams_numericUpDown.Value); })); Thread.Sleep((int)Whois_TimeOut_numericUpDown.Value * 1000); lock (whoisreader) { whoisreader.insertParsed(parsedResult); } } } }); t.Start(); threads.Add(t); } WhoisStart_button.Text = "Pause"; WhoisCancel_button.Enabled = true; break; case "Pause": iswhoistoken = false; WhoisStart_button.Text = "Continue"; break; case "Continue": iswhoistoken = true; WhoisStart_button.Text = "Pause"; break; default: break; } }