1:- module(bitrix24_config, [
    2              load_config/1,
    3              config/3,
    4              app_info/2,
    5              portal_app_info/3,
    6              assert_portal_app_info/3,
    7              retractall_portal_app_info/3,
    8              open_db/1,
    9              catalog_app/1
   10          ]).   11
   12:- use_module(library(persistency)).   13
   14 :- dynamic config/3.   15
   16:- persistent
   17  app_info(key:atom, value:any),
   18  portal_app_info(member_id:atom, key:atom, value:any).
   19
   20:- setting(database, callable, 'app_info.db',
   21           "Данные приложения полученные при установке").   22
   23load_config(Dir) :-
   24    atom_concat(Dir, '{*.yaml}', TemplateFiles),
   25    expand_file_name(TemplateFiles, Files),
   26    ( length(Files, Len), Len \== 0
   27    ->
   28      load_yaml_config(Files)
   29      ;  throw(error(config,
   30                    context(Dir),
   31                    'Отсутствуют конфигурационные файлы'))
   32    ).
   33
   34load_yaml_config(Fs) :-
   35    forall(member(F, Fs),
   36           (
   37               yaml_read(F, Yaml),
   38               dict_pairs(Yaml, _Tag, Xs),
   39               save_config(Xs)
   40           )).
   41
   42save_config([]).
   43save_config([Tag-Yaml|Xs]) :-
   44    dict_pairs(Yaml, _, Ys),
   45    forall(member(Key - Value, Ys),
   46           (normalize_config_value(Value, AValue),
   47           retractall(config(Tag, Key, _)),
   48           assertz(config(Tag, Key, AValue))
   49          )
   50        ),
   51    save_config(Xs).
   52
   53normalize_config_value(Value, AtomValue) :-
   54    string(Value),
   55    !,
   56    string_to_atom(Value, AtomValue).
   57normalize_config_value([], []) :-
   58    !.
   59normalize_config_value([X|Xs], [Y|Ys]) :-
   60    !,
   61    normalize_config_value(X, Y),
   62    normalize_config_value(Xs, Ys).
   63normalize_config_value(Value, Value).
   64
   65catalog_app(Dir) :-
   66    exists_directory(Dir).
   67
   68catalog_app(Dir) :-
   69    make_directory(Dir).
   70
   71open_db(_) :-
   72    db_attached(_),
   73    !.
   74open_db(Dir) :-
   75    setting(database, Spec),
   76    atom_concat(Dir, Spec, PathFile),
   77    db_attach(PathFile, [sync(close)]), !