Skip to content

Commit

Permalink
Format files using DocumentFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
davidanthoff authored Jul 26, 2022
1 parent e23f914 commit 1b9b1f4
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 242 deletions.
10 changes: 5 additions & 5 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using Documenter, CSVFiles

makedocs(
modules = [CSVFiles],
sitename = "CSVFiles.jl",
analytics="UA-132838790-1",
pages = [
modules=[CSVFiles],
sitename="CSVFiles.jl",
analytics="UA-132838790-1",
pages=[
"Introduction" => "index.md"
]
)

deploydocs(
repo = "github.com/queryverse/CSVFiles.jl.git"
repo="github.com/queryverse/CSVFiles.jl.git"
)
28 changes: 14 additions & 14 deletions src/CSVFiles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,36 +50,36 @@ end

Base.showable(::MIME"application/vnd.dataresource+json", source::CSVStream) = true

function fileio_load(f::FileIO.File{FileIO.format"CSV"}, deprecated_delim=nothing; delim=deprecated_delim===nothing ? ',' : deprecated_delim, args...)
if deprecated_delim!==nothing
deprecated_delim!=delim && error("deprecated_delim and delim can not both be used at the same time.")
function fileio_load(f::FileIO.File{FileIO.format"CSV"}, deprecated_delim=nothing; delim=deprecated_delim === nothing ? ',' : deprecated_delim, args...)
if deprecated_delim !== nothing
deprecated_delim != delim && error("deprecated_delim and delim can not both be used at the same time.")
Base.depwarn("The positional `delim` keyword in the `load` function is deprecated. Instead use the keyword argument `delim`.", :CSVFiles)
end

return CSVFile(f.filename, delim, args)
end

function fileio_load(f::FileIO.File{FileIO.format"TSV"}, deprecated_delim=nothing; delim=deprecated_delim===nothing ? '\t' : deprecated_delim, args...)
if deprecated_delim!==nothing
deprecated_delim!=delim && error("deprecated_delim and delim can not both be used at the same time.")
function fileio_load(f::FileIO.File{FileIO.format"TSV"}, deprecated_delim=nothing; delim=deprecated_delim === nothing ? '\t' : deprecated_delim, args...)
if deprecated_delim !== nothing
deprecated_delim != delim && error("deprecated_delim and delim can not both be used at the same time.")
Base.depwarn("The positional `delim` keyword in the `load` function is deprecated. Instead use the keyword argument `delim`.", :CSVFiles)
end

return CSVFile(f.filename, delim, args)
end

function fileio_load(s::FileIO.Stream{FileIO.format"CSV"}, deprecated_delim=nothing; delim=deprecated_delim===nothing ? ',' : deprecated_delim, args...)
if deprecated_delim!==nothing
deprecated_delim!=delim && error("deprecated_delim and delim can not both be used at the same time.")
function fileio_load(s::FileIO.Stream{FileIO.format"CSV"}, deprecated_delim=nothing; delim=deprecated_delim === nothing ? ',' : deprecated_delim, args...)
if deprecated_delim !== nothing
deprecated_delim != delim && error("deprecated_delim and delim can not both be used at the same time.")
Base.depwarn("The positional `delim` keyword in the `load` function is deprecated. Instead use the keyword argument `delim`.", :CSVFiles)
end

return CSVStream(s.io, delim, args)
end

function fileio_load(s::FileIO.Stream{FileIO.format"TSV"}, deprecated_delim=nothing; delim=deprecated_delim===nothing ? '\t' : deprecated_delim, args...)
if deprecated_delim!==nothing
deprecated_delim!=delim && error("deprecated_delim and delim can not both be used at the same time.")
function fileio_load(s::FileIO.Stream{FileIO.format"TSV"}, deprecated_delim=nothing; delim=deprecated_delim === nothing ? '\t' : deprecated_delim, args...)
if deprecated_delim !== nothing
deprecated_delim != delim && error("deprecated_delim and delim can not both be used at the same time.")
Base.depwarn("The positional `delim` keyword in the `load` function is deprecated. Instead use the keyword argument `delim`.", :CSVFiles)
end

Expand Down Expand Up @@ -114,7 +114,7 @@ end

function TableTraits.get_columns_copy_using_missing(file::CSVFile)
columns, colnames = _loaddata(file)
return NamedTuple{(Symbol.(colnames)...,), Tuple{typeof.(columns)...}}((columns...,))
return NamedTuple{(Symbol.(colnames)...,),Tuple{typeof.(columns)...}}((columns...,))
end

function IteratorInterfaceExtensions.getiterator(s::CSVStream)
Expand All @@ -127,7 +127,7 @@ end

function TableTraits.get_columns_copy_using_missing(s::CSVStream)
columns, colnames = TextParse.csvread(s.io, s.delim; stringarraytype=Array, s.keywords...)
return NamedTuple{(Symbol.(colnames)...,), Tuple{typeof.(columns)...}}((columns...,))
return NamedTuple{(Symbol.(colnames)...,),Tuple{typeof.(columns)...}}((columns...,))
end

function Base.collect(x::CSVFile)
Expand Down
42 changes: 21 additions & 21 deletions src/csv_writer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ end
function _writevalue(io::IO, value::AbstractString, delim, quotechar, escapechar, nastring)
print(io, quotechar)
for c in value
if c==quotechar || c==escapechar
if c == quotechar || c == escapechar
print(io, escapechar)
end
print(io, c)
Expand All @@ -31,12 +31,12 @@ end
n = length(col_names)
push_exprs = Expr(:block)
for i in 1:n
push!(push_exprs.args, :( _writevalue(io, i.$(col_names[i]), delim, quotechar, escapechar, nastring) ))
if i<n
push!(push_exprs.args, :( print(io, delim ) ))
push!(push_exprs.args, :(_writevalue(io, i.$(col_names[i]), delim, quotechar, escapechar, nastring)))
if i < n
push!(push_exprs.args, :(print(io, delim)))
end
end
push!(push_exprs.args, :( println(io) ))
push!(push_exprs.args, :(println(io)))

quote
for i in it
Expand All @@ -52,10 +52,10 @@ function _save(io, data; delim=',', quotechar='"', escapechar='"', nastring="NA"
colnames = collect(eltype(it).parameters[1])

if header
if quotechar===nothing
join(io,[string(colname) for colname in colnames],delim)
if quotechar === nothing
join(io, [string(colname) for colname in colnames], delim)
else
join(io,["$(quotechar)" * replace(string(colname), quotechar => "$(escapechar)$(quotechar)") * "$(quotechar)" for colname in colnames],delim)
join(io, ["$(quotechar)" * replace(string(colname), quotechar => "$(escapechar)$(quotechar)") * "$(quotechar)" for colname in colnames], delim)
end
println(io)
end
Expand All @@ -69,11 +69,11 @@ function _save(filename::AbstractString, data; delim=',', quotechar='"', escapec

if ext == "gz" # Gzipped
open(GzipCompressorStream, filename, "w") do io
_save(io, data, delim=delim, quotechar=quotechar, escapechar=escapechar, nastring=nastring, header=header)
_save(io, data, delim=delim, quotechar=quotechar, escapechar=escapechar, nastring=nastring, header=header)
end
else
open(filename, "w") do io
_save(io, data, delim=delim, quotechar=quotechar, escapechar=escapechar, nastring=nastring, header=header)
_save(io, data, delim=delim, quotechar=quotechar, escapechar=escapechar, nastring=nastring, header=header)
end
end
end
Expand All @@ -99,7 +99,7 @@ end
#
# Streaming version writes header (if any) on first call, then appends on subsequent calls.
#
const CSV_or_TSV = Union{FileIO.format"CSV", FileIO.format"TSV"}
const CSV_or_TSV = Union{FileIO.format"CSV",FileIO.format"TSV"}

_delim(T) = T <: FileIO.format"CSV" ? ',' : '\t'

Expand All @@ -112,26 +112,26 @@ mutable struct CSVFileSaveStream{T}
nastring::AbstractString
header::Bool
end
function fileio_savestreaming(f::FileIO.File{T}, data=nothing; delim=_delim(T), quotechar='"', escapechar='"', nastring="NA",
header=true) where T <: CSV_or_TSV

function fileio_savestreaming(f::FileIO.File{T}, data=nothing; delim=_delim(T), quotechar='"', escapechar='"', nastring="NA",
header=true) where T<:CSV_or_TSV
io = open(f.filename, "w")

if data!==nothing
if data !== nothing
_save(io, data; delim=delim, quotechar=quotechar, escapechar=escapechar, nastring=nastring, header=header)
end

return CSVFileSaveStream(io, data!==nothing, delim, quotechar, escapechar, nastring, header)
return CSVFileSaveStream(io, data !== nothing, delim, quotechar, escapechar, nastring, header)
end

function fileio_savestreaming(s::FileIO.Stream{T}, data=nothing; delim=_delim(T), quotechar='"', escapechar='"', nastring="NA",
header=false) where T <: CSV_or_TSV
function fileio_savestreaming(s::FileIO.Stream{T}, data=nothing; delim=_delim(T), quotechar='"', escapechar='"', nastring="NA",
header=false) where T<:CSV_or_TSV

if data!==nothing
if data !== nothing
_save(s.io, data; delim=delim, quotechar=quotechar, escapechar=escapechar, nastring=nastring, header=header)
end
return CSVFileSaveStream(s.io, data!==nothing, delim, quotechar, escapechar, nastring, header)

return CSVFileSaveStream(s.io, data !== nothing, delim, quotechar, escapechar, nastring, header)
end

function Base.write(s::CSVFileSaveStream, data)
Expand Down
Loading

0 comments on commit 1b9b1f4

Please sign in to comment.