Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/easier plugin creation #31

Open
wants to merge 2 commits into
base: beatrice
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libraries/app/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ namespace graphene { namespace app {
if( _app.get_plugin( "affiliate_stats" ) )
_affiliate_stats_api = std::make_shared<graphene::affiliate_stats::affiliate_stats_api>(std::ref(_app));
}
#include <graphene/utilities/generated_api_cpp_case_code.hpp>

return;
}

Expand Down Expand Up @@ -349,6 +351,7 @@ namespace graphene { namespace app {
FC_ASSERT(_affiliate_stats_api);
return *_affiliate_stats_api;
}
#include <graphene/utilities/generated_api_cpp_login_api_code.hpp>

#if 0
vector<account_id_type> get_relevant_accounts( const object* obj )
Expand Down
2 changes: 2 additions & 0 deletions libraries/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ namespace detail {
wild_access.allowed_apis.push_back( "crypto_api" );
wild_access.allowed_apis.push_back( "bookie_api" );
wild_access.allowed_apis.push_back( "affiliate_stats_api" );
#include <graphene/utilities/generated_application_cpp_allowed_apis_code.hpp>

_apiaccess.permission_map["*"] = wild_access;
}

Expand Down
3 changes: 3 additions & 0 deletions libraries/app/include/graphene/app/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <graphene/debug_witness/debug_api.hpp>
#include <graphene/affiliate_stats/affiliate_stats_api.hpp>
#include <graphene/bookie/bookie_api.hpp>
#include <graphene/utilities/generated_api_hpp_include_code.hpp>

#include <graphene/net/node.hpp>

Expand Down Expand Up @@ -385,6 +386,7 @@ namespace graphene { namespace app {
fc::api<graphene::bookie::bookie_api> bookie()const;
/// @brief Retrieve the affiliate_stats API (if available)
fc::api<graphene::affiliate_stats::affiliate_stats_api> affiliate_stats()const;
#include <graphene/utilities/generated_api_hpp_login_api_method_def_code.hpp>

/// @brief Called to enable an API, not reflected.
void enable_api( const string& api_name );
Expand All @@ -401,6 +403,7 @@ namespace graphene { namespace app {
optional< fc::api<graphene::debug_witness::debug_api> > _debug_api;
optional< fc::api<graphene::bookie::bookie_api> > _bookie_api;
optional< fc::api<graphene::affiliate_stats::affiliate_stats_api> > _affiliate_stats_api;
#include <graphene/utilities/generated_api_hpp_login_api_apiobj_code.hpp>
};

}} // graphene::app
Expand Down
20 changes: 9 additions & 11 deletions libraries/plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
add_subdirectory( witness )
add_subdirectory( account_history )
add_subdirectory( accounts_list )
add_subdirectory( affiliate_stats )
add_subdirectory( market_history )
add_subdirectory( delayed_node )
add_subdirectory( bookie )
add_subdirectory( generate_genesis )
add_subdirectory( generate_uia_sharedrop_genesis )
add_subdirectory( debug_witness )
add_subdirectory( snapshot )
# for each subdirectory containing a CMakeLists.txt, add that subdirectory
file( GLOB children RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} * )
foreach( child ${children} )
if( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${child}" )
if( EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${child}/CMakeLists.txt" )
add_subdirectory( "${child}" )
endif()
endif()
endforeach()
6 changes: 6 additions & 0 deletions libraries/plugins/TestPlugin1/plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"plugin_name": "TestPlugin1",
"plugin_namespace": "TestPlugin1",
"plugin_project": "TestPlugin1_plugin"
}

6 changes: 6 additions & 0 deletions libraries/plugins/TestPlugin2/plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"plugin_name": "TestPlugin2",
"plugin_namespace": "TestPlugin2",
"plugin_project": "TestPlugin2_plugin"
}

8 changes: 8 additions & 0 deletions libraries/utilities/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Executes Python script to generate various files related to plugins
include(FindPythonInterp)
set (plugin_code_generator "libraries/utilities/plugin_code_generator.py")
execute_process(
COMMAND ${PYTHON_EXECUTABLE} ${plugin_code_generator}
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
)

list( APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/libraries/fc/GitVersionGen" )
include( GetGitRevisionDescription )
get_git_head_revision(GIT_REFSPEC GRAPHENE_GIT_REVISION_SHA)
Expand Down
138 changes: 138 additions & 0 deletions libraries/utilities/plugin_code_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#!/usr/bin/python

# Copyright (c) 2018 Blockchain B.V.
#
# The MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

#################################################################################
# This script is called everytime cmake is called. It generates code to certain #
# files which are related to the creation of plugins. #
# The working directory of the script is the project root directory. #
# Script is called from libraries/utilities/CMakeLists.txt #
#################################################################################


import json
import os


# list of files where generated code is used (relative to project root)
# libraries/app/include/graphene/api.hpp
# libraries/app/api.cpp
# libraries/app/application.cpp

generated_files_root_dir = "libraries/utilities/include/graphene/utilities/"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generated files are not really headers (except generated_api_hpp_include_code.hpp maybe), therefore they shouldn't be put into the include directory. Also don't use .hpp for them, perhaps .hxx or .cxx depending on content.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will change that for more clearance.

But generally would you say this is way of solving this issue? I think it looks kinda weird to do includes in the middle of the code. But I heard in some projects this is common. Still wouldn't call it good practice.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I wouldn't call it beautiful. :-)
But if it gets the job done it's ok I guess.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's okay for me. :)


plugin_root_dir = "libraries/plugins/"

generated_code_warning = "\
/*****************************************/\n\
/* This code is generated automatically. */\n\
/* To generate new plugin code create a */\n\
/* plugin.json file in a subdirectory */\n\
/* of the plugin root directory. */\n\
/*****************************************/\n\
\n\
\n"

# this is a dictionary of the generated filename and the generated code
# variables in the code are replaced by the variables from the plugin.json file
templates = {
"generated_api_cpp_case_code.hpp":
"else if( api_name == \"{plugin_name}_api\" ) {{ \n\
_{plugin_name}_api = std::make_shared< {plugin_name}_api >( std::ref(_app) );\n\
}} \n",

"generated_api_cpp_login_api_code.hpp":
"fc::api<graphene::{plugin_namespace}::{plugin_name}_api> login_api::{plugin_name}() const \n\
{{ \n\
FC_ASSERT(_{plugin_name}_api); \n\
return *_{plugin_name}_api; \n\
}}\n\n",

"generated_api_hpp_include_code.hpp":
"#include <graphene/{plugin_name}/{plugin_name}_api.hpp> \n",

"generated_api_hpp_login_api_method_def_code.hpp":
"/// @brief Retrieve the {plugin_name} API \n\
fc::api<graphene::{plugin_namespace}::{plugin_name}_api> {plugin_name}()const;\n",

"generated_api_hpp_login_api_apiobj_code.hpp":
"optional< fc::api<graphene::{plugin_namespace}::{plugin_name}_api> > _{plugin_name}_api;\n",

"generated_api_hpp_reflect_code.hpp":
"({plugin_name})\n",

"generated_application_cpp_allowed_apis_code.hpp":
"wild_access.allowed_apis.push_back( \"{plugin_name}_api\" );\n",

}


# searches for all plugin.json files in the plugin directory
# parses them and returns a list of dictionarys with the parsed data
def get_all_plugin_data():

# holds content of all plugin.json files as dictionary
plugin_data_list = []
# traverse plugin root dir search for plugin.json files
for root, dirs, files in os.walk( "./" + plugin_root_dir ):
for file in files:
if file == "plugin.json":
with open( os.path.join( root, file ) ) as f:
parsed_data = json.load( f )
data_dic = dict()
data_dic['plugin_name'] = parsed_data['plugin_name']
data_dic['plugin_namespace'] = parsed_data['plugin_namespace']
data_dic['plugin_project'] = parsed_data['plugin_project']
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a python expert - is it really necessary to copy the fields instead of appending parsed_data directly?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it would be possible to iterate inside there. But my thought was, we don't have much data hence the speed isn't really necessary here. And like this it's just better separated.

plugin_data_list.append( data_dic )

return plugin_data_list


def generate_all_files( plugin_data_list ):
print "--"
# t_fn = template_filename, t_con = template_content
for t_fn, t_con in templates.items():
f_dir = os.path.join( generated_files_root_dir, t_fn )
with open( f_dir, 'w') as f:
f.write( generated_code_warning )
# p_data = plugin_data
for p_data in plugin_data_list:
# **p_data = values of the dic
content = t_con.format( **p_data )
f.write( content )
print( "-- plugin_code_generator: " + t_fn + " successfully generated" )


def main():

plugin_data_list = get_all_plugin_data()

generate_all_files( plugin_data_list )

print "-- plugin_code_generator: plugin code generation complete\n--"


if __name__ == "__main__":
main()