Skip to content

Commit

Permalink
Format files using DocumentFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Mar 13, 2020
1 parent aa8dfd9 commit 3895d5d
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 83 deletions.
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using Documenter, CSVFiles
makedocs(
modules = [CSVFiles],
sitename = "CSVFiles.jl",
analytics="UA-132838790-1",
analytics = "UA-132838790-1",
pages = [
"Introduction" => "index.md"
]
Expand Down
36 changes: 18 additions & 18 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 All @@ -98,9 +98,9 @@ function _loaddata(file)
if startswith(file.filename, "https://") || startswith(file.filename, "http://")
response = HTTP.get(file.filename)
data = String(response.body)
return TextParse._csvread(data, file.delim; stringarraytype=Array, file.keywords...)
return TextParse._csvread(data, file.delim; stringarraytype = Array, file.keywords...)
else
return csvread(file.filename, file.delim; stringarraytype=Array, file.keywords...)
return csvread(file.filename, file.delim; stringarraytype = Array, file.keywords...)
end
end

Expand All @@ -114,20 +114,20 @@ 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)
res = TextParse.csvread(s.io, s.delim; stringarraytype=Array, s.keywords...)
res = TextParse.csvread(s.io, s.delim; stringarraytype = Array, s.keywords...)

it = TableTraitsUtils.create_tableiterator([i for i in res[1]], [Symbol(i) for i in res[2]])

return it
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...,))
columns, colnames = TextParse.csvread(s.io, s.delim; stringarraytype = Array, s.keywords...)
return NamedTuple{(Symbol.(colnames)...,),Tuple{typeof.(columns)...}}((columns...,))
end

function Base.collect(x::CSVFile)
Expand Down
64 changes: 32 additions & 32 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 @@ -32,8 +32,8 @@ end
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 ) ))
if i < n
push!(push_exprs.args, :( print(io, delim) ))
end
end
push!(push_exprs.args, :( println(io) ))
Expand All @@ -45,61 +45,61 @@ end
end
end

function _save(io, data; delim=',', quotechar='"', escapechar='"', nastring="NA", header=true)
function _save(io, data; delim = ',', quotechar = '"', escapechar = '"', nastring = "NA", header = true)
isiterabletable(data) || error("Can't write this data to a CSV file.")

it = getiterator(data)
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
_writecsv(io, it, eltype(it), delim, quotechar, escapechar, nastring)
end

function _save(filename::AbstractString, data; delim=',', quotechar='"', escapechar='"', nastring="NA", header=true)
function _save(filename::AbstractString, data; delim = ',', quotechar = '"', escapechar = '"', nastring = "NA", header = true)
isiterabletable(data) || error("Can't write this data to a CSV file.")

ext = last(split(filename, '.'))

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



function fileio_save(f::FileIO.File{FileIO.format"CSV"}, data; delim=',', quotechar='"', escapechar='"', nastring="NA", header=true)
return _save(f.filename, data, delim=delim, quotechar=quotechar, escapechar=escapechar, nastring=nastring, header=header)
function fileio_save(f::FileIO.File{FileIO.format"CSV"}, data; delim = ',', quotechar = '"', escapechar = '"', nastring = "NA", header = true)
return _save(f.filename, data, delim = delim, quotechar = quotechar, escapechar = escapechar, nastring = nastring, header = header)
end

function fileio_save(f::FileIO.File{FileIO.format"TSV"}, data; delim='\t', quotechar='"', escapechar='"', nastring="NA", header=true)
return _save(f.filename, data, delim=delim, quotechar=quotechar, escapechar=escapechar, nastring=nastring, header=header)
function fileio_save(f::FileIO.File{FileIO.format"TSV"}, data; delim = '\t', quotechar = '"', escapechar = '"', nastring = "NA", header = true)
return _save(f.filename, data, delim = delim, quotechar = quotechar, escapechar = escapechar, nastring = nastring, header = header)
end

function fileio_save(s::FileIO.Stream{FileIO.format"CSV"}, data; delim=',', quotechar='"', escapechar='"', nastring="NA", header=true)
return _save(s.io, data, delim=delim, quotechar=quotechar, escapechar=escapechar, nastring=nastring, header=header)
function fileio_save(s::FileIO.Stream{FileIO.format"CSV"}, data; delim = ',', quotechar = '"', escapechar = '"', nastring = "NA", header = true)
return _save(s.io, data, delim = delim, quotechar = quotechar, escapechar = escapechar, nastring = nastring, header = header)
end

function fileio_save(s::FileIO.Stream{FileIO.format"TSV"}, data; delim='\t', quotechar='"', escapechar='"', nastring="NA", header=true)
return _save(s.io, data, delim=delim, quotechar=quotechar, escapechar=escapechar, nastring=nastring, header=header)
function fileio_save(s::FileIO.Stream{FileIO.format"TSV"}, data; delim = '\t', quotechar = '"', escapechar = '"', nastring = "NA", header = true)
return _save(s.io, data, delim = delim, quotechar = quotechar, escapechar = escapechar, nastring = nastring, header = header)
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,30 +112,30 @@ 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
_save(io, data; delim=delim, quotechar=quotechar, escapechar=escapechar, nastring=nastring, header=header)
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
_save(s.io, data; delim=delim, quotechar=quotechar, escapechar=escapechar, nastring=nastring, header=header)
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)
_save(s.io, data; delim=s.delim, quotechar=s.quotechar, escapechar=s.escapechar, nastring=s.nastring, header=s.first_data_written ? false : header)
_save(s.io, data; delim = s.delim, quotechar = s.quotechar, escapechar = s.escapechar, nastring = s.nastring, header = s.first_data_written ? false : header)
end

function Base.close(s::CSVFileSaveStream)
Expand Down
Loading

0 comments on commit 3895d5d

Please sign in to comment.