Skip to content

Commit

Permalink
Implement libbson
Browse files Browse the repository at this point in the history
- Update compose.py, Makefile and README
- Create vapi folder where the current composed vapi is stored
- Add .deps file with dependencies
- Implement the remaining part of libbson

Note: libbson bindings are almost complete, but not tested
  • Loading branch information
bynect committed Sep 29, 2020
1 parent 4028670 commit 209b776
Show file tree
Hide file tree
Showing 11 changed files with 1,085 additions and 40 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ __pycache__/
build*/

test_*
libmongoc-1.0.vapi
libmongoc-1.0.*
!vapi/*
29 changes: 17 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
.DEFAULT: compose
.SILENT: compose license clean help test-status test test-crud verbose build-crud build-status
.PHONY: compose license clean help test-status test test-crud verbose build-crud build-status
.SILENT: compose license clean help test-status test test-crud verbose build-crud build-status quiet
.PHONY: compose license clean help test-status test test-crud verbose build-crud build-status quiet

compose: clean
echo "Generating VAPI file"
python3 ./compose.py

verbose: clean
echo "Generating VAPI file"
echo "Generating VAPI file [debug]\n"
python3 ./compose.py --verbose

quiet: clean
python3 ./compose.py --quiet

license:
python3 ./compose.py --license

clean:
find . -type d -name '__pycache__' -exec rm -rf {} + 2> /dev/null
find . -name 'test_*' -exec rm -f {} + 2> /dev/null
find . -name 'libmongoc-1.0.vapi' -exec rm -f {} + 2> /dev/null
clear
find . -type d -name 'vapi' -exec rm -rf {} + 2> /dev/null
find . -type d -name 'build' -exec rm -rf {} + 2> /dev/null

help:
echo "help: this message"
Expand All @@ -30,13 +33,15 @@ help:
test: test-status

test-crud: build-crud
./test_crud
./build/test_crud

test-status: build-status
./test_status
./build/test_status

build-crud: clean compose
valac --pkg glib-2.0 --pkg libmongoc-1.0 --vapidir . -o test_crud test/crud.vala
build-crud: clean quiet
mkdir -p build 2> /dev/null
valac --pkg glib-2.0 --pkg posix --pkg libmongoc-1.0 --vapidir ./vapi -o ./build/test_crud test/crud.vala

build-status: clean compose
valac --pkg glib-2.0 --pkg posix --pkg libmongoc-1.0 --vapidir . -o test_status test/status.vala
build-status: clean quiet
mkdir -p build 2> /dev/null
valac --pkg glib-2.0 --pkg posix --pkg libmongoc-1.0 --vapidir ./vapi -o ./build/test_status test/status.vala
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# Mongo Vapi

Vala bindings for the [MongoDB C Driver](http://mongoc.org/ "mongoc.org").
Vala bindings for the [MongoDB C Driver](https://github.com/mongodb/mongo-c-driver "mongoc driver").

>**Still work in progess, not ready for CRUDs.**
Because the codebase to bind is fairly big, the vapis are divided in partials file and a file can be easily generated with the included script.
If you need only the VAPI file, you can download it from the releases allegates.

While the vast majority of the methods and classes have the same or a very similar name to the C counterpart, some of the names maybe a little tweaked to be more Vala-friendly.
>While the vast majority of the methods and classes have the same or a very similar name to the C counterpart, some of the names maybe a little tweaked to be more Vala-friendly.
>Still work in progess, not ready for CRUDs.
Deprecated or next-to-deprecation features of mongoc are actually not being binded.

## Generate VAPI file

Expand All @@ -26,4 +29,4 @@ $ make test
## License
Licensed under MIT, you are free to use this VAPI. See LICENSE for more.

>If you want contribute you are very welcome.
>If you want contribute you are welcome.
57 changes: 42 additions & 15 deletions compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def prefixed_lines():
#composing
HEADER = '\n[CCode (cheader_filename = "{0}", has_type_id = false, cprefix = "mongoc_", lower_case_cprefix = "mongoc_")]\nnamespace {1} {{'

DEPENDENCIES = ['posix']


def prepare_license(div = '', wrapped = True):

Expand Down Expand Up @@ -61,21 +63,39 @@ def prepare_partials(partials_path, div):
return final_partials


def compose_vapi(onefile: bool, folder: str, div: str, out: str):
def prepare_deps(deps: list):

out = ''
for dep in deps:
out += '{}\n'.format(dep)

return out


def compose_vapi(onefile: bool, folder: str, div: str, out: str, deps: bool, filename: str = 'libmongoc-1.0'):

logging.debug('Composing VAPI content from partials')

if not os.path.exists(out):
os.makedirs(out)
logging.debug("Created folder {}".format(out))

wrap_license = prepare_license(div = div)
mongoc_wrapped = prepare_partials(find_partials(folder = './{}/mongoc'.format(folder), suffix = '.vapi'), div = div)
bson_wrapped = prepare_partials(find_partials(folder = './{}/bson'.format(folder), suffix = '.vapi'), div = div)

joined = ''.join(mongoc_wrapped + bson_wrapped if onefile else mongoc_wrapped)

formatted = '{0}\n{1}\n{2}\n}}'.format(wrap_license, HEADER.format('mongoc.h,bson.h', 'Mongo'), joined)
formatted = '{0}\n{1}\n{2}\n}}\n'.format(wrap_license, HEADER.format('mongoc.h,bson.h', 'Mongo'), joined)

with open(out, 'w') as f:
with open('{0}/{1}.vapi'.format(out, filename), 'w') as f:
f.write(formatted)
logging.debug('Writing on {}'.format(out))
logging.debug('Writing on {0}/{1}.vapi'.format(out, filename))

if deps:
with open('{0}/{1}.deps'.format(out, filename), 'w') as f:
f.write(prepare_deps(DEPENDENCIES))
logging.debug('Writing on {0}/{1}.deps'.format(out, filename))


#arg parsing
Expand All @@ -85,35 +105,42 @@ def compose_vapi(onefile: bool, folder: str, div: str, out: str):
description = 'Compose the partials and generate a VAPI file'
)

parser.add_argument('-o', '--out', dest = 'out', type = str, action = 'store', default = 'libmongoc-1.0.vapi', help = 'Specify output filename')
parser.add_argument('-d', '--dir', dest = 'dir', action = 'store', type = str, default = 'partials', help = 'Specify partials dir')
parser.add_argument('-v', '--verbose', dest = 'verbose', action = 'store_true', help = 'Activate verbose mode')
parser.add_argument('-d', '--deps', dest = 'deps', action = 'store_true', default = True, help = 'Specify if .deps file will be generated')
parser.add_argument('-o', '--out', dest = 'out', type = str, action = 'store', default = 'vapi', help = 'Specify output folder')
parser.add_argument('-f', '--folder', dest = 'folder', action = 'store', type = str, default = 'partials', help = 'Specify partials folder')
parser.add_argument('-v', '--verbose', dest = 'verbose', action = 'store_true', help = 'Activate verbose mode [Debug]')
parser.add_argument('-q', '--quiet', dest = 'quiet', action = 'store_true', help = 'Activate quiet mode [Error]')
parser.add_argument('-l', '--license', dest = 'license', action = 'store_true', help = 'Show license')
parser.add_argument('-i', '--indent', dest = 'indent', action = 'store', default = ' ', help = 'Specify indentation, default 4 spaces')
parser.add_argument('-i', '--indent', dest = 'indent', type = int, action = 'store', default = 4, help = 'Specify indentation, default 4 spaces')
parser.add_argument('--onefile', dest = 'onefile', action = 'store_true', default = True, help = 'Specify if the ouput will be one file')

args = parser.parse_args()

def set_verbose(level):

if level:
def set_verbosity(verbose, quiet):

if verbose and not quiet:
logging.basicConfig(level=logging.DEBUG, format='%(message)s')
logging.debug("Verbose mode setted")


elif quiet and not verbose:
logging.basicConfig(level=logging.ERROR, format='%(message)s')

logging.basicConfig(level=logging.INFO, format='%(message)s')

set_verbose(args.verbose)

set_verbosity(args.verbose, args.quiet)

if args.license:
print(prepare_license(wrapped = False), '\n')

elif args.out and args.dir:
elif args.out and args.folder:
try:
compose_vapi(onefile = args.onefile, folder = args.dir, div = args.indent, out = args.out)
compose_vapi(onefile = args.onefile, folder = args.folder, div = ' ' * args.indent, out = args.out, deps = args.deps)
except:
logging.critical("An error occurred, please retry", exc_info = True if args.verbose else False)
else:
logging.info('Done. VAPI generated in `{}`'.format(args.out))
print('Done. VAPI generated in `{}`'.format(args.out))
finally:
logging.info('\nLicensed under MIT, see LICENSE or `make license`')
logging.info('Do `python3 compose.py -h` to see usage options')
39 changes: 39 additions & 0 deletions partials/bson/entities.vapi
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@

/**
* bson_type_t
*/

[CCode (cname = "bson_type_t", has_type_id = false, cprefix = "BSON_TYPE_")]
public enum BsonType {
EOD,
DOUBLE,
UTF8,
DOCUMENT,
ARRAY,
BINARY,
UNDEFINED,
OID,
BOOL,
DATE_TIME,
NULL,
REGEX,
DBPOINTER,
CODE,
SYMBOL,
CODEWSCOPE,
INT32,
TIMESTAMP,
INT64,
DECIMAL128,
MAXKEY,
MINKEY
}

/**
* bson_subtype_t
*/
Expand Down Expand Up @@ -120,3 +150,12 @@ public struct BsonContext {
public struct BsonValue {

}

/**
* bson_visitor_t
*/

[CCode (cname = "bson_visitor_t", free_function = "", has_type_id = false)]
public struct BsonVisitor {

}
74 changes: 67 additions & 7 deletions partials/bson/others.vapi
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,73 @@ public struct BsonError {
}

/**
* bson_json_error_code_t
* bson_get_monotonic_time
*/

[CCode (cname = "bson_json_error_code_t", has_type_id = false, cprefix = "BSON_JSON_ERROR_READ_")]
[Flags]
public enum JsonErrorCode {
CORRUPT_JS,
INVALID_PARAM,
CB_FAILURE
[CCode (cname = "bson_get_monotonic_time")]
public int64 get_monotonic_time ();

/**
* bson_gettimeofday
*/

[CCode (cname = "bson_gettimeofday")]
public int gettimeofday (GLib.TimeVal tv, GLib.TimeZone tz);

/**
* bson_check_version
*/

[CCode (cname = "bson_check_version")]
public bool bson_check_version (int required_major, int required_minor, int required_micro);

/**
* bson_get_version
*/

[CCode (cname = "bson_get_version")]
public string bson_get_version ();

/**
* bson_get_major_version
*/

[CCode (cname = "bson_get_major_version")]
public int bson_get_major_version ();

/**
* bson_get_micro_version
*/

[CCode (cname = "bson_get_micro_version")]
public int bson_get_micro_version ();

/**
* bson_get_minor_version
*/

[CCode (cname = "bson_get_minor_version")]
public int bson_get_minor_version ();

/**
* bson_mem_restore_vtable
*/

[CCode (cname = "bson_mem_restore_vtable")]
public void bson_mem_restore_vtable ();

/**
* bson_mem_set_vtable
*/

[CCode (cname = "bson_mem_set_vtable")]
public void bson_mem_set_vtable (BsomMemVTable vtable);

/**
* bson_mem_vtable_t
*/

[CCode (cname = "bson_mem_vtable_t", has_type_id = false, free_function = "")]
public struct BsomMemVTable {

}
12 changes: 12 additions & 0 deletions partials/bson/reader.vapi
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,15 @@ public delegate ssize_t BsonReaderReadFunc<T> (T handle, T buf, size_t count);

[CCode (cname = "bson_reader_destroy_func_t", has_type_id = false, simple_generics = true)]
public delegate void BsonReaderDestroyFunc<T> (T handle);

/**
* bson_json_error_code_t
*/

[CCode (cname = "bson_json_error_code_t", has_type_id = false, cprefix = "BSON_JSON_ERROR_READ_")]
[Flags]
public enum JsonErrorCode {
CORRUPT_JS,
INVALID_PARAM,
CB_FAILURE
}
Loading

0 comments on commit 209b776

Please sign in to comment.