Skip to content

Commit

Permalink
[sipify] allow to define python operator in headers
Browse files Browse the repository at this point in the history
sipify will take care of injecting them in python/{module}/__init__.py
  • Loading branch information
3nids committed Jun 3, 2018
1 parent 7f97a2f commit 7cb18cd
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 33 deletions.
3 changes: 1 addition & 2 deletions python/core/__init__.py
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -85,3 +83,4 @@
QgsGeometry.BufferSide.baseClass = QgsGeometry
QgsGeometry.EndCapStyle.baseClass = QgsGeometry
QgsGeometry.JoinStyle.baseClass = QgsGeometry
QgsDefaultValue.__bool__ = lambda self: self.isValid()
22 changes: 0 additions & 22 deletions python/core/additions/qgsdefaultvalue.py

This file was deleted.

1 change: 0 additions & 1 deletion python/core/auto_generated/qgsdefaultvalue.sip.in
Expand Up @@ -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;

};

Expand Down
35 changes: 28 additions & 7 deletions scripts/sipify.pl
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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');
Expand All @@ -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;
}

Expand Down
6 changes: 6 additions & 0 deletions src/core/qgis_sip.h
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/core/qgsdefaultvalue.h
Expand Up @@ -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;
Expand Down

0 comments on commit 7cb18cd

Please sign in to comment.