414 | You have access to canvas, iface, app (QGIS application) objects and to all qgis and PyQtcore and gui modules directly from the shell. %s Don't forget that you have access to all your underlying shell commands too!
415 | Enjoy IPyConsole! Another hack by ItOpen
416 | """) % propertize_text)
417 |
418 | def monkey_patch_columnize(control):
419 | """As the name suggests... dynamic column number: stock
420 | qtconsole doesn't resize its column number on window
421 | resize but sticks to 80"""
422 | from qtconsole.util import columnize
423 |
424 | old_columnize = columnize
425 |
426 | def new_columnize(control):
427 | items = [sug["text"] for sug in control.get_suggestions()]
428 | displaywidth = control.get_columns()
429 | separator = " "
430 | return old_columnize(items, separator, displaywidth)
431 |
432 | columnize = new_columnize
433 |
434 | monkey_patch_columnize(self.control)
435 | QTimer.singleShot(0, shout)
436 |
437 |
438 | if __name__ == "__main__":
439 | pass
440 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 |
3 | Version 2, June 1991
4 |
5 | Copyright (C) 1989, 1991 Free Software Foundation, Inc.
6 |
7 | Everyone is permitted to copy and distribute verbatim copies
8 | of this license document, but changing it is not allowed.
9 |
10 | Preamble
11 |
12 | The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too.
13 |
14 | When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things.
15 |
16 | To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it.
17 |
18 | For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights.
19 |
20 | We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software.
21 |
22 | Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations.
23 |
24 | Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all.
25 |
26 | The precise terms and conditions for copying, distribution and modification follow.
27 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
28 |
29 | 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you".
30 |
31 | Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does.
32 |
33 | 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program.
34 |
35 | You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee.
36 |
37 | 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions:
38 |
39 | a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change.
40 | b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License.
41 | c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.)
42 |
43 | These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it.
44 |
45 | Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program.
46 |
47 | In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License.
48 |
49 | 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following:
50 |
51 | a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or,
52 | b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or,
53 | c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.)
54 |
55 | The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable.
56 |
57 | If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code.
58 |
59 | 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance.
60 |
61 | 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it.
62 |
63 | 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License.
64 |
65 | 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program.
66 |
67 | If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances.
68 |
69 | It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice.
70 |
71 | This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License.
72 |
73 | 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License.
74 |
75 | 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
76 |
77 | Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation.
78 |
79 | 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally.
80 |
81 | NO WARRANTY
82 |
83 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
84 |
85 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
86 | END OF TERMS AND CONDITIONS
87 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | # Makefile for a PyQGIS plugin
2 |
3 | all: compile
4 |
5 | dist: package
6 |
7 | install: copy2qgis
8 |
9 | # QGIS3 default
10 | QGISDIR=.local/share/QGIS/QGIS3/profiles/default
11 |
12 | PY_FILES = IPyConsole.py __init__.py
13 | EXTRAS = icon.png settings.svg
14 | #UI_FILES = Ui_SettingsDialog.py
15 | #RESOURCE_FILES=resources_rc.py
16 |
17 | compile: $(UI_FILES) $(RESOURCE_FILES)
18 |
19 | %_rc.py : %.qrc
20 | pyrcc5 -o $@ $<
21 |
22 | %.py : %.ui
23 | pyuic5 -o $@ $<
24 |
25 |
26 | deploy:
27 | ln -s `pwd` $(HOME)/$(QGISDIR)/python/plugins/ipyconsole
28 |
29 | clean:
30 | find ./ -name "*.pyc" -exec rm -rf \{\} \;
31 | rm -f ../IPyConsole.zip
32 | rm -f Ui_IPyConsole.py resources_rc.py
33 |
34 | package:
35 | cd .. && if \[ -f IPyConsole.zip \] ; \
36 | then \
37 | rm IPyConsole.zip ; \
38 | fi;
39 | cd .. && find IPyConsole/ -print|grep -v Make |grep -v run* | grep -v .pyc | grep -v zip | grep -v vscode | grep -v .git | zip IPyConsole.zip -@
40 |
41 | localrepo:
42 | cp ../IPyConsole.zip ~/public_html/qgis/IPyConsole.zip
43 |
44 | copy2qgis: package
45 | unzip -o ../IPyConsole.zip -d ~/.qgis/python/plugins
46 |
47 | check test:
48 | @echo "Sorry: not implemented yet."
49 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # qgis-ipythonconsole
2 | IPython QGIS plugin
3 |
4 | Home page:
5 | http://www.itopen.it/qgis-and-ipython-the-definitive-interactive-console/
6 |
--------------------------------------------------------------------------------
/Ui_SettingsDialog.ui:
--------------------------------------------------------------------------------
1 |
2 |
3 | SettingsDialog
4 |
5 |
6 |
7 | 0
8 | 0
9 | 806
10 | 473
11 |
12 |
13 |
14 | Settings
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 | 0
24 | 0
25 |
26 |
27 |
28 | 2
29 |
30 |
31 |
32 |
33 |
34 |
35 | General
36 |
37 |
38 |
39 |
40 |
41 | Startup options
42 |
43 |
44 |
45 |
46 |
47 | Launch automatically when QGIS starts
48 |
49 |
50 |
51 |
52 |
53 |
54 | Show help message when IPyConsole starts
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 | 0
63 | 0
64 | 964
65 | 2
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 | Current console font
78 |
79 |
80 |
81 |
82 |
83 |
84 | Select Font
85 |
86 |
87 |
88 |
89 |
90 |
91 | Default window mode
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 | Windowed
100 |
101 |
102 |
103 |
104 |
105 |
106 | Docked
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 | Theme (requires restart)
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 | Light
127 |
128 |
129 |
130 |
131 |
132 |
133 | Dark
134 |
135 |
136 |
137 |
138 |
139 |
140 | Black and White
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 | Qt::Vertical
155 |
156 |
157 |
158 | 20
159 | 40
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 | Python
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 | <p><code><b>propertize</b></code> adds <code><b>p_</b></code> prefixed alias properties for all (0-arguments and returning-something) functions in <code><b>Qgs*</b></code> classes from <code><b>qgis.core</b></code> and <code><b>qgis.gui</b></code> modules.</p>
177 | <p>This is extremely useful when working with IPython's amazing <b>TAB</b> completion feature.</b>
178 | <p>For example, you will be able to <b>TAB</b>-complete the following statement in IPython console:
179 | <pre><b>
180 | registry = core.QgsMapLayerRegistry.instance()
181 | layer = registry.p_mapLayers.values()[0]
182 | layer.p_crs.p_authid
183 | </b></pre>
184 | </p>
185 |
186 |
187 | Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop
188 |
189 |
190 | true
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 | Enable propertize
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 | About
208 |
209 |
210 |
211 |
212 |
213 | <html><head/><body><p><span style=" font-size:xx-large; font-weight:600;">IPython QGIS Console </span></p><p>An IPython interactive console with batteries included. </p><p><a href="http://www.itopen.it/qgis-and-ipython-the-definitive-interactive-console/"><span style=" text-decoration: underline; color:#0057ae;">Plugin Home Page</span></a> (feed-back is highly appreciated!)</p><p><a href="http://www.ipython.org"><span style=" text-decoration: underline; color:#0057ae;">IPython Home Page</span></a></p><p><a href="https://github.com/elpaso/qgis-ipythonconsole"><span style=" text-decoration: underline; color:#0057ae;">Source Code and Bug Tracker</span></a></p>
214 | <p><i>Enjoy IPyConsole!<br>Made in Italy by<a href="http://www.itopen.it"><span style=" text-decoration: underline; color:#0057ae;"> Alessandro Pasotti (ItOpen)</a></i></p> </body></html>
215 |
216 |
217 | Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop
218 |
219 |
220 | true
221 |
222 |
223 | true
224 |
225 |
226 |
227 |
228 |
229 |
230 | <html><head/><body><p><img src=":/plugins/IPyConsole/icons/icon.png"/></p></body></html>
231 |
232 |
233 | Qt::AlignRight|Qt::AlignTop|Qt::AlignTrailing
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 | Qt::Horizontal
245 |
246 |
247 | QDialogButtonBox::Cancel|QDialogButtonBox::Ok
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 | buttonBox
261 | accepted()
262 | SettingsDialog
263 | accept()
264 |
265 |
266 | 548
267 | 491
268 |
269 |
270 | 548
271 | 260
272 |
273 |
274 |
275 |
276 | buttonBox
277 | rejected()
278 | SettingsDialog
279 | reject()
280 |
281 |
282 | 548
283 | 491
284 |
285 |
286 | 548
287 | 260
288 |
289 |
290 |
291 |
292 |
293 |
--------------------------------------------------------------------------------
/__init__.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 | """
3 | This script initializes the plugin, making it known to QGIS.
4 | """
5 |
6 | def classFactory(iface):
7 | from .IPyConsole import IPyConsole
8 | return IPyConsole(iface)
9 |
--------------------------------------------------------------------------------
/icons/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/elpaso/qgis-ipythonconsole/5d08da375657ac89d7376ebd285a05d15e5d21a8/icons/icon.png
--------------------------------------------------------------------------------
/icons/paypal.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/elpaso/qgis-ipythonconsole/5d08da375657ac89d7376ebd285a05d15e5d21a8/icons/paypal.png
--------------------------------------------------------------------------------
/icons/settings.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
580 |
--------------------------------------------------------------------------------
/metadata.txt:
--------------------------------------------------------------------------------
1 | [general]
2 | name=IPyConsole
3 | qgisMinimumVersion=2.14
4 | qgisMaximumVersion=3.99
5 | description=IPython QGIS Console
6 | version=version 2.0
7 | author=Alessandro Pasotti
8 | email=apasotti@gmail.com
9 |
10 |
11 | changelog=
12 | 2.0 Dark theme
13 | 1.9 Fixes #16 and #21 undefined dock
14 | 1.8 Fixes #10 and remove paypal link
15 | 1.7 Bring into front fixes #8 default to docked fixes #9
16 | 1.6 Console font selection in settings
17 | 1.5 tested on current master QGIS 3.0 with Qt5/Py3, added instructions for windows installation
18 | 1.4 removed limit IPython version == 3.1.0: now tested on Jupyter 1.0.0
19 | 1.3 fixed loading issue ans set IPython version == 3.1.0, thanks to "dmofot"
20 | 1.2 added error message in case IPython version is less than 3.1
21 | 1.1 added exception message to exception handler on IPython import
22 | 1.0 added option to hide help message, auto-resize for settings dialog
23 | 0.6 added option to launch at start, monkey patched IPython console resize
24 | 0.5 fixed propertize on classmethods, added option to disable
25 | 0.4 added configuration options for font size and default mode (window/dock)
26 | added about dialog and links
27 | 0.3 fixed double prompt at start
28 | 0.2 better messages and usage instructions
29 | 0.1 first version
30 |
31 | tags=console,ipython
32 |
33 | tracker=https://github.com/elpaso/qgis-ipythonconsole/issues
34 | homepage=http://www.itopen.it/qgis-and-ipython-the-definitive-interactive-console/
35 | repository=https://github.com/elpaso/qgis-ipythonconsole
36 |
37 | experimental=False
38 |
39 | about=This plugin adds a Python console based on IPython.
40 | IPython has full introspection support and TAB completion, syntax highlight, online help ad a lot of cool features you will love! Optionally, this plugin creates more "pythonic" interface to Qgs classmethods for an easier introspection and TAB completion of Qgs classes.
41 |
42 | Note: you must separately install IPython and IPython Qt console to use this plugin. On a Debian-like distributions this is as easy as running "pip install 'ipython[all]==3.1.0 qtconsole' or (better) "pip install jupyter==1.0.0". To install packages with 'pip' you may need root/admin privileges.
43 |
44 |
45 |
46 | ; change icon...
47 | icon=icons/icon.png
48 |
--------------------------------------------------------------------------------
/propertize.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | # -*- coding: utf-8 -*-
3 |
4 | """
5 | ***************************************************************************
6 | propertize.py
7 |
8 | More Pythonic bindings for the masses.
9 | Transform all methods of a class without arguments and that do return
10 | something into properties for the given input class or for all classes
11 | in the given module.
12 |
13 | ---------------------
14 | Date : April 2015
15 | Copyright : (C) 2015 by Alessandro Pasotti (ItOpen)
16 | Email : apasotti at gmail dot com
17 | ***************************************************************************
18 | * *
19 | * This program is free software; you can redistribute it and/or modify *
20 | * it under the terms of the GNU General Public License as published by *
21 | * the Free Software Foundation; either version 2 of the License, or *
22 | * (at your option) any later version. *
23 | * *
24 | ***************************************************************************
25 | """
26 |
27 | __author__ = 'Alessandro Pasotti'
28 | __date__ = 'April 2015'
29 | __copyright__ = '(C) 2015, Alessandro Pasotti'
30 |
31 | try:
32 | from qgis.core import Qgis
33 | QGIS_IS3 = True
34 | except:
35 | QGIS_IS3 = False
36 |
37 | import inspect
38 | import functools
39 | def propertize(cls, prefix='p_', quiet=True):
40 | """Transform all methods without arguments and that do return something into
41 | properties for the given input class or module, an optional prefix
42 | (defaults to p_) is added to the property so that the original function is
43 | still available"""
44 |
45 | def _cls_propertize(cls, prefix, quiet):
46 |
47 | def _caller(self, k):
48 | return getattr(self, k)()
49 |
50 | def is_supported(k, v):
51 | return (k.find('__') == -1
52 | and (inspect.isroutine(v) or inspect.isbuiltin(v))
53 | and inspect.getdoc(v)
54 | and inspect.getdoc(v).find('()' if not QGIS_IS3 else '(self)') != -1
55 | and inspect.getdoc(v).find('->') != -1)
56 |
57 |
58 | for k, v in inspect.getmembers(cls):
59 | if is_supported(k, v):
60 | attr_name = prefix + k
61 |
62 | # Works fine on bound virtual methods, doesn't work on
63 | # classmethods
64 | try:
65 | setattr(cls, attr_name, property(functools.partial(_caller, k=k)))
66 | if not quiet:
67 | print('Propertized %s' % attr_name)
68 | except TypeError as e:
69 | if not quiet:
70 | print('Propertized failed for %s' % attr_name)
71 |
72 | if inspect.ismodule(cls):
73 | if inspect.isbuiltin(cls):
74 | return
75 | for k, v in inspect.getmembers(cls):
76 | if (k.find('__') == -1
77 | and inspect.isclass(v)
78 | and str(v) != 'str'):
79 | _cls_propertize(v, prefix, quiet)
80 | elif inspect.isclass(cls):
81 | _cls_propertize(cls, prefix, quiet)
82 |
83 |
84 |
85 |
86 | if __name__ == '__main__':
87 | # Tests #
88 | import unittest
89 |
90 | class Animal():
91 | @classmethod
92 | def whoami(cls):
93 | """ (self) -> """
94 | return 'Animal'
95 | def say(self):
96 | """ (self) -> """
97 | return 'sound'
98 | def move(self):
99 | """ (self) -> """
100 | return 'move'
101 |
102 | class Duck(Animal):
103 | @classmethod
104 | def whoami(cls):
105 | """ (self) -> """
106 | return 'Duck'
107 | def say(self):
108 | """ (self) -> """
109 | return 'quack'
110 | def move(self):
111 | """ (self) -> """
112 | return 'fly'
113 |
114 | class Dog(Animal):
115 | def eat(self):
116 | """ (self) -> """
117 | return 'meat'
118 |
119 | propertize(Animal)
120 | propertize(Dog)
121 | propertize(Duck)
122 |
123 | class TestInheritance(unittest.TestCase):
124 |
125 | def setUp(self):
126 | self.animal = Animal()
127 | self.duck = Duck()
128 | self.dog = Dog()
129 |
130 | def test_calls(self):
131 | self.assertEqual(self.animal.say(), 'sound')
132 | self.assertEqual(self.animal.move(), 'move')
133 | self.assertEqual(self.duck.say(), 'quack')
134 | self.assertEqual(self.duck.move(), 'fly')
135 | self.assertEqual(self.dog.say(), 'sound')
136 | self.assertEqual(self.dog.eat(), 'meat')
137 | self.assertEqual(self.dog.move(), 'move')
138 |
139 | def test_properties(self):
140 | self.assertEqual(self.animal.p_say, 'sound')
141 | self.assertEqual(self.animal.p_move, 'move')
142 | self.assertEqual(self.duck.p_say, 'quack')
143 | self.assertEqual(self.duck.p_move, 'fly')
144 | self.assertEqual(self.dog.p_say, 'sound')
145 | self.assertEqual(self.dog.p_eat, 'meat')
146 | self.assertEqual(self.dog.p_move, 'move')
147 |
148 | def test_raise(self):
149 | with self.assertRaises(AttributeError):
150 | self.duck.eat()
151 |
152 | def test_classmethods(self):
153 | self.assertEqual(self.animal.whoami(), 'Animal')
154 | self.assertEqual(self.animal.p_whoami, 'Animal')
155 | self.assertEqual(self.duck.whoami(), 'Duck')
156 | self.assertEqual(self.duck.p_whoami, 'Duck')
157 |
158 |
159 | # Now testing QGIS bindings: this is the hard part!
160 | from qgis import core
161 | class TestQGIS(unittest.TestCase):
162 |
163 | def setUp(self):
164 | self.app = core.QgsApplication([], True)
165 | propertize(core)
166 |
167 |
168 | def test_project_instance(self):
169 | project = core.QgsProject.instance()
170 | self.assertEqual(project.mapLayers(), {})
171 | self.assertEqual(project.p_mapLayers, project.mapLayers())
172 |
173 | def test_application_instance(self):
174 | application = core.QgsApplication
175 | self.assertEqual(application.instance().p_children, core.QgsApplication.instance().children())
176 |
177 |
178 | unittest.main()
179 |
--------------------------------------------------------------------------------