Skip to content

Instantly share code, notes, and snippets.

@gbenatti
Last active December 30, 2015 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbenatti/7791009 to your computer and use it in GitHub Desktop.
Save gbenatti/7791009 to your computer and use it in GitHub Desktop.
Parser das permissoes do aapg, feito em F#
module ParseAapgPermissions.Main
open System
open System.IO
open System.Text.RegularExpressions
let lineSequence(file) =
let reader = File.OpenText(file)
Seq.unfold(fun line ->
if line = null then
reader.Close()
None
else
Some(line,reader.ReadLine())) (reader.ReadLine())
let parseFile file =
let comma = ','
let resources = [ "CONTA_SAMBATECH"; "DEFINICOES_TRANSFORMACAO_IMAGEM"; "IMAGEM"; "VIDEO" ]
let isResource value = not (Seq.forall (fun elem -> not (elem = value)) resources)
let firstWord phrase =
let wordMatch =
Regex.Matches(phrase, "(\w+)")
|> Seq.cast<Match>
|> Seq.head
wordMatch.Value
let printResult (role, resource_action) =
printfn "%s:" role
resource_action
|> Seq.iter(fun (resource, actions) -> printfn " %s:" resource;
actions
|> Seq.map (fun (actionData:array<string>) -> actionData.[1])
|> Seq.distinct
|> Seq.iter (fun action -> printfn " - %s" action))
file
|> lineSequence
|> Seq.filter (fun line -> not (String.IsNullOrEmpty line))
|> Seq.map (fun line -> line.Split([| comma |]))
|> Seq.filter (fun data -> Array.length(data) = 5)
|> Seq.map (fun data -> Array.map (fun (item:string) -> item.Trim("\"".ToCharArray())) data)
|> Seq.map (fun data -> [| firstWord data.[0]; data.[2]; data.[3] |])
|> Seq.filter (fun data -> isResource data.[2])
|> Seq.groupBy (fun data -> data.[0])
|> Seq.map (fun (key, group) -> (key, Seq.groupBy (fun (groupData:array<string>) -> groupData.[2]) group))
|> Seq.iter (fun data -> printResult data)
let tryParseFile file =
try
parseFile file
with
| :? FileNotFoundException -> printfn "Impossível abrir o arquivo: %s." file
[<EntryPoint>]
let main args =
match args.Length with
| 0 -> printfn ("Informe o path do CSV.")
| _ -> printfn ("Start parsing"); tryParseFile args.[0]
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment