<commit_before>%% Copyright 2009-2010 <NAME>.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% @author <NAME> <<EMAIL>>
%% @copyright 2009-2010 <NAME>.

-module(similar_config).
-behaviour(gen_server).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
        terminate/2, code_change/3]).

-export([start_link/0]).

-include("similar.hrl").

start_link() ->
	gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

%%====================================================================
%% gen_server callbacks
%%====================================================================

%%--------------------------------------------------------------------
%% Function: init(Args) -> {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%% Description: Initiates the server
%%--------------------------------------------------------------------
init([]) ->
	EtcDir = get_etc_dir(),
	ConfigFile = filename:join(EtcDir, "similar.config"),
	Exists = filelib:is_regular(ConfigFile),
	Dict = case Exists of
		false ->
			similar_log:info("No configuration file named [~p]! Defaulted!", [ConfigFile]),
			get_default_configuration();
		true  ->
			similar_log:info("Reading configuration file named [~p]!", [ConfigFile]),
			read_configuration(ConfigFile)
	end,
	similar_log:info("Similar Config with ~p entries!", [dict:size(Dict)]),
	{ok, Dict}.

read_configuration(ConfigFile) ->
	List = case parse_file(ConfigFile) of
		{error, Why} ->
			throw({error, Why});
		{ok, Terms} ->
			Terms
	end,
	Dict = dict:from_list(List),
	Dict.

%% TODO for now just an empty set
get_default_configuration() ->
	dict:new().

%% TODO probably a bad idea to fall back to /tmp for security reason
get_etc_dir() ->
    {ok, CurrentDir} = file:get_cwd(),
    Candidate = filename:join(CurrentDir, "etc"),
    IsDir = filelib:is_dir(Candidate),
    DataDir = if IsDir ->
        Candidate;
    true ->
        "/tmp"
    end,
    DataDir.

%%--------------------------------------------------------------------
%% Function:
%% handle_call(Request, From, State) -> {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} |
%% {stop, Reason, State}
%% Description: Handling call messages
%%--------------------------------------------------------------------
handle_call(_Args, _From, State) ->
	{reply, ok, State}.

%%--------------------------------------------------------------------
%% Function: handle_cast(Msg, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% Description: Handling cast messages
%%--------------------------------------------------------------------<commit_msg>Add functions to retrieve and set configuration items (key/value pairs).<commit_after>%% Copyright 2009-2010 <NAME>.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% @author <NAME> <<EMAIL>>
%% @copyright 2009-2010 <NAME>.

-module(similar_config).
-behaviour(gen_server).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
        terminate/2, code_change/3]).

-export([start_link/0, get/1, set/2]).

-include("similar.hrl").

start_link() ->
	gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

get(Key) ->
	gen_server:call(?MODULE, {get, Key}).

set(Key, Value) ->
	gen_server:call(?MODULE, {set, Key, Value}).

%%====================================================================
%% gen_server callbacks
%%====================================================================

%%--------------------------------------------------------------------
%% Function: init(Args) -> {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%% Description: Initiates the server
%%--------------------------------------------------------------------
init([]) ->
	EtcDir = get_etc_dir(),
	ConfigFile = filename:join(EtcDir, "similar.config"),
	Exists = filelib:is_regular(ConfigFile),
	Dict = case Exists of
		false ->
			similar_log:info("No configuration file named [~p]! Defaulted!", [ConfigFile]),
			get_default_configuration();
		true  ->
			similar_log:info("Reading configuration file named [~p]!", [ConfigFile]),
			read_configuration(ConfigFile)
	end,
	similar_log:info("Similar Config with ~p entries!", [dict:size(Dict)]),
	{ok, Dict}.

read_configuration(ConfigFile) ->
	List = case parse_file(ConfigFile) of
		{error, Why} ->
			throw({error, Why});
		{ok, Terms} ->
			Terms
	end,
	Dict = dict:from_list(List),
	Dict.

%% TODO for now just an empty set
get_default_configuration() ->
	dict:new().

%% TODO probably a bad idea to fall back to /tmp for security reason
get_etc_dir() ->
    {ok, CurrentDir} = file:get_cwd(),
    Candidate = filename:join(CurrentDir, "etc"),
    IsDir = filelib:is_dir(Candidate),
    DataDir = if IsDir ->
        Candidate;
    true ->
        "/tmp"
    end,
    DataDir.

%%--------------------------------------------------------------------
%% Function:
%% handle_call(Request, From, State) -> {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} |
%% {stop, Reason, State}
%% Description: Handling call messages
%%--------------------------------------------------------------------
handle_call({get, Key}, _From, State) ->
	Value = try dict:fetch(Key, State) catch _:_ -> nil end,
	{reply, {ok, Value}, State};

handle_call({set, Key, Value}, _From, State) ->
	UpdatedDict = dict:store(Key, Value, State),
	{reply, ok, UpdatedDict};

handle_call(_Args, _From, State) ->
	{reply, ok, State}.

%%--------------------------------------------------------------------
%% Function: handle_cast(Msg, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% Description: Handling cast messages
%%--------------------------------------------------------------------
{'fraction_non_alphanumeric': 0.2936017253774263, 'fraction_numerical': 0.00805176132278936, 'mean_word_length': 2.641884816753927, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3588795', 'n_tokens_mistral': 2053, 'n_tokens_neox': 1930, 'n_words': 570}