Skip to content

Commit

Permalink
Removed FilePath dependency.
Browse files Browse the repository at this point in the history
FilePaths is no longer used to enforce distinction between a
String and a filesystem location.  New method 'cif_from_string'
should be used where the string content is in CIF format. The
CIF constructor will still work when passed a FilePath type.
  • Loading branch information
James.Hester committed Oct 10, 2024
1 parent 13d9794 commit fa51881
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 112 deletions.
4 changes: 1 addition & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
name = "CrystalInfoFramework"
uuid = "6007d9b0-c6b2-11e8-0510-1d10e825f3f1"
authors = ["James.Hester <[email protected]>"]
version = "0.6.3"
version = "0.7.0"

[deps]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
FilePaths = "8fc22ac5-c921-52a6-82fd-178b2807b824"
Lerche = "d42ef402-04e6-4356-9f73-091573ea58dc"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Expand All @@ -16,7 +15,6 @@ cif_api_jll = "6fcef0ae-1c05-5fc1-b206-1cf994addbad"

[compat]
DataFrames = "^1.1.0"
FilePaths = "^0.8.0"
Lerche = "^0.5.0"
PrecompileTools = "1.2.0"
URIs = "1.5.0"
Expand Down
6 changes: 3 additions & 3 deletions src/CrystalInfoFramework.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
module CrystalInfoFramework
using DataFrames
using URIs
using FilePaths #easy cross-platform URI
using Lerche # for native parser
using cif_api_jll # for cif API parser
using PrecompileTools #for fast startup

# **Exports**

export CifValue,Cif,Block,CifBlock
export cif_from_string
export CifContainer, NestedCifContainer
export get_frames,get_contents
export get_loop, eachrow, add_to_loop!, create_loop!
Expand Down Expand Up @@ -81,8 +81,8 @@ end

#
@compile_workload begin
c = Cif(joinpath(@__PATH__,"../test/nick1.cif"), native=true)
d = DDLm_Dictionary(joinpath(@__PATH__,"../test/ddl.dic"), ignore_imports=true)
c = Cif(joinpath(@__DIR__, "../test/nick1.cif"), native=true)
d = DDLm_Dictionary(joinpath(@__DIR__, "../test/ddl.dic"), ignore_imports=true)
end

end
2 changes: 1 addition & 1 deletion src/cif2_transformer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ already been processed.
==#

struct TreeToCif <: Transformer
source_name::AbstractPath
source_name::AbstractString
header_comments::AbstractString
end

Expand Down
34 changes: 17 additions & 17 deletions src/cif_base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ A CIF data block or save frame containing no nested save frames.
mutable struct Block{V} <: CifContainer{V}
loop_names::Vector{Vector{String}} #one loop is a list of datanames
data_values::Dict{String,Vector{V}}
original_file::AbstractPath
original_file::AbstractString
end

Block{V}() where V = begin
Block(Vector{String}[],Dict{String,Vector{V}}(),p"")
Block(Vector{String}[],Dict{String,Vector{V}}(),"")
end

"""
Expand All @@ -205,7 +205,7 @@ mutable struct CifBlock{V} <: NestedCifContainer{V}
save_frames::Dict{String,Block{V}}
loop_names::Vector{Vector{String}} #one loop is a list of datanames
data_values::Dict{String,Vector{V}}
original_file::AbstractPath
original_file::AbstractString
end

Block(f::CifBlock) = Block(get_loop_names(f),get_data_values(f),get_source_file(f))
Expand Down Expand Up @@ -356,12 +356,12 @@ recording the source of the collection.
"""
struct Cif{V,T <: CifContainer{V}} <: CifCollection{V}
contents::Dict{String,T}
original_file::AbstractPath
original_file::AbstractString
header_comments::String
end

Cif{V,T}() where V where T = begin
return Cif(Dict{String,T}(), p"", "")
return Cif(Dict{String,T}(), "", "")
end

Cif() = Cif{ CifValue, CifBlock{CifValue} }
Expand Down Expand Up @@ -472,7 +472,7 @@ get_frames(f::CifBlock{V}) where V = Cif{V,Block{V}}(f.save_frames,get_source_fi
mutable struct cif_builder_context
actual_cif::Dict{String,CifContainer{CifValue}}
block_stack::Array{CifContainer{CifValue}}
filename::AbstractPath
filename::AbstractString
verbose::Bool
end

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

# This sets up the callbacks and configures the cifapi parser.

default_options(s::AbstractPath;verbose=false) = begin
default_options(s::AbstractString;verbose=false) = begin
handle_cif_start_c = @cfunction(handle_cif_start,Cint,(cif_tp_ptr,Ref{cif_builder_context}))
handle_cif_end_c = @cfunction(handle_cif_end,Cint,(cif_tp_ptr,Ref{cif_builder_context}))
handle_block_start_c = @cfunction(handle_block_start,Cint,(cif_container_tp_ptr,Ref{cif_builder_context}))
Expand Down Expand Up @@ -693,7 +693,7 @@ default_options(s::AbstractPath;verbose=false) = begin
end

"""
Cif(s::AbstractPath;verbose=false,native=true,version=0)
Cif(somepath; verbose=false, native=true, version=0)
Read in filename `s` as a CIF file. If `verbose` is true, print
progress information during parsing. If `native` is `false`, use the
Expand All @@ -711,33 +711,33 @@ uses 6-10 times as much memory. However, from a cold start
(e.g. in a standalone script) the cif_api parser is around
30% faster.
"""
Cif(s::AbstractPath;verbose=false,native=true,version=0) = begin
## get the full filename
full = realpath(s)
Cif(somepath; verbose=false, native=true, version=0) = begin
## get the full filename and make sure we have a string.
full = convert(String, realpath(somepath))
if !Sys.iswindows() && !native
pathstring = URI(full).path
p_opts = default_options(full,verbose=verbose)
result = cif_tp_ptr(p_opts)
## the real result is in our user data context
return Cif(p_opts.user_data.actual_cif,full,"")
return Cif(p_opts.user_data.actual_cif, full ,"")
end
if Sys.iswindows() && !native
@debug "Did not use cif_api on Windows"
end
full_contents = read(full,String)
Cif(full_contents,verbose=verbose,version=version,source=full)
full_contents = read(full, String)
cif_from_string(full_contents, verbose=verbose, version=version, source=full)
end

"""
Cif(s::AbstractString;verbose=false,version=0,source=p"")
cif_from_string(s::AbstractString; verbose=false, version=0, source="")
Process contents of `s` as a CIF file using the native Julia parser.
Process `s` as the text of a CIF file using the native Julia parser.
If `verbose` is true, print progress information during parsing.
`version` may be `1`, `2` or `0` (default) for auto-detected CIF
version. If `source` is provided, it is a filesystem location to
record as the source for `s`.
"""
Cif(s::AbstractString;verbose=false,version=0,source=p"") = begin
cif_from_string(s::AbstractString; verbose=false, version=0, source="") = begin
if length(s) > 1 && s[1] == '\ufeff'
s = s[(nextind(s,1)):end]
end
Expand Down
5 changes: 2 additions & 3 deletions src/ddl2_dictionary_ng.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ DDL2_Dictionary(c::Cif) = begin
end

"""
DDL2_Dictionary(a::AbstractPath;verbose=false)
DDL2_Dictionary(a; verbose=false)
Create a `DDL2_Dictionary` given filename `a`. `verbose = true` will print
extra debugging information during reading.
"""
DDL2_Dictionary(a::AbstractPath;verbose=false) = DDL2_Dictionary(Cif(a,verbose=verbose))
DDL2_Dictionary(a::String;verbose=false) = DDL2_Dictionary(Cif(Path(a),verbose=verbose))
DDL2_Dictionary(a; verbose=false) = DDL2_Dictionary(Cif(a,verbose=verbose))

DDL2_Dictionary(b::CifBlock,blockname::AbstractString) = begin
all_dict_info = Dict{Symbol,DataFrame}()
Expand Down
53 changes: 11 additions & 42 deletions src/ddlm_dictionary_ng.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ DDLm_Dictionary(c::Cif;kwargs...) = begin
end

"""
DDLm_Dictionary(a::AbstractPath;verbose=false,ignore_imports="None",
DDLm_Dictionary(a; verbose=false, ignore_imports="None",
cache_imports=false)
Create a `DDLm_Dictionary` given filename `a`. `verbose = true` will print
Expand All @@ -121,15 +121,11 @@ unless `import_dir` is specified, in which case the search is relative to
that directory.
"""
DDLm_Dictionary(a::AbstractPath;verbose=false,kwargs...) = begin
c = Cif(a,verbose=verbose,native=true) #Native to catch header comments
DDLm_Dictionary(a; verbose=false, kwargs...) = begin
c = Cif(a, verbose=verbose, native=true) #Native to catch header comments
DDLm_Dictionary(c;kwargs...)
end

DDLm_Dictionary(a::String;kwargs...) = begin
DDLm_Dictionary(Path(a);kwargs...)
end

DDLm_Dictionary(b::CifBlock;ignore_imports=:None,header="",cache_imports=true,import_dir="") = begin
all_dict_info = Dict{Symbol,DataFrame}()
# Namespace
Expand Down Expand Up @@ -1325,57 +1321,30 @@ const ddlm_categories = [
Turn a possibly relative URL into an absolute one. Will probably fail if file
component starts with "."
"""
fix_url(s::String,parent) = begin
fix_url(s::String, parent) = begin
scheme = match(r"^[a-zA-Z]+:",s)
if scheme == nothing
if s[1]=='/'
return URI(Path(s))
return URI(path = s, scheme = "file")
elseif s[1]=="." # really shouldn't accept this
return URI(Path(joinpath(parent,s)))
return URI(path = joinpath(parent,s), scheme = "file")
else
return URI(Path(joinpath(parent,s)))
return URI(path = joinpath(parent,s), scheme = "file")
end
end
return URI(s)
end

# Following code copied from URIs/uris.jl. For some reason
# this function was not recognised during precompilation

const absent = SubString("absent", 1, 0)

function URIs.URI(p::AbstractPath; query=absent, fragment=absent)
if isempty(p.root)
throw(ArgumentError("$p is not an absolute path"))
end

b = IOBuffer()
print(b, "file://")

if !isempty(p.drive)
print(b, "/")
print(b, p.drive)
end

for s in p.segments
print(b, "/")
print(b, URIs.escapeuri(s))
end

return URIs.URI(URIs.URI(String(take!(b))); query=query, fragment=fragment)
end

"""
to_path(::URI)
Convert a file: URI to a Path.
Really belongs in FilePaths.jl but for now this will work.
Convert a file: URI to a path.
"""
to_path(u::URI) = begin
if Sys.iswindows()
Path(unescapeuri(u.path[2:end]))
unescapeuri(u.path[2:end])
else
Path(unescapeuri(u.path))
unescapeuri(u.path)
end
end

Expand Down Expand Up @@ -1657,7 +1626,7 @@ check_import_block(d::DDLm_Dictionary,name,cat,obj,val) = begin
spec = x[:import].get[]
for one_spec in spec
if get(one_spec,"mode","Contents") == "Full" continue end
templ_file_name = joinpath(Path(d.import_dir), one_spec["file"])
templ_file_name = joinpath(d.import_dir, one_spec["file"])
if !(templ_file_name in keys(d.cached_imports))
println("Warning: cannot find $templ_file_name when checking imports for $name")
continue
Expand Down
1 change: 0 additions & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[deps]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
FilePaths = "8fc22ac5-c921-52a6-82fd-178b2807b824"
Lerche = "d42ef402-04e6-4356-9f73-091573ea58dc"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
3 changes: 1 addition & 2 deletions test/creation.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Test CIF object creation and destruction
# Now adapted for NativeCif

testdir = @__PATH__
testdir = @__DIR__

@testset "Test simple CIF creation and destruction" begin

Expand Down
8 changes: 4 additions & 4 deletions test/data_and_dictionaries.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Test combinations of data and dictionaries

prepare_dd() = begin
t = DDLm_Dictionary(joinpath(@__PATH__,"cif_core.dic"))
n = first(Cif(joinpath(@__PATH__,"nick1.cif"))).second
t = DDLm_Dictionary(joinpath(@__DIR__,"cif_core.dic"))
n = first(Cif(joinpath(@__DIR__,"nick1.cif"))).second
return t,n
end

Expand Down Expand Up @@ -40,8 +40,8 @@ is_looped(b, n) = any(x -> n in x, CrystalInfoFramework.get_loop_names(b))
end

@testset "Merging blocks" begin
t = DDLm_Dictionary(joinpath(@__PATH__,"cif_core.dic"))
n = Cif(joinpath(@__PATH__,"nick1_mergeable.cif"))
t = DDLm_Dictionary(joinpath(@__DIR__,"cif_core.dic"))
n = Cif(joinpath(@__DIR__,"nick1_mergeable.cif"))
println("About to merge blocks")
merge_blocks!(n,t)
f = first(n).second
Expand Down
6 changes: 3 additions & 3 deletions test/dc_base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ using Test

# Test a plain CIF as data source

const cif_test_file = joinpath(@__PATH__,"nick1.cif")
const multi_block_test_file = joinpath(@__PATH__,"cif_img_1.7.11.dic")
const core_dic = joinpath(@__PATH__,"cif_core.dic")
const cif_test_file = joinpath(@__DIR__,"nick1.cif")
const multi_block_test_file = joinpath(@__DIR__,"cif_img_1.7.11.dic")
const core_dic = joinpath(@__DIR__,"cif_core.dic")

prepare_files() = begin
c = Cif(cif_test_file)
Expand Down
Loading

0 comments on commit fa51881

Please sign in to comment.