-
Notifications
You must be signed in to change notification settings - Fork 14
Python API
CCDB CLI is written in python, thus there are at least 2 ways to interact with CCDB using python.
-
Using CLI-like API. Which resembles using 'ccdb' command
-
Using so called low-level API.
The code looks like this:
context.process_command_line("mkdir /test/testable2 x y z #Some comment")
This python API is what actually under the hood of 'ccdb' command.
Why to use it? - If you ever wanted to create ccdb CLI commands programmatically and run them using subprocess... Then you can just skip subprocess using this API directly.
Procs:
- Almost the same as ccdb CLI. But you get exceptions directly and you don't have to use subprocess
- Documentation from ccdb CLI is valid for this API
- It is as safe in terms of DB integrity as ccdb CLI
Cons:
- You don't get results as python objects. (So, in example, you can't read table names and do
for
on them)
The code looks like this:
tables = provider.get_type_tables("/test/test_vars")
for table in tables:
print(table.name)
It is called "Low-level" to highlight that it becomes possible to screw up DB integrity using this API. Still, if you just read data, you can't screw up anything. And if you follow the examples with writing to DB it is relatively save too.
Why to use it? - You work directly with python objects that represent CCDB structure. You can iterate them, go from one to another through hierarchy, etc. Also you can add custom data here, massively change or delete data, etc.
Procs:
- Full access to CCDB data through python
- Working with python objects
- SQLAlchemy querries to DB are possible
Cons:
- One can screw up data layout. Especially on delete operations (so please, follow instructions and examples, make backups!)
- More complex
The data class model, where you can find classes for tables or directories, is located in:
$CCDB_HOME/python/ccdb/model.py
And the core API functions are located in:
$CCDB_HOME/python/ccdb/provider.py
You can see more of... kind of examples of using the API at
$CCDB_HOME/python/tests/provider_fixture.py
Read next: CLI-like API | Low Level API