HulkImport (empty) → 0.1.0.0
raw patch · 9 files changed
+197/−0 lines, 9 filesdep +HulkImportdep +attoparsecdep +basesetup-changed
Dependencies added: HulkImport, attoparsec, base, pretty, tasty, tasty-golden, text
Files
- HulkImport.cabal +50/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- app/Main.hs +9/−0
- src/CSV/Parse.hs +48/−0
- src/CSV/SQL.hs +24/−0
- src/CSV/Types.hs +4/−0
- src/HulkImport.hs +19/−0
- test/Tests.hs +11/−0
+ HulkImport.cabal view
@@ -0,0 +1,50 @@+name: HulkImport+version: 0.1.0.0+synopsis: Initial project template from stack+description: Please see README.md+homepage: http://github.com/tobs169/HulkImport#readme+license: BSD3+license-file: LICENSE+author: Toby Smyth+maintainer: tobs169@gmail.com+copyright: 2015+category: Data+build-type: Simple+-- extra-source-files:+cabal-version: >=1.10++library+ hs-source-dirs: src+ exposed-modules: CSV.Types,+ CSV.Parse,+ CSV.SQL, + HulkImport+ build-depends: base < 5,+ attoparsec,+ text,+ pretty+ default-language: Haskell2010++executable HulkImport-exe+ hs-source-dirs: app+ main-is: Main.hs+ ghc-options: -threaded -rtsopts -with-rtsopts=-N+ build-depends: base+ , HulkImport+ , text+ default-language: Haskell2010++test-suite HulkImport-test+ type: exitcode-stdio-1.0+ hs-source-dirs: test+ main-is: Tests.hs+ build-depends: base+ , HulkImport+ , tasty+ , tasty-golden+ ghc-options: -threaded -rtsopts -with-rtsopts=-N+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/tobs169/HulkImport
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Toby Smyth (c) 2015++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Toby Smyth nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ app/Main.hs view
@@ -0,0 +1,9 @@+module Main where++import HulkImport++main :: IO ()+main = importFile testFile "./output"++testFile :: FilePath+testFile = "/Users/toby/Downloads/Countries_v1.csv"
+ src/CSV/Parse.hs view
@@ -0,0 +1,48 @@+module CSV.Parse+ ( parse+ ) where++import qualified Data.Attoparsec.Text as A+import qualified Data.Text as Text++import CSV.Types++-- | 'parse' parses text in the CSV format. Delimiters are ',' and new lines.+-- | Quotation is performed with '"'.+-- | TODO: Files in the wrong format throw an error :( +parse :: Text.Text -> CSV Text.Text+parse text =+ case A.parseOnly csvParser text of+ Left s -> error s+ Right c -> c++csvParser :: A.Parser (CSV Text.Text)+csvParser = do+ rows <- A.sepBy1 rowParser (A.skip A.isEndOfLine) + return (CSV rows)+++rowParser :: A.Parser [Text.Text]+rowParser = A.sepBy1 elementParser $ A.char ','++elementParser :: A.Parser Text.Text+elementParser = do + text <- A.many1 $ A.choice+ [ textParser+ , quotationParser ]+ return (Text.concat text)++textParser :: A.Parser Text.Text+textParser = A.takeWhile1 (A.notInClass [',','\n', '"'])++quotationParser :: A.Parser Text.Text+quotationParser = + let quote = '"' in+ let f q1 t q2 = Text.cons q1 (Text.snoc t q2) in+ f <$> A.char quote <*> + quotedString [quote] <*>+ A.char quote++quotedString :: String -> A.Parser Text.Text+quotedString quote = A.takeWhile (A.notInClass quote)+
+ src/CSV/SQL.hs view
@@ -0,0 +1,24 @@+module CSV.SQL + (toSQL) +where++import CSV.Types+import Text.PrettyPrint+import qualified Data.Text as T++-- | Converts CSV data to the Values clause of an insert statement+toSQL :: CSV T.Text -> String+toSQL = render . csvDoc++csvDoc :: CSV T.Text -> Doc+csvDoc (CSV rows) = hcat (text "VALUES " : fmap rowDoc rows)+ <> text "\nGO"++rowDoc :: [T.Text] -> Doc+rowDoc =+ (<> text ", ") .+ parens+ . hcat+ . punctuate (text ",")+ . map (text . T.unpack)+
+ src/CSV/Types.hs view
@@ -0,0 +1,4 @@+module CSV.Types ( CSV (..)) where +++data CSV a = CSV [[a]] deriving Show
+ src/HulkImport.hs view
@@ -0,0 +1,19 @@+module HulkImport (+ importFile+ )+where++import CSV.Parse+import CSV.SQL+import qualified Data.Text.IO as TIO+import qualified Data.Text as T ++-- | 'importFile' reads in a CSV file and writes out the corresponsing fragment of the insert statement to another file+importFile :: FilePath -- ^Location of the input file+ -> FilePath -- ^Desired output file. Will be created if it doesn't exist+ -> IO ()+importFile input output = do+ contents <- TIO.readFile input+ TIO.writeFile output $ T.pack $ toSQL $ parse contents++
+ test/Tests.hs view
@@ -0,0 +1,11 @@++import Test.Tasty+import qualified Gold.Tests++main :: IO ()+main = defaultMain tests++tests :: TestTree+tests = testGroup "Tests" [+ Gold.Tests.tests+ ]