├── .github
└── workflows
│ └── python-publish.yml
├── LICENSE
├── README.md
├── examples
├── srcml_convenience
│ ├── main.cpp
│ └── srcml_convenience.py
├── srcml_copy_archive
│ ├── project.xml
│ └── srcml_copy_archive.py
├── srcml_create_archive_fd
│ ├── main.cpp
│ └── srcml_create_archive_fd.py
├── srcml_create_archive_file
│ ├── main.cpp
│ └── srcml_create_archive_file.py
├── srcml_create_archive_filename
│ ├── main.cpp
│ └── srcml_create_archive_filename.py
├── srcml_create_archive_full
│ ├── main.cpp
│ └── srcml_create_archive_full.py
├── srcml_create_archive_memory
│ ├── main.cpp
│ └── srcml_create_archive_memory.py
├── srcml_direct_language
│ ├── a.cpp
│ └── srcml_direct_language.py
├── srcml_direct_language_list
│ └── srcml_direct_language_list.py
├── srcml_direct_language_xml
│ ├── a.cpp.xml
│ └── srcml_direct_language_xml.py
├── srcml_direct_src2srcml
│ ├── a.cpp
│ └── srcml_direct_src2srcml.py
├── srcml_direct_srcml2src
│ ├── a.cpp.xml
│ └── srcml_direct_srcml2src.py
├── srcml_list
│ ├── project.xml
│ └── srcml_list.py
├── srcml_read_archive_fd
│ ├── project.xml
│ └── srcml_read_archive_fd.py
├── srcml_read_archive_file
│ ├── project.xml
│ └── srcml_read_archive_file.py
├── srcml_read_archive_filename
│ ├── project.xml
│ └── srcml_read_archive_filename.py
├── srcml_read_archive_full
│ ├── project.xml
│ └── srcml_read_archive_full.py
├── srcml_read_archive_memory
│ └── srcml_read_archive_memory.py
├── srcml_relaxng
│ ├── project.xml
│ ├── schema.rng
│ └── srcml_relaxng.py
├── srcml_sort_archive
│ ├── project.xml
│ └── srcml_sort_archive.py
├── srcml_split_archive
│ ├── project.xml
│ └── srcml_split_archive.py
├── srcml_transform
│ ├── copy.xsl
│ ├── project.xml
│ ├── schema.rng
│ └── srcml_transform.py
├── srcml_xpath
│ ├── project.xml
│ └── srcml_xpath.py
└── srcml_xslt
│ ├── project.xml
│ └── srcml_xslt.py
├── pyproject.toml
├── setup.py
├── src
└── pylibsrcml
│ ├── __init__.py
│ ├── exception.py
│ ├── gen_options.py
│ ├── globals.py
│ ├── options.py
│ ├── srcml.py
│ ├── srcml_archive.py
│ └── srcml_unit.py
└── test
├── a.foo
├── copy.xsl
├── project.xml
├── schema.rng
├── setlanguage.xsl
└── test.py
/.github/workflows/python-publish.yml:
--------------------------------------------------------------------------------
1 | # This workflow will upload a Python Package using Twine when a release is created
2 | # For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries
3 |
4 | # This workflow uses actions that are not certified by GitHub.
5 | # They are provided by a third-party and are governed by
6 | # separate terms of service, privacy policy, and support
7 | # documentation.
8 |
9 | name: Upload Python Package to PyPI
10 |
11 | on:
12 | workflow_dispatch:
13 | # Right now, boolean inputs are broken. this can be reenabled when this is fixed
14 | # inputs:
15 | # isTest:
16 | # description: Should the package be pushed to TestPyPI?
17 | # type: boolean
18 | # required: false
19 | # default: false
20 |
21 | jobs:
22 | build-and-publish:
23 | name: Build and Publish Python Package to PyPI
24 | runs-on: ubuntu-latest
25 | steps:
26 | - uses: actions/checkout@master
27 | - name: Set up Python 3.9
28 | uses: actions/setup-python@v1
29 | with:
30 | python-version: 3.9
31 | - name: Install pypa/build
32 | run: >-
33 | python -m
34 | pip install
35 | build
36 | --user
37 | - name: Build a binary whell and a source tarball
38 | run: >-
39 | python -m
40 | build
41 | --sdist
42 | --wheel
43 | --outdir dist/
44 | .
45 | - name: Publish distribution to PyPI
46 | # if: ${{ !github.event.inputs.isTest }} && startsWith(github.ref, 'refs/tags')
47 | uses: pypa/gh-action-pypi-publish@master
48 | with:
49 | password: ${{ secrets.PYPI_API_TOKEN }}
50 | # Enable if you want to publish to TestPyPI
51 | # - name: Publish distribution to Test PyPI
52 | # if: ${{ github.event.inputs.isTest }}
53 | # uses: pypa/gh-action-pypi-publish@master
54 | # with:
55 | # password: ${{ secrets.TEST_PYPI_API_TOKEN }}
56 | # repository_url: https://test.pypi.org/legacy/
57 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # pylibsrcml
2 | Python bindings for libsrcml. V1.0 May 2022
3 |
4 | All of libsrcml is supported (and tested). To install:
5 |
6 | Make sure the latest version of srcML (developer) is installed. Also the recent version of Python. Then run
7 |
8 | `pip install pylibsrcml`
9 |
--------------------------------------------------------------------------------
/examples/srcml_convenience/main.cpp:
--------------------------------------------------------------------------------
1 | // Sample program
2 | #include
3 |
4 | int main(int argc, char* argv[]) {
5 | std::cout << "Hello World!" << std::endl;
6 | }
--------------------------------------------------------------------------------
/examples/srcml_convenience/srcml_convenience.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file srcml_convenience.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | # Example program of the use of the convenience funcion Python API for srcML.
13 |
14 | import importlib
15 | import sys
16 | import difflib
17 | import os
18 | import ctypes
19 | from pylibsrcml import srcml
20 |
21 | # Setup options and attributes
22 | srcml.set_version("211")
23 | srcml.set_tabstop(4)
24 |
25 | # Treat ".h" as C++
26 | srcml.register_file_extension("h", srcml.SRCML_LANGUAGE_CXX)
27 |
28 | # Change prefix of standard namespace
29 | srcml.register_namespace("s", "http://www.sdml.info/srcML/src")
30 |
31 | # Default prefix is now for cpp namespace
32 | srcml.register_namespace("", "http://www.sdml.info/srcML/cpp")
33 |
34 | # New prefix for further processing
35 | srcml.register_namespace("doc", "http://www.sdml.info/srcML/doc")
36 |
37 | # Translates source code file "main.cpp" to srcML file "main.cpp.xml".
38 | # - Translate using the above global options
39 | # - The language will be automatically based on the extension of the input (first) filename
40 | # - Since there is only a single input file, the output file will be a non-archive by default.
41 | # Convenience function can also convert to archive
42 | srcml.srcml("main.cpp", "main.cpp.xml")
--------------------------------------------------------------------------------
/examples/srcml_copy_archive/project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/examples/srcml_copy_archive/srcml_copy_archive.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file srcml_copy_archive.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | # Example program of the use of the Python API for srcML.
13 | # Copy an archive.
14 |
15 | import importlib
16 | import string
17 | import sys
18 | import difflib
19 | import os
20 | import ctypes
21 | from pylibsrcml import srcml
22 |
23 | # Open up an existing archive
24 | archive = srcml.srcml_archive()
25 | print("ARCHIVE CREATED")
26 | archive.read_open_filename("project.xml")
27 | print("ARCHIVE READ")
28 |
29 | # Create a new srcml archive structure
30 | # Options and attributes of cloned archive start the same as the original archive
31 | oarchive = archive.clone()
32 |
33 | # Open a srcML archive for output
34 | oarchive.write_open_filename("project2.xml")
35 | print("OTHER ARCHIVE OPENED FOR WRITING")
36 |
37 | # Copy files from the input archive to the output archive
38 | unit = archive.read_unit()
39 | print("INITIAL READ")
40 | while (unit != None) :
41 | print("IN LOOP")
42 | # Translate to srcml and append to the archive
43 | oarchive.write_unit(unit)
44 | unit = archive.read_unit()
45 |
46 | # Close the archives
47 | archive.close()
48 | oarchive.close()
--------------------------------------------------------------------------------
/examples/srcml_create_archive_fd/main.cpp:
--------------------------------------------------------------------------------
1 | // Sample program
2 | #include
3 |
4 | int main(int argc, char* argv[]) {
5 | std::cout << "Hello World!" << std::endl;
6 | }
--------------------------------------------------------------------------------
/examples/srcml_create_archive_fd/srcml_create_archive_fd.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file srcml_create_archive_fd.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | # Example program of the use of the Python API for srcML.
13 | # Create an archive, file by file, with an output file descriptor
14 |
15 | from stat import S_IWUSR
16 | import sys
17 | import difflib
18 | import os
19 | import ctypes
20 | import stat
21 | from pylibsrcml import srcml
22 |
23 | # Create a new srcml archive structure
24 | archive = srcml.srcml_archive()
25 |
26 | # Setup our output file using a file descriptor
27 | srcml_output = os.open("project.xml", os.O_WRONLY | os.O_CREAT, stat.S_IRUSR | stat.S_IWUSR)
28 |
29 | # Open a srcML archive for output
30 | archive.write_open_fd(srcml_output)
31 |
32 | # Add all the files to the archive
33 | for x in sys.argv[1:] :
34 |
35 | unit = srcml.srcml_unit(archive)
36 |
37 | unit.set_language(archive.check_extension(x))
38 |
39 | # Translate to srcml
40 | srcml_input = os.open(x, os.O_RDONLY, 0)
41 | unit.parse_fd(srcml_input)
42 |
43 | # Append to the archive
44 | archive.write_unit(unit)
45 |
46 | os.close(srcml_input)
47 |
48 | # Close srcml archive
49 | archive.close()
50 |
51 | # File can now be closed also
52 | os.close(srcml_output)
--------------------------------------------------------------------------------
/examples/srcml_create_archive_file/main.cpp:
--------------------------------------------------------------------------------
1 | // Sample program
2 | #include
3 |
4 | int main(int argc, char* argv[]) {
5 | std::cout << "Hello World!" << std::endl;
6 | }
--------------------------------------------------------------------------------
/examples/srcml_create_archive_file/srcml_create_archive_file.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file srcml_create_archive_file.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | # Example program of the use of the Python API for srcML.
13 | # Create an archive, file by file, with an output FILE*
14 |
15 | from stat import S_IWUSR
16 | import sys
17 | import difflib
18 | import os
19 | import ctypes
20 | import stat
21 | from pylibsrcml import srcml
22 |
23 | # Set up libc link
24 | LIBC_PATH = ""
25 | if sys.platform == "darwin" :
26 | LIBC_PATH = "libc.dylib"
27 | elif sys.platform == "linux" :
28 | LIBC_PATH = "libc.so.6"
29 | else :
30 | LIBC_PATH = "msvcrt.dll"
31 |
32 | libc = ctypes.cdll.LoadLibrary(LIBC_PATH)
33 |
34 | if sys.platform == "win32" or sys.platform == "cygin" :
35 | os.open = libc._open
36 | os.close = libc._close
37 | os.O_WRONLY = 1
38 | os.O_CREAT = 256
39 | os.O_RDONLY = 0
40 |
41 | libc.fopen.restype = ctypes.c_void_p
42 | libc.fopen.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
43 |
44 | libc.fclose.restype = ctypes.c_int
45 | libc.fclose.argtypes = [ctypes.c_void_p]
46 |
47 | # Create a new srcml archive structure
48 | archive = srcml.srcml_archive()
49 |
50 | # Setup our output file using a FILE
51 | srcml_output = libc.fopen(str.encode("project.xml"), str.encode("w"))
52 |
53 | # Open a srcML archive for output
54 | archive.write_open_FILE(srcml_output)
55 |
56 | # Add all the files to the archive
57 | for x in sys.argv[1:] :
58 |
59 | unit = srcml.srcml_unit(archive)
60 |
61 | unit.set_language(archive.check_extension(x))
62 |
63 | # Translate to srcml
64 | srcml_input = libc.fopen(str.encode(x), str.encode("r"))
65 | unit.parse_FILE(srcml_input)
66 |
67 | # Append to the archive
68 | archive.write_unit(unit)
69 |
70 | libc.fclose(srcml_input)
71 |
72 | # Close srcml archive
73 | archive.close()
74 |
75 | # File can now be closed also
76 | libc.fclose(srcml_output)
--------------------------------------------------------------------------------
/examples/srcml_create_archive_filename/main.cpp:
--------------------------------------------------------------------------------
1 | // Sample program
2 | #include
3 |
4 | int main(int argc, char* argv[]) {
5 | std::cout << "Hello World!" << std::endl;
6 | }
--------------------------------------------------------------------------------
/examples/srcml_create_archive_filename/srcml_create_archive_filename.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file srcml_create_archive_filename.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | # Example program of the use of the Python API for srcML.
13 | # Create an archive, file by file, with an output filename.
14 |
15 | import sys
16 | import difflib
17 | import os
18 | import ctypes
19 | import stat
20 | from pylibsrcml import srcml
21 |
22 | # Create a new srcml archive structure
23 | archive = srcml.srcml_archive()
24 |
25 | # Open a srcML archive for output
26 | archive.write_open_filename("project.xml")
27 |
28 | # Add all files to the archive
29 | for x in sys.argv[1:] :
30 | unit = srcml.srcml_unit(archive)
31 | unit.set_filename(x)
32 |
33 | # Translate to srcml and append to the archive
34 | unit.parse_filename(x)
35 | archive.write_unit(unit)
36 |
37 | # Close srcML archive
38 | archive.close()
--------------------------------------------------------------------------------
/examples/srcml_create_archive_full/main.cpp:
--------------------------------------------------------------------------------
1 | // Sample program
2 | #include
3 |
4 | int main(int argc, char* argv[]) {
5 | std::cout << "Hello World!" << std::endl;
6 | }
--------------------------------------------------------------------------------
/examples/srcml_create_archive_full/srcml_create_archive_full.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file srcml_create_archive_full.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | # Example program of the use of the Python API for srcML.
13 | # Create an archive, file by file, with an output filename, showing
14 | # most of the option features
15 |
16 | import sys
17 | import difflib
18 | import os
19 | import ctypes
20 | import stat
21 | from pylibsrcml import srcml
22 |
23 | # Create a new srcml archive structure
24 | archive = srcml.srcml_archive()
25 |
26 | # Setup options and attributes
27 | archive.set_version("211")
28 | archive.set_tabstop(4)
29 |
30 | # Treat .h as C++
31 | archive.register_file_extension("h", srcml.SRCML_LANGUAGE_CXX)
32 |
33 | # Change prefix of standard namespace
34 | archive.register_namespace("s", "http://www.sdml.info/srcML/src")
35 |
36 | # Default prefix is now for cpp namespace
37 | archive.register_namespace("", "http://www.sdml.info/srcML/cpp")
38 |
39 | # New prefix for further processing
40 | archive.register_namespace("doc", "http://www.sdml.info/srcML/doc")
41 |
42 | ###############################
43 | # Open and write to the archive
44 | ###############################
45 |
46 | # Open an archive for output
47 | archive.write_open_filename("project.xml")
48 |
49 | # Add all files on the command line to the archive
50 | for x in sys.argv[1:] :
51 | # Setup this entry
52 | unit = srcml.srcml_unit(archive)
53 | unit.set_language(srcml.SRCML_LANGUAGE_C)
54 | unit.set_filename(x)
55 |
56 | # Translate the entry to srcML
57 | unit.parse_filename(x)
58 |
59 | # Append unit to the archive
60 | archive.write_unit(unit)
61 |
62 | ###############################
63 | # Finish up
64 | ###############################
65 |
66 | # Close the srcML archive
67 | archive.close()
--------------------------------------------------------------------------------
/examples/srcml_create_archive_memory/main.cpp:
--------------------------------------------------------------------------------
1 | // Sample program
2 | #include
3 |
4 | int main(int argc, char* argv[]) {
5 | std::cout << "Hello World!" << std::endl;
6 | }
--------------------------------------------------------------------------------
/examples/srcml_create_archive_memory/srcml_create_archive_memory.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file srcml_create_archive_memory.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | # Example program of the use of the Python API for srcML.
13 | # Create an archive, file by file, with an output memory.
14 |
15 | import sys
16 | import difflib
17 | import os
18 | import ctypes
19 | import stat
20 | from pylibsrcml import srcml
21 |
22 | # Create a new srcml archive structure
23 | archive = srcml.srcml_archive()
24 |
25 | # Open a srcml archive for output
26 | archive.write_open_memory()
27 |
28 | for x in sys.argv[1:] :
29 | unit = srcml.srcml_unit(archive)
30 |
31 | # Translate to srcml and append to the archive
32 | srcml_input = os.open(x, os.O_RDONLY, 0)
33 | buffer = str(os.read(srcml_input, 256))
34 | os.close(srcml_input)
35 | unit.set_language(archive.check_extension(x))
36 |
37 | unit.parse_memory(buffer)
38 |
39 | # Translate to srcml and append to the archive
40 | archive.write_unit(unit)
41 |
42 | # Close archive
43 | archive.close()
44 |
45 | # Dump archive contents
46 | print(archive.srcML())
--------------------------------------------------------------------------------
/examples/srcml_direct_language/a.cpp:
--------------------------------------------------------------------------------
1 | // Sample program
2 | #include
3 |
4 | int main(int argc, char* argv[]) {
5 | std::cout << "Hello World!" << std::endl;
6 | }
--------------------------------------------------------------------------------
/examples/srcml_direct_language/srcml_direct_language.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file srcml_direct_language.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | # Example program of the use of the C API for srcML.
13 | # A straightforward translation of source code to the srcML format.
14 | # Translates the file "a.cpp" to the srcML format in "a.cpp.xml", however
15 | # in this case the language is specified:
16 | # * This creates a single-unit srcML file, i.e., a non-archive srcML
17 | # * The srcML attribute filename will be the name of the file passed as the first
18 | # parameter.
19 |
20 | import sys
21 | import difflib
22 | import os
23 | import ctypes
24 | import stat
25 | from pylibsrcml import srcml
26 |
27 | # Translate from a source code file to a srcML file
28 | srcml.set_language(srcml.SRCML_LANGUAGE_CXX)
29 | srcml.srcml("a.cpp", "a.cpp.xml")
--------------------------------------------------------------------------------
/examples/srcml_direct_language_list/srcml_direct_language_list.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file srcml_direct_language_list.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | # Example program of the use of the Python API for srcML.
13 | # A null-terminated list of the supported srcML source-code language.
14 |
15 | import sys
16 | import difflib
17 | import os
18 | import ctypes
19 | import stat
20 | from pylibsrcml import srcml
21 |
22 | # Print list of languages
23 | for i in range(0, srcml.get_language_list_size()) :
24 | print(srcml.get_language_list(i))
--------------------------------------------------------------------------------
/examples/srcml_direct_language_xml/a.cpp.xml:
--------------------------------------------------------------------------------
1 |
2 | // Sample program
3 | #include <iostream>
4 |
5 | int main(int argc, char* argv[]) {
6 | std::cout << "Hello World!" << std::endl;
7 | }
8 |
--------------------------------------------------------------------------------
/examples/srcml_direct_language_xml/srcml_direct_language_xml.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file srcml_direct_language_xml.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | # Example program of the use of the Python API for srcML.
13 | # A straightforward translation from the srcML format back to source code.
14 | # Translates the srcML file "a.cpp.xml" to the source-code file "a.cpp", however
15 | # in this case the language is specified:
16 | # * This creates a single-unit srcML file, i.e., a non-archive srcML
17 | # * The srcML attribute filename will be the name of the file passed as the first
18 | # parameter.
19 |
20 | import sys
21 | import difflib
22 | import os
23 | import ctypes
24 | import stat
25 | from pylibsrcml import srcml
26 |
27 | # Translate from a source-code file to a srcML file
28 | srcml.set_language(srcml.SRCML_LANGUAGE_XML)
29 | srcml.srcml("a.cpp.xml", "a.cpp")
--------------------------------------------------------------------------------
/examples/srcml_direct_src2srcml/a.cpp:
--------------------------------------------------------------------------------
1 | // Sample program
2 | #include
3 |
4 | int main(int argc, char* argv[]) {
5 | std::cout << "Hello World!" << std::endl;
6 | }
--------------------------------------------------------------------------------
/examples/srcml_direct_src2srcml/srcml_direct_src2srcml.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file srcml_direct_language_src2srcml.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | # Example program of the use of the Python API for srcML.
13 | # A straightforward translation of source code to the srcML format.
14 | # Translates the file "a.cpp" to the srcML format in "a.cpp.xml":
15 | # * The language is determined automatically from the source file extension
16 | # * This creates a single-unit srcML file, i.e., a non-archive srcML
17 | # * The srcML attribute filename will be the name of the file passed as the first
18 | # parameter.
19 |
20 | import sys
21 | import difflib
22 | import os
23 | import ctypes
24 | import stat
25 | from pylibsrcml import srcml
26 |
27 | # Translate from a source-code file to a srcML file
28 | srcml.srcml("a.cpp", "a.cpp.xml")
--------------------------------------------------------------------------------
/examples/srcml_direct_srcml2src/a.cpp.xml:
--------------------------------------------------------------------------------
1 |
2 | // Sample program
3 | #include <iostream>
4 |
5 | int main(int argc, char* argv[]) {
6 | std::cout << "Hello World!" << std::endl;
7 | }
8 |
--------------------------------------------------------------------------------
/examples/srcml_direct_srcml2src/srcml_direct_srcml2src.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file srcml_direct_language_srcml2src.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | # Example program of the use of the Python API for srcML.
13 | # Translates the srcML file "a.cpp.xml" to the source-code file "a.cpp":
14 | # * This creates a single-unit srcML file, i.e., a non-archive srcML
15 | # * The srcML attribute filename will be the name of the file passed as the first
16 | # parameter.
17 |
18 | import sys
19 | import difflib
20 | import os
21 | import ctypes
22 | import stat
23 | from pylibsrcml import srcml
24 |
25 | # Translate from a srcML file to a source-code file
26 | srcml.srcml("a.cpp.xml", "a.cpp")
--------------------------------------------------------------------------------
/examples/srcml_list/project.xml:
--------------------------------------------------------------------------------
1 |
2 | // Sample program
3 | #include <iostream>
4 |
5 | int main(int argc, char* argv[]) {
6 | std::cout << "Hello World!" << std::endl;
7 | }
8 |
--------------------------------------------------------------------------------
/examples/srcml_list/srcml_list.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file srcml_list.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | # Example program of the use of the Python API for srcML.
13 | # Gather info from an archive from a given unit.
14 |
15 | import sys
16 | import difflib
17 | import os
18 | import ctypes
19 | import stat
20 | from pylibsrcml import srcml
21 |
22 | # Create srcml archive
23 | archive = srcml.srcml_archive()
24 | archive.read_open_filename("project.xml")
25 |
26 | unit = archive.read_unit()
27 | while unit != None :
28 | print(unit.get_filename())
29 | unit = archive.read_unit()
30 |
31 | archive.close()
--------------------------------------------------------------------------------
/examples/srcml_read_archive_fd/project.xml:
--------------------------------------------------------------------------------
1 |
2 | // Sample program
3 | #include <iostream>
4 |
5 | int main(int argc, char* argv[]) {
6 | std::cout << "Hello World!" << std::endl;
7 | }
8 |
--------------------------------------------------------------------------------
/examples/srcml_read_archive_fd/srcml_read_archive_fd.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file srcml_read_archive_fd.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | # Example program of the use of the Python API for srcML.
13 | # Take an archive and extract the invidual units and write to a filesystem.
14 |
15 | from stat import S_IWUSR
16 | import sys
17 | import difflib
18 | import os
19 | import ctypes
20 | import stat
21 | from pylibsrcml import srcml
22 |
23 | # Create a new srcml archive
24 | archive = srcml.srcml_archive()
25 |
26 | # Open srcml archive for input
27 | input = os.open("project.xml", os.O_RDONLY, 0)
28 | archive.read_open_fd(input)
29 |
30 | # Add all files to the archive
31 | unit = archive.read_unit()
32 | while unit != None :
33 | # Can inquire about the current unit
34 | language = unit.get_language()
35 | filename = unit.get_filename()
36 |
37 | # Unparse and write to a file
38 | output = os.open(filename, os.O_WRONLY | os.O_CREAT, stat.S_IRUSR | S_IWUSR)
39 | unit.unparse_fd(output)
40 |
41 | os.close(output)
42 | unit = archive.read_unit()
43 |
44 | # Close the srcML archive
45 | archive.close()
--------------------------------------------------------------------------------
/examples/srcml_read_archive_file/project.xml:
--------------------------------------------------------------------------------
1 |
2 | // Sample program
3 | #include <iostream>
4 |
5 | int main(int argc, char* argv[]) {
6 | std::cout << "Hello World!" << std::endl;
7 | }
8 |
--------------------------------------------------------------------------------
/examples/srcml_read_archive_file/srcml_read_archive_file.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file srcml_read_archive_file.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | # Example program of the use of the Python API for srcML.
13 | # Take an archive and extract the invidual units and write to a filesystem.
14 |
15 | from stat import S_IWUSR
16 | import sys
17 | import difflib
18 | import os
19 | import ctypes
20 | import stat
21 | from pylibsrcml import srcml
22 |
23 | # Set up libc link
24 | LIBC_PATH = ""
25 | if sys.platform == "darwin" :
26 | LIBC_PATH = "libc.dylib"
27 | elif sys.platform == "linux" :
28 | LIBC_PATH = "libc.so.6"
29 | else :
30 | LIBC_PATH = "msvcrt.dll"
31 |
32 | libc = ctypes.cdll.LoadLibrary(LIBC_PATH)
33 |
34 | if sys.platform == "win32" or sys.platform == "cygin" :
35 | os.open = libc._open
36 | os.close = libc._close
37 | os.O_WRONLY = 1
38 | os.O_CREAT = 256
39 | os.O_RDONLY = 0
40 |
41 | libc.fopen.restype = ctypes.c_void_p
42 | libc.fopen.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
43 |
44 | libc.fclose.restype = ctypes.c_int
45 | libc.fclose.argtypes = [ctypes.c_void_p]
46 |
47 | # Create a new srcml archive structure
48 | archive = srcml.srcml_archive()
49 |
50 | # Open a srcml archive for input
51 | input = libc.fopen(str.encode("project.xml"), str.encode("r"))
52 | archive.read_open_FILE(input)
53 |
54 | # Add all files to the archive
55 | unit = archive.read_unit()
56 | while unit != None :
57 | # Can inquire about current unit
58 | language = unit.get_language()
59 | filename = unit.get_filename()
60 |
61 | # Unparse and write to a file
62 | output = libc.fopen(str.encode(filename), str.encode("w"))
63 | unit.unparse_FILE(output)
64 |
65 | libc.fclose(output)
66 | unit = archive.read_unit()
67 |
68 | # Close the srcml archive
69 | archive.close()
--------------------------------------------------------------------------------
/examples/srcml_read_archive_filename/project.xml:
--------------------------------------------------------------------------------
1 |
2 | // Sample program
3 | #include <iostream>
4 |
5 | int main(int argc, char* argv[]) {
6 | std::cout << "Hello World!" << std::endl;
7 | }
8 |
--------------------------------------------------------------------------------
/examples/srcml_read_archive_filename/srcml_read_archive_filename.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file srcml_read_archive_filename.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | # Example program of the use of the Python API for srcML.
13 | # Take an archive and extract the invidual units and write to a filesystem.
14 |
15 | from stat import S_IWUSR
16 | import sys
17 | import difflib
18 | import os
19 | import ctypes
20 | import stat
21 | from pylibsrcml import srcml
22 |
23 | # Create a new srcml archive structure
24 | archive = srcml.srcml_archive()
25 |
26 | # Open a srcml archive for input
27 | archive.read_open_filename("project.xml")
28 |
29 | # Add all the files to the archive
30 | unit = archive.read_unit()
31 | while unit != None :
32 | # Can inquire about the current unit
33 | language = unit.get_language()
34 | filename = unit.get_filename()
35 |
36 | # Unparse and write to a file
37 | unit.unparse_filename(filename)
38 |
39 | unit = archive.read_unit()
40 |
41 | # Close the srcml archive
42 | archive.close()
--------------------------------------------------------------------------------
/examples/srcml_read_archive_full/project.xml:
--------------------------------------------------------------------------------
1 |
2 | // Sample program
3 | #include <iostream>
4 |
5 | int main(int argc, char* argv[]) {
6 | std::cout << "Hello World!" << std::endl;
7 | }
8 |
--------------------------------------------------------------------------------
/examples/srcml_read_archive_full/srcml_read_archive_full.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file srcml_read_archive_full.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | # Example program of the use of the Python API for srcML.
13 | # Take an archive and extract the invidual units and write to a filesystem.
14 |
15 | from stat import S_IWUSR
16 | import sys
17 | import difflib
18 | import os
19 | import ctypes
20 | import stat
21 | from pylibsrcml import srcml
22 |
23 | # Create a new srcml archive structure
24 | archive = srcml.srcml_archive()
25 |
26 | # Open a srcml archive for input
27 | archive.read_open_filename("project.xml")
28 |
29 | # Add all the files to the archive
30 | unit = archive.read_unit()
31 | while unit != None :
32 | # Can inquire about the current unit
33 | language = unit.get_language()
34 | filename = unit.get_filename()
35 |
36 | # Unparse and write to a file
37 | unit.unparse_filename(filename)
38 |
39 | unit = archive.read_unit()
40 |
41 | # Close the srcml archive
42 | archive.close()
--------------------------------------------------------------------------------
/examples/srcml_read_archive_memory/srcml_read_archive_memory.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file srcml_read_archive_memory.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | # Example program of the use of the Python API for srcML.
13 | # Take an archive and extract the invidual units and write to a filesystem.
14 |
15 | from stat import S_IWUSR
16 | import sys
17 | import difflib
18 | import os
19 | import ctypes
20 | import stat
21 | from pylibsrcml import srcml
22 |
23 | s = """
24 | // Sample program
25 | #include <iostream>
26 |
27 | int main(int argc, char* argv[]) {
28 | std::cout << "Hello World!" << std::endl;
29 | }
30 | """
31 |
32 | # Create a new srcml archive
33 | archive = srcml.srcml_archive()
34 | archive.read_open_memory(s)
35 |
36 | # Add all the files to the archive
37 | unit = archive.read_unit()
38 | while unit != None :
39 | # Can inquire about the current unit
40 | language = unit.get_language()
41 | filename = unit.get_filename()
42 |
43 | # Unparse and write to terminal
44 | unit.unparse_memory()
45 | print(unit.src())
46 |
47 | unit = archive.read_unit()
48 |
49 | # Close the srcml archive
50 | archive.close()
--------------------------------------------------------------------------------
/examples/srcml_relaxng/project.xml:
--------------------------------------------------------------------------------
1 |
2 | // Sample program
3 | #include <iostream>
4 |
5 | int main(int argc, char* argv[]) {
6 | std::cout << "Hello World!" << std::endl;
7 | }
8 |
--------------------------------------------------------------------------------
/examples/srcml_relaxng/schema.rng:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/examples/srcml_relaxng/srcml_relaxng.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file srcml_relaxng.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | # Example program of the use of the Python API for srcML.
13 | # RelaxNG usage.
14 |
15 | from stat import S_IWUSR
16 | import sys
17 | import difflib
18 | import os
19 | import ctypes
20 | import stat
21 | from pylibsrcml import srcml
22 |
23 | # Create srcml archive
24 | iarchive = srcml.srcml_archive()
25 | iarchive.read_open_filename("project.xml")
26 |
27 | # Create clone archive
28 | oarchive = iarchive.clone()
29 | oarchive.write_open_filename("relaxng.xml")
30 |
31 | # Append relaxng file
32 | iarchive.append_transform_relaxng_filename("schema.rng")
33 |
34 | # Close archives
35 | iarchive.close()
36 | oarchive.close()
--------------------------------------------------------------------------------
/examples/srcml_sort_archive/project.xml:
--------------------------------------------------------------------------------
1 |
2 | // Sample program
3 | #include <iostream>
4 |
5 | int main(int argc, char* argv[]) {
6 | std::cout << "Hello World!" << std::endl;
7 | }
8 |
--------------------------------------------------------------------------------
/examples/srcml_sort_archive/srcml_sort_archive.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file srcml_sort_archive.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | # Example program of the use of the Python API for srcML.
13 | # Sorts an archive in alphabetical filename order.
14 | # Not especially useful, but does show how units can be rearranged.
15 |
16 | from stat import S_IWUSR
17 | import sys
18 | import difflib
19 | import os
20 | import ctypes
21 | import stat
22 | from unicodedata import numeric
23 | from pylibsrcml import srcml
24 |
25 | num_units = 0
26 | units = [None] * 10
27 | inputFile = "project.xml"
28 | outputFile = "project_tmp.xml"
29 |
30 | # Open an existing archive
31 | iarchive = srcml.srcml_archive()
32 |
33 | # Create a new srcml archive structure
34 | # Options and attributes of cloned archive start the same as
35 | # the original archive
36 | oarchive = iarchive.clone()
37 | iarchive.read_open_filename(inputFile)
38 | while True :
39 | units[num_units] = iarchive.read_unit()
40 | if(units[num_units] == None) :
41 | break
42 | num_units += 1
43 |
44 | i = 1
45 | while i < num_units :
46 | j = i
47 |
48 | while j > 0 :
49 | if units[j].get_filename() == units[j - 1].get_filename() :
50 | tmp_unit = units[j]
51 | units[j] = units[j - 1]
52 | units[j - 1] = tmp_unit
53 | else :
54 | break
55 |
56 | j -= 1
57 |
58 | i += 1
59 |
60 | # Open a srcml archive for output
61 | oarchive.write_open_filename(outputFile)
62 |
63 | i = 0
64 | for i in range(0, num_units) :
65 | # Copy the files from the input archive to the output archive
66 | # Translate to srcml and append to the archive
67 | oarchive.write_unit(units[i])
68 |
69 | # Close archives
70 | oarchive.close()
71 | iarchive.close()
--------------------------------------------------------------------------------
/examples/srcml_split_archive/project.xml:
--------------------------------------------------------------------------------
1 |
2 | // Sample program
3 | #include <iostream>
4 |
5 | int main(int argc, char* argv[]) {
6 | std::cout << "Hello World!" << std::endl;
7 | }
8 |
--------------------------------------------------------------------------------
/examples/srcml_split_archive/srcml_split_archive.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file srcml_split_archive.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | # Example program of the use of the Python API for srcML.
13 | # Split an archive into two, one for .h files and one for other extensions
14 |
15 | from posixpath import isabs
16 | from stat import S_IWUSR
17 | import sys
18 | import difflib
19 | import os
20 | import ctypes
21 | import stat
22 | from unicodedata import numeric
23 | from pylibsrcml import srcml
24 |
25 | # Open an existing archive
26 | iarchive = srcml.srcml_archive()
27 | iarchive.read_open_filename("project.xml")
28 |
29 | # Create a new srcml archive structure
30 | # Options and attributes of cloned archive start the same as
31 | # the original archive
32 | includearchive = iarchive.clone()
33 | otherarchive = iarchive.clone()
34 |
35 | # Open srcml archive for output
36 | includearchive.write_open_filename("project_include.xml")
37 | otherarchive.write_open_filename("project_other.xml")
38 |
39 | # Copy the files from the input archive to the output archive
40 | unit = iarchive.read_unit()
41 | while unit != None :
42 | # Get the filename
43 | filename = unit.get_filename()
44 |
45 | # Add to archive according to file type
46 | if ".h" == filename[len(filename)-2:len(filename)] :
47 | includearchive.write_unit(unit)
48 | else :
49 | otherarchive.write_unit(unit)
50 |
51 | unit = iarchive.read_unit()
52 |
53 |
54 |
55 | # Close the archives
56 | includearchive.close()
57 | otherarchive.close()
58 | iarchive.close()
--------------------------------------------------------------------------------
/examples/srcml_transform/copy.xsl:
--------------------------------------------------------------------------------
1 |
6 |
7 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/examples/srcml_transform/project.xml:
--------------------------------------------------------------------------------
1 |
2 | // Sample program
3 | #include <iostream>
4 |
5 | int main(int argc, char* argv[]) {
6 | std::cout << "Hello World!" << std::endl;
7 | }
8 |
--------------------------------------------------------------------------------
/examples/srcml_transform/schema.rng:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/examples/srcml_transform/srcml_transform.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file srcml_transform.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | # Example program of the use of the Python API for srcML.
13 | # Use XPath, XSLT, and RelaxNG.
14 |
15 | from posixpath import isabs
16 | from re import I
17 | from stat import S_IWUSR
18 | import sys
19 | import difflib
20 | import os
21 | import ctypes
22 | import stat
23 | from unicodedata import numeric
24 | from pylibsrcml import srcml
25 |
26 | # Create an archive and read in input
27 | iarchive = srcml.srcml_archive()
28 | iarchive.read_open_filename("project.xml")
29 |
30 | # Create a clone and open it for output
31 | oarchive = iarchive.clone()
32 | oarchive.write_open_filename("transform.xml")
33 |
34 | # Append transforms
35 | iarchive.append_transform_xpath("//src:unit")
36 | iarchive.append_transform_xslt_filename("copy.xsl")
37 | iarchive.append_transform_relaxng_filename("schema.rng")
38 |
39 | # Close archives
40 | iarchive.close()
41 | oarchive.close()
--------------------------------------------------------------------------------
/examples/srcml_xpath/project.xml:
--------------------------------------------------------------------------------
1 |
2 | // Sample program
3 | #include <iostream>
4 |
5 | int main(int argc, char* argv[]) {
6 | std::cout << "Hello World!" << std::endl;
7 | }
8 |
--------------------------------------------------------------------------------
/examples/srcml_xpath/srcml_xpath.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file srcml_xpath.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | # Example program of the use of the Python API for srcML.
13 | # XPath usage.
14 |
15 | from posixpath import isabs
16 | from re import I
17 | from stat import S_IWUSR
18 | import sys
19 | import difflib
20 | import os
21 | import ctypes
22 | import stat
23 | from unicodedata import numeric
24 | from pylibsrcml import srcml
25 |
26 | # Create an archive and read in input
27 | iarchive = srcml.srcml_archive()
28 | iarchive.read_open_filename("project.xml")
29 |
30 | # Create a clone and open it for output
31 | oarchive = iarchive.clone()
32 | oarchive.write_open_filename("xpath.xml")
33 |
34 | # Append transform
35 | iarchive.append_transform_xpath("//src:unit")
36 |
37 | # Close archives
38 | iarchive.close()
39 | oarchive.close()
--------------------------------------------------------------------------------
/examples/srcml_xslt/project.xml:
--------------------------------------------------------------------------------
1 |
2 | // Sample program
3 | #include <iostream>
4 |
5 | int main(int argc, char* argv[]) {
6 | std::cout << "Hello World!" << std::endl;
7 | }
8 |
--------------------------------------------------------------------------------
/examples/srcml_xslt/srcml_xslt.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file srcml_xslt.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | # Example program of the use of the C API for srcML.
13 | # XSLT usage.
14 |
15 | from posixpath import isabs
16 | from re import I
17 | from stat import S_IWUSR
18 | import sys
19 | import difflib
20 | import os
21 | import ctypes
22 | import stat
23 | from unicodedata import numeric
24 | from pylibsrcml import srcml
25 |
26 | # Create an archive and read in input
27 | iarchive = srcml.srcml_archive()
28 | iarchive.read_open_filename("project.xml")
29 |
30 | # Create a clone and open it for output
31 | oarchive = iarchive.clone()
32 | oarchive.write_open_filename("xslt.xml")
33 |
34 | # Append transform
35 | iarchive.append_transform_xslt_filename("copy.xsl")
36 |
37 | # Close archives
38 | iarchive.close()
39 | oarchive.close()
--------------------------------------------------------------------------------
/pyproject.toml:
--------------------------------------------------------------------------------
1 | [build-system]
2 | requires = [
3 | "setuptools>=42",
4 | "wheel"
5 | ]
6 | build-backend = "setuptools.build_meta"
7 |
--------------------------------------------------------------------------------
/setup.py:
--------------------------------------------------------------------------------
1 | #############################################################################
2 | # @file setup.py
3 | #
4 | # @copyright Copyright (C) 2018-2019 srcML, LLC. (www.srcML.org)
5 | #
6 | # This file is part of the srcML Toolkit.
7 | #
8 | # The srcML Toolkit is free software; you can redistribute it and/or modify
9 | # it under the terms of the GNU General Public License as published by
10 | # the Free Software Foundation; either version 2 of the License, or
11 | # (at your option) any later version.
12 | #
13 | # The srcML Toolkit is distributed in the hope that it will be useful,
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 | # GNU General Public License for more details.
17 | # You should have received a copy of the GNU General Public License
18 | # along with the srcML Toolkit; if not, write to the Free Software
19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 | #############################################################################
21 |
22 | import setuptools
23 |
24 | setuptools.setup(
25 | name = 'pylibsrcml',
26 | version = '1.0.0-1',
27 | author = 'srcML Team',
28 | author_email = 'srcml@kent.edu',
29 | package_dir={"": "src"},
30 | packages=setuptools.find_packages(where="src"),
31 | py_modules = ['libsrcml'],
32 | url = 'https://www.srcml.org/',
33 | license = 'LICENSE.txt',
34 | description = 'A set of Python bindings for srcML.',
35 | long_description = open('README.md').read(),
36 | python_requires=">=3.7",
37 | classifiers=[
38 | "Programming Language :: Python :: 3",
39 | "License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
40 | "Operating System :: OS Independent",
41 | ]
42 | )
43 |
--------------------------------------------------------------------------------
/src/pylibsrcml/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/srcML/pylibsrcml/193563f918e08c061c0da0194fdffeb223ce54cf/src/pylibsrcml/__init__.py
--------------------------------------------------------------------------------
/src/pylibsrcml/exception.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file exception.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | from pylibsrcml.options import SRCML_STATUS_OK
13 |
14 | # Checks return status value of libsrcml call
15 | # Raises a srcML exception if status is not okay
16 | def check_return(value) :
17 | if value == SRCML_STATUS_OK :
18 | return
19 | raise srcMLException("Recieved invalid return status: " + str(value))
20 |
21 | # Class representing a srcml exception
22 | class srcMLException(Exception) :
23 |
24 | def __init__(self, message) :
25 | self.message = message
26 |
27 | def __str__(self) :
28 | return self.message
29 |
--------------------------------------------------------------------------------
/src/pylibsrcml/gen_options.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file gen_options.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | SRC2SRCML = src2srcml
13 | SRCML2SRC = srcml2src
14 |
15 | $SRC2SRCML ../../src/libsrcml/srcml.h | $SRCML2SRC --xpath "//cpp:define | //src:comment[following-sibling::*[1][self::cpp:define]]" | $SRCML2SRC -0 | tr "\0" "\n" | tail +2 | sed "s/#define *//" | sed "s/ [^ ]/ =&/" | sed "s/\/[*]* =/\#/" | sed "s/ *\*\///"
16 |
--------------------------------------------------------------------------------
/src/pylibsrcml/globals.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file globals.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | import os
13 | from ctypes import cdll, c_int, c_size_t, c_char_p, POINTER, c_ulonglong
14 | from pylibsrcml.exception import *
15 |
16 | LIBSRCML_PATH = ""
17 | if os.path.exists('../bin/libsrcml.dylib') :
18 | LIBSRCML_PATH = "../bin/libsrcml.dylib"
19 | elif os.path.exists('../bin/libsrcml.so') :
20 | LIBSRCML_PATH = "../bin/libsrcml.so"
21 | elif os.path.exists('../bin/libsrcml.dll') :
22 | LIBSRCML_PATH = "../bin/libsrcml.dll"
23 | elif os.path.exists('../../bin/libsrcml.dylib') :
24 | LIBSRCML_PATH = "../../bin/libsrcml.dylib"
25 | elif os.path.exists('../../bin/libsrcml.so') :
26 | LIBSRCML_PATH = "../../bin/libsrcml.so"
27 | elif os.path.exists('../../bin/libsrcml.dll') :
28 | LIBSRCML_PATH = "../../bin/libsrcml.dll"
29 | elif os.path.exists('../bin/Release/libsrcml.dylib') :
30 | LIBSRCML_PATH = "../bin/Release/libsrcml.dylib"
31 | elif os.path.exists('../bin/Release/libsrcml.so') :
32 | LIBSRCML_PATH = "../bin/Release/libsrcml.so"
33 | elif os.path.exists('../bin/Release/libsrcml.dll') :
34 | LIBSRCML_PATH = "../bin/Release/libsrcml.dll"
35 | elif os.path.exists('../bin/Debug/libsrcml.dylib') :
36 | LIBSRCML_PATH = "../bin/Debug/libsrcml.dylib"
37 | elif os.path.exists('../bin/Debug/libsrcml.so') :
38 | LIBSRCML_PATH = "../bin/Debug/libsrcml.so"
39 | elif os.path.exists('../bin/Debug/libsrcml.dll') :
40 | LIBSRCML_PATH = "../bin/Debug/libsrcml.dll"
41 | elif os.path.exists("/usr/lib/libsrcml.so") :
42 | LIBSRCML_PATH = "/usr/lib/libsrcml.so"
43 | elif os.path.exists("/usr/local/lib/libsrcml.dylib") :
44 | LIBSRCML_PATH = "/usr/local/lib/libsrcml.dylib"
45 |
46 | libsrcml = cdll.LoadLibrary(LIBSRCML_PATH)
47 |
48 | def printPath(p = LIBSRCML_PATH) :
49 | print("PATH:" + p)
50 |
51 | # UTILITY FUNCTIONS
52 |
53 | # int srcml_version_number();
54 | # -------------------------------------------------------------------------------------------
55 | # The current version of the library
56 | # Return: Version of libsrcml as a number
57 | # -------------------------------------------------------------------------------------------
58 | libsrcml.srcml_version_number.restype = c_int
59 | libsrcml.srcml_version_number.argtypes = []
60 |
61 | def version_number() :
62 | return libsrcml.srcml_version_number()
63 |
64 | # const char* srcml_version_string();
65 | # -------------------------------------------------------------------------------------------
66 | # The current version of the library
67 | # Return: Version of libsrcml as a string
68 | # -------------------------------------------------------------------------------------------
69 | libsrcml.srcml_version_string.restype = c_char_p
70 | libsrcml.srcml_version_string_argtypes = []
71 |
72 | def version_string() :
73 | result = libsrcml.srcml_version_string()
74 | if result != None :
75 | result = bytes.decode(result)
76 |
77 | return result
78 |
79 | # int srcml_check_language(const char* language);
80 | # -------------------------------------------------------------------------------------------
81 | # Checks if a source-code language is supported.
82 | # Parameter: language -> The language to check support for as a string
83 | # Return Value: pos -> The numeric representation for that language
84 | # Return Value: 0 -> If the language is not supported
85 | # -------------------------------------------------------------------------------------------
86 | libsrcml.srcml_check_language.restype = c_int
87 | libsrcml.srcml_check_language.argtypes = [c_char_p]
88 |
89 | def check_language(language) :
90 | if language != None :
91 | language = str.encode(language)
92 |
93 | result = libsrcml.srcml_check_language(language)
94 | if result == 0 :
95 | return None
96 | return result
97 |
98 | # const char* srcml_check_extension(const char* filename);
99 | # -------------------------------------------------------------------------------------------
100 | # Check the current registered language for a file extension
101 | # Parameter: filename -> The name of a file. When a full filename is given, the extension is extracted
102 | # Return: The language name registered with that extension on success
103 | # Return: NULL on failure
104 | # -------------------------------------------------------------------------------------------
105 | libsrcml.srcml_check_extension.restype = c_char_p
106 | libsrcml.srcml_check_extension.argtypes = [c_char_p]
107 |
108 | def check_extension(filename) :
109 | if filename != None :
110 | filename = str.encode(filename)
111 |
112 | result = libsrcml.srcml_check_extension(filename)
113 | if result != None :
114 | result = bytes.decode(result)
115 | return result
116 |
117 | # size_t srcml_get_language_list_size();
118 | # -------------------------------------------------------------------------------------------
119 | # Gets the number of supported source-code languages
120 | # Return: The number of source-code languages supported
121 | # -------------------------------------------------------------------------------------------
122 | libsrcml.srcml_get_language_list_size.restype = c_size_t
123 | libsrcml.srcml_get_language_list_size.argtypes = []
124 |
125 | def get_language_list_size() :
126 | return libsrcml.srcml_get_language_list_size()
127 |
128 | # const char* srcml_get_language_list(size_t pos);
129 | # -------------------------------------------------------------------------------------------
130 | # Gets the name of the supported language at a given position
131 | # Parameter: pos -> The position of the language in the supported language list
132 | # Return: The name of the supported source-code language on success
133 | # Return: NULL on failure
134 | # -------------------------------------------------------------------------------------------
135 | libsrcml.srcml_get_language_list.restype = c_char_p
136 | libsrcml.srcml_get_language_list.argtypes = [c_size_t]
137 |
138 | def get_language_list(pos) :
139 | res = libsrcml.srcml_get_language_list(pos)
140 | if res != None:
141 | res = bytes.decode(res)
142 | return res
143 |
144 | # int srcml_check_encoding(const char* encoding);
145 | # -------------------------------------------------------------------------------------------
146 | # Check if a particular encoding is supported for input and output
147 | # Parameter: encoding -> The name of the encoding
148 | # -------------------------------------------------------------------------------------------
149 | libsrcml.srcml_check_encoding.restype = c_int
150 | libsrcml.srcml_check_encoding.argtypes = [c_char_p]
151 |
152 | def check_encoding(encoding) :
153 | if encoding != None :
154 | encoding = str.encode(encoding)
155 |
156 | res = libsrcml.srcml_check_encoding(encoding)
157 | if res == 0:
158 | return None
159 | return res
160 |
161 | # int srcml_check_xslt();
162 | # -------------------------------------------------------------------------------------------
163 | # Check if XSLT is available
164 | # Return Value: 1 -> if XSLT is available
165 | # Return Value: 0 -> if it is unavailable
166 | # -------------------------------------------------------------------------------------------
167 | libsrcml.srcml_check_xslt.restype = c_int
168 | libsrcml.srcml_check_xslt.argtypes = []
169 |
170 | def check_xslt() :
171 | res = libsrcml.srcml_check_xslt()
172 | if res == 0:
173 | return None
174 | return res
175 |
176 | # int srcml_check_exslt();
177 | # -------------------------------------------------------------------------------------------
178 | # Check if EXSLT is available
179 | # Return Value: 1 -> if EXSLT is available
180 | # Return Value: 0 -> if it is unavailable
181 | # -------------------------------------------------------------------------------------------
182 | libsrcml.srcml_check_exslt.restype = c_int
183 | libsrcml.srcml_check_exslt.argtypes = []
184 |
185 | def check_exslt() :
186 | res = libsrcml.srcml_check_exslt()
187 | if res == 0:
188 | return None
189 | return res
190 |
191 | # const char* srcml_error_string();
192 | # -------------------------------------------------------------------------------------------
193 | # Provides a description of the last error to occur
194 | # Return: A string describing last recorded error
195 | # -------------------------------------------------------------------------------------------
196 | libsrcml.srcml_error_string.restype = c_char_p
197 | libsrcml.srcml_error_string.argtypes = []
198 |
199 | def error_string() :
200 | result = libsrcml.srcml_error_string()
201 | if result != None :
202 | result = bytes.decode(result)
203 |
204 | return result
205 |
206 | # void srcml_memory_free(char * buffer);
207 | # -------------------------------------------------------------------------------------------
208 | # Free a memory buffer allocated by functions such as srcml_archive_write_open_memory()
209 | # Parameter: buffer -> The allocated buffer
210 | # -------------------------------------------------------------------------------------------
211 | libsrcml.srcml_memory_free.restype = None
212 | libsrcml.srcml_memory_free.argtypes = [c_char_p]
213 |
214 | def memory_free(buffer) :
215 | if buffer != None :
216 | buffer = str.encode(buffer)
217 |
218 | libsrcml.srcml_memory_free(buffer)
219 |
220 |
221 |
222 | # CONVENIENCE FUNCTIONS
223 |
224 | # int srcml(const char* input_filename, const char* output_filename);
225 | # -------------------------------------------------------------------------------------------
226 | # Translate to and from the srcML format
227 | # Translates from source code to srcML if the input_filename
228 | # extension is for source code, e.g., .c, .cpp, .java Language
229 | # determined by file extension if language is not set with
230 | # srcml_set_language(). Translates from srcML to source code if the
231 | # input_filename extension is '.xml'
232 | # Parameter: input_filename -> The name of a source-code file or srcML file
233 | # Parameter: output_filename -> The name of the output srcML file or source-code file
234 | # Return: SRCML_STATUS_OK on success
235 | # Return: Status error on failure
236 | # -------------------------------------------------------------------------------------------
237 | libsrcml.srcml.restype = c_int
238 | libsrcml.srcml.argtypes = [c_char_p, c_char_p]
239 |
240 | def srcml(input_filename, output_filename) :
241 | check_return(libsrcml.srcml(str.encode(input_filename), str.encode(output_filename)))
242 |
243 | # int srcml_set_src_encoding(const char* encoding);
244 | # -------------------------------------------------------------------------------------------
245 | # Set the source encoding for the srcML
246 | # Parameter: encoding -> An output encoding
247 | # Return Value: SRCML_STATUS_OK -> on success
248 | # Return Value: SRCML_STATUS_INVALID_ARGUMENT
249 | # -------------------------------------------------------------------------------------------
250 | libsrcml.srcml_set_src_encoding.restype = c_char_p
251 | libsrcml.srcml_set_src_encoding.argtypes = [c_char_p]
252 |
253 | def set_src_encoding(encoding) :
254 | check_return(libsrcml.srcml_set_src_encoding(str.encode(encoding)))
255 |
256 | # int srcml_set_xml_encoding(const char* encoding);
257 | # -------------------------------------------------------------------------------------------
258 | # Set the xml encoding for the srcML
259 | # Parameter: encoding -> An output encoding
260 | # Return Value: SRCML_STATUS_OK -> on success
261 | # Return Value: SRCML_STATUS_INVALID_ARGUMENT
262 | # -------------------------------------------------------------------------------------------
263 | libsrcml.srcml_set_xml_encoding.restype = c_int
264 | libsrcml.srcml_set_xml_encoding.argtypes = [c_char_p]
265 |
266 | def set_xml_encoding(encoding) :
267 | check_return(libsrcml.srcml_set_xml_encoding(str.encode(encoding)))
268 |
269 | # int srcml_set_language(const char* language);
270 | # -------------------------------------------------------------------------------------------
271 | # Set the language used to parse for the srcML
272 | # Parameter: language -> A supported source-code language
273 | # Return Value: SRCML_STATUS_OK -> on success
274 | # Return Value: SRCML_STATUS_INVALID_ARGUMENT
275 | # -------------------------------------------------------------------------------------------
276 | libsrcml.srcml_set_language.restype = c_int
277 | libsrcml.srcml_set_language.argtypes = [c_char_p]
278 |
279 | def set_language(language) :
280 | check_return(libsrcml.srcml_set_language(str.encode(language)))
281 |
282 | # int srcml_set_filename(const char* filename);
283 | # -------------------------------------------------------------------------------------------
284 | # Set the filename attribute for the srcML
285 | # Parameter: filename -> Name of a file
286 | # Return Value: SRCML_STATUS_OK -> on success
287 | # Return Value: SRCML_STATUS_INVALID_ARGUMENT
288 | # -------------------------------------------------------------------------------------------
289 | libsrcml.srcml_set_filename.restype = c_int
290 | libsrcml.srcml_set_filename.argtypes = [c_char_p]
291 |
292 | def set_filename(filename) :
293 | check_return(libsrcml.srcml_set_filename(str.encode(filename)))
294 |
295 | # int srcml_set_url(const char* url);
296 | # -------------------------------------------------------------------------------------------
297 | # Set the url attribute for the srcML
298 | # Note: The url is not checked for validity
299 | # Parameter: url -> A url path
300 | # Return Value: SRCML_STATUS_OK -> on success
301 | # Return Value: SRCML_STATUS_INVALID_ARGUMENT
302 | # -------------------------------------------------------------------------------------------
303 | libsrcml.srcml_set_url.restype = c_int
304 | libsrcml.srcml_set_url.argtypes = [c_char_p]
305 |
306 | def set_url(url) :
307 | check_return(libsrcml.srcml_set_url(str.encode(url)))
308 |
309 | # int srcml_set_version(const char* version);
310 | # -------------------------------------------------------------------------------------------
311 | # Set the version attribute for the srcML
312 | # Note: The version value is user-defined, and can be any value
313 | # Parameter: version -> A version
314 | # Return Value: SRCML_STATUS_OK -> on success
315 | # Return Value: SRCML_STATUS_INVALID_ARGUMENT
316 | # -------------------------------------------------------------------------------------------
317 | libsrcml.srcml_set_version.restype = c_int
318 | libsrcml.srcml_set_version.argtypes = [c_char_p]
319 |
320 | def set_version(version) :
321 | check_return(libsrcml.srcml_set_version(str.encode(version)))
322 |
323 | # int srcml_set_timestamp(const char* timestamp);
324 | # -------------------------------------------------------------------------------------------
325 | # Set the timestamp attribute for the srcML
326 | # Parameter: timestamp -> A timestamp string in any format
327 | # Return Value: SRCML_STATUS_OK on success
328 | # Return Value: SRCML_STATUS_INVALID_ARGUMENT
329 | # -------------------------------------------------------------------------------------------
330 | libsrcml.srcml_set_timestamp.restype = c_int
331 | libsrcml.srcml_set_timestamp.argtypes = [c_char_p]
332 |
333 | def set_timestamp(timestamp) :
334 | check_return(libsrcml.srcml_set_timestamp(str.encode(timestamp)))
335 |
336 | # int srcml_set_options(size_t option);
337 | # -------------------------------------------------------------------------------------------
338 | # Set options on the srcML, clearing all previously set options
339 | # Parameter: option -> A srcML option
340 | # Return Value: SRCML_STATUS_OK on success
341 | # Return Value: SRCML_STATUS_INVALID_ARGUMENT
342 | # -------------------------------------------------------------------------------------------
343 | libsrcml.srcml_set_options.restype = c_int
344 | libsrcml.srcml_set_options.argtypes = [c_size_t]
345 |
346 | def set_options(option) :
347 | check_return(libsrcml.srcml_set_options(option))
348 |
349 | # int srcml_enable_option(size_t option);
350 | # -------------------------------------------------------------------------------------------
351 | # Enable (set) a specific option on the srcML
352 | # Parameter: option -> The srcML option(s)
353 | # Return Value: SRCML_STATUS_OK -> on success
354 | # Return Value: SRCML_STATUS_INVALID_ARGUMENT
355 | # -------------------------------------------------------------------------------------------
356 | libsrcml.srcml_enable_option.restype = c_int
357 | libsrcml.srcml_enable_option.argtypes = [c_size_t]
358 |
359 | def enable_option(option) :
360 | check_return(libsrcml.srcml_enable_option(option))
361 |
362 | # int srcml_disable_option(size_t option);
363 | # -------------------------------------------------------------------------------------------
364 | # Disable (unset) a specific option on the srcML
365 | # Parameter: option -> The srcML option(s)
366 | # Return Value: SRCML_STATUS_OK -> on success
367 | # Return Value: SRCML_STATUS_INVALID_ARGUMENT
368 | # -------------------------------------------------------------------------------------------
369 | libsrcml.srcml_disable_option.restype = c_int
370 | libsrcml.srcml_disable_option.argtypes = [c_size_t]
371 |
372 | def disable_option(option) :
373 | check_return(libsrcml.srcml_disable_option(option))
374 |
375 | # int srcml_set_tabstop(size_t tabstop);
376 | # -------------------------------------------------------------------------------------------
377 | # Set the size of the tabstop on the srcML
378 | # Parameter: tabstop -> Tabstop size
379 | # Return Value: SRCML_STATUS_OK -> on success
380 | # Return Value: SRCML_STATUS_INVALID_ARGUMENT
381 | # -------------------------------------------------------------------------------------------
382 | libsrcml.srcml_set_tabstop.restype = c_int
383 | libsrcml.srcml_set_tabstop.argtypes = [c_size_t]
384 |
385 | def set_tabstop(tabstop) :
386 | check_return(libsrcml.srcml_set_tabstop(tabstop))
387 |
388 | # int srcml_register_file_extension(const char* extension, const char* language);
389 | # -------------------------------------------------------------------------------------------
390 | # Associate an extension with a supported source-code language on the srcML
391 | # Parameter: extension -> A source file extension
392 | # Parameter: language -> A supported source code language
393 | # Return: SRCML_STATUS_OK on success
394 | # Return: Status error code on failure
395 | # -------------------------------------------------------------------------------------------
396 | libsrcml.srcml_register_file_extension.restype = c_int
397 | libsrcml.srcml_register_file_extension.argtypes = [c_char_p, c_char_p]
398 |
399 | def register_file_extension(extension, language) :
400 | check_return(libsrcml.srcml_register_file_extension(str.encode(extension), str.encode(language)))
401 |
402 | # int srcml_register_namespace(const char* prefix, const char* ns);
403 | # -------------------------------------------------------------------------------------------
404 | # Add a new namespace or change the prefix of an existing namespace on the srcML
405 | # Parameter: prefix -> An XML namespace prefix
406 | # Parameter: ns -> An XML namespace
407 | # Return Value: SRCML_STATUS_OK -> on success
408 | # Return Value: SRCML_STATUS_* -> Status error code on failure
409 | # -------------------------------------------------------------------------------------------
410 | libsrcml.srcml_register_namespace.restype = c_int
411 | libsrcml.srcml_register_namespace.argtypes = [c_char_p, c_char_p]
412 |
413 | def register_namespace(prefix, ns) :
414 | check_return(libsrcml.srcml_register_namespace(str.encode(prefix), str.encode(ns)))
415 |
416 | # int srcml_set_processing_instruction(const char* target, const char* data);
417 | # -------------------------------------------------------------------------------------------
418 | # Set a processing instruction that will be output before the root element of an archive
419 | # Parameter: target -> The processing instruction's target
420 | # Parameter: data -> The processing instruciton's data
421 | # Return: SRCML_STATUS_OK on success
422 | # Return: Status error code on failure
423 | # -------------------------------------------------------------------------------------------
424 | libsrcml.srcml_set_processing_instruction.restype = c_int
425 | libsrcml.srcml_set_processing_instruction.argtypes = [c_char_p, c_char_p]
426 |
427 | def set_processing_instruction(target, data) :
428 | check_return(libsrcml.srcml_set_processing_instruction(str.encode(target), str.encode(data)))
429 |
430 | # int srcml_set_eol(size_t eol);
431 | # -------------------------------------------------------------------------------------------
432 | # Set the end of line characters to be used for unparse
433 | # Parameter: eol -> The kind of eol to use for unparse
434 | # Return Value: SRCML_STATUS_OK -> on success
435 | # Return Value: SRCML_STATUS_INVALID_ARGUMENT
436 | # -------------------------------------------------------------------------------------------
437 | libsrcml.srcml_set_eol.restype = c_int
438 | libsrcml.srcml_set_eol.argtypes = [c_size_t]
439 |
440 | def set_eol(eol) :
441 | check_return(libsrcml.srcml_set_eol(eol))
442 |
443 | # int srcml_set_srcdiff_revision(size_t revision_number);
444 | # -------------------------------------------------------------------------------------------
445 | # Set what revision in a srcDiff document to operate with
446 | # Parameter: revision_number -> The revision to operate with
447 | # Return: SRCML_STATUS_OK on success
448 | # Return: Status error code on failure
449 | # -------------------------------------------------------------------------------------------
450 | libsrcml.srcml_set_srcdiff_revision.restype = c_int
451 | libsrcml.srcml_set_srcdiff_revision.argtypes = [c_size_t]
452 |
453 | def set_srcdiff_revision(revision_number) :
454 | check_return(libsrcml.srcml_set_srcdiff_revision(revision_number))
455 |
456 | # const char* srcml_get_src_encoding();
457 | # -------------------------------------------------------------------------------------------
458 | # Return: The source encoding on success
459 | # Return: NULL on failure
460 | # -------------------------------------------------------------------------------------------
461 | libsrcml.srcml_get_src_encoding.restype = c_char_p
462 | libsrcml.srcml_get_src_encoding.argtypes = []
463 |
464 | def get_src_encoding() :
465 | result = libsrcml.srcml_get_src_encoding()
466 | if result != None :
467 | result = bytes.decode(result)
468 |
469 | return result
470 |
471 | # const char* srcml_get_xml_encoding();
472 | # -------------------------------------------------------------------------------------------
473 | # Return: The XML encoding on success
474 | # Return: NULL on failure
475 | # -------------------------------------------------------------------------------------------
476 | libsrcml.srcml_get_xml_encoding.restype = c_char_p
477 | libsrcml.srcml_get_xml_encoding.argtypes = []
478 |
479 | def get_xml_encoding() :
480 | result = libsrcml.srcml_get_xml_encoding()
481 | if result != None :
482 | result = bytes.decode(result)
483 |
484 | return result
485 |
486 | # const char* srcml_get_revision();
487 | # -------------------------------------------------------------------------------------------
488 | # Return: The srcML revision attribute on success
489 | # Return: NULL on failure
490 | # -------------------------------------------------------------------------------------------
491 | libsrcml.srcml_get_revision.restype = c_char_p
492 | libsrcml.srcml_get_revision.argtypes = []
493 |
494 | def get_revision() :
495 | result = libsrcml.srcml_get_revision()
496 | if result != None :
497 | result = bytes.decode(result)
498 |
499 | return result
500 |
501 | # const char* srcml_get_language();
502 | # -------------------------------------------------------------------------------------------
503 | # Return: The language attribute on success
504 | # Return: NULL on failure
505 | # -------------------------------------------------------------------------------------------
506 | libsrcml.srcml_get_language.restype = c_char_p
507 | libsrcml.srcml_get_language.argtypes = []
508 |
509 | def get_language() :
510 | result = libsrcml.srcml_get_language()
511 | if result != None :
512 | result = bytes.decode(result)
513 |
514 | return result
515 |
516 | # const char* srcml_get_filename();
517 | # -------------------------------------------------------------------------------------------
518 | # Return: The filename attribute on success
519 | # Return: NULL on failure
520 | # -------------------------------------------------------------------------------------------
521 | libsrcml.srcml_get_filename.restype = c_char_p
522 | libsrcml.srcml_get_filename.argtypes = []
523 |
524 | def get_filename() :
525 | result = libsrcml.srcml_get_filename()
526 | if result != None :
527 | result = bytes.decode(result)
528 |
529 | return result
530 |
531 | # const char* srcml_get_url();
532 | # -------------------------------------------------------------------------------------------
533 | # Return: The url attribute for the root unit on success
534 | # Return: NULL on failure
535 | # -------------------------------------------------------------------------------------------
536 | libsrcml.srcml_get_url.restype = c_char_p
537 | libsrcml.srcml_get_url.argtypes = []
538 |
539 | def get_url() :
540 | result = libsrcml.srcml_get_url()
541 | if result != None :
542 | result = bytes.decode(result)
543 |
544 | return result
545 |
546 | # const char* srcml_get_version();
547 | # -------------------------------------------------------------------------------------------
548 | # Return: The version attribute on success
549 | # Return: NULL on failure
550 | # -------------------------------------------------------------------------------------------
551 | libsrcml.srcml_get_version.restype = c_char_p
552 | libsrcml.srcml_get_version.argtypes = []
553 |
554 | def get_version() :
555 | result = libsrcml.srcml_get_version()
556 | if result != None :
557 | result = bytes.decode(result)
558 |
559 | return result
560 |
561 | # const char* srcml_get_timestamp();
562 | # -------------------------------------------------------------------------------------------
563 | # Return: The timestamp attribute on success
564 | # Return: NULL on failure
565 | # -------------------------------------------------------------------------------------------
566 | libsrcml.srcml_get_timestamp.restype = c_char_p
567 | libsrcml.srcml_get_timestamp.argtypes = []
568 |
569 | def get_timestamp() :
570 | result = libsrcml.srcml_get_timestamp()
571 | if result != None :
572 | result = bytes.decode(result)
573 |
574 | return result
575 |
576 | # const char* srcml_get_hash();
577 | # -------------------------------------------------------------------------------------------
578 | # Return: The loc of the source code on success
579 | # Return: -1 on failure
580 | # -------------------------------------------------------------------------------------------
581 | libsrcml.srcml_get_hash.restype = c_char_p
582 | libsrcml.srcml_get_hash.argtypes = []
583 |
584 | def get_hash() :
585 | result = libsrcml.srcml_get_hash()
586 | if result != None :
587 | result = bytes.decode(result)
588 |
589 | return result
590 |
591 | # int srcml_get_loc();
592 | # -------------------------------------------------------------------------------------------
593 | # Return: The eol for to-src output (unparse)
594 | # Return: NULL on failure
595 | # -------------------------------------------------------------------------------------------
596 | libsrcml.srcml_get_loc.restype = c_int
597 | libsrcml.srcml_get_loc.argtypes = []
598 |
599 | def get_loc() :
600 | return libsrcml.srcml_get_loc()
601 |
602 | # size_t srcml_get_eol();
603 | # -------------------------------------------------------------------------------------------
604 | # Return: The currently set options on success
605 | # Return: NULL on failure
606 | # -------------------------------------------------------------------------------------------
607 | libsrcml.srcml_get_eol.restypes = c_size_t
608 | libsrcml.srcml_get_eol.argtypes = []
609 |
610 | def get_eol() :
611 | return libsrcml.srcml_get_eol()
612 |
613 | # int srcml_get_options();
614 | # -------------------------------------------------------------------------------------------
615 | # Return: The currently set options on success
616 | # Return: NULL on failure
617 | # -------------------------------------------------------------------------------------------
618 | libsrcml.srcml_get_options.restype = c_int
619 | libsrcml.srcml_get_options.argtypes = []
620 |
621 | def get_options() :
622 | return libsrcml.srcml_get_options()
623 |
624 | # size_t srcml_get_tabstop();
625 | # -------------------------------------------------------------------------------------------
626 | # Return: The tabstop size on success
627 | # Return: NULL on failure
628 | # -------------------------------------------------------------------------------------------
629 | libsrcml.srcml_get_tabstop.restype = c_size_t
630 | libsrcml.srcml_get_tabstop.argtypes = []
631 |
632 | def get_tabstop() :
633 | return libsrcml.srcml_get_tabstop()
634 |
635 | # const char* srcml_get_processing_instruction_target();
636 | # -------------------------------------------------------------------------------------------
637 | # Return: The processing instruction target
638 | # -------------------------------------------------------------------------------------------
639 | libsrcml.srcml_get_processing_instruction_target.restype = c_char_p
640 | libsrcml.srcml_get_processing_instruction_target.argtypes = []
641 |
642 | def get_processing_instruction_target() :
643 | result = libsrcml.srcml_get_processing_instruction_target()
644 | if result != None :
645 | result = bytes.decode(result)
646 |
647 | return result
648 |
649 | # const char* srcml_get_processing_instruction_data();
650 | # -------------------------------------------------------------------------------------------
651 | # Return: The processing instruction data
652 | # -------------------------------------------------------------------------------------------
653 | libsrcml.srcml_get_processing_instruction_data.restype = c_char_p
654 | libsrcml.srcml_get_processing_instruction_data.argtypes = []
655 |
656 | def get_processing_instruction_data() :
657 | result = libsrcml.srcml_get_processing_instruction_data()
658 | if result != None :
659 | result = bytes.decode(result)
660 |
661 | return result
662 |
663 | # size_t srcml_get_namespace_size();
664 | # -------------------------------------------------------------------------------------------
665 | # Return: Number of declared XML namespaces
666 | # -------------------------------------------------------------------------------------------
667 | libsrcml.srcml_get_namespace_size.restype = c_size_t
668 | libsrcml.srcml_get_namespace_size.argtypes = []
669 |
670 | def get_namespace_size() :
671 | return libsrcml.srcml_get_namespace_size()
672 |
673 | # const char* srcml_get_namespace_prefix(size_t pos);
674 | # -------------------------------------------------------------------------------------------
675 | # Get the prefix of the namespace at that position
676 | # Parameter: pos -> The position to get the namespace prefix at
677 | # Return: The prefix, where empty namespace is an empty string
678 | # Return: 0 if given an invalid position
679 | # -------------------------------------------------------------------------------------------
680 | libsrcml.srcml_get_namespace_prefix.restype = c_char_p
681 | libsrcml.srcml_get_namespace_prefix.argtypes = [c_size_t]
682 |
683 | def get_namespace_prefix(pos) :
684 | result = libsrcml.srcml_get_namespace_prefix(pos)
685 | if result != None :
686 | result = bytes.decode(result)
687 |
688 | return result
689 |
690 | # const char* srcml_get_prefix_from_uri(const char* namespace_uri);
691 | # -------------------------------------------------------------------------------------------
692 | # Get the registered prefix for the given namespace
693 | # Parameter: namespace_uri -> An XML namespace
694 | # Return: The registered prefix for the given namespace
695 | # Return: NULL on failure
696 | # -------------------------------------------------------------------------------------------
697 | libsrcml.srcml_get_prefix_from_uri.restype = c_char_p
698 | libsrcml.srcml_get_prefix_from_uri.argtypes = [c_char_p]
699 |
700 | def get_prefix_from_uri(namespace_uri) :
701 | if(namespace_uri != None) :
702 | namespace_uri = str.encode(namespace_uri)
703 |
704 | result = libsrcml.srcml_get_prefix_from_uri(namespace_uri)
705 | if result != None :
706 | result = bytes.decode(result)
707 |
708 | return result
709 |
710 | # const char* srcml_get_namespace_uri(size_t pos);
711 | # -------------------------------------------------------------------------------------------
712 | # Parameter: pos -> position in namespaces
713 | # Return: The namespace URI at that position on succcess
714 | # Return: NULL on failure
715 | # -------------------------------------------------------------------------------------------
716 | libsrcml.srcml_get_namespace_uri.restype = c_char_p
717 | libsrcml.srcml_get_namespace_uri.argtypes = [c_size_t]
718 |
719 | def get_namespace_uri(pos) :
720 | result = libsrcml.srcml_get_namespace_uri(pos)
721 | if result != None :
722 | result = bytes.decode(result)
723 |
724 | return result
725 |
726 | # const char* srcml_get_uri_from_prefix(const char* prefix);
727 | # -------------------------------------------------------------------------------------------
728 | # Parameter: prefix -> An XML prefix
729 | # Return: The first namespace URI for the given prefix on success
730 | # Return: NULL on failure
731 | # -------------------------------------------------------------------------------------------
732 | libsrcml.srcml_get_uri_from_prefix.restype = c_char_p
733 | libsrcml.srcml_get_uri_from_prefix.argtypes = [c_char_p]
734 |
735 | def get_uri_from_prefix(prefix) :
736 | if(prefix != None) :
737 | prefix = str.encode(prefix)
738 |
739 | result = libsrcml.srcml_get_uri_from_prefix(prefix)
740 | if result != None :
741 | result = bytes.decode(result)
742 |
743 | return result
744 |
745 | # void srcml_cleanup_globals();
746 | # -------------------------------------------------------------------------------------------
747 | # Cleanup and free globally allocated items (usually by libxml2)
748 | # -------------------------------------------------------------------------------------------
749 | libsrcml.srcml_cleanup_globals.restype = None
750 | libsrcml.srcml_cleanup_globals.argtypes = []
751 |
752 | def cleanup_globals() :
753 | libsrcml.srcml_cleanup_globals()
--------------------------------------------------------------------------------
/src/pylibsrcml/options.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file options.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | # Current Version
13 | # --------------------------------------------------------
14 | # Number representing libsrcml version
15 | SRCML_VERSION_NUMBER = 1000
16 | # String containing libsrcml version
17 | SRCML_VERSION_STRING = "1.0.0"
18 | # --------------------------------------------------------
19 |
20 |
21 | # Status
22 | # --------------------------------------------------------
23 | # Return status indicating no errors
24 | SRCML_STATUS_OK = 0
25 | # Return status indicating general errors occurred
26 | SRCML_STATUS_ERROR = 1
27 | # Return status indicating an invalid argument
28 | SRCML_STATUS_INVALID_ARGUMENT = 2
29 | # Return status indicating that there is some problem with the input
30 | SRCML_STATUS_INVALID_INPUT = 3
31 | # Return status indicating an invalid read I/O operation (such as write on read only archive)
32 | SRCML_STATUS_INVALID_STATUS_INVALID_IO_OPERATION = 4
33 | # Return status indicating that there is some problem with the input
34 | SRCML_STATUS_IO_ERROR = 5
35 | # Return status indicating an unitialized unit
36 | SRCML_STATUS_UNINITIALIZED_UNIT = 6
37 | # Return status indicating an unset language
38 | SRCML_STATUS_UNSET_LANGUAGE = 7
39 | # Return status indicating their are no transformations
40 | SRCML_STATUS_NO_TRANSFORMATION = 8
41 | # --------------------------------------------------------
42 |
43 |
44 | # Language Set
45 | # --------------------------------------------------------
46 | # Language not set
47 | SRCML_LANGUAGE_NONE = 0
48 | # Language C
49 | SRCML_LANGUAGE_C = "C"
50 | # Language C++
51 | SRCML_LANGUAGE_CXX = "C++"
52 | # Language C#
53 | SRCML_LANGUAGE_CSHARP = "C#"
54 | # Language Java
55 | SRCML_LANGUAGE_JAVA = "Java"
56 | # Language XML
57 | SRCML_LANGUAGE_XML = "XML"
58 | # --------------------------------------------------------
59 |
60 |
61 | # Options
62 | # --------------------------------------------------------
63 | # Do not issue an XML declaration (default: include XML declaration
64 | SRCML_OPTION_NO_XML_DECL = 1<<1
65 | # Include line/column position attributes
66 | SRCML_OPTION_POSITION = 1<<2
67 | # Markup preprocessor elements (default for C, C++)
68 | SRCML_OPTION_CPP = 1<<3
69 | # Leave as text preprocessor else parts (default: markup)
70 | SRCML_OPTION_CPP_TEXT_ELSE = 1<<4
71 | # Markup preprocessor @code #if 0 @endcode sections (default: leave as text)
72 | SRCML_OPTION_CPP_MARKUP_IF0 = 1<<5
73 | # Encode the original source encoding as an attribute
74 | SRCML_OPTION_STORE_ENCODING = 1<<6
75 | # --------------------------------------------------------
76 |
77 |
78 | # Source Output EOL Options
79 | # --------------------------------------------------------
80 | # Source-code end of line determined automatically
81 | SOURCE_OUTPUT_EOL_AUTO = 0
82 | # Source-code end of line determined according to operating system
83 | SOURCE_OUTPUT_EOL_NATIVE = 0
84 | # Source-code end of line is new line only
85 | SOURCE_OUTPUT_EOL_LF = 1
86 | # Source-code end of line is carriage return only
87 | SOURCE_OUTPUT_EOL_CR = 2
88 | # Source-code end of line is carriage return and new line
89 | SOURCE_OUTPUT_EOL_CRLF = 3
90 | # --------------------------------------------------------
--------------------------------------------------------------------------------
/src/pylibsrcml/srcml.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file srcml.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | import sys
13 | import ctypes
14 | import pylibsrcml.srcml_archive
15 | import pylibsrcml.srcml_unit
16 | from pylibsrcml.globals import *
17 | from pylibsrcml.options import *
18 | from pylibsrcml.exception import srcMLException
19 | from ctypes import c_int, c_void_p, c_char_p, CFUNCTYPE
20 |
21 | srcml_archive = pylibsrcml.srcml_archive.srcml_archive
22 | srcml_unit = pylibsrcml.srcml_unit.srcml_unit
23 |
24 | write_callback_t = CFUNCTYPE(c_int, c_void_p, c_char_p, c_int)
25 | read_callback_t = CFUNCTYPE(c_int, c_void_p, c_char_p, c_int)
26 | close_callback_t = CFUNCTYPE(c_int, c_void_p)
--------------------------------------------------------------------------------
/src/pylibsrcml/srcml_unit.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file srcml_unit.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | from pylibsrcml.globals import libsrcml
13 | from ctypes import c_ushort, c_int, c_size_t, c_void_p, c_char_p, pointer, CFUNCTYPE
14 | from pylibsrcml.exception import *
15 |
16 | write_callback_t = CFUNCTYPE(c_int, c_void_p, c_char_p, c_size_t)
17 | read_callback_t = CFUNCTYPE(c_int, c_void_p, c_char_p, c_size_t)
18 | close_callback_t = CFUNCTYPE(c_int, c_void_p)
19 |
20 | # struct srcml_unit* srcml_unit_create(struct srcml_archive* archive);
21 | libsrcml.srcml_unit_create.restype = c_void_p
22 | libsrcml.srcml_unit_create.argtypes = [c_void_p]
23 |
24 | # struct srcml_unit* srcml_unit_clone(const struct srcml_unit* unit);
25 | libsrcml.srcml_unit_clone.restype = c_void_p
26 | libsrcml.srcml_unit_clone.argtypes = [c_void_p]
27 |
28 | # int srcml_unit_error_number(const struct srcml_unit* unit);
29 | libsrcml.srcml_unit_error_number.restype = c_int
30 | libsrcml.srcml_unit_error_number.argtypes = [c_void_p]
31 |
32 | # const char* srcml_unit_error_string(const struct srcml_unit* unit);
33 | libsrcml.srcml_unit_error_string.restype = c_char_p
34 | libsrcml.srcml_unit_error_string.argtypes = [c_void_p]
35 |
36 | # void srcml_unit_free(struct srcml_unit* unit);
37 | libsrcml.srcml_unit_free.restype = None
38 | libsrcml.srcml_unit_free.argtypes = [c_void_p]
39 |
40 | # int srcml_unit_set_src_encoding(struct srcml_unit* unit, const char* encoding);
41 | libsrcml.srcml_unit_set_src_encoding.restype = c_int
42 | libsrcml.srcml_unit_set_src_encoding.argtypes = [c_void_p, c_char_p]
43 |
44 | # int srcml_unit_set_language(struct srcml_unit* unit, const char* language);
45 | libsrcml.srcml_unit_set_language.restype = c_int
46 | libsrcml.srcml_unit_set_language.argtypes = [c_void_p, c_char_p]
47 |
48 | # int srcml_unit_set_filename(struct srcml_unit* unit, const char* filename);
49 | libsrcml.srcml_unit_set_filename.restype = c_int
50 | libsrcml.srcml_unit_set_filename.argtypes = [c_void_p, c_char_p]
51 |
52 | # int srcml_unit_set_version(struct srcml_unit* unit, const char* version);
53 | libsrcml.srcml_unit_set_version.restype = c_int
54 | libsrcml.srcml_unit_set_version.argtypes = [c_void_p, c_char_p]
55 |
56 | # int srcml_unit_set_timestamp(struct srcml_unit* unit, const char* timestamp);
57 | libsrcml.srcml_unit_set_timestamp.restype = c_int
58 | libsrcml.srcml_unit_set_timestamp.argtypes = [c_void_p, c_char_p]
59 |
60 | # int srcml_unit_set_eol(struct srcml_unit* unit, size_t eol);
61 | libsrcml.srcml_unit_set_eol.restype = c_int
62 | libsrcml.srcml_unit_set_eol.argtypes = [c_void_p, c_size_t]
63 |
64 | # const char* srcml_unit_get_src_encoding(const struct srcml_unit* unit);
65 | libsrcml.srcml_unit_get_src_encoding.restype = c_char_p
66 | libsrcml.srcml_unit_get_src_encoding.argtypes = [c_void_p]
67 |
68 | # const char* srcml_unit_get_revision(const struct srcml_unit* unit);
69 | libsrcml.srcml_unit_get_revision.restype = c_char_p
70 | libsrcml.srcml_unit_get_revision.argtypes = [c_void_p]
71 |
72 | # const char* srcml_unit_get_language(const struct srcml_unit* unit);
73 | libsrcml.srcml_unit_get_language.restype = c_char_p
74 | libsrcml.srcml_unit_get_language.argtypes = [c_void_p]
75 |
76 | # const char* srcml_unit_get_filename(const struct srcml_unit* unit);
77 | libsrcml.srcml_unit_get_filename.restype = c_char_p
78 | libsrcml.srcml_unit_get_filename.argtypes = [c_void_p]
79 |
80 | # const char* srcml_unit_get_version(const struct srcml_unit* unit);
81 | libsrcml.srcml_unit_get_version.restype = c_char_p
82 | libsrcml.srcml_unit_get_version.argtypes = [c_void_p]
83 |
84 | # const char* srcml_unit_get_timestamp(const struct srcml_unit* unit);
85 | libsrcml.srcml_unit_get_timestamp.restype = c_char_p
86 | libsrcml.srcml_unit_get_timestamp.argtypes = [c_void_p]
87 |
88 | # const char* srcml_unit_get_hash(const struct srcml_unit* unit);
89 | libsrcml.srcml_unit_get_hash.restype = c_char_p
90 | libsrcml.srcml_unit_get_hash.argtypes = [c_void_p]
91 |
92 | # int srcml_unit_get_loc(const struct srcml_unit* unit);
93 | libsrcml.srcml_unit_get_loc.restype = c_int
94 | libsrcml.srcml_unit_get_loc.argtypes = [c_void_p]
95 |
96 | # size_t srcml_unit_get_eol(struct srcml_unit* unit);
97 | libsrcml.srcml_unit_get_eol.restype = c_size_t
98 | libsrcml.srcml_unit_get_eol.argtypes = [c_void_p]
99 |
100 | # const char* srcml_unit_get_srcml(struct srcml_unit* unit);
101 | libsrcml.srcml_unit_get_srcml.restype = c_char_p
102 | libsrcml.srcml_unit_get_srcml.argtypes = [c_void_p]
103 |
104 | # const char* srcml_unit_get_srcml_outer(struct srcml_unit* unit);
105 | libsrcml.srcml_unit_get_srcml_outer.restype = c_char_p
106 | libsrcml.srcml_unit_get_srcml_outer.argtypes = [c_void_p]
107 |
108 | # const char* srcml_unit_get_srcml_inner(struct srcml_unit* unit);
109 | libsrcml.srcml_unit_get_srcml_inner.restype = c_char_p
110 | libsrcml.srcml_unit_get_srcml_inner.argtypes = [c_void_p]
111 |
112 | # int srcml_unit_parse_filename(struct srcml_unit* unit, const char* src_filename);
113 | libsrcml.srcml_unit_parse_filename.restype = c_int
114 | libsrcml.srcml_unit_parse_filename.argtypes = [c_void_p, c_char_p]
115 |
116 | # int srcml_unit_parse_memory(struct srcml_unit* unit, const char* src_buffer, size_t buffer_size);
117 | libsrcml.srcml_unit_parse_memory.restype = c_int
118 | libsrcml.srcml_unit_parse_memory.argtypes = [c_void_p, c_char_p, c_size_t]
119 |
120 | # int srcml_unit_parse_FILE(struct srcml_unit* unit, FILE* src_file);
121 | libsrcml.srcml_unit_parse_FILE.restype = c_int
122 | libsrcml.srcml_unit_parse_FILE.argtypes = [c_void_p, c_void_p]
123 |
124 | # int srcml_unit_parse_fd(struct srcml_unit* unit, int src_fd);
125 | libsrcml.srcml_unit_parse_fd.restype = c_int
126 | libsrcml.srcml_unit_parse_fd.argtypes = [c_void_p, c_int]
127 |
128 | # int srcml_unit_parse_io(struct srcml_unit* unit, void * context, ssize_t (*read_callback)(void * context, void * buffer, size_t len), int (*close_callback)(void * context));
129 | libsrcml.srcml_unit_parse_io.restype = c_int
130 | libsrcml.srcml_unit_parse_io.argtypes = [c_void_p, c_void_p, read_callback_t, close_callback_t]
131 |
132 | # int srcml_unit_unparse_filename(struct srcml_unit* unit, const char* src_filename);
133 | libsrcml.srcml_unit_unparse_filename.restype = c_int
134 | libsrcml.srcml_unit_unparse_filename.argtypes = [c_void_p, c_char_p]
135 |
136 | # int srcml_unit_unparse_memory(struct srcml_unit* unit, char** src_buffer, size_t * src_size);
137 | libsrcml.srcml_unit_unparse_memory.restype = c_int
138 | libsrcml.srcml_unit_unparse_memory.argtypes = [c_void_p, c_void_p, c_void_p]
139 |
140 | # int srcml_unit_unparse_FILE(struct srcml_unit* unit, FILE* file);
141 | libsrcml.srcml_unit_unparse_FILE.restype = c_int
142 | libsrcml.srcml_unit_unparse_FILE.argtypes = [c_void_p, c_void_p]
143 |
144 | # int srcml_unit_unparse_fd(struct srcml_unit* unit, int fd);
145 | libsrcml.srcml_unit_unparse_fd.restype = c_int
146 | libsrcml.srcml_unit_unparse_fd.argtypes = [c_void_p, c_int]
147 |
148 | # int srcml_unit_unparse_io(struct srcml_unit* unit, void * context, int (*write_callback)(void * context, const char* buffer, int len), int (*close_callback)(void * context));
149 | libsrcml.srcml_unit_unparse_io.restype = c_int
150 | libsrcml.srcml_unit_unparse_io.argtypes = [c_void_p, c_void_p, read_callback_t, close_callback_t]
151 |
152 | # int srcml_write_start_unit(struct srcml_unit* unit);
153 | libsrcml.srcml_write_start_unit.restype = c_int
154 | libsrcml.srcml_write_start_unit.argtypes = [c_void_p]
155 |
156 | # int srcml_write_end_unit(struct srcml_unit* unit);
157 | libsrcml.srcml_write_end_unit.restype = c_int
158 | libsrcml.srcml_write_end_unit.argtypes = [c_void_p]
159 |
160 | # int srcml_write_start_element(struct srcml_unit* unit, const char* prefix, const char* name, const char* uri);
161 | libsrcml.srcml_write_start_element.restype = c_int
162 | libsrcml.srcml_write_start_element.argtypes = [c_void_p, c_char_p, c_char_p, c_char_p]
163 |
164 | # int srcml_write_end_element(struct srcml_unit* unit);
165 | libsrcml.srcml_write_end_element.restype = c_int
166 | libsrcml.srcml_write_end_element.argtypes = [c_void_p]
167 |
168 | # int srcml_write_namespace(struct srcml_unit* unit, const char* prefix, const char* uri);
169 | libsrcml.srcml_write_namespace.restype = c_int
170 | libsrcml.srcml_write_namespace.argtypes = [c_void_p, c_char_p, c_char_p]
171 |
172 | # int srcml_write_attribute(struct srcml_unit* unit, const char* prefix, const char* name, const char* uri, const char* content);
173 | libsrcml.srcml_write_attribute.restype = c_int
174 | libsrcml.srcml_write_attribute.argtypes = [c_void_p, c_char_p, c_char_p, c_char_p, c_char_p]
175 |
176 | # int srcml_write_string(struct srcml_unit* unit, const char* content);
177 | libsrcml.srcml_write_string.restype = c_int
178 | libsrcml.srcml_write_string.argtypes = [c_void_p, c_char_p]
179 |
180 | class srcml_unit :
181 |
182 | # -------------------------------------------------------------------------------------------
183 | # Create a new srcml_unit tied to the srcml archive
184 | # Parameter: archive -> A srcml archive
185 | # -------------------------------------------------------------------------------------------
186 | def __init__(self, archive, unit = 0) :
187 | self.unit = unit
188 | if self.unit == 0 :
189 | self.unit = libsrcml.srcml_unit_create(archive.archive)
190 |
191 | # -------------------------------------------------------------------------------------------
192 | # Clone the setup of an existing unit
193 | # Parameter: unit -> A srcml unit
194 | # Return: The cloned unit
195 | # -------------------------------------------------------------------------------------------
196 | def clone(self) :
197 | return srcml_unit(self.unit, self.archive)
198 |
199 | # -------------------------------------------------------------------------------------------
200 | # Provides a code of the last error to occur for a unit
201 | # Parameter: unit -> A srcml_unit
202 | # Return: A code for the last recorded error
203 | # -------------------------------------------------------------------------------------------
204 | def error_number(self) :
205 | return libsrcml.srcml_unit_error_number(self.unit)
206 |
207 | # -------------------------------------------------------------------------------------------
208 | # Provides a description of the last error to occur for a unit
209 | # Parameter: unit -> A srcml unit
210 | # Return: A string describing last recorded error
211 | # -------------------------------------------------------------------------------------------
212 | def error_string(self) :
213 | result = libsrcml.srcml_unit_error_string(self.unit)
214 | if result != None :
215 | result = bytes.decode(result)
216 |
217 | return result
218 |
219 | # -------------------------------------------------------------------------------------------
220 | # Free an allocated unit
221 | # -------------------------------------------------------------------------------------------
222 | def unit_free(self) :
223 | check_return(libsrcml.srcml_unit_free(self.unit))
224 |
225 | # -------------------------------------------------------------------------------------------
226 | # Set the source-code encoding for the srcml unit
227 | # Parameter: encoding -> A source-code encoding
228 | # Return: SRCML_STATUS_OK on success
229 | # Return: SRCML_STATUS_INVALID_ARGUMENT
230 | # -------------------------------------------------------------------------------------------
231 | def set_src_encoding(self, encoding) :
232 | if encoding != None :
233 | encoding = str.encode(encoding)
234 |
235 | check_return(libsrcml.srcml_unit_set_src_encoding(self.unit, encoding))
236 |
237 | # -------------------------------------------------------------------------------------------
238 | # Set the source-code language for the srcml unit
239 | # Parameter: language -> A supported source-code language
240 | # Return: SRCML_STATUS_OK on success
241 | # Return: SRCML_STATUS_INVALID_ARGUMENT
242 | # -------------------------------------------------------------------------------------------
243 | def set_language(self, language) :
244 | if language != None :
245 | language = str.encode(language)
246 |
247 | check_return(libsrcml.srcml_unit_set_language(self.unit, language))
248 |
249 | # -------------------------------------------------------------------------------------------
250 | # Set the filename attribute for the srcml unit
251 | # Parameter: filename -> The name of a file
252 | # Return: SRCML_STATUS_OK on success
253 | # Return: SRCML_STATUS_INVALID_ARGUMENT
254 | # -------------------------------------------------------------------------------------------
255 | def set_filename(self, filename) :
256 | if filename != None :
257 | filename = str.encode(filename)
258 |
259 | check_return(libsrcml.srcml_unit_set_filename(self.unit, filename))
260 |
261 | # -------------------------------------------------------------------------------------------
262 | # Set the version attribute for the srcml unit
263 | # Parameter: version -> A version string
264 | # Return: SRCML_STATUS_OK on success
265 | # Return: SRCML_STATUS_INVALID_ARGUMENT
266 | # -------------------------------------------------------------------------------------------
267 | def set_version(self, version) :
268 | if version != None :
269 | version = str.encode(version)
270 |
271 | check_return(libsrcml.srcml_unit_set_version(self.unit, version))
272 |
273 | # -------------------------------------------------------------------------------------------
274 | # Set the timestamp attribute for the srcml unit
275 | # Parameter: timestamp -> A timestamp string
276 | # Return: SRCML_STATUS_OK on success
277 | # Return: SRCML_STATUS_INVALID_ARGUMENT
278 | # -------------------------------------------------------------------------------------------
279 | def set_timestamp(self, timestamp) :
280 | if timestamp != None :
281 | timestamp = str.encode(timestamp)
282 |
283 | check_return(libsrcml.srcml_unit_set_timestamp(self.unit, timestamp))
284 |
285 | # -------------------------------------------------------------------------------------------
286 | # Set the type of end of line to be used for unparse
287 | # Parameter: eol -> The kind of eol to use for unparse
288 | # Return: SRCML_STATUS_OK on success
289 | # Return: SRCML_STATUS_INVALID_ARGUMENT
290 | # -------------------------------------------------------------------------------------------
291 | def set_eol(self, eol) :
292 | check_return(libsrcml.srcml_unit_set_eol(self.unit, eol))
293 |
294 | # -------------------------------------------------------------------------------------------
295 | # Return: The source-code encoding for the unit on success, or NULL
296 | # -------------------------------------------------------------------------------------------
297 | def get_src_encoding(self) :
298 | return bytes.decode(libsrcml.srcml_unit_get_src_encoding(self.unit))
299 |
300 | # -------------------------------------------------------------------------------------------
301 | # Return: The revision for the unit on success, or NULL
302 | # -------------------------------------------------------------------------------------------
303 | def get_revision(self) :
304 | result = libsrcml.srcml_unit_get_revision(self.unit)
305 | if result != None :
306 | result = bytes.decode(result)
307 |
308 | return result
309 |
310 | # -------------------------------------------------------------------------------------------
311 | # Return: The source-code language for the unit on success, or NULL
312 | # -------------------------------------------------------------------------------------------
313 | def get_language(self) :
314 | result = libsrcml.srcml_unit_get_language(self.unit)
315 | if result != None :
316 | result = bytes.decode(result)
317 |
318 | return result
319 |
320 | # -------------------------------------------------------------------------------------------
321 | # Return: The filename attribute on the unit on success, or NULL
322 | # -------------------------------------------------------------------------------------------
323 | def get_filename(self) :
324 | result = libsrcml.srcml_unit_get_filename(self.unit)
325 | if result != None :
326 | result = bytes.decode(result)
327 |
328 | return result
329 |
330 | # -------------------------------------------------------------------------------------------
331 | # The version for the unit on success, or NULL
332 | # -------------------------------------------------------------------------------------------
333 | def get_version(self) :
334 | result = libsrcml.srcml_unit_get_version(self.unit)
335 | if result != None :
336 | result = bytes.decode(result)
337 |
338 | return result
339 |
340 | # -------------------------------------------------------------------------------------------
341 | # Return: The timestamp attribute on the unit on success, or NULL
342 | # -------------------------------------------------------------------------------------------
343 | def get_timestamp(self) :
344 | result = libsrcml.srcml_unit_get_timestamp(self.unit)
345 | if result != None :
346 | result = bytes.decode(result)
347 |
348 | return result
349 |
350 | # -------------------------------------------------------------------------------------------
351 | # Return: The hash attribute on the unit on success, or NULL
352 | # -------------------------------------------------------------------------------------------
353 | def get_hash(self) :
354 | result = libsrcml.srcml_unit_get_hash(self.unit)
355 | if result != None :
356 | result = bytes.decode(result)
357 |
358 | return result
359 |
360 | # -------------------------------------------------------------------------------------------
361 | # Return: The loc of the source code in the unit, or -1 on failure
362 | # -------------------------------------------------------------------------------------------
363 | def get_loc(self) :
364 | check_return(libsrcml.srcml_unit_get_loc(self.unit))
365 |
366 | # -------------------------------------------------------------------------------------------
367 | # Return: The eol for to-src output (unparse), or NULL
368 | # -------------------------------------------------------------------------------------------
369 | def get_eol(self) :
370 | check_return(libsrcml.srcml_unit_get_eol(self.unit))
371 |
372 | # -------------------------------------------------------------------------------------------
373 | # Get a complete, valid XML of the srcML from this unit
374 | # The XML returned is a complete solo srcML unit
375 | # Note: Do not free
376 | # Note: String is valid until the unit is freed, or another unit.get_srcml*() is called
377 | # Return: The standalone unit srcML on success and NULL on failure.
378 | # -------------------------------------------------------------------------------------------
379 | def get_srcml(self) :
380 | result = libsrcml.srcml_unit_get_srcml(self.unit)
381 | if result != None :
382 | result = bytes.decode(result)
383 |
384 | return result
385 |
386 | # -------------------------------------------------------------------------------------------
387 | # Get a fragment of the srcML from this unit
388 | # The XML returned is UTF-8 encoded XML. It is not well-formed XML, e.g., it is missing
389 | # the archive namespace declarations
390 | # Note: Do not free
391 | # Note: String is valid until the unit is freed, or another unit.get_srcml*() is called
392 | # Return: The fragment unit srcML on success and NULL on failure.
393 | # -------------------------------------------------------------------------------------------
394 | def get_srcml_outer(self) :
395 | result = libsrcml.srcml_unit_get_srcml_outer(self.unit)
396 | if result != None :
397 | result = bytes.decode(result)
398 |
399 | return result
400 |
401 | # -------------------------------------------------------------------------------------------
402 | # Get the srcML without the enclosing unit tags
403 | # The XML fragment returned is UTF-8 encoded XML. It is not well-formed XML, e.g., it is missing
404 | # the archive namespace declarations and may not have a single root.
405 | # Note: Do not free
406 | # Note: String is valid until the unit is freed, or another unit.get_srcml*() is called
407 | # Return: The fragment unit srcML on success and NULL on failure.
408 | # -------------------------------------------------------------------------------------------
409 | def get_srcml_inner(self) :
410 | result = libsrcml.srcml_unit_get_srcml_inner(self.unit)
411 | if result != None :
412 | result = bytes.decode(result)
413 |
414 | return result
415 |
416 | # -------------------------------------------------------------------------------------------
417 | # Convert the contents of the file with the name src_filename to srcML and store in the unit
418 | # Parameter: src_filename -> Name of a file to parse into srcML
419 | # Return: SRCML_STATUS_OK on success
420 | # Return: Status error code on failure.
421 | # -------------------------------------------------------------------------------------------
422 | def parse_filename(self, src_filename) :
423 | if src_filename != None :
424 | src_filename = str.encode(src_filename)
425 |
426 | check_return(libsrcml.srcml_unit_parse_filename(self.unit, src_filename))
427 |
428 | # -------------------------------------------------------------------------------------------
429 | # Convert the contents of the src_buffer to srcML and store in the unit
430 | # Parameter: src_buffer -> Buffer containing source code to parse into srcML
431 | # Return: SRCML_STATUS_OK on success
432 | # Return: Status error code on failure.
433 | # -------------------------------------------------------------------------------------------
434 | def parse_memory(self, src_buffer) :
435 | length = len(src_buffer)
436 | if src_buffer != None :
437 | src_buffer = str.encode(src_buffer)
438 |
439 | check_return(libsrcml.srcml_unit_parse_memory(self.unit, src_buffer, length))
440 |
441 | # -------------------------------------------------------------------------------------------
442 | # Convert the contents of the source-code FILE* to srcML and store in the unit
443 | # Parameter: src_file -> A FILE* opened for reading
444 | # Return: SRCML_STATUS_OK on success
445 | # Return: Status error code on failure.
446 | # -------------------------------------------------------------------------------------------
447 | def parse_FILE(self, src_file) :
448 | check_return(libsrcml.srcml_unit_parse_FILE(self.unit, src_file))
449 |
450 | # -------------------------------------------------------------------------------------------
451 | # Convert the contents of a file descriptor and stored in the unit
452 | # Parameter: unit -> A srcml_unit to parse the results to
453 | # Return: SRCML_STATUS_OK on success
454 | # Return: Status error code on failure.
455 | # -------------------------------------------------------------------------------------------
456 | def parse_fd(self, src_fd) :
457 | check_return(libsrcml.srcml_unit_parse_fd(self.unit, src_fd))
458 |
459 | # -------------------------------------------------------------------------------------------
460 | # Convert to srcML the contents from the opened context accessed via read and close callbacks and place it into a unit
461 | # Return: context -> an io context
462 | # Return: read_callback -> a read callback function
463 | # Return: close_callback -> a close callback function
464 | # Return: SRCML_STATUS_OK on success
465 | # Return: Status error code on failure.
466 | # -------------------------------------------------------------------------------------------
467 | def parse_io(self, context, read_callback, close_callback) :
468 | check_return(libsrcml.srcml_unit_parse_io(self.unit, context, read_callback, close_callback))
469 |
470 | # -------------------------------------------------------------------------------------------
471 | # Convert the srcML in a unit into source code and place it into a filename
472 | # If the srcML was not read in, but the attributes were, the XML is read in and that value is unparsed
473 | # Parameter: src_filename -> Name of a file to output contents of unit as source
474 | # Return: SRCML_STATUS_OK on success
475 | # Return: Status error code on failure.
476 | # -------------------------------------------------------------------------------------------
477 | def unparse_filename(self, src_filename) :
478 | if src_filename != None :
479 | src_filename = str.encode(src_filename)
480 |
481 | check_return(libsrcml.srcml_unit_unparse_filename(self.unit, src_filename))
482 |
483 | # -------------------------------------------------------------------------------------------
484 | # Convert the srcML in a unit into source code and place it into a buffer
485 | # The buffer is allocated in the function and needs to be freed after using.
486 | # Return: SRCML_STATUS_OK on success
487 | # Return: Status error code on failure.
488 | # -------------------------------------------------------------------------------------------
489 | def unparse_memory(self) :
490 | self.src_size = c_size_t()
491 | self.src_buffer = c_char_p()
492 | check_return(libsrcml.srcml_unit_unparse_memory(self.unit, pointer(self.src_buffer), pointer(self.src_size)))
493 |
494 | # -------------------------------------------------------------------------------------------
495 | # Convert the srcML in a unit into source code and output to the FILE*
496 | # Parameter: file -> FILE* opened for writing to output the source file
497 | # Return: SRCML_STATUS_OK on success
498 | # Return: Status error code on failure.
499 | # -------------------------------------------------------------------------------------------
500 | def unparse_FILE(self, file) :
501 | check_return(libsrcml.srcml_unit_unparse_FILE(self.unit, file))
502 |
503 | # -------------------------------------------------------------------------------------------
504 | # Convert the srcML in a unit into source code and output to the file descriptor
505 | # Parameter: fd File descriptor opened for writing to output the source file
506 | # Return: SRCML_STATUS_OK on success
507 | # Return: Status error code on failure.
508 | # -------------------------------------------------------------------------------------------
509 | def unparse_fd(self, fd) :
510 | check_return(libsrcml.srcml_unit_unparse_fd(self.unit, fd))
511 |
512 | # -------------------------------------------------------------------------------------------
513 | # Convert the srcML in a unit into source code and output using write callbacks
514 | # Parameter: context -> an io context
515 | # Parameter: write_callback -> a write callback function
516 | # Parameter: close_callback -> a close callback function
517 | # Return: SRCML_STATUS_OK on success
518 | # Return: Status error code on failure.
519 | # -------------------------------------------------------------------------------------------
520 | def unparse_io(self, context, write_callback, close_callback) :
521 | check_return(libsrcml.srcml_unit_unparse_io(self.unit, context, write_callback, close_callback))
522 |
523 | # -------------------------------------------------------------------------------------------
524 | # Write a start tag for a unit
525 | # Return: SRCML_STATUS_OK on success
526 | # Return: Status error code on failure.
527 | # -------------------------------------------------------------------------------------------
528 | def write_start_unit(self) :
529 | check_return(libsrcml.srcml_write_start_unit(self.unit))
530 |
531 | # -------------------------------------------------------------------------------------------
532 | # Write an end tag for a unit
533 | # Return: SRCML_STATUS_OK on success
534 | # Return: Status error code on failure.
535 | # -------------------------------------------------------------------------------------------
536 | def write_end_unit(self) :
537 | check_return(libsrcml.srcml_write_end_unit(self.unit))
538 |
539 | # -------------------------------------------------------------------------------------------
540 | # Write a start tag for a general element
541 | # Parameter: prefix -> Element prefix
542 | # Parameter: name -> Element name
543 | # Parameter: uri -> URI of the prefix
544 | # Return: SRCML_STATUS_OK on success
545 | # Return: Status error code on failure.
546 | # -------------------------------------------------------------------------------------------
547 | def write_start_element(self, prefix, name, uri) :
548 | if prefix != None :
549 | prefix = str.encode(prefix)
550 | if name != None :
551 | name = str.encode(name)
552 | if uri != None :
553 | uri = str.encode(uri)
554 |
555 | check_return(libsrcml.srcml_write_start_element(self.unit, prefix, name, uri))
556 |
557 | # -------------------------------------------------------------------------------------------
558 | # Write an end tag for a general element
559 | # Return: SRCML_STATUS_OK on success
560 | # Return: Status error code on failure.
561 | # -------------------------------------------------------------------------------------------
562 | def write_end_element(self) :
563 | check_return(libsrcml.srcml_write_end_element(self.unit))
564 |
565 | # -------------------------------------------------------------------------------------------
566 | # Write a namespace
567 | # Parameter: prefix -> Namespace prefix
568 | # Parameter: uri -> Namespace URI
569 | # Return: SRCML_STATUS_OK on success
570 | # Return: Status error code on failure.
571 | # -------------------------------------------------------------------------------------------
572 | def write_namespace(self, prefix, uri) :
573 | if prefix != None :
574 | prefix = str.encode(prefix)
575 | if uri != None :
576 | uri = str.encode(uri)
577 |
578 | check_return(libsrcml.srcml_write_namespace(self.unit, prefix, uri))
579 |
580 | # -------------------------------------------------------------------------------------------
581 | # Write an attribute
582 | # Parameter: prefix -> Element prefix
583 | # Parameter: name -> Element name
584 | # Parameter: uri -> URI of the prefix
585 | # Parameter: content -> Value of the attribute
586 | # Return: SRCML_STATUS_OK on success
587 | # Return: Status error code on failure.
588 | # -------------------------------------------------------------------------------------------
589 | def write_attribute(self, prefix, name, uri, content) :
590 | if prefix != None :
591 | prefix = str.encode(prefix)
592 | if name != None :
593 | name = str.encode(name)
594 | if uri != None :
595 | uri = str.encode(uri)
596 | if content != None :
597 | content = str.encode(content)
598 |
599 | check_return(libsrcml.srcml_write_attribute(self.unit, prefix, name, uri, content))
600 |
601 | # -------------------------------------------------------------------------------------------
602 | # Write a general string
603 | # Parameter: content -> Null-terminated string to write
604 | # Return: SRCML_STATUS_OK on success
605 | # Return: Status error code on failure.
606 | # -------------------------------------------------------------------------------------------
607 | def write_string(self, content) :
608 | if content != None :
609 | content = str.encode(content)
610 |
611 | check_return(libsrcml.srcml_write_string(self.unit, content))
612 |
613 | # -------------------------------------------------------------------------------------------
614 | # Return: the buffer of the srcml unit
615 | # -------------------------------------------------------------------------------------------
616 | def src(self) :
617 | result = self.src_buffer.value
618 | if result != None :
619 | result = bytes.decode(self.src_buffer.value)
620 |
621 | return result
622 |
623 | # -------------------------------------------------------------------------------------------
624 | # Free an allocated unit
625 | # -------------------------------------------------------------------------------------------
626 | def __del__(self) :
627 | libsrcml.srcml_unit_free(self.unit)
628 |
--------------------------------------------------------------------------------
/test/a.foo:
--------------------------------------------------------------------------------
1 | a;
2 |
--------------------------------------------------------------------------------
/test/copy.xsl:
--------------------------------------------------------------------------------
1 |
6 |
7 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/test/project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | a;
4 |
5 |
6 |
--------------------------------------------------------------------------------
/test/schema.rng:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/test/setlanguage.xsl:
--------------------------------------------------------------------------------
1 |
7 |
8 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/test/test.py:
--------------------------------------------------------------------------------
1 | # ********************************************************************************************************************************************************
2 | # @file test.py
3 | #
4 | # @copyright Copyright (C) 2022 srcML, LLC (srcML.org)
5 | #
6 | # This file is part of srcML Infrastructure www.srcml.org.
7 | # srcML Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
8 | # srcML Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 | # You should have received a copy of the GNU General Public License along with srcML Infrastructure. If not, see .
10 | # ********************************************************************************************************************************************************
11 |
12 | import sys
13 | sys.path.append("../src/libsrcml")
14 | from pylibsrcml import srcml
15 | import difflib
16 | import os
17 | import ctypes
18 |
19 | test_count = 0
20 | error_count = 0
21 |
22 | LIBC_PATH = ""
23 | if sys.platform == "darwin" :
24 | LIBC_PATH = "libc.dylib"
25 | elif sys.platform == "linux" :
26 | LIBC_PATH = "libc.so.6"
27 | else :
28 | LIBC_PATH = "msvcrt.dll"
29 |
30 | libc = ctypes.cdll.LoadLibrary(LIBC_PATH)
31 |
32 | if sys.platform == "win32" or sys.platform == "cygin" :
33 | os.open = libc._open
34 | os.close = libc._close
35 | os.O_WRONLY = 1
36 | os.O_CREAT = 256
37 | os.O_RDONLY = 0
38 |
39 | libc.fopen.restype = ctypes.c_void_p
40 | libc.fopen.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
41 |
42 | libc.fclose.restype = ctypes.c_int
43 | libc.fclose.argtypes = [ctypes.c_void_p]
44 |
45 | def verify_test(correct, output) :
46 | globals()['test_count'] += 1
47 |
48 | if sys.platform == "win32" or sys.platform == "cygwin" :
49 | correct = str(correct).replace("\r", "")
50 | output = str(output).replace("\r", "")
51 |
52 | if str(correct) != str(output) :
53 | print(str(globals()['test_count']) + "\t")
54 | for line in difflib.unified_diff(str(correct).split("\n"), str(output).split("\n")) :
55 | print(line)
56 | globals()['error_count'] += 1
57 | print("\nERROR:\n" + str(output) + "\n\n!=\n\n" + str(correct) + "\n\n")
58 |
59 | return
60 |
61 | def read_callback_f(context, buffer, len):
62 | return libc.fread(buffer, 1, len, context)
63 |
64 | def write_callback_f(context, buffer, len):
65 | return libc.fwrite(buffer, 1, len, context)
66 |
67 | def close_callback_f(context):
68 | return 0
69 |
70 | read_callback = srcml.read_callback_t(read_callback_f)
71 | write_callback = srcml.write_callback_t(write_callback_f)
72 | close_callback = srcml.close_callback_t(close_callback_f)
73 |
74 | # -----------------------------------------------------------------
75 | # test_srcml_append_transform
76 | # -----------------------------------------------------------------
77 |
78 | # Variable Definitions
79 | asrcml = """
80 |
81 | a;
82 |
83 | b;
84 |
85 |
86 | """
87 | f = open("copy.xsl", "r")
88 | copy = f.read()
89 | f.close()
90 |
91 | # append_transform_xpath
92 | archive = srcml.srcml_archive()
93 | archive.append_transform_xpath("//src:unit")
94 | archive.close()
95 |
96 | # append_transform_xpath_attribute
97 | archive = srcml.srcml_archive()
98 | archive.append_transform_xpath_attribute("//src:unit", "sup", "http://srcML.org/Supplement", "type", "supplement")
99 | archive.close()
100 |
101 | # append_transform_xpath_element
102 | archive = srcml.srcml_archive()
103 | archive.append_transform_xpath_element("//src:unit", "sup", "http://srcML.org/Supplement", "contain")
104 | archive.close()
105 |
106 | # append_transform_xslt_filename
107 | archive = srcml.srcml_archive()
108 | archive.append_transform_xslt_filename("copy.xsl")
109 | archive.close()
110 |
111 | # append_transform_xslt_memory
112 | archive = srcml.srcml_archive()
113 | archive.append_transform_xslt_memory(copy)
114 | archive.close()
115 |
116 | # append_transform_xslt_FILE
117 | archive = srcml.srcml_archive()
118 | f = libc.fopen(b"copy.xsl", b"r")
119 | archive.append_transform_xslt_FILE(f)
120 | libc.fclose(f)
121 | archive.close()
122 |
123 | # append_transform_xslt_fd
124 | archive = srcml.srcml_archive()
125 | fd = os.open("copy.xsl", os.O_RDONLY, 0)
126 | archive.append_transform_xslt_fd(fd)
127 | os.close(fd)
128 | archive.close()
129 |
130 | # append_transform_telaxng
131 | archive = srcml.srcml_archive()
132 | archive.append_transform_relaxng_filename("schema.rng")
133 | archive.close()
134 |
135 | # append_transform_relaxng_memory
136 | archive = srcml.srcml_archive()
137 | f = open("schema.rng", "r")
138 | schema = f.read()
139 | f.close()
140 | archive.append_transform_relaxng_memory(schema)
141 | archive.close()
142 |
143 | # append_transform_relaxng_FILE
144 | archive = srcml.srcml_archive()
145 | f = libc.fopen(b"schema.rng", b"r")
146 | archive.append_transform_relaxng_FILE(f)
147 | libc.fclose(f)
148 | archive.close()
149 |
150 | # append_transform_relaxng_fd
151 | archive = srcml.srcml_archive()
152 | fd = os.open("schema.rng", os.O_RDONLY, 0)
153 | archive.append_transform_relaxng_fd(fd)
154 | os.close(fd)
155 | archive.close()
156 |
157 | # append_transform_param
158 | archive = srcml.srcml_archive()
159 | archive.append_transform_xslt_filename("copy.xsl")
160 | archive.append_transform_param("sup", "http://srcML.org/Supplement")
161 | archive.close()
162 |
163 | # append_transform_stringparam
164 | archive = srcml.srcml_archive()
165 | archive.append_transform_xslt_filename("copy.xsl")
166 | archive.append_transform_stringparam("sup", "http://srcML.org/Supplement")
167 | archive.close()
168 |
169 | srcml.cleanup_globals()
170 |
171 | print("Append transform bindings tested...")
172 |
173 | # -----------------------------------------------------------------
174 | # test_srcml_archive_check_extension
175 | # -----------------------------------------------------------------
176 |
177 | archive = srcml.srcml_archive()
178 | archive.register_file_extension("h", "C++")
179 |
180 | verify_test("C++", archive.check_extension("a.h"))
181 | verify_test("C++", archive.check_extension("a.h.gz"))
182 | verify_test(None, archive.check_extension("a.foo"))
183 |
184 | print("Check extension binding tested...")
185 |
186 | # -----------------------------------------------------------------
187 | # test_srcml_archive_clone
188 | # -----------------------------------------------------------------
189 |
190 | archive = srcml.srcml_archive()
191 | archive.set_src_encoding("e")
192 | archive.set_url("u")
193 | archive.set_version("v")
194 |
195 | new_archive = archive.clone()
196 |
197 | verify_test("e", new_archive.get_src_encoding())
198 | verify_test("u", new_archive.get_url())
199 | verify_test("v", new_archive.get_version())
200 |
201 | print("Archive clone binding tested...")
202 |
203 | # -----------------------------------------------------------------
204 | # test_srcml_archive_create
205 | # -----------------------------------------------------------------
206 |
207 | archive = srcml.srcml_archive()
208 | verify_test(None, archive.get_language())
209 | verify_test(None, archive.get_xml_encoding())
210 | verify_test(None, archive.get_src_encoding())
211 | verify_test(None, archive.get_url())
212 |
213 | print("Archive create binding tested...")
214 |
215 | # -----------------------------------------------------------------
216 | # test_srcml_archive_get
217 | # -----------------------------------------------------------------
218 |
219 | archive = srcml.srcml_archive()
220 |
221 | # src encoding
222 | archive.set_src_encoding("foo")
223 | verify_test("foo", archive.get_src_encoding())
224 |
225 | # xml encoding
226 | archive.set_xml_encoding("foo")
227 | verify_test("foo", archive.get_xml_encoding())
228 |
229 | # revision
230 | verify_test(srcml.SRCML_VERSION_STRING, archive.get_revision())
231 |
232 | # language
233 | archive.set_language("foo")
234 | verify_test("foo", archive.get_language())
235 |
236 | # url
237 | archive.set_url("foo")
238 | verify_test("foo", archive.get_url())
239 |
240 | # version
241 | archive.set_version("foo")
242 | verify_test("foo", archive.get_version())
243 |
244 | # option
245 | archive.set_options(srcml.SRCML_OPTION_CPP)
246 | verify_test(srcml.SRCML_OPTION_CPP, archive.get_options())
247 |
248 | # tabstop
249 | archive.set_tabstop(4)
250 | verify_test(4, archive.get_tabstop())
251 |
252 | # namespace prefix
253 | verify_test(None, archive.get_namespace_prefix(2))
254 |
255 | # namespace size
256 | archive.register_namespace("foo1", "bar1")
257 | archive.register_namespace("foo2", "bar2")
258 | verify_test(3, archive.get_namespace_size())
259 |
260 | # prefix from uri
261 | verify_test("foo2", archive.get_prefix_from_uri("bar2"))
262 |
263 | # namespace uri
264 | verify_test("bar2", archive.get_namespace_uri(2))
265 |
266 | # uri from prefix
267 | verify_test("bar1", archive.get_uri_from_prefix("foo1"))
268 |
269 | archive.close()
270 |
271 | print("Archive get bindings tested...")
272 |
273 | # -----------------------------------------------------------------
274 | # test_srcml_archive_read_open
275 | # -----------------------------------------------------------------
276 |
277 | # Variable Definitions
278 | asrcml = """
279 |
280 | a;
281 |
282 | b;
283 |
284 |
285 | """
286 |
287 | # filename
288 | archive = srcml.srcml_archive()
289 | file = open("project.xml", "w+")
290 | file.write(asrcml)
291 | file.close()
292 |
293 | archive.read_open_filename("project.xml")
294 | verify_test("test", archive.get_url())
295 | verify_test("1", archive.get_version())
296 | verify_test(0, archive.get_options())
297 |
298 | archive.close()
299 |
300 | # memory
301 | archive = srcml.srcml_archive()
302 | archive.read_open_memory(asrcml)
303 |
304 | verify_test("test", archive.get_url())
305 | verify_test("1", archive.get_version())
306 | verify_test(0, archive.get_options())
307 |
308 | archive.close()
309 |
310 | # fd
311 | archive = srcml.srcml_archive()
312 | file = open("project.xml", "w")
313 | file.write(asrcml)
314 | file.close()
315 |
316 | fd = os.open("project.xml", os.O_RDONLY)
317 | archive.read_open_fd(fd)
318 |
319 | verify_test("test", archive.get_url())
320 | verify_test("1", archive.get_version())
321 | verify_test(0, archive.get_options())
322 |
323 | archive.close()
324 | os.close(fd)
325 | os.remove("project.xml")
326 |
327 | # FILE
328 | archive = srcml.srcml_archive()
329 | file = open("project.xml", "w")
330 | file.write(asrcml)
331 | file.close()
332 |
333 | file = libc.fopen(str.encode("project.xml"), str.encode("r"))
334 | archive.read_open_FILE(file)
335 |
336 | verify_test("test", archive.get_url())
337 | verify_test("1", archive.get_version())
338 | verify_test(0, archive.get_options())
339 |
340 | archive.close()
341 | libc.fclose(file)
342 | os.remove("project.xml")
343 |
344 | # io
345 | archive = srcml.srcml_archive()
346 | file = open("project.xml", "w")
347 | file.write(asrcml)
348 | file.close()
349 |
350 | file = libc.fopen(str.encode("project.xml"), str.encode("r"))
351 |
352 | # archive.read_open_io(file, srcml.read_callback_t, srcml.close_callback_t)
353 |
354 | os.remove("project.xml")
355 | archive.close()
356 |
357 | print("Archive read open bindings tested...")
358 |
359 | # -----------------------------------------------------------------
360 | # test_srcml_archive_set
361 | # -----------------------------------------------------------------
362 |
363 | # src encoding
364 | archive = srcml.srcml_archive()
365 | archive.set_src_encoding("ISO-8859-1")
366 | verify_test("ISO-8859-1", archive.get_src_encoding())
367 |
368 | # xml encoding
369 | archive = srcml.srcml_archive()
370 | archive.set_xml_encoding("ISO-8859-1")
371 | verify_test("ISO-8859-1", archive.get_xml_encoding())
372 |
373 | # language
374 | archive = srcml.srcml_archive()
375 | archive.set_language("Java")
376 | verify_test("Java", archive.get_language())
377 |
378 | # url
379 | archive = srcml.srcml_archive()
380 | archive.set_url("https://srcML.org")
381 | verify_test("https://srcML.org", archive.get_url())
382 |
383 | # version
384 | archive = srcml.srcml_archive()
385 | archive.set_version("foo")
386 | verify_test("foo", archive.get_version())
387 |
388 | # options
389 | archive = srcml.srcml_archive()
390 | archive.set_options(srcml.SRCML_OPTION_CPP)
391 | verify_test(srcml.SRCML_OPTION_CPP, archive.get_options())
392 |
393 | # enable option
394 | archive = srcml.srcml_archive()
395 | archive.enable_option(srcml.SRCML_OPTION_CPP)
396 | verify_test(srcml.SRCML_OPTION_CPP, archive.get_options())
397 |
398 | # disable option
399 | archive = srcml.srcml_archive()
400 | archive.enable_option(srcml.SRCML_OPTION_CPP)
401 | archive.disable_option(srcml.SRCML_OPTION_CPP)
402 | verify_test(0, archive.get_options())
403 |
404 | # tabstop
405 | archive = srcml.srcml_archive()
406 | archive.set_tabstop(4)
407 | verify_test(4, archive.get_tabstop())
408 |
409 | # register file extension
410 | archive = srcml.srcml_archive()
411 | archive.register_file_extension("foo", "C++")
412 | verify_test("C++", archive.check_extension("main.foo"))
413 |
414 | # register namespace
415 | archive = srcml.srcml_archive()
416 | archive.register_namespace("foo", "bar")
417 | pos = archive.get_namespace_size() - 1
418 | verify_test("foo", archive.get_namespace_prefix(pos))
419 | verify_test("bar", archive.get_namespace_uri(pos))
420 |
421 | print("Archive set bindings tested...")
422 |
423 | # -----------------------------------------------------------------
424 | # test_srcml_archive_write_open
425 | # -----------------------------------------------------------------
426 |
427 | # Variable Declarations
428 | src = "a;\n"
429 | asrcml = """
430 |
431 | a;
432 |
433 |
434 | """
435 |
436 | # filename
437 | archive = srcml.srcml_archive()
438 | archive.write_open_filename("project.xml")
439 | archive.close()
440 |
441 | file = open("project.xml", "r")
442 | gen = file.read()
443 | file.close()
444 | verify_test(False, gen == "")
445 |
446 | os.remove("project.xml")
447 |
448 | # memory
449 | archive = srcml.srcml_archive()
450 | archive.write_open_memory()
451 | archive.close()
452 |
453 | # FILE
454 | archive = srcml.srcml_archive()
455 | file = libc.fopen(str.encode("project.xml"), str.encode("w"))
456 | archive.write_open_FILE(file)
457 |
458 | archive.close()
459 | libc.fclose(file)
460 |
461 | file = open("project.xml", "r")
462 | gen = file.read()
463 | file.close()
464 | verify_test(False, gen == "")
465 |
466 | os.remove("project.xml")
467 |
468 | # fd
469 | archive = srcml.srcml_archive()
470 | fd = os.open("project.xml", os.O_WRONLY | os.O_CREAT)
471 | archive.write_open_fd(fd)
472 |
473 | archive.close()
474 | os.close(fd)
475 |
476 | file = open("project.xml", "r")
477 | gen = file.read()
478 | file.close()
479 | verify_test(False, gen == "")
480 |
481 | os.remove("project.xml")
482 |
483 | # io
484 | archive = srcml.srcml_archive()
485 | file = open("project.xml", "w")
486 | # archive.write_open_io(file, write_callback, close_callback)
487 | gen = file.write(src)
488 |
489 | file.close()
490 | archive.close()
491 |
492 | file = open("project.xml", "r")
493 | gen = file.read()
494 | file.close()
495 | verify_test(False, gen == "")
496 |
497 | os.remove("project.xml")
498 |
499 | print("Archive write open bindings tested...")
500 |
501 | # -----------------------------------------------------------------
502 | # test_srcml_archive_write_unit
503 | # -----------------------------------------------------------------
504 |
505 | asrcml = """
506 | a;
507 | """
508 |
509 | archive = srcml.srcml_archive()
510 | archive.enable_solitary_unit()
511 | archive.disable_hash()
512 | archive.write_open_memory()
513 |
514 | unit = srcml.srcml_unit(archive)
515 | unit.set_filename("a.cpp")
516 | unit.set_language("C++")
517 | unit.parse_memory("a;\n")
518 | archive.write_unit(unit)
519 | archive.close()
520 |
521 | print("Archive write unit binding tested...")
522 |
523 | # -----------------------------------------------------------------
524 | # test_srcml_clear_transforms
525 | # -----------------------------------------------------------------
526 |
527 | archive = srcml.srcml_archive()
528 | archive.clear_transforms()
529 |
530 | archive = srcml.srcml_archive()
531 | archive.append_transform_xpath("//src:unit")
532 | archive.append_transform_xslt_filename("copy.xsl")
533 | archive.clear_transforms()
534 |
535 | print("Archive clear transform binding tested...")
536 |
537 | # -----------------------------------------------------------------
538 | # test_srcml_convenience && test_global_access
539 | # -----------------------------------------------------------------
540 |
541 | # Variable Declarations
542 | asrcml = """
543 | int a;
544 |
545 | """
546 |
547 | # srcml
548 | file = open("a.cpp", "w")
549 | file.write("int a;\n")
550 | file.close()
551 |
552 | srcml.srcml("a.cpp", "project.xml")
553 | file = open("project.xml", "r")
554 | xml = file.read()
555 | file.close()
556 |
557 | verify_test(asrcml, xml)
558 |
559 | srcml.set_language("C++")
560 | srcml.set_filename("a.cpp")
561 | srcml.set_url("url")
562 | srcml.set_version("version")
563 | srcml.set_timestamp("timestamp")
564 |
565 | verify_test("C++", srcml.get_language())
566 | verify_test(srcml.SRCML_VERSION_STRING, srcml.get_revision())
567 | verify_test("a.cpp", srcml.get_filename())
568 | verify_test("url", srcml.get_url())
569 | verify_test("version", srcml.get_version())
570 | verify_test("timestamp", srcml.get_timestamp())
571 |
572 | srcml.set_options(0)
573 | srcml.enable_option(1)
574 | verify_test(1, srcml.get_options())
575 |
576 | srcml.set_options(2)
577 | verify_test(2, srcml.get_options())
578 |
579 | srcml.set_options(1 | 2)
580 | srcml.disable_option(2)
581 | verify_test(1, srcml.get_options())
582 |
583 | srcml.set_tabstop(4)
584 | verify_test(4, srcml.get_tabstop())
585 |
586 | os.remove("project.xml")
587 |
588 | archive = srcml.srcml_archive();
589 | archive.register_file_extension("foo", "C++")
590 | archive.register_namespace("s", "http://www.srcML.org/srcML/src")
591 |
592 | verify_test(2, srcml.check_language("C++"))
593 | verify_test("C++", srcml.check_extension("a.cpp"))
594 | verify_test(1, srcml.check_encoding("UTF-8"))
595 | verify_test(1, srcml.check_xslt())
596 | verify_test(1, srcml.check_exslt())
597 |
598 | srcml.cleanup_globals()
599 |
600 | print("Convenience and global access bindings tested...")
601 |
602 | # -----------------------------------------------------------------
603 | # test_srcml_global
604 | # -----------------------------------------------------------------
605 |
606 | # check_language
607 | verify_test(1, srcml.check_language("C"))
608 | verify_test(2, srcml.check_language("C++"))
609 | verify_test(4, srcml.check_language("Java"))
610 | verify_test(8, srcml.check_language("C#"))
611 | verify_test(17, srcml.check_language("Objective-C"))
612 |
613 | # get_language_list_size
614 | verify_test(5, srcml.get_language_list_size())
615 |
616 | # get_language_list
617 | verify_test("C", srcml.get_language_list(0))
618 | verify_test("C++", srcml.get_language_list(1))
619 | verify_test("C#", srcml.get_language_list(2))
620 | verify_test("Java", srcml.get_language_list(3))
621 | verify_test("Objective-C", srcml.get_language_list(4))
622 | verify_test(None, srcml.get_language_list(5))
623 |
624 | # check_extension
625 | verify_test("C++", srcml.check_extension("a.cpp"))
626 | verify_test("C++", srcml.check_extension("a.cpp.gz"))
627 | srcml.register_file_extension("foo", "C++")
628 | verify_test("C++", srcml.check_extension("a.foo"))
629 | verify_test("C++", srcml.check_extension("a.foo.gz"))
630 | verify_test(None, srcml.check_extension("a.bar"))
631 |
632 | # check_encoding
633 | verify_test(1, srcml.check_encoding("UTF-8"))
634 | verify_test(None, srcml.check_encoding("UTF-64"))
635 | verify_test(None, srcml.check_encoding(""))
636 |
637 | srcml.cleanup_globals()
638 |
639 | print("Global bindings tested...")
640 |
641 | # -----------------------------------------------------------------
642 | # test_srcml_read_unit
643 | # -----------------------------------------------------------------
644 |
645 | # Variable Declaration
646 | asrcml = """
647 |
648 | a;
649 |
650 |
651 | """
652 |
653 | file = open("project.xml", "w")
654 | gen = file.write(asrcml)
655 | file.close()
656 | archive = srcml.srcml_archive()
657 | archive.read_open_filename("project.xml")
658 | unit = archive.read_unit()
659 | unit.unparse_filename("a.cpp")
660 | archive.close()
661 |
662 | file = open("a.cpp", "r")
663 | gen = file.read()
664 | file.close()
665 | verify_test(src, gen)
666 |
667 | os.remove("a.cpp")
668 |
669 | print("Read unit bindings tested...")
670 |
671 | # -----------------------------------------------------------------
672 | # test_srcml_unit_create
673 | # -----------------------------------------------------------------
674 |
675 | archive = srcml.srcml_archive()
676 | unit = srcml.srcml_unit(archive)
677 |
678 | verify_test(None, unit.get_language())
679 | verify_test(None, unit.get_filename())
680 | verify_test(None, unit.get_version())
681 | verify_test(None, unit.get_timestamp())
682 | verify_test(None, unit.get_hash())
683 | verify_test(None, unit.get_srcml())
684 | verify_test(None, unit.get_srcml_outer())
685 | verify_test(None, unit.get_srcml_inner())
686 |
687 | print("Create unit bindings tested...")
688 |
689 | # -----------------------------------------------------------------
690 | # test_srcml_unit_get && test_srcml_unit_set
691 | # -----------------------------------------------------------------
692 |
693 | archive = srcml.srcml_archive()
694 | unit = srcml.srcml_unit(archive)
695 |
696 | unit.set_src_encoding("foo")
697 | unit.set_language("C++")
698 | unit.set_filename("main.cpp")
699 | unit.set_version("1.5")
700 | unit.set_timestamp("Fri Nov 30 14:15:27 EST 2018")
701 |
702 | verify_test("foo", unit.get_src_encoding())
703 | verify_test(srcml.SRCML_VERSION_STRING, unit.get_revision())
704 | verify_test("C++", unit.get_language())
705 | verify_test("main.cpp", unit.get_filename())
706 | verify_test("1.5", unit.get_version())
707 | verify_test("Fri Nov 30 14:15:27 EST 2018", unit.get_timestamp())
708 |
709 | # unparse_memory()
710 |
711 | text = """
712 |
713 | a;
714 |
715 | """
716 |
717 | archive.read_open_memory(text)
718 | unit = archive.read_unit()
719 | unit.set_eol(srcml.SOURCE_OUTPUT_EOL_LF)
720 | unit.unparse_memory()
721 |
722 | verify_test("\na;\n", unit.src())
723 |
724 | # parse_memory()
725 | archive = srcml.srcml_archive()
726 | archive.enable_hash()
727 |
728 | unit = srcml.srcml_unit(archive)
729 | unit.set_language("C++")
730 | unit.parse_memory("a;")
731 |
732 | verify_test(1, archive.has_hash())
733 | verify_test("a301d91aac4aa1ab4e69cbc59cde4b4fff32f2b8", unit.get_hash())
734 |
735 | srcml.cleanup_globals()
736 |
737 | print("Unit get, set, parse, unparse bindings tested...")
738 |
739 | # -----------------------------------------------------------------
740 | # test_srcml_write_by_element
741 | # -----------------------------------------------------------------
742 |
743 | # Variable Declaration
744 | asrcml = """
745 |
746 |
747 | source
748 |
749 |
750 |
751 | """
752 |
753 | archive = srcml.srcml_archive()
754 | archive.disable_hash()
755 | archive.write_open_memory()
756 |
757 | unit = srcml.srcml_unit(archive)
758 | unit.set_language("C++")
759 |
760 | unit.write_start_unit()
761 | unit.write_start_element("f", "foo", "bar")
762 | unit.write_namespace("s", "srcML")
763 | unit.write_attribute("s", "src", None, "ML")
764 | unit.write_string("source")
765 | unit.write_end_element()
766 | unit.write_string("\n")
767 | unit.write_end_unit()
768 |
769 | archive.write_unit(unit)
770 | archive.close()
771 |
772 | verify_test(asrcml, archive.srcML())
773 |
774 | print("Write by element bindings tested...")
775 |
776 | print("------------------------------------------------------")
777 | print("All bindings have been tested: " + str(error_count) + " errors / " + str(test_count) + " tests")
778 |
--------------------------------------------------------------------------------