Created
December 4, 2013 20:16
-
-
Save gbenatti/7794741 to your computer and use it in GitHub Desktop.
Coversor de csv do aapg para o yaml do mcp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
namespace aapg2mcp | |
{ | |
class MainClass | |
{ | |
public static void Main (string[] args) | |
{ | |
Console.WriteLine ("Processando arquivo"); | |
if (args.Length != 0) | |
TryParseFile (args [0]); | |
else | |
Console.WriteLine ("Informe o path do CSV."); | |
} | |
static void TryParseFile(string filename) | |
{ | |
try | |
{ | |
ParseFile(filename); | |
} catch (FileNotFoundException) | |
{ | |
Console.WriteLine (String.Format("Impossível abrir o arquivo: {0}", filename)); | |
} | |
} | |
static void ParseFile (string filename) | |
{ | |
var lines = from line in File.ReadAllLines (filename) | |
where !String.IsNullOrEmpty (line) | |
select line.Split (",".ToCharArray ()); | |
var tuples = from data in lines | |
where data.Length == 5 | |
select new {Role = FirstWord (data [0]), | |
Action = data [2].Trim ("\"".ToCharArray ()), | |
Resource = data [3].Trim ("\"".ToCharArray ())}; | |
var result = from tuple in tuples | |
where IsResource (tuple.Resource) | |
group new {Action = tuple.Action, Resource = tuple.Resource} by tuple.Role into g | |
select new {Role = g.Key, ActionsResources = g.ToList ().GroupBy (item => item.Resource, item => item.Action)}; | |
foreach (var item in result) | |
{ | |
Console.WriteLine (String.Format ("{0}:", item.Role)); | |
foreach (var actionResource in item.ActionsResources) | |
{ | |
Console.WriteLine (String.Format (" {0}:", actionResource.Key)); | |
foreach (var action in actionResource.ToList ().Distinct ()) | |
{ | |
Console.WriteLine (String.Format (" - {0}", action)); | |
} | |
} | |
} | |
} | |
static string FirstWord (string str) | |
{ | |
return str.Split().First().Trim ("\"".ToCharArray ()); | |
} | |
static bool IsResource(string resource) | |
{ | |
var resources = new string[] {"CONTA_SAMBATECH", "DEFINICOES_TRANSFORMACAO_IMAGEM", "IMAGEM", "VIDEO"}; | |
return resources.Contains(resource.Trim("\"".ToCharArray())); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment