public void PutInQuotesIfNeeded() { CSVEntry entry = new CSVEntry("1,2", "3"); string strEntry = entry.ToString(','); Assert.AreEqual("\"1,2\",3", strEntry); }
/// <summary> /// Imports a CSV file into a dictionary format, /// Outer key is filename, inner key is fieldname, inner value is translated text /// </summary> /// <param name="file">The CSV file to read from</param> /// <param name="loc">The current culture string</param> /// <returns>Dictionary where outer key is filename, inner key is fieldname, inner value is translated text</returns> private static Dictionary <string, Dictionary <string, CSVEntry> > ImportCSV(string file, string loc, Func <CSVEntry, bool> filter) { Dictionary <string, Dictionary <string, CSVEntry> > values = new Dictionary <string, Dictionary <string, CSVEntry> >(); using (CSVReader r = new CSVReader(file)) { List <string> fields = null; while ((fields = r.AdvanceLine()) != null) { if (fields.Count >= 5) { CSVEntry e = new CSVEntry(fields); e.Filename = System.IO.Path.Combine(System.IO.Path.Combine(Application.StartupPath, loc), e.Filename); if (filter != null && filter(e)) { continue; } if (!values.ContainsKey(e.Filename)) { values.Add(e.Filename, new Dictionary <string, CSVEntry>()); } Dictionary <string, CSVEntry> l = values[e.Filename]; l[e.Fieldkey] = e; } } } return(values); }
public bool Load(string lang, string rawCSV) { CSVParser parser = new CSVParser(); parser.Parse(rawCSV); CSVObject csv = parser.Flush(); if (csv != null) { int keyCol = csv.Header.IndexOf(KEY); int langCol = csv.Header.IndexOf(lang); if (langCol < 0 || keyCol < 0) { throw new Exception("Could not find required columns"); } for (int x = 0; x < csv.Count; x++) { CSVEntry entry = csv[x]; string key = entry[keyCol]; string value = entry[langCol]; m_Translations[key] = value; } } return(true); }
public void SingleEntry() { CSVTable table = m_Parser.Parse("a;b").Flush(); Assert.AreEqual(1, table.Entries.Count); CSVEntry entry = table.Entries[0]; AssertEntry(entry, "a", "b"); }
public bool AddEntry(int ID, CSVEntry csvEntry) { if (this.EntryDic.ContainsKey(ID.ToString())) { //SGG_Logger.Print(SGG_Logger.CSV, "ID" + ID + "已经存在,无法添加!!"); return(false); } this.EntryDic.Add(ID.ToString(), csvEntry); return(true); }
private static void AssertEntry(CSVEntry entry, params string[] values) { int length = values.Length; Assert.AreEqual(length, entry.Values.Length); for (int x = 0; x < length; ++x) { Assert.AreEqual(values[x], entry.Values[x]); } }
private void AddMissingKeys(CSVObject csv) { string[] keys = Enum.GetNames(typeof(ELocKey)); for (int x = 0; x < keys.Length; x++) { if (csv.Find(0, keys[x]) == null) { CSVEntry entry = new CSVEntry(csv.Width); for (int entryColumn = 0; entryColumn < entry.Size; entryColumn++) { entry[entryColumn] = keys[entryColumn]; } csv.Add(entry); } } }
public static List <CSVEntry> ReadCSV(string csvFilePath) { string[] lines = File.ReadAllLines(csvFilePath); List <CSVEntry> nominations = new List <CSVEntry>(); for (int i = 1; i < lines.Length; i++) { string[] lineElements = lines[i].Split(",,"); CSVEntry nomination = new CSVEntry(); nomination.Year = int.Parse(lineElements[0]); nomination.Category = lineElements[1]; nomination.IsWinner = bool.Parse(lineElements[2]); nomination.Entity = lineElements[3]; nomination.Film = lineElements[4]; nominations.Add(nomination); } ; return(nominations); }
public void BasicSplitTransactionTest() { // setup var entry1 = new CSVEntry { Number = 123, Account = "Account 1", Amount = 100.00M, CategoryString = SplitTransaction.SplitCategoryString, Classification = "Big Purchase", Memo = "A really awesome thing.", Payee = "Evil corp", TransactionDate = new DateTime(2001, 12, 25) }; var entry2 = new CSVEntry { Amount = 20.00M, CategoryString = "Groceries", Memo = "Part 1.", }; var entry3 = new CSVEntry { Amount = 80.00M, CategoryString = "Gifts", Memo = "Part 2.", }; // test var factory = new TransactionFactory(new[] { entry1 }); var result = factory.ToArray(); // result //ToDo: This won't work at the moment because there's no standard transaction to follow through and flush the buffer. // This isn't great design. Better to 1) Detect the end of a split by summing the parts against the header. 2) return // transactions not transaction strings. this should allow easier testing. 3) Work out a way to flush the factory (possibly // automatically: standards flush after every line, splits flush once everything adds up). var expected = "123,2001-12-25,\"Evil corp\",Account 1,\"A really awesome thing.\",\"Category 1\",Big Purchase,100.00\r\n"; Assert.AreEqual(expected, result); }
private CSVObject LoadCSV(string path) { string[] lines = File.ReadAllLines(path); CSVParser parser = new CSVParser(); for (int x = 0; x < lines.Length; x++) { parser.ParseLine(lines[x]); } CSVObject csv = parser.Flush(); if (csv == null) { csv = new CSVObject(); } csv.AddHeaders("key", "en-us"); string[] keyNames = Enum.GetNames(typeof(ELocKey)); HashSet <string> knownNames = new HashSet <string>(); for (int x = 0; x < csv.Count; x++) { knownNames.Add(csv[x][0]); } for (int x = 0; x < keyNames.Length; x++) { string key = keyNames[x]; if (!knownNames.Contains(key)) { CSVEntry row = new CSVEntry(csv.Width); for (int iRow = 0; iRow < row.Size; iRow++) { row[iRow] = key; } csv.Add(row); } } return(csv); }
private void DrawLocalizationEntries() { m_EditedHeader = EditorGUILayout.Popup(m_EditedHeader, m_Headers); int column = m_EditedHeader + 1; EditorGUILayout.BeginVertical(); for (int x = 0; x < m_CSV.Count; x++) { EditorGUILayout.BeginHorizontal(); CSVEntry entry = m_CSV[x]; string oldValue = entry[column]; entry[column] = EditorGUILayout.DelayedTextField(entry[0], entry[column]); if (entry[column] != oldValue) { SaveCSV(); } EditorGUILayout.EndHorizontal(); } EditorGUILayout.EndVertical(); }
public void BasicStandardTransactionTest() { // setup var singleCSVEntry = new CSVEntry { Number = 123, Account = "Account 1", Amount = 100.00M, CategoryString = "Category 1", Classification = "Big Purchase", Memo = "A really awesome thing.", Payee = "Evil corp", TransactionDate = new DateTime(2001, 12, 25) }; // test var factory = new TransactionFactory(new[] { singleCSVEntry }); var result = factory.Single(); // result var expected = "123,2001-12-25,\"Evil corp\",Account 1,\"A really awesome thing.\",\"Category 1\",Big Purchase,100.00\r\n"; Assert.AreEqual(expected, result); }
private static void Export(string cultures) { Update(cultures); Report(cultures); foreach (string culture in GetLocaleFolders(cultures)) { XDocument doc = XDocument.Load(System.IO.Path.Combine(Application.StartupPath, "report." + culture + ".xml")); string outfile = System.IO.Path.Combine(Application.StartupPath, "report." + culture + ".csv"); Dictionary<string, Dictionary<string, CSVEntry>> elems = new Dictionary<string, Dictionary<string, CSVEntry>>(); foreach (var file in doc.Element("root").Elements("file")) { string filename = file.Attribute("filename").Value.Substring(Duplicati.Library.Utility.Utility.AppendDirSeparator(Application.StartupPath).Length); filename = filename.Substring(culture.Length + 1); Dictionary<string, CSVEntry> e = new Dictionary<string, CSVEntry>(); elems[filename] = e; foreach (var item in file.Element("updated").Elements("item")) { CSVEntry c = new CSVEntry(); c.Filename = filename; c.Fieldkey = item.Attribute("name").Value; c.Status = item.Parent.Name.LocalName; c.Origvalue = item.Element("original").Value; c.Value = item.Element("translated").Value; e.Add(c.Fieldkey, c); } foreach (var item in file.Element("missing").Elements("item")) { CSVEntry c = new CSVEntry(); c.Filename = filename; c.Fieldkey = item.Attribute("name").Value; c.Status = item.Parent.Name.LocalName; c.Origvalue = item.Value; c.Value = ""; e.Add(c.Fieldkey, c); } foreach (var item in file.Element("not-updated").Elements("item")) { CSVEntry c = new CSVEntry(); c.Filename = filename; c.Fieldkey = item.Attribute("name").Value; c.Status = item.Parent.Name.LocalName; c.Origvalue = item.Value; c.Value = ""; e.Add(c.Fieldkey, c); } foreach (var item in file.Element("unused").Elements("item")) { CSVEntry c = new CSVEntry(); c.Filename = filename; c.Fieldkey = item.Attribute("name").Value; c.Status = item.Parent.Name.LocalName; c.Origvalue = ""; c.Value = item.Value; e.Add(c.Fieldkey, c); } } WriteCSVFile(outfile, culture, elems, null); } }
/// <summary> /// Imports a CSV file into a dictionary format, /// Outer key is filename, inner key is fieldname, inner value is translated text /// </summary> /// <param name="file">The CSV file to read from</param> /// <param name="loc">The current culture string</param> /// <returns>Dictionary where outer key is filename, inner key is fieldname, inner value is translated text</returns> private static Dictionary<string, Dictionary<string, CSVEntry>> ImportCSV(string file, string loc, Func<CSVEntry, bool> filter) { Dictionary<string, Dictionary<string, CSVEntry>> values = new Dictionary<string, Dictionary<string, CSVEntry>>(); using (CSVReader r = new CSVReader(file)) { List<string> fields = null; while ((fields = r.AdvanceLine()) != null) if (fields.Count >= 5) { CSVEntry e = new CSVEntry(fields); e.Filename = System.IO.Path.Combine(System.IO.Path.Combine(Application.StartupPath, loc), e.Filename); if (filter != null && filter(e)) continue; if (!values.ContainsKey(e.Filename)) values.Add(e.Filename, new Dictionary<string, CSVEntry>()); Dictionary<string, CSVEntry> l = values[e.Filename]; l[e.Fieldkey] = e; } } return values; }
private static void Export(string cultures) { Update(cultures); Report(cultures); foreach (string culture in GetLocaleFolders(cultures)) { XDocument doc = XDocument.Load(System.IO.Path.Combine(Application.StartupPath, "report." + culture + ".xml")); string outfile = System.IO.Path.Combine(Application.StartupPath, "report." + culture + ".csv"); Dictionary <string, Dictionary <string, CSVEntry> > elems = new Dictionary <string, Dictionary <string, CSVEntry> >(); foreach (var file in doc.Element("root").Elements("file")) { string filename = file.Attribute("filename").Value.Substring(Duplicati.Library.Utility.Utility.AppendDirSeparator(Application.StartupPath).Length); filename = filename.Substring(culture.Length + 1); Dictionary <string, CSVEntry> e = new Dictionary <string, CSVEntry>(); elems[filename] = e; foreach (var item in file.Element("updated").Elements("item")) { CSVEntry c = new CSVEntry(); c.Filename = filename; c.Fieldkey = item.Attribute("name").Value; c.Status = item.Parent.Name.LocalName; c.Origvalue = item.Element("original").Value; c.Value = item.Element("translated").Value; e.Add(c.Fieldkey, c); } foreach (var item in file.Element("missing").Elements("item")) { CSVEntry c = new CSVEntry(); c.Filename = filename; c.Fieldkey = item.Attribute("name").Value; c.Status = item.Parent.Name.LocalName; c.Origvalue = item.Value; c.Value = ""; e.Add(c.Fieldkey, c); } foreach (var item in file.Element("not-updated").Elements("item")) { CSVEntry c = new CSVEntry(); c.Filename = filename; c.Fieldkey = item.Attribute("name").Value; c.Status = item.Parent.Name.LocalName; c.Origvalue = item.Value; c.Value = ""; e.Add(c.Fieldkey, c); } foreach (var item in file.Element("unused").Elements("item")) { CSVEntry c = new CSVEntry(); c.Filename = filename; c.Fieldkey = item.Attribute("name").Value; c.Status = item.Parent.Name.LocalName; c.Origvalue = ""; c.Value = item.Value; e.Add(c.Fieldkey, c); } } WriteCSVFile(outfile, culture, elems, null); } }
private static void ExportDiff(string culture, string inputfile) { inputfile = System.IO.Path.GetFullPath(inputfile); string currentFile = System.IO.Path.Combine(Application.StartupPath, "report." + culture + ".csv"); string diffFile = System.IO.Path.Combine(Application.StartupPath, "report." + culture + ".diff.csv"); if (currentFile.Equals(inputfile, Duplicati.Library.Utility.Utility.ClientFilenameStringComparision)) { throw new Exception("Input file will be overwritten, please use another file"); } Export(culture); //Outer key is filename, inner key is fieldname, inner value is translated text Dictionary <string, Dictionary <string, CSVEntry> > inputValues = ImportCSV(inputfile, culture, null); Dictionary <string, Dictionary <string, CSVEntry> > currentValues = ImportCSV(currentFile, culture, null); Dictionary <string, Dictionary <string, CSVEntry> > added = new Dictionary <string, Dictionary <string, CSVEntry> >(); Dictionary <string, Dictionary <string, CSVEntry> > removed = new Dictionary <string, Dictionary <string, CSVEntry> >(); foreach (var f in currentValues) { Dictionary <string, CSVEntry> other; inputValues.TryGetValue(f.Key, out other); if (other == null) { added.Add(f.Key, f.Value); } else { Dictionary <string, CSVEntry> a = new Dictionary <string, CSVEntry>(); foreach (KeyValuePair <string, CSVEntry> s in f.Value) { if (other.ContainsKey(s.Key)) { other.Remove(s.Key); } else { a.Add(s.Key, s.Value); } } if (a.Count > 0) { added.Add(f.Key, a); } if (other.Count > 0) { removed.Add(f.Key, other); } } } Dictionary <string, Dictionary <string, string> > overrides = new Dictionary <string, Dictionary <string, string> >(); Dictionary <string, Dictionary <string, CSVEntry> > diffElems = new Dictionary <string, Dictionary <string, CSVEntry> >(); foreach (KeyValuePair <string, Dictionary <string, CSVEntry> > f in added) { foreach (KeyValuePair <string, CSVEntry> s in f.Value) { if (s.Value.Origvalue == s.Value.Value || s.Value.Value.Trim().Length == 0) { if (!overrides.ContainsKey(f.Key)) { overrides.Add(f.Key, new Dictionary <string, string>()); } if (!diffElems.ContainsKey(f.Key)) { diffElems.Add(f.Key, new Dictionary <string, CSVEntry>()); } overrides[f.Key].Add(s.Key, "not-updated"); diffElems[f.Key].Add(s.Key, s.Value); } } } foreach (KeyValuePair <string, Dictionary <string, CSVEntry> > f in removed) { foreach (KeyValuePair <string, CSVEntry> s in f.Value) { if (!overrides.ContainsKey(f.Key)) { overrides.Add(f.Key, new Dictionary <string, string>()); } if (!diffElems.ContainsKey(f.Key)) { diffElems.Add(f.Key, new Dictionary <string, CSVEntry>()); } overrides[f.Key].Add(s.Key, "unused"); diffElems[f.Key].Add(s.Key, s.Value); } } WriteCSVFile(diffFile, culture, diffElems, overrides); //Re-read the file inputValues = ImportCSV(inputfile, culture, null); //Add the new entries foreach (KeyValuePair <string, Dictionary <string, CSVEntry> > f in added) { if (!inputValues.ContainsKey(f.Key)) { inputValues.Add(f.Key, new Dictionary <string, CSVEntry>()); } Dictionary <string, CSVEntry> o = inputValues[f.Key]; foreach (KeyValuePair <string, CSVEntry> s in f.Value) { o.Add(s.Key, s.Value); } } foreach (KeyValuePair <string, Dictionary <string, CSVEntry> > f in currentValues) { Dictionary <string, CSVEntry> o = inputValues[f.Key]; foreach (KeyValuePair <string, CSVEntry> s in f.Value) { CSVEntry c = o[s.Key]; if (s.Value.Status == "unused") { c.Status = "unused"; } else if (c.Origvalue == c.Value) { c.Status = "not-updated"; } else if (string.IsNullOrEmpty(c.Value) || c.Value.Trim().Length == 0) { c.Status = "missing"; } else { c.Status = "updated"; } o[s.Key] = c; } } //Update the removed entries foreach (KeyValuePair <string, Dictionary <string, CSVEntry> > f in removed) { Dictionary <string, CSVEntry> o = inputValues[f.Key]; foreach (KeyValuePair <string, CSVEntry> s in f.Value) { CSVEntry c = o[s.Key]; c.Status = "unused"; o[s.Key] = c; } } //Write the output file diffFile = System.IO.Path.Combine(Application.StartupPath, "report." + culture + ".updated.csv"); WriteCSVFile(diffFile, culture, inputValues, null); }
/// <summary> /// Method to a new line entry into the transaction. /// </summary> /// <remarks>For split transactions, this might be called /// multiple times to build up the complete representation of the transaction.</remarks> /// <param name="entry">A CSVEntry containing some or all of the transaction data.</param> /// <returns>true if the entry was valid for the transaction and successfully added.</returns> public abstract bool AddEntry(CSVEntry entry);