Skip to content
Permalink
Browse files

[sipify] allow to define python operator in headers

sipify will take care of injecting them in python/{module}/__init__.py
  • Loading branch information
3nids committed May 18, 2018
1 parent 7f97a2f commit 7cb18cd5f1e18ec19d2db3c08daade31dbe31974
@@ -31,7 +31,6 @@
from .additions.metaenum import metaEnumFromType, metaEnumFromValue
from .additions.processing import processing_output_layer_repr, processing_source_repr
from .additions.projectdirtyblocker import ProjectDirtyBlocker
from .additions.qgsdefaultvalue import _isValid
from .additions.qgsfeature import mapping_feature
from .additions.qgsfunction import register_function, qgsfunction
from .additions.qgsgeometry import _geometryNonZero, mapping_geometry
@@ -40,7 +39,6 @@
from .additions.readwritecontextentercategory import ReadWriteContextEnterCategory

# Injections into classes
QgsDefaultValue.__bool__ = _isValid
QgsFeature.__geo_interface__ = property(mapping_feature)
QgsGeometry.__bool__ = _geometryNonZero
QgsGeometry.__geo_interface__ = property(mapping_geometry)
@@ -85,3 +83,4 @@
QgsGeometry.BufferSide.baseClass = QgsGeometry
QgsGeometry.EndCapStyle.baseClass = QgsGeometry
QgsGeometry.JoinStyle.baseClass = QgsGeometry
QgsDefaultValue.__bool__ = lambda self: self.isValid()

This file was deleted.

@@ -77,7 +77,6 @@ Returns if this default value should be applied.
:return: false if the expression is a null string.
%End

operator bool() const;

};

@@ -649,6 +649,7 @@ sub detect_non_method_member{
}
next;
}
# insert in python output (python/module/__init__.py)
if ($LINE =~ m/Q_(ENUM|FLAG)\(\s*(\w+)\s*\)/ ){
if ($LINE !~ m/SIP_SKIP/){
my $is_flag = $1 eq 'FLAG' ? 1 : 0;
@@ -664,11 +665,11 @@ sub detect_non_method_member{
}
}
if ($enum_helper ne ''){
if ($is_flag == 1){
# SIP seems to introduce the flags in the module rather than in the class itself
# as a dirty hack, inject directly in module, hopefully we don't have flags with the same name....
$enum_helper .= "\n$2 = $ACTUAL_CLASS # dirty hack since SIP seems to introduce the flags in module";
}
if ($is_flag == 1){
# SIP seems to introduce the flags in the module rather than in the class itself
# as a dirty hack, inject directly in module, hopefully we don't have flags with the same name....
$enum_helper .= "\n$2 = $ACTUAL_CLASS # dirty hack since SIP seems to introduce the flags in module";
}
print FH "$enum_helper\n";
}
close(FH);
@@ -683,9 +684,8 @@ sub detect_non_method_member{
}

# SIP_SKIP
if ( $LINE =~ m/SIP_SKIP/ ){
if ( $LINE =~ m/SIP_SKIP|SIP_PYTHON_OPERATOR_/ ){
dbg_info('SIP SKIP!');
$COMMENT = '';
# if multiline definition, remove previous lines
if ( $MULTILINE_DEFINITION != MULTILINE_NO){
dbg_info('SIP_SKIP with MultiLine');
@@ -700,6 +700,27 @@ sub detect_non_method_member{
# also skip method body if there is one
detect_and_remove_following_body_or_initializerlist();
# line skipped, go to next iteration

if ($LINE =~ m/SIP_PYTHON_OPERATOR_(\w+)\(\s*(\w+)\s*\)/ ){
my $pyop = "${ACTUAL_CLASS}.__" . lc($1) . "__ = lambda self: self.$2()";
dbg_info("PYTHON OPERATOR $pyop");
if ($python_output ne ''){
my $pl;
open(FH, '+<', $python_output) or die $!;
foreach $pl (<FH>) {
if ($pl =~ m/$pyop/){
$pyop = '';
last;
}
}
if ($pyop ne ''){
print FH "$pyop\n";
}
close(FH);
}
}

$COMMENT = '';
next;
}

@@ -197,4 +197,10 @@
*/
#define SIP_DOC_TEMPLATE

/*
* Define the __bool__ operator using the given method
* sipify.pl will take care of creating the injection in qgis/{module}/__init__.py
*/
#define SIP_PYTHON_OPERATOR_BOOL(method)

#endif // QGIS_SIP_H
@@ -95,7 +95,7 @@ class CORE_EXPORT QgsDefaultValue
* Checks if a default value is set. Alias for isValid().
* \returns false if the expression is a null string.
*/
operator bool() const;
operator bool() const SIP_PYTHON_OPERATOR_BOOL( isValid );

private:
QString mExpression;

0 comments on commit 7cb18cd

Please sign in to comment.
You can’t perform that action at this time.