From b3e4ab794c0979ded641d209fbef086094fc33e9 Mon Sep 17 00:00:00 2001
From: florian <f.spreckelsen@inidscale.com>
Date: Mon, 16 Aug 2021 11:38:47 +0200
Subject: [PATCH] MAINT: Move remaining modules to separate files

---
 src/Authentication.jl |  90 +++++++++++++++
 src/CaosDB.jl         | 254 +++++++++++-------------------------------
 src/Exceptions.jl     |  99 ++++++++++++++++
 src/Info.jl           |  43 +++++++
 src/Utility.jl        |  50 +++++++++
 5 files changed, 345 insertions(+), 191 deletions(-)
 create mode 100644 src/Authentication.jl
 create mode 100644 src/Exceptions.jl
 create mode 100644 src/Info.jl
 create mode 100644 src/Utility.jl

diff --git a/src/Authentication.jl b/src/Authentication.jl
new file mode 100644
index 0000000..9051a7c
--- /dev/null
+++ b/src/Authentication.jl
@@ -0,0 +1,90 @@
+# ** header v3.0
+# This file is a part of the CaosDB Project.
+#
+# Copyright (C) 2021 Indiscale GmbH <info@indiscale.com>
+# Copyright (C) 2021 Florian Spreckelsen <f.spreckelsen@indiscale.com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public
+# License along with this program. If not, see
+# <https://www.gnu.org/licenses/>.
+#
+# ** end header
+#
+module Authentication
+
+using ..CaosDB
+
+"""
+Struct containing a pointer to the wrapped cpp authenticator
+class. Meant for internal use; call a
+`CaosDB.Authentication.create_<authenticator>` function to create an
+authenticator object from a configuration.
+"""
+mutable struct _Authenticator
+    wrapped_authenticator::Ptr{Cvoid}
+    _deletable::Bool
+
+    function _Authenticator(managed_by_julia::Bool = false)
+        auth = new()
+        auth._deletable = false
+        if managed_by_julia
+            # Only append a finalizer for this if the object is
+            # actually managed by Julia and not created and destroyed
+            # internally by libcaosdb.
+            function f(t)
+                ccall(
+                    (:caosdb_authentication_delete_authenticator, CaosDB.library_name),
+                    Cint,
+                    (Ref{_Authenticator},),
+                    Ref{_Authenticator}(t),
+                )
+            end
+            finalizer(f, auth)
+        end
+        return auth
+    end
+end
+
+"""
+    create_plain_password_authenticator(
+        username::AbstractString,
+        password::AbstractString,
+    )
+
+Return an authenticator object that contains a wrapped cpp
+plain-password authenticator configured with `username` and
+`password`.
+"""
+function create_plain_password_authenticator(
+    username::AbstractString,
+    password::AbstractString,
+)
+
+    auth = Ref{_Authenticator}(_Authenticator(true))
+
+    err_code = ccall(
+        (:caosdb_authentication_create_plain_password_authenticator, CaosDB.library_name),
+        Cint,
+        (Ref{_Authenticator}, Cstring, Cstring),
+        auth,
+        username,
+        password,
+    )
+
+    CaosDB.Exceptions.evaluate_return_code(err_code)
+
+    return auth
+
+end
+
+end # Authentication
diff --git a/src/CaosDB.jl b/src/CaosDB.jl
index cf2cc35..9d4d4ab 100644
--- a/src/CaosDB.jl
+++ b/src/CaosDB.jl
@@ -24,6 +24,62 @@
 
 module CaosDB
 
+# Exports from module Exceptions
+export evaluate_return_code,
+    CaosDBException, ClientException, GenericCaosDBException, CaosDBMessage
+
+# Exports from module Utility
+export get_env_var
+
+# Export from module Connection
+export connect, connect_manually
+
+# Exports from module Entity
+# Creators
+export create_entity,
+    create_parent, create_property, create_property_entity, create_record, create_recordtype
+
+# getters
+export get_id,
+    get_role,
+    get_name,
+    get_description,
+    get_datatype,
+    get_unit,
+    get_value,
+    get_version_id,
+    get_property,
+    get_properties,
+    get_parent,
+    get_parents,
+    get_error,
+    get_errors,
+    get_warning,
+    get_warnings,
+    get_info,
+    get_infos,
+    get_importance,
+    get_code
+
+# setters
+export append_parent,
+    append_parents,
+    append_property,
+    append_properties,
+    remove_parent,
+    remove_property,
+    set_id,
+    set_role,
+    set_name,
+    set_description,
+    set_datatype,
+    set_unit,
+    set_value,
+    set_importance
+
+# Exports from module Transaction
+export create_transaction, add_retrieve_by_id, add_query
+
 using Libdl
 
 """
@@ -34,201 +90,15 @@ if isempty(find_library(library_name))
     @error "Could not find $library_name"
 end
 
-module Exceptions
+# include modules from other files
 
-export evaluate_return_code, CaosDBException, GenericCaosDBException, CaosDBMessage
+include("Exceptions.jl")
 
-using Logging
-using CaosDB
+include("Info.jl")
 
-"""
-The parent type of all CaosDB errors that can also be used for testing.
-"""
-abstract type CaosDBException <: Exception end
+include("Utility.jl")
 
-"""
-A generic exception that will be raised in case of non-zero return
-values of the calls to libccaosdb. May carry a message string and a
-code.
-"""
-struct GenericCaosDBException <: CaosDBException
-    msg::String
-    code::Cint
-end
-
-"""
-Something went wrong on the client-side or the user is attempting to
-conduct an invalid operation.
-"""
-struct ClientException <: CaosDBException
-    msg::String
-    code::Cint
-
-    function ClientException(message::AbstractString)
-        client_error_code =
-            ccall((:caosdb_status_code_OTHER_CLIENT_ERROR, CaosDB.library_name), Cint, ())
-        new(message, client_error_code)
-    end
-end
-
-
-Base.showerror(io::IO, e::CaosDBException) =
-    print(io, "CaosDBException: ", e.msg, " (Status code ", e.code, ")")
-
-"""
-Struct containing Messages and codes for status codes<0 that do not
-correspond to errors or success.
-"""
-struct CaosDBMessage
-    msg::String
-    code::Cint
-end
-
-Base.show(io::IO, m::CaosDBMessage) = print(io, m.msg, " (Status code ", m.code, ")")
-
-"""
-    function evaluate_return_code(code::Cint)
-
-Evaluate the return code of a libccaosdb ccall and raise a
-`GenericCaosDBException` in case of a non-zero return code.
-"""
-function evaluate_return_code(code::Cint)
-    if code != 0
-        msg = ccall(
-            (:caosdb_get_status_description, CaosDB.library_name),
-            Cstring,
-            (Cint,),
-            code,
-        )
-        if code > 0
-            throw(GenericCaosDBException(unsafe_string(msg), code))
-        else
-            @info CaosDBMessage(unsafe_string(msg), code)
-        end
-    end
-end
-
-end # Exceptions
-
-module Info
-
-"""
-Struct containing version information of the CaosDB server. Meant
-mainly for internal usage; use `CaosDB.Connection.get_version_info` or
-`CaosDB.Connection.print_version_info` to retrieve the version of the
-connected CaosDB server.
-"""
-mutable struct _VersionInfo
-
-    major::Cint
-    minor::Cint
-    patch::Cint
-    pre_release::Cstring
-    build::Cstring
-
-    _VersionInfo() = new()
-
-end
-
-end # Info
-
-module Utility
-
-export get_env_var
-
-using ..CaosDB
-
-"""
-    get_env_var(var[, default])
-
-Return the environmental variable `var` if it exists, `default`
-otherwise. If no `default` is given an empty string is returned
-instead.
-"""
-function get_env_var(var::AbstractString, default::AbstractString = "")
-
-    ret = ccall(
-        (:caosdb_utility_get_env_var, CaosDB.library_name),
-        Cstring,
-        (Cstring, Cstring),
-        var,
-        default,
-    )
-
-    return unsafe_string(ret)
-
-end
-
-end # Utility
-
-module Authentication
-
-using ..CaosDB
-
-"""
-Struct containing a pointer to the wrapped cpp authenticator
-class. Meant for internal use; call a
-`CaosDB.Authentication.create_<authenticator>` function to create an
-authenticator object from a configuration.
-"""
-mutable struct _Authenticator
-    wrapped_authenticator::Ptr{Cvoid}
-    _deletable::Bool
-
-    function _Authenticator(managed_by_julia::Bool = false)
-        auth = new()
-        auth._deletable = false
-        if managed_by_julia
-            # Only append a finalizer for this if the object is
-            # actually managed by Julia and not created and destroyed
-            # internally by libcaosdb.
-            function f(t)
-                ccall(
-                    (:caosdb_authentication_delete_authenticator, CaosDB.library_name),
-                    Cint,
-                    (Ref{_Authenticator},),
-                    Ref{_Authenticator}(t),
-                )
-            end
-            finalizer(f, auth)
-        end
-        return auth
-    end
-end
-
-"""
-    create_plain_password_authenticator(
-        username::AbstractString,
-        password::AbstractString,
-    )
-
-Return an authenticator object that contains a wrapped cpp
-plain-password authenticator configured with `username` and
-`password`.
-"""
-function create_plain_password_authenticator(
-    username::AbstractString,
-    password::AbstractString,
-)
-
-    auth = Ref{_Authenticator}(_Authenticator(true))
-
-    err_code = ccall(
-        (:caosdb_authentication_create_plain_password_authenticator, CaosDB.library_name),
-        Cint,
-        (Ref{_Authenticator}, Cstring, Cstring),
-        auth,
-        username,
-        password,
-    )
-
-    CaosDB.Exceptions.evaluate_return_code(err_code)
-
-    return auth
-
-end
-
-end # Authentication
+include("Authentication.jl")
 
 include("Connection.jl")
 
@@ -236,4 +106,6 @@ include("Entity.jl")
 
 include("Transaction.jl")
 
+using .Exceptions, .Info, .Authentication, .Connection, .Utility, .Entity, .Transaction
+
 end # CaosDB
diff --git a/src/Exceptions.jl b/src/Exceptions.jl
new file mode 100644
index 0000000..3ffd0ee
--- /dev/null
+++ b/src/Exceptions.jl
@@ -0,0 +1,99 @@
+# ** header v3.0
+# This file is a part of the CaosDB Project.
+#
+# Copyright (C) 2021 Indiscale GmbH <info@indiscale.com>
+# Copyright (C) 2021 Florian Spreckelsen <f.spreckelsen@indiscale.com>
+# Copyright (C) 2021 Alexander Kreft
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public
+# License along with this program. If not, see
+# <https://www.gnu.org/licenses/>.
+#
+# ** end header
+#
+module Exceptions
+
+export evaluate_return_code,
+    CaosDBException, ClientException, GenericCaosDBException, CaosDBMessage
+
+using Logging
+using CaosDB
+
+"""
+The parent type of all CaosDB errors that can also be used for testing.
+"""
+abstract type CaosDBException <: Exception end
+
+"""
+A generic exception that will be raised in case of non-zero return
+values of the calls to libccaosdb. May carry a message string and a
+code.
+"""
+struct GenericCaosDBException <: CaosDBException
+    msg::String
+    code::Cint
+end
+
+"""
+Something went wrong on the client-side or the user is attempting to
+conduct an invalid operation.
+"""
+struct ClientException <: CaosDBException
+    msg::String
+    code::Cint
+
+    function ClientException(message::AbstractString)
+        client_error_code =
+            ccall((:caosdb_status_code_OTHER_CLIENT_ERROR, CaosDB.library_name), Cint, ())
+        new(message, client_error_code)
+    end
+end
+
+
+Base.showerror(io::IO, e::CaosDBException) =
+    print(io, "CaosDBException: ", e.msg, " (Status code ", e.code, ")")
+
+"""
+Struct containing Messages and codes for status codes<0 that do not
+correspond to errors or success.
+"""
+struct CaosDBMessage
+    msg::String
+    code::Cint
+end
+
+Base.show(io::IO, m::CaosDBMessage) = print(io, m.msg, " (Status code ", m.code, ")")
+
+"""
+    function evaluate_return_code(code::Cint)
+
+Evaluate the return code of a libccaosdb ccall and raise a
+`GenericCaosDBException` in case of a non-zero return code.
+"""
+function evaluate_return_code(code::Cint)
+    if code != 0
+        msg = ccall(
+            (:caosdb_get_status_description, CaosDB.library_name),
+            Cstring,
+            (Cint,),
+            code,
+        )
+        if code > 0
+            throw(GenericCaosDBException(unsafe_string(msg), code))
+        else
+            @info CaosDBMessage(unsafe_string(msg), code)
+        end
+    end
+end
+
+end # Exceptions
diff --git a/src/Info.jl b/src/Info.jl
new file mode 100644
index 0000000..8eaf328
--- /dev/null
+++ b/src/Info.jl
@@ -0,0 +1,43 @@
+# ** header v3.0
+# This file is a part of the CaosDB Project.
+#
+# Copyright (C) 2021 Indiscale GmbH <info@indiscale.com>
+# Copyright (C) 2021 Florian Spreckelsen <f.spreckelsen@indiscale.com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public
+# License along with this program. If not, see
+# <https://www.gnu.org/licenses/>.
+#
+# ** end header
+#
+module Info
+
+"""
+Struct containing version information of the CaosDB server. Meant
+mainly for internal usage; use `CaosDB.Connection.get_version_info` or
+`CaosDB.Connection.print_version_info` to retrieve the version of the
+connected CaosDB server.
+"""
+mutable struct _VersionInfo
+
+    major::Cint
+    minor::Cint
+    patch::Cint
+    pre_release::Cstring
+    build::Cstring
+
+    _VersionInfo() = new()
+
+end
+
+end # Info
diff --git a/src/Utility.jl b/src/Utility.jl
new file mode 100644
index 0000000..289d666
--- /dev/null
+++ b/src/Utility.jl
@@ -0,0 +1,50 @@
+# ** header v3.0
+# This file is a part of the CaosDB Project.
+#
+# Copyright (C) 2021 Indiscale GmbH <info@indiscale.com>
+# Copyright (C) 2021 Florian Spreckelsen <f.spreckelsen@indiscale.com>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public
+# License along with this program. If not, see
+# <https://www.gnu.org/licenses/>.
+#
+# ** end header
+#
+module Utility
+
+export get_env_var
+
+using ..CaosDB
+
+"""
+    get_env_var(var[, default])
+
+Return the environmental variable `var` if it exists, `default`
+otherwise. If no `default` is given an empty string is returned
+instead.
+"""
+function get_env_var(var::AbstractString, default::AbstractString = "")
+
+    ret = ccall(
+        (:caosdb_utility_get_env_var, CaosDB.library_name),
+        Cstring,
+        (Cstring, Cstring),
+        var,
+        default,
+    )
+
+    return unsafe_string(ret)
+
+end
+
+end # Utility
-- 
GitLab