Skip to content

Commit

Permalink
libkart bindings: Add TreeEntry.type property
Browse files Browse the repository at this point in the history
  • Loading branch information
craigds committed Mar 3, 2022
1 parent f83498a commit 9d02634
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion libkart/bindings/python/CMakeLists.txt
Expand Up @@ -4,7 +4,7 @@ find_package(Python3 3.7 REQUIRED COMPONENTS Interpreter)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake)
include(PyCreateVirtualEnvironment)

createvirtualenvironment(cythonEnv REQUIREMENTS "cython;wheel")
createvirtualenvironment(cythonEnv REQUIREMENTS "cython==3.0.0a10;wheel")

# Prepare Python's setup.cfg
file(
Expand Down
26 changes: 25 additions & 1 deletion libkart/bindings/python/libkart.pyx
Expand Up @@ -6,7 +6,24 @@ from libcpp.string cimport string
from libcpp.utility cimport move

from libkart_ cimport *

from enum import Enum

# FIXME: In *theory* the `cpdef enum class` in libkart_.pxd should mean we can just use `object_type`,
# because it should be usable in a Python context.
# However, in practice it doesn't seem to work at all.
# I think the `cimport` above just imports the C-ish symbol and not the Python one.
# And you can't `import` from libkart_.pxd. So I'm not sure how you're meant to use the cpdef enum.
# So here we work around it by just defining a python-style enum and using it below.
class ObjectType(Enum):
# cppgit2::object::object_type
any = -2
invalid = -1
commit = 1
tree = 2
blob = 3
tag = 4
ofs_delta = 6
ref_delta = 7

cdef class Oid:
cdef cppgit2_oid cpp
Expand All @@ -33,6 +50,10 @@ cdef class TreeEntry:
def id(self):
return Oid._wrap(self.cpp.id())

@property
def type(self):
return ObjectType(self.cpp.type())

def __repr__(self):
return f"<TreeEntry: {self.filename}>"

Expand All @@ -51,6 +72,9 @@ cdef class TreeEntryWithPath:
def id(self):
return Oid._wrap(self.cpp.id())
@property
def type(self):
return ObjectType(self.cpp.type())
@property
def rel_path(self):
return self.cpp.rel_path()

Expand Down
16 changes: 16 additions & 0 deletions libkart/bindings/python/libkart_.pxd
Expand Up @@ -2,13 +2,28 @@ from libcpp.memory cimport unique_ptr
from libcpp.string cimport string
from libcpp.vector cimport vector

cdef extern from "cppgit2/object.hpp" namespace "cppgit2::object":
cpdef enum class object_type "cppgit2::object::object_type":
# cppgit2::object::object_type
any = -2
invalid = -1
commit = 1
tree = 2
blob = 3
tag = 4
ofs_delta = 6
ref_delta = 7


cdef extern from "kart.hpp" namespace "kart":
# cppgit stuff

cdef cppclass cppgit2_oid "cppgit2::oid":
string to_hex_string()
cdef cppclass cppgit2_tree_entry "cppgit2::tree::entry":
string filename()
cppgit2_oid id()
object_type type()
cdef cppclass cppgit2_tree "cppgit2::tree":
cppgit2_oid id()
vector[cppgit2_tree_entry] entries()
Expand All @@ -18,6 +33,7 @@ cdef extern from "kart.hpp" namespace "kart":
string rel_path()
string filename()
cppgit2_oid id()
object_type type()

cdef cppclass CppTreeEntryIterator "kart::TreeEntryIterator":
CppTreeEntryWithPath operator*()
Expand Down

0 comments on commit 9d02634

Please sign in to comment.