add api usage examples for cpp and python binding
This commit is contained in:
committed by
Eberhard Graether
parent
4517be935c
commit
1c07e5b4dc
@@ -0,0 +1,30 @@
|
||||
cmake_minimum_required (VERSION 2.6)
|
||||
|
||||
set(EXAMPLE_NAME "python_binding_example")
|
||||
|
||||
# --- Configure Sourcetrail Project File ---
|
||||
|
||||
add_custom_target(
|
||||
${EXAMPLE_NAME} ALL
|
||||
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/src/example.py ${CMAKE_CURRENT_BINARY_DIR}/example.py
|
||||
)
|
||||
|
||||
add_custom_command(
|
||||
TARGET ${EXAMPLE_NAME} POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy ${PYTHON_BINDING_OUTPUT_DIR}/_sourcetraildb* ${CMAKE_CURRENT_BINARY_DIR}
|
||||
COMMAND ${CMAKE_COMMAND} -E copy ${PYTHON_BINDING_OUTPUT_DIR}/sourcetraildb.py ${CMAKE_CURRENT_BINARY_DIR}
|
||||
)
|
||||
|
||||
if (APPLE)
|
||||
add_custom_command(
|
||||
TARGET ${EXAMPLE_NAME} POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E rename ${CMAKE_CURRENT_BINARY_DIR}/_sourcetraildb.dylib ${CMAKE_CURRENT_BINARY_DIR}/_sourcetraildb.so
|
||||
)
|
||||
endif()
|
||||
|
||||
set(PYTHON_BINDING_EXAMPLE_DATA_PATH "${CMAKE_CURRENT_SOURCE_DIR}/data")
|
||||
|
||||
configure_file(
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/${EXAMPLE_NAME}.srctrlprj.in"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/${EXAMPLE_NAME}.srctrlprj"
|
||||
)
|
||||
@@ -0,0 +1,7 @@
|
||||
|
||||
class MyType:
|
||||
|
||||
my_member = true
|
||||
|
||||
def my_method():
|
||||
return my_member
|
||||
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<config>
|
||||
<source_groups>
|
||||
<source_group_78efd997-84bf-4db6-9736-f5ddd702053c>
|
||||
<custom_command>python3 example.py --database-file-path=$DB_PATH --source-file-path=$SOURCE_PATH</custom_command>
|
||||
<name>Custom Command Source Group</name>
|
||||
<source_extensions>
|
||||
<source_extension>.py</source_extension>
|
||||
</source_extensions>
|
||||
<source_paths>
|
||||
<source_path>@PYTHON_BINDING_EXAMPLE_DATA_PATH@/file.py</source_path>
|
||||
</source_paths>
|
||||
<status>enabled</status>
|
||||
<type>Custom Command Source Group</type>
|
||||
</source_group_78efd997-84bf-4db6-9736-f5ddd702053c>
|
||||
</source_groups>
|
||||
<version>7</version>
|
||||
</config>
|
||||
@@ -0,0 +1,71 @@
|
||||
import argparse
|
||||
import os
|
||||
import sourcetraildb as srctrl
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Index a Python source file and store the indexed data to a Sourcetrail database file.")
|
||||
parser.add_argument("--database-file-path", help="path to the generated Sourcetrail database file", type=str, required=True)
|
||||
parser.add_argument("--source-file-path", help="path to the source file to index", type=str, required=True)
|
||||
|
||||
args = parser.parse_args()
|
||||
databaseFilePath = args.database_file_path
|
||||
sourceFilePath = args.source_file_path
|
||||
|
||||
if not srctrl.open(databaseFilePath):
|
||||
print("ERROR: " + srctrl.getLastError())
|
||||
|
||||
if srctrl.isEmpty():
|
||||
print("Loaded database is empty.")
|
||||
else:
|
||||
print("Loaded database contains data.")
|
||||
|
||||
print("start all")
|
||||
srctrl.beginTransaction()
|
||||
|
||||
fileId = srctrl.recordFile(sourceFilePath)
|
||||
srctrl.recordFileLanguage(fileId, "python")
|
||||
|
||||
if len(srctrl.getLastError()) > 0:
|
||||
print("ERROR: " + srctrl.getLastError())
|
||||
|
||||
symbolId = srctrl.recordSymbol('{ "name_delimiter": ".", "name_elements": [ { "prefix": "", "name": "MyType", "postfix": "" } ] }')
|
||||
srctrl.recordSymbolDefinitionKind(symbolId, srctrl.DEFINITION_EXPLICIT)
|
||||
srctrl.recordSymbolKind(symbolId, srctrl.SYMBOL_CLASS)
|
||||
srctrl.recordSymbolLocation(symbolId, fileId, 2, 7, 2, 12)
|
||||
srctrl.recordSymbolScopeLocation(symbolId, fileId, 2, 1, 7, 1)
|
||||
|
||||
if len(srctrl.getLastError()) > 0:
|
||||
print("ERROR: " + srctrl.getLastError())
|
||||
|
||||
memberId = srctrl.recordSymbol('{ "name_delimiter": ".", "name_elements": [ { "prefix": "", "name": "MyType", "postfix": "" }, { "prefix": "", "name": "my_member", "postfix": "" } ] }')
|
||||
srctrl.recordSymbolDefinitionKind(memberId, srctrl.DEFINITION_EXPLICIT)
|
||||
srctrl.recordSymbolKind(memberId, srctrl.SYMBOL_FIELD)
|
||||
srctrl.recordSymbolLocation(memberId, fileId, 4, 2, 4, 10)
|
||||
|
||||
if len(srctrl.getLastError()) > 0:
|
||||
print("ERROR: " + srctrl.getLastError())
|
||||
|
||||
methodId = srctrl.recordSymbol('{ "name_delimiter": ".", "name_elements": [ { "prefix": "", "name": "MyType", "postfix": "" }, { "prefix": "", "name": "my_method", "postfix": "" } ] }')
|
||||
srctrl.recordSymbolDefinitionKind(methodId, srctrl.DEFINITION_EXPLICIT)
|
||||
srctrl.recordSymbolKind(methodId, srctrl.SYMBOL_METHOD)
|
||||
srctrl.recordSymbolLocation(methodId, fileId, 6, 6, 6, 14)
|
||||
srctrl.recordSymbolScopeLocation(methodId, fileId, 6, 1, 7, 1)
|
||||
|
||||
if len(srctrl.getLastError()) > 0:
|
||||
print("ERROR: " + srctrl.getLastError())
|
||||
|
||||
useageId = srctrl.recordReference(methodId, memberId, srctrl.REFERENCE_USAGE)
|
||||
srctrl.recordReferenceLocation(useageId, fileId, 7, 10, 7, 18)
|
||||
|
||||
if len(srctrl.getLastError()) > 0:
|
||||
print("ERROR: " + srctrl.getLastError())
|
||||
|
||||
srctrl.commitTransaction()
|
||||
print("end all")
|
||||
|
||||
if not srctrl.close():
|
||||
print("ERROR: " + srctrl.getLastError())
|
||||
|
||||
if len(srctrl.getLastError()) > 0:
|
||||
print("ERROR: " + srctrl.getLastError())
|
||||
main()
|
||||
Reference in New Issue
Block a user