diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..5d2af0d67735d6ad75cf65a7a7d8174781795c72 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ + +.idea/ + +__pycache__/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..424621d848e9ed1902e56fd71a960c08fee93124 --- /dev/null +++ b/LICENSE @@ -0,0 +1,504 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + <one line to give the library's name and a brief idea of what it does.> + Copyright (C) 2024 Benedikt + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 + USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random + Hacker. + + <signature of Ty Coon>, 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! diff --git a/MANIFFEST.in b/MANIFFEST.in new file mode 100644 index 0000000000000000000000000000000000000000..735ce2c3b82561c8852bd0c472f2c140e67472ef --- /dev/null +++ b/MANIFFEST.in @@ -0,0 +1 @@ +include src/dccXMLJSONConv/data/*.json diff --git a/README.md b/README.md index 9e29aba3db113914a9829f83360cf8dae9974965..d3b87ae7e875874650ce3de259c89c61c92d335c 100644 --- a/README.md +++ b/README.md @@ -1,93 +1,209 @@ -# dccXMLJSONConv - - - -## Getting started - -To make it easy for you to get started with GitLab, here's a list of recommended next steps. - -Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)! - -## Add your files - -- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files -- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command: - +# DCC XML<-->JSON Conversion +Thie Package contains an Libary for XML<-->JSON Conversion for the DCC (Digital Calibration Certificate). +Witch contains an Python Module for the conversion. Aswell as an tornador REST-API Server to remote call this conversion. +## Conversion Rules +0. The XML Document ist converted into a nested dict, containing lists for repatable elements. Repeated elements are determined by there name or by the name of the parent Element. [listDCCElements.json](src/dccXMLJSONConv/data/listDCCElements.json) contains a comprehensive list of the repeated fild names/parent names. +1. The actual `string` Conent of an Element is stored as `string` in the `#text` Element of the JSON-Nodes +2. Since JSON Don't support attributes, all attributes are converted to dict Keys Starting with an @ +3. Comments are converted to an adition dict key '@_Comment' containing a list of all comments in the coresonding XML-Element, the order ist preserved. The actual position of the comment is not. + +### Error handling +The Converter does not check any XSD-rules and allways tries to return an response. + +### Conversion example +```xml +<dcc:quantity refType="vib_phase"> + <!-- This is only an example --> + <dcc:name> + <dcc:content lang="de">Phasenverzögerung</dcc:content> + <dcc:content lang="en">Phase delay</dcc:content> + </dcc:name> + <!-- This is an additional Comment --> + <si:realListXMLList> + <si:valueXMLList>1.7 1.5 1.3 1.1 1.02 0.92 0.83 0.76 0.69 0.63 0.57 0.53 0.48 0.44 0.42 0.38 0.33 0.28 0.39 0.30 0.30 0.15 0.17 0.1 0.1 0.1 0.1</si:valueXMLList> + <si:unitXMLList>\degree</si:unitXMLList> + <si:expandedUncXMLList> + <si:uncertaintyXMLList>1.5 1.0 1.0 1.0 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 1.0 1.0 1.0 1.0</si:uncertaintyXMLList> + <si:coverageFactorXMLList>2.0</si:coverageFactorXMLList> + <si:coverageProbabilityXMLList>0.95</si:coverageProbabilityXMLList> + <si:distributionXMLList>normal</si:distributionXMLList> + </si:expandedUncXMLList> + </si:realListXMLList> +</dcc:quantity> ``` -cd existing_repo -git remote add origin https://gitlab1.ptb.de/digitaldynamicmeasurement/dccxmljsonconv.git -git branch -M main -git push -uf origin main +```json +{ + "dcc:quantity": { + "@refType": "vib_phase", + "@_Comment": [ + " This is only an example ", + " This is an additional Comment " + ], + "dcc:name": { + "dcc:content": [ + { + "@lang": "de", + "#text": "Phasenverz\u00f6gerung" + }, + { + "@lang": "en", + "#text": "Phase delay" + } + ] + }, + "si:realListXMLList": { + "si:valueXMLList": { + "#text": "1.7 1.5 1.3 1.1 1.02 0.92 0.83 0.76 0.69 0.63 0.57 0.53 0.48 0.44 0.42 0.38 0.33 0.28 0.39 0.30 0.30 0.15 0.17 0.1 0.1 0.1 0.1" + }, + "si:unitXMLList": { + "#text": "\\degree" + }, + "si:expandedUncXMLList": { + "si:uncertaintyXMLList": { + "#text": "1.5 1.0 1.0 1.0 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 1.0 1.0 1.0 1.0" + }, + "si:coverageFactorXMLList": { + "#text": "2.0" + }, + "si:coverageProbabilityXMLList": { + "#text": "0.95" + }, + "si:distributionXMLList": { + "#text": "normal" + } + } + } + } +} ``` -## Integrate with your tools +# Using the libary +Hint! The Repo [dccXMLJSONConvGUI](https://gitlab1.ptb.de/digitaldynamicmeasurement/dcc_XMLJSONConvGUI) contains a dockerfile to deploy the dcc_XMLJSONConv running on port 8000 as well as an GUI. Using this is an easy deployment option. +## Installing system dependencies: +Ubuntu: +```bash +sudo apt-get install libxml2-dev libxslt-dev python-lxml +``` +Fedora: +```bash +sudo dnf install libxml2-devel libxslt-devel python-lxml +``` +## Installation +Methode 1. Using Git-Repo +Clone Repo and install Python dependencies: +``` +git clone https://gitlab1.ptb.de/digitaldynamicmeasurement/dcc_XMLJSONConv.git +cd dcc_XMLJSONConv +pip install -r ./requirements.txt +``` +Methode 2. Use Python Pacakge +2. Alternativly install the converter as python package with pip: +```bash +pip install -e git+https://pipPull:jLBHrAnhPp9s1-qHvB5A@gitlab1.ptb.de/digitaldynamicmeasurement/dcc_XMLJSONConv.git@release#egg=dcc-XMLJSONConv +``` -- [ ] [Set up project integrations](https://gitlab1.ptb.de/digitaldynamicmeasurement/dccxmljsonconv/-/settings/integrations) -## Collaborate with your team +# Usage of local conversion Libary: -- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/) -- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html) -- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically) -- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/) -- [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html) +```python +#Imports +import json +from dccXMLJSONConv.dccConv import XMLToJson,JSONToXML -## Test and Deploy -Use the built-in continuous integration in GitLab. +jsonDict=json.loads('COPY CONTENT FROM EXAMPLE ABOVE') +xmlSTr='COPY CONTENT FROM EXAMPLE ABOVE' +xmltoJsonDict=XMLToJson(xmlSTr) +jsontoXMLStr=JSONToXML(jsonDict) +``` -- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html) -- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/) -- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html) -- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/) -- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html) -*** -# Editing this README +# Using the dcc_rest_server -When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template. +1. Install Additional requrenments , namly `fastapi~=0.70.0` and `uvicorn~=0.27.1` from [requirementsServer.txt](requirementsServer.txt) +```bash +pip install -r requirementsServer.txt +``` +2. Start the dcc_server with + +```bash +cd ~/repos/dcc_XMLJSONConv/src/dccXMLJSONConv +(venv) seeger01@n23017:~/repos/dccXMLJSONConv/src/dcc_XMLJSONConv$ uvicorn dccServer:app --reload +INFO: Will watch for changes in these directories: ['/home/seeger01/repos/dccXMLJSONConv/src/dccXMLJSONConv'] +INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) +INFO: Started reloader process [16614] using StatReload +INFO: Started server process [16616] +INFO: Waiting for application startup. +INFO: Application startup complete. +``` +The REST-Server is now runing on Port `8000` use the `--port ` and `--host` params to change port and host. -## Suggestions for a good README +```bash +(venv) seeger01@n23017:~/repos/dccXMLJSONConv/src/dccXMLJSONConv$ uvicorn dccServer:app --reload --port 8001 --host 127.0.0.2 +INFO: Will watch for changes in these directories: ['/home/seeger01/repos/dccXMLJSONConv/src/dccXMLJSONConv'] +INFO: Uvicorn running on http://127.0.0.2:8001 (Press CTRL+C to quit) +``` -Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information. +# REST API Documentation -## Name -Choose a self-explaining name for your project. +This document outlines the REST API endpoints available in the application, detailing the request methods, endpoints, expected request bodies, and response formats. -## Description -Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors. +## Endpoints -## Badges -On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge. +| Endpoint | Method | Description | Request Body | Response | +|----------------|--------|--------------------------------------------------------|-------------------------------------------------------------------|----------------------------------------------------| +| `/` | GET | Returns a greeting message and usage instructions. | N/A | `{ "message": "<instructions>" }` | +| `/dcc2json/` | POST | Converts XML string input to JSON format. | `{ "xml": "<XML string>" }` | JSON object representing the original XML. | +| `/json2dcc/` | POST | Converts JSON input to XML string format. | `{ "js": <JSON object> }` | XML string representing the original JSON. | -## Visuals -Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method. +## Details -## Installation -Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection. +### `/` -## Usage -Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README. +- **Method**: GET +- **Description**: Provides a greeting message along with instructions for using the available endpoints. +- **Request Body**: None +- **Response**: + - **Status Code**: 200 OK + - **Body**: + ```json + { + "message": "Hello!\nBetter use the URL /json2dcc/?js={...} \n or /dcc2json (POST method)" + } + ``` -## Support -Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc. +### `/dcc2json/` -## Roadmap -If you have ideas for releases in the future, it is a good idea to list them in the README. +- **Method**: POST +- **Description**: Accepts an XML string and converts it into a JSON representation. +- **Request Body**: Required. The body should contain an XML string within a JSON object under the key `"xml"`. +- **Response**: + - **Status Code**: 200 OK for successful conversion; 400 Bad Request for empty input. + - **Body**: JSON object representing the parsed XML. -## Contributing -State if you are open to contributions and what your requirements are for accepting them. +### `/json2dcc/` -For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self. +- **Method**: POST +- **Description**: Accepts a JSON object and converts it into an XML string. +- **Request Body**: Required. The body should contain a JSON object under the key `"js"`. +- **Response**: + - **Status Code**: 200 OK for successful conversion; 400 Bad Request for empty input. + - **Body**: XML string representing the JSON object. -You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser. +## Further usage examples -## Authors and acknowledgment -Show your appreciation to those who have contributed to the project. +For detailed usage examples, including request and response examples, refer to the automated tests provided in[test_dccXMLJSONConv.py](test/test_dccXMLJSONConv.py) and [test_dcc_server.py](test/test_dcc_server.py) . -## License -For open source projects, say how it is licensed. +## Update repeatable Fieldnames from schema URL +The skript [listTypeFinder.py][src/dcc_XMLJSONConv/listTypeFinder.py] can be used to update the list of repeated fields. +Either with the functions in the skript (see Docstings for further Information) +or calling the script with the XSD Url and optinal the output path like +```bash +(venv) user@host:~/repos/dccXMLJSONConv/src/dccXMLJSONConv$ python3 ./listTypeFinder.py https://ptb.de/dcc/v3.2.1/dcc.xsd tmp.json +List types have been written to tmp.json +``` -## Project status -If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. +# dcc_rest_server debugging and launching with Pycharm +For Pycharm debuigging see : [https://www.jetbrains.com/help/pycharm/fastapi-project.html#run-debug-configuration}(https://www.jetbrains.com/help/pycharm/fastapi-project.html#run-debug-configuration) +Create new fastApi launch configuration instead of python diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000000000000000000000000000000000000..fed528d4a7a148fd0bf0b0198a6461f8c91b87e9 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..40e998eaee6a35567bd445eee237f2854bc2978d --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +lxml~=4.9.3 +Jinja2~=3.1.2 +requests~=2.31.0 +pytest~=7.4.3 \ No newline at end of file diff --git a/requirementsServer.txt b/requirementsServer.txt new file mode 100644 index 0000000000000000000000000000000000000000..24f0712388a46b19261b4303d19767feef933e2f --- /dev/null +++ b/requirementsServer.txt @@ -0,0 +1,6 @@ +lxml~=4.9.3 +Jinja2~=3.1.2 +requests~=2.31.0 +pytest~=7.4.3 +fastapi~=0.70.0 +uvicorn~=0.27.1 diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000000000000000000000000000000000000..69215f7e053b41e5ce4505508ceb70eb5cf0fd57 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,25 @@ +[metadata] +name = dccxmljsonconv +version = 2.1. +description = Python module for converting XML DCC (https://www.ptb.de/dcc/) to JSON and vice versa +long_description = file: README.md +long_description_content_type = text/markdown +license = LGPL-2.1-or-later +author = Benedikt Seeger,Thomas Bruns, Vanessa Stehr +author_email = benedikt.seeger@ptb.de + +[options] +python_requires = >=3.10 +package_dir = + = src +packages = find: +install_requires = + jinja2 + lxml +include_package_data = True + +[options.packages.find] +where = src +include = * +[options.package_data] +dcc_XMLJSONConv = data/* diff --git a/src/dccXMLJSONConv/__init__.py b/src/dccXMLJSONConv/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/src/dccXMLJSONConv/data/__init__.py b/src/dccXMLJSONConv/data/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/src/dccXMLJSONConv/data/listDCCElements.json b/src/dccXMLJSONConv/data/listDCCElements.json new file mode 100644 index 0000000000000000000000000000000000000000..5ba907e6f4077166e970d9c50dec92f063005667 --- /dev/null +++ b/src/dccXMLJSONConv/data/listDCCElements.json @@ -0,0 +1,264 @@ +{ + "schemaUrl": "https://ptb.de/dcc/v3.2.1/dcc.xsd", + "listTypeElements": [ + "measurementResults", + "dccSoftware", + "refTypeDefinitions", + "items", + "respPersons", + "statements", + "identifications", + "identifications", + "measuringEquipmentQuantities", + "charsXMLList", + "identifications", + "identifications", + "installedSoftwares", + "identifications", + "itemQuantities", + "usedMethods", + "usedSoftware", + "measuringEquipments", + "influenceConditions", + "results", + "measurementMetaData", + "charsXMLList", + "usedMethods", + "usedSoftware", + "measuringEquipments", + "influenceConditions", + "measurementMetaData", + "relativeUncertaintyXmlList", + "dateTimeXMLList", + "usedMethods", + "usedSoftware", + "measuringEquipments", + "influenceConditions", + "measurementMetaData", + "validXMLList", + "conformityXMLList" + ], + "listTypeElementsDetails": [ + { + "name": "measurementResults", + "type": "dcc:measurementResultListType" + }, + { + "name": "dccSoftware", + "type": "dcc:softwareListType" + }, + { + "name": "refTypeDefinitions", + "type": "dcc:refTypeDefinitionListType", + "minOccurs": "0" + }, + { + "name": "items", + "type": "dcc:itemListType" + }, + { + "name": "respPersons", + "type": "dcc:respPersonListType" + }, + { + "name": "statements", + "type": "dcc:statementListType", + "minOccurs": "0" + }, + { + "name": "identifications", + "type": "dcc:identificationListType", + "minOccurs": "0" + }, + { + "name": "identifications", + "type": "dcc:identificationListType", + "minOccurs": "0" + }, + { + "name": "measuringEquipmentQuantities", + "type": "dcc:measuringEquipmentQuantityListType", + "minOccurs": "0" + }, + { + "name": "charsXMLList", + "type": "dcc:charsXMLListType" + }, + { + "name": "identifications", + "type": "dcc:identificationListType", + "minOccurs": "0" + }, + { + "name": "identifications", + "type": "dcc:identificationListType", + "minOccurs": "0" + }, + { + "name": "installedSoftwares", + "type": "dcc:softwareListType", + "minOccurs": "0" + }, + { + "name": "identifications", + "type": "dcc:identificationListType" + }, + { + "name": "itemQuantities", + "type": "dcc:itemQuantityListType", + "minOccurs": "0" + }, + { + "name": "usedMethods", + "type": "dcc:usedMethodListType", + "minOccurs": "0" + }, + { + "name": "usedSoftware", + "type": "dcc:softwareListType", + "minOccurs": "0" + }, + { + "name": "measuringEquipments", + "type": "dcc:measuringEquipmentListType", + "minOccurs": "0" + }, + { + "name": "influenceConditions", + "type": "dcc:influenceConditionListType", + "minOccurs": "0" + }, + { + "name": "results", + "type": "dcc:resultListType" + }, + { + "name": "measurementMetaData", + "type": "dcc:measurementMetaDataListType", + "minOccurs": "0" + }, + { + "name": "charsXMLList", + "type": "dcc:charsXMLListType" + }, + { + "name": "usedMethods", + "type": "dcc:usedMethodListType", + "minOccurs": "0" + }, + { + "name": "usedSoftware", + "type": "dcc:softwareListType", + "minOccurs": "0" + }, + { + "name": "measuringEquipments", + "type": "dcc:measuringEquipmentListType", + "minOccurs": "0" + }, + { + "name": "influenceConditions", + "type": "dcc:influenceConditionListType", + "minOccurs": "0" + }, + { + "name": "measurementMetaData", + "type": "dcc:measurementMetaDataListType", + "minOccurs": "0" + }, + { + "name": "relativeUncertaintyXmlList", + "type": "si:realListXMLListType" + }, + { + "name": "dateTimeXMLList", + "type": "dcc:dateTimeXMLListType", + "minOccurs": "0" + }, + { + "name": "usedMethods", + "type": "dcc:usedMethodListType", + "minOccurs": "0" + }, + { + "name": "usedSoftware", + "type": "dcc:softwareListType", + "minOccurs": "0" + }, + { + "name": "measuringEquipments", + "type": "dcc:measuringEquipmentListType", + "minOccurs": "0" + }, + { + "name": "influenceConditions", + "type": "dcc:influenceConditionListType", + "minOccurs": "0" + }, + { + "name": "measurementMetaData", + "type": "dcc:measurementMetaDataListType", + "minOccurs": "0" + }, + { + "name": "validXMLList", + "type": "dcc:booleanXMLListType" + }, + { + "name": "conformityXMLList", + "type": "dcc:stringConformityStatementStatusXMLListType" + } + ], + "repeatedFieldNames": [ + null, + "software", + "refTypeDefinition", + "equipmentClass", + "measuringEquipment", + "measuringEquipmentQuantity", + "usedLangCodeISO639_1", + "mandatoryLangCodeISO639_1", + "item", + "itemQuantity", + "identification", + "respPerson", + "statement", + "measurementResult", + "usedMethod", + "norm", + "reference", + "influenceCondition", + "result", + "metaData", + "countryCodeISO3166_1", + "content", + "file", + "formula" + ], + "repeatedFieldNamesDetails": { + "null": "unbounded", + "software": "unbounded", + "refTypeDefinition": "unbounded", + "equipmentClass": "unbounded", + "measuringEquipment": "unbounded", + "measuringEquipmentQuantity": "unbounded", + "usedLangCodeISO639_1": "unbounded", + "mandatoryLangCodeISO639_1": "unbounded", + "item": "unbounded", + "itemQuantity": "unbounded", + "identification": "unbounded", + "respPerson": "unbounded", + "statement": "unbounded", + "measurementResult": "unbounded", + "usedMethod": "unbounded", + "norm": "unbounded", + "reference": "unbounded", + "influenceCondition": "unbounded", + "result": "unbounded", + "metaData": "unbounded", + "countryCodeISO3166_1": "unbounded", + "content": "unbounded", + "file": "unbounded", + "formula": "unbounded" + } +} \ No newline at end of file diff --git a/src/dccXMLJSONConv/dccConv.py b/src/dccXMLJSONConv/dccConv.py new file mode 100755 index 0000000000000000000000000000000000000000..168427e8333f9aeed4f2aec12a5e2bb8fa033d82 --- /dev/null +++ b/src/dccXMLJSONConv/dccConv.py @@ -0,0 +1,601 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +#Created on Sun Dec 12 17:02:59 2021 by Thomas Bruns +# This file is part of dcc-xmljsonconv (https://gitlab1.ptb.de/digitaldynamicmeasurement/dcc_XMLJSONConv) +# Copyright 2024 [Thomas Bruns (PTB), Benedikt Seeger(PTB), Vanessa Stehr(PTB)] +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program 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 Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +from jinja2 import Template +import lxml.etree as ElementTree +import json +import os +import warnings +import pathlib + +TRACBACKLENGTH = 40 + +try: + script_location = pathlib.Path(__file__).resolve() + path = script_location.parent / 'data' / 'listDCCElements.json' + listDCCElements = json.load(open(path, 'r')) +except: + # Path to the directory containing the script + # Get the absolute path to the directory where the script is located + script_location = pathlib.Path(__file__).resolve().parent.parent + + # Construct the path to the data file + data_file_path = script_location.parent / 'data' / 'listDCCElements.json' + listDCCElements = json.load(open(data_file_path, 'r')) + +listElementsNames = listDCCElements['listTypeElements'] +repeatedFieldNames = listDCCElements['repeatedFieldNames'] + + +class dcc(): + def __init__(self): + self.root = None + self.xml = "" + self.json = "" + self.nsmap = {} + self.dic = {} + self.parsingErrorInRecursionOccured = False # flag if an error occurred within the recursive parsing + self.TpComment = Template("\n<!-- {{ comment }} -->\n") # Jinja template for a comment + self.TpTag = Template("<{{ name }}{{attributes}}>{{ text }}</{{ name }}>\n") # jinja template for a tag + + # --------------- generate a dict from (DCC)-XML --------------- + def read_dcc_file(self, filename): + """ + Reads the XML-file in filename into self.xml + + Parameters + ---------- + filename : existing filepath + Path to an existing XML-file + + Returns + ------- + + """ + print("read_dcc_file:... %s" % filename) + with open(filename, "r") as file: + self.xml = file.read() + return + + def read_dcc_string(self, xml): + """ + Setter method takes xml and puts it into self.xml + + Parameters + ---------- + xml : string + xml-document as string + + Returns + ------- + + """ + self.xml = xml + return + + def read_json_file(self, filename): + """ + Reads the JSON-file in filename into self.json + + Parameters + ---------- + filename : existing filepath + Path to an existing XML-file + + Returns + ------- + + """ + print("read_json_file:... %s" % filename) + with open(filename, "r") as file: + self.json = file.read() + return + + def read_json_string(self, json): + """ + Setter method takes xml and puts it into self.xml + + Parameters + ---------- + xml : string + xml-document as string + + Returns + ------- + + """ + self.json = json + return + + def __update_tree__(self): + + """ + updates self.tree (lxml structure) from self.xml + sets self.root + sets self.nsmap + + Parameters + ---------- + xml : string + xml-document as string + + Returns + ------- + + """ + try: + parser = ElementTree.XMLParser(recover=True) # recover from bad characters. + self.tree = ElementTree.fromstring(self.xml.encode(), parser=parser) # encode->bytes necessary! + if self.tree.nsmap: + self.nsmap = self.tree.nsmap + return + except: + print("dcc_stuff.__update_tree__() validation failed for given xml-string!") + return + + def __tree2dict__(self, node): + """ + traverses the XML-tree starting at the root element and transforms it into + a python dict of dicts of dicts ... + + Parameters + ---------- + root : lxml.etree Element (not necessarilly the root of the tree) + The starting point of traversal. Get it like: + + import lxml.etree as ElementTree + with open(filename,"r") as file: + tree = ElementTree.parse(file) + root = tree.getroot() + + Returns + ------- + ret : dict + the information from the xml in a dict of dicts + forceArrayTopLevel : bool default=False + flag if the top level is forced to be an array + the parser found a repeated field on the actual level so the top level must be an array + """ + dc = {} + forceArray = False + forceArrayTopLevel = False + if node.nsmap and (node == self.tree): + tmp = {"@xmlns:" + key: node.nsmap[key] for key in node.nsmap} + self.merge_dicts(dc, tmp) + + if node.attrib: + tmp = {"@" + self.ns_clean(key): node.attrib[key] for key in node.attrib} + self.merge_dicts(dc, tmp) + + if isinstance(node, ElementTree._Comment): + tmp = json.loads(json.dumps({"@_Comment": node.text})) + self.merge_dicts(dc, tmp) + return (dc,False) + + elif isinstance(node, ElementTree._Element): + + if node.text: + if not node.text.isspace(): + self.merge_dicts(dc, json.loads(json.dumps({"#text": node.text}))) + if node.tag.split('}')[-1] in listElementsNames: # check if this Element is a listType + # print("Found listType: " + node.tag) + forceArray = True # set flag for listForcing + if node.tag.split('}')[-1] in repeatedFieldNames: # check if this Element is a repeatable field + forceArrayTopLevel = True # set flag to force the top level to be an array + if len(node) > 0: + for i, child in enumerate(node): + cildresult, forceArrayForMemberFlag = self.__tree2dict__(child) + self.merge_dicts(dc, cildresult, forceArray=forceArray or forceArrayForMemberFlag) + + return {self.ns_clean(str(node.tag)): dc}, forceArrayTopLevel + + def xml2dict(self): + """ + takes the self.xml attribute and by using lxml converts it into + self.tree and by by using self.__tree2dict__ fills the + self.dic attribute + + Parameters + ---------- + None + + Returns + ------- + None + + """ + + if self.xml == "": + print("dcc_stuff.xml2dict: ERROR xml-string is empty!") + return {} + else: + self.__update_tree__() # update the xml-tree from self.xml + self.dic, retunFlag = self.__tree2dict__(self.tree) # pares the tree from root + return + + def dict2json(self): + """ + takes self.dic and generates self.json as json-textstring + + Parameters + ---------- + xml : string + xml-document as string + + Returns + ------- + + """ + + try: + self.json = json.dumps(self.dic) + except Exception: + print("<ERROR>dcc.dict2json failed in json.dumps()</ERROR>") + + def get_json(self, beauty=True, indent=4): + """ + returns the (beautyfied) json string of self. + + Parameters + ---------- + beauty : bool (True) + sets whether the string is beautyfied (for humans) + + indent : integer + sets the indentation width for beautification + + Returns + ------- + json-string + + """ + + self.dict2json() # update the json from self.dic + if beauty: + ret = json.dumps(self.dic, indent=indent) # dump the dict to string + else: + ret = json.dumps(self.dic) + return ret + + def ns_clean(self, tag): + """ + shortens namespace information in xml-tags + + Parameters + ---------- + tag : string + the tag that has to be checked for know namespaces + + Returns + ------- + tag : string + the shortened version of the tag. + + """ + for k, v in self.nsmap.items(): # check for the namespaces in nsmap + if "{" + str(v) + "}" in tag: + tag = tag.replace("{" + str(v) + "}", k + ":") + break + return tag + + def merge_dicts(self, d1, d2, forceArray=False): + """ + merge dict d2 into d1 without loosing entries in either of both. + If a key of d2 already exists in d1, + make the element of d1 an array if neccessary and append the + value from d2 to that array + + Parameters + ---------- + d1 : dict + target of the merger + d2 : dict + source of the merger + forceArray : bool (False) + sets whether d1 is forced to be an array. + The default is False. + Returns + ------- + d1 : dict + merged dict + + """ + for k, v in d2.items(): # iterate of d2 + if k in d1: # emergency array check if keys are doubled it must be an array even if forceArray is False + + if isinstance(d1[k], list): + d1[k].append(v) + else: + warnings.warn( + str("Key " + str(k) + " is doubled in dict1 and dict2. It will be forced to be an array!"), + RuntimeWarning) + d1[k] = [d1[k], v] + elif forceArray: + d1[k] = [v] # convert v to list this is the effect of forceArray + else: + d1[k] = v + return d1 + + # ------------ Generate XML from dict --------- + def __dict2attrib__(self, value): + attrib = "" + if isinstance(value, dict): + for k, v in value.items(): + if self.__isattribute__(k): + if v[0] != '"': + v = '"' + v + if v[-1] != '"': + v = v + '"' + attrib = attrib + (" %s=%s" % (k[1:], v)) # index 1 removes leading @ + if isinstance(value, list): + for v in value: + attrib += self.dict2attrib(v) + return attrib + + def __isattribute__(self, key): + """ + Checks whether the key is meant to indicate an attribute + i.e. the first character is "@" + + Parameters + ---------- + key : dict key + The Key which is checked for being an attribute + + Returns + ------- + ret : boolean + True="the key is an attribute", False="the key is a tag" + + """ + # ret = (key in ["schemaVersion","schemaLocation","lang","id","refType"]) or ("xmlns" in key) + ret = (str(key)[0] == "@" and str(key) != "@_Comment") + return ret + + def __dict2text__(self, value): + # search for "#text" keys and return the value + text = "" + if isinstance(value, dict): + for k, v in value.items(): + if k[0] == "#": + text = v + + return text + + def json2dict(self): + """ + Takes the self.json attribute and parses it to a dict + sets self.dic to the resulting dict. + + Parameters + ---------- + + Returns + ------- + + """ + self.dic = json.loads(self.json) + return + + def __dict2xml__(self, dic): + """ + Takes the dict representation and generates an xml-string + Operates recursive and return the string, therefore. + (it does not set self.xml!) + + Parameters + ---------- + dic : dict with the dcc information + + Returns + ------- + + """ + + attribs = "" # collect the attributes for this tag + ret = "" # collect the text in this tag + + try: + for k, v in dic.items(): + if isinstance(v, list): # repeated tags of same type + for item in v: # unroll + ret += self.__dict2xml__({k: item}) # process each as single dict + else: + if k == "@_Comment": + ret += self.TpComment.render(comment=v) + "\n" # Exceptional case for comments + elif k == "#text": + ret = str(v) # exceptional case for the tag-content + else: # remaining are attributes and usual tags + attribs = self.__dict2attrib__(v) # collect all attributes + if k[0] == "@": + pass # ignore attributes here + else: # work on the single tag with template + ret += "\n" + self.TpTag.render(name=k, + attributes=attribs, + text=self.__dict2xml__(v)) + "\n" + # print(ret) debugPrint add option for that to reactivate + # Exception Handling + except Exception as exceptInst: + if not self.parsingErrorInRecursionOccured: + # OK we are at the cause of the Error + self.firstParsingError = str(exceptInst) # save the fist error Message + # create an list with the first chars of the dict we ware parsing at the moment + if len(str(dic)) > TRACBACKLENGTH: + self.parsingErrorTraceBack = [str(dic)[:TRACBACKLENGTH].replace('\n', '\\n')] + else: + self.parsingErrorTraceBack = [str(dic).replace('\n', '\\n')] + self.parsingErrorInRecursionOccured = True + else: + # we had an error before so we don't want the error message but the first chars of the dict at this recursion level + if len(str(dic)) > TRACBACKLENGTH: + self.parsingErrorTraceBack.append(str(dic)[:TRACBACKLENGTH].replace('\n', '\\n')) + else: + self.parsingErrorTraceBack.append(str(dic).replace('\n', '\\n')) + raise RuntimeError(exceptInst + "dic to parse " + str(dic)) + # End Exception Handling + return ret + + def dict2xml(self): + """ + Takes the self.dic attribute and generates an xml-string + and sets it in self.xml + (calls the recursive privat version __dict2xml__) + + Parameters + ---------- + + Returns + ------- + + """ + xml = self.__dict2xml__(self.dic) # the private method runs recursively! + # now remove blank lines in xml + self.xml = os.linesep.join([s for s in xml.splitlines() if s.strip()]) + return + + def get_xml(self, beauty=True): + """ + returns the (beautyfied) xml string of self. + + Parameters + ---------- + beauty : bool (True) + sets whether the string is beautyfied (for humans) + + Returns + ------- + xml-string + + """ + self.__update_tree__() # update the tree from the current self.xml + try: + ret = ElementTree.tostring(self.tree, pretty_print=beauty, encoding="unicode") + + except: + print("<ERROR>dcc_stuff.get_xml() failed </ERROR>") + ret = self.xml + return ret + + +def XMLToJson(xml): + dccInst = dcc() + if isinstance(xml, str): + dccInst.read_dcc_string(xml) + else: + dccInst.read_dcc_string(str(xml)) + dccInst.xml2dict() + return json.loads(dccInst.get_json()) + + +def JSONToXML(jsonData): + dccInst = dcc() + if isinstance(jsonData, str): + dccInst.read_json_string(jsonData) + elif isinstance(jsonData, dict): + dccInst.read_json_string(json.dumps(jsonData)) + else: + dccInst.read_json_string(str(jsonData)) + dccInst.json2dict() + dccInst.dict2xml() + return dccInst.get_xml() + + +def to_dsi_real(lab, val, dsi_unit, + expUnc=None, + covFactor=None, + covProb=None, + distib=None): + """ + create the xml entry for a single value with/wo uncertainty + + Parameters + ---------- + lab : string + label of the entry (measurand) + val : numerical + numerical value of the entry. + dsi_unit : string + D-SI conformal string of the unit + expUnc : numerical, optional + absolute expanded uncertainty of val + covFactor : numerical, optional + value of the coverage factor (like k=2). + The default is None. + covProb : numerical, optional + value of the coverage probability. + The default is None. + distib : string, optional + type of probability distribution of the entry. + The default is None. + + Returns + ------- + dsi : dict + dict of the structure of a D-SI entry. + + """ + dic = {'si:value': {"text": str(val)}} + if dsi_unit is not None: + dic.update({'si:unit': {"text": dsi_unit}}) + if lab is not None: + dic.update({'si:label': {"text": lab}}) + if covFactor is not None: + dic.update({'si:coveragefactor': {"text": str(covFactor)}}) + if expUnc is not None: + dic.update({'si:uncertainty': {"text": str(expUnc)}}) + + return {'si:real': dic} + + +def to_dsi_reallist(arr, lab=None): + """ + build an dict of a list of dicts in order to represent a + realList entry according to D-SI + + Parameters + ---------- + arr : list of dicts + the list of real entries to enclose in the list + lab : string, optional + the label associated with the list. + The default is None. + + Returns + ------- + dic : dict + a dict of a list of dicts. + + """ + dic = {'si:realList': arr} + if lab is not None: + dic.update({'si:label': lab}) + + return dic + + +def beautify_xml(text): + parser = ElementTree.XMLParser(remove_blank_text=True, ns_clean=True) + test = text.replace("\\\"", "\"") + # print(test) + try: + tree = ElementTree.fromstring(test, parser=parser) + ret = ElementTree.tostring(tree, pretty_print=True, encoding="unicode") + except: + ret = "<ERROR>dcc_stuff:beautyfy_xml failed </ERROR>" + return ret \ No newline at end of file diff --git a/src/dccXMLJSONConv/dccServer.py b/src/dccXMLJSONConv/dccServer.py new file mode 100755 index 0000000000000000000000000000000000000000..3b8d21e1676a22d68ce72e229d8db7f160ff2240 --- /dev/null +++ b/src/dccXMLJSONConv/dccServer.py @@ -0,0 +1,123 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +#Created on Sun Dec 12 16:06:24 2021 by Thomas Bruns +# This file is part of dcc-xmljsonconv (https://gitlab1.ptb.de/digitaldynamicmeasurement/dcc_XMLJSONConv) +# Copyright 2024 [Thomas Bruns (PTB), Benedikt Seeger(PTB), Vanessa Stehr(PTB)] +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program 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 Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +from fastapi import FastAPI, HTTPException, Response +import json +import os + +from dccXMLJSONConv.dcc_Conv import dcc as DCC + +#remove proxy from env +for k in os.environ: + if "proxy" in k.lower(): + os.environ.pop(k) + +app = FastAPI() + + +@app.get("/") +async def root(): + return {"message": "Hello!\nBetter use the URL /json2dcc/?js={...} \n or /dcc2json (POST method)"} + + +from pydantic import BaseModel +class Data(BaseModel): + xml: str + +@app.post("/dcc2json/") +async def dcc2json(data: Data): + """ + convert an XML-string into json by parsing through the document-tree + + Parameters + ---------- + xml : str + XML-document as string. + + Returns + ------- + str + A JSON-String representing the original XML + + """ + print("start of xml-input:") + print(str(data.xml[0:40])+"\n") + + if data.xml == "": # error for empty json string + raise HTTPException(status_code=400, detail="XML Content Empty!") # 400 Bad Request + else: + try: + dcc=DCC() + dcc.read_dcc_string(data.xml) + dcc.xml2dict() + return Response(dcc.get_json()) + except Exception as e: + print("dcc_server.dcc2json(): failed !") + # raise HTTPException(status_code=400, detail="xml parsing failed for: %s" % dcc.xml) + raise HTTPException(status_code=400, detail="xml parsing failed: "+str(e))#400 Bad Request + + + +############################################################################### +from pydantic import BaseModel +class Json(BaseModel): + js: dict + + +@app.post("/json2dcc/") +async def json2dcc(data: Json): + """ + convert an JSON-string into XML by parsing through the dict + + Parameters + ---------- + data : str + JSON-document as string. + + Returns + ------- + str + A XML-String representing the original JSON + + """ + print("type(data.js) = %s" % str(type(data.js))) + #print(data.js) + if data.js == {}: # error for empty json + raise HTTPException(status_code=400, detail="JSON Content Empty!") # 400 Bad Request + else: + try: + dcc=DCC() + dcc.dic= data.js + dcc.dict2xml() + return Response(dcc.get_xml()) + except Exception as exceptInst: + print("dcc_server.json2xml(): failed !") + # raise HTTPException(status_code=400, detail="xml parsing failed for: %s" % dcc.xml) + + #now we will reverse the list with content in which the error occurred since it was recursive created + tracbacklistAsStr='' + dcc.parsingErrorTraceBack.reverse() + for line in dcc.parsingErrorTraceBack: + tracbacklistAsStr+=str(line)+' ...\n' + raise HTTPException(status_code=400, detail="JSON parsing failed:\n"+str(dcc.firstParsingError)+'\n'+tracbacklistAsStr) + + + +####### To Do ###### +# - CouchDB2dcc(_id) diff --git a/src/dccXMLJSONConv/listTypeFinder.py b/src/dccXMLJSONConv/listTypeFinder.py new file mode 100644 index 0000000000000000000000000000000000000000..5a3fe3338d96a6e19571d4b1f735c712bd333fe2 --- /dev/null +++ b/src/dccXMLJSONConv/listTypeFinder.py @@ -0,0 +1,137 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# This file is part of dcc-xmljsonconv (https://gitlab1.ptb.de/digitaldynamicmeasurement/dcc_XMLJSONConv) +# Copyright 2024 [Benedikt Seeger(PTB), Thomas Bruns (PTB), Vanessa Stehr(PTB)] +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program 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 Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. +import requests +import json +from lxml import etree +import sys +import pathlib +import io + +def download_xsd(url: str): + """ + Downloads an XSD file from a given URL. + + Parameters: + url (str): The URL of the XSD file to download. + + Returns: + io.BytesIO: A bytes stream of the downloaded XSD file if successful, None otherwise. + """ + response = requests.get(url) + if response.status_code == 200: + return io.BytesIO(response.content) + return None + +def find_list_types(xsd_path: str): + """ + Parses an XSD file to find elements with types ending in 'ListType'. + + Parameters: + xsd_path (str): The path to the XSD file. + + Returns: + tuple: A tuple containing two elements; the first is a list of names of the list types, + and the second is a list of attributes of the list types. + """ + tree = etree.parse(xsd_path) + list_types = [] + for element in tree.iter(): + if 'type' in element.attrib and element.attrib['type'].endswith('ListType'): + list_types.append(dict(element.attrib)) + names = [list_type['name'] for list_type in list_types] + return names, list_types + +def find_repeated_elements(xsd_path: str): + """ + Parses an XSD file to identify elements that can be repeated. + + Parameters: + xsd_path (str): The path to the XSD file. + + Returns: + tuple: A tuple containing two elements; the first is a list of names of the repeated elements, + and the second is a dictionary with the names as keys and the max occurrences as values. + """ + tree = etree.parse(xsd_path) + repeated_elements = {} + for element in tree.iter(): + if element.tag is etree.Comment: + continue + if element.tag.endswith('element'): + name = element.get('name') + max_occurs = element.get('maxOccurs', '1') # Default is 1 if not specified + if max_occurs not in ('1', 'unbounded'): + repeated_elements[name] = max_occurs + elif max_occurs == 'unbounded': + repeated_elements[name] = 'unbounded' + names = list(repeated_elements.keys()) + return names, repeated_elements + +def write_to_json(data, file_path: str): + """ + Writes a given data structure to a JSON file. + + Parameters: + data (dict): The data to write to the file. + file_path (str): The path to the JSON file where the data should be saved. + """ + with open(file_path, 'w') as file: + json.dump(data, file, indent=4) + +def getListTypesFromUrl(url: str): + """ + Downloads an XSD file from a URL and analyzes it to find list types and repeated elements. + + Parameters: + url (str): The URL of the XSD file. + + Returns: + tuple: A tuple containing information about list types and repeated elements found in the XSD file. + """ + xsd_file = download_xsd(url) + if xsd_file: + names, list_types = find_list_types(xsd_file) + xsd_file.seek(0) # Reset file pointer to the beginning + repeated_field_names, repeated_field_types = find_repeated_elements(xsd_file) + return names, list_types, repeated_field_names, repeated_field_types + else: + print("Failed to download XSD file.") + return ['Failed to download XSD file.'], ['Failed to download XSD file.'], ['Failed to download XSD file.'], ['Failed to download XSD file.'] + +def main(): + """ + The main function to run the script. It handles command-line arguments for the XSD URL + and output JSON file path, processes the XSD file to extract list types and repeated elements, + and writes the results to a JSON file. + """ + if len(sys.argv) > 1: + url = sys.argv[1] + else: + url = "https://ptb.de/dcc/v3.2.1/dcc.xsd" + url= "https://ptb.de/si/v2.1.0/SI_Format.xsd" + if len(sys.argv) > 2: + json_file_path = sys.argv[2] + else: + script_location = pathlib.Path(__file__).resolve().parent.parent + json_file_path = script_location.parent / 'data' / 'listDCCElements.json' + names, list_types, repeatedFiledNames, repeatedFiledTypes = getListTypesFromUrl(url) + write_to_json({'schemaUrl': url, 'listTypeElements': names, 'listTypeElementsDetails': list_types, 'repeatedFieldNames': repeatedFiledNames, 'repeatedFieldNamesDetails': repeatedFiledTypes}, json_file_path) + print(f"List types have been written to {json_file_path}") + +if __name__ == "__main__": + main() diff --git a/tests/data/20220708_8305_SN1842876.xml b/tests/data/20220708_8305_SN1842876.xml new file mode 100644 index 0000000000000000000000000000000000000000..c292fd524bfe4962bae799445a6ab8e8de653225 --- /dev/null +++ b/tests/data/20220708_8305_SN1842876.xml @@ -0,0 +1,466 @@ +<?xml version="1.0" encoding="UTF-8"?> +<dcc:digitalCalibrationCertificate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dcc="https://ptb.de/dcc" xmlns:si="https://ptb.de/si" xsi:schemaLocation="https://ptb.de/dcc https://ptb.de/dcc/v3.2.1/dcc.xsd" schemaVersion="3.2.1"> + <dcc:administrativeData> + <dcc:dccSoftware> + <dcc:software> + <dcc:name> + <dcc:content>GEMIMEG Tool</dcc:content> + </dcc:name> + <dcc:release>v1.1.2</dcc:release> + </dcc:software> + <dcc:software> + <dcc:name> + <dcc:content>dccTableTool</dcc:content> + </dcc:name> + <dcc:release>0.2.1</dcc:release> + </dcc:software> + </dcc:dccSoftware> + <dcc:coreData> + <dcc:countryCodeISO3166_1>DE</dcc:countryCodeISO3166_1> + <dcc:usedLangCodeISO639_1>en</dcc:usedLangCodeISO639_1> + <dcc:usedLangCodeISO639_1>de</dcc:usedLangCodeISO639_1> + <dcc:mandatoryLangCodeISO639_1>de</dcc:mandatoryLangCodeISO639_1> + <dcc:uniqueIdentifier>PTB-17042-2022</dcc:uniqueIdentifier> + <dcc:identifications> + <dcc:identification> + <dcc:issuer>calibrationLaboratory</dcc:issuer> + <dcc:value>1.71-1254/0622</dcc:value> + <dcc:name> + <dcc:content lang="de">Geschäftszeichen:</dcc:content> + <dcc:content lang="en">Reference no.:</dcc:content> + </dcc:name> + </dcc:identification> + </dcc:identifications> + <dcc:receiptDate>2022-09-06</dcc:receiptDate> + <dcc:beginPerformanceDate>2022-07-08</dcc:beginPerformanceDate> + <dcc:endPerformanceDate>2022-07-08</dcc:endPerformanceDate> + <dcc:performanceLocation>laboratory</dcc:performanceLocation> + </dcc:coreData> + <dcc:items> + <dcc:item refType="vib_Item vib_AccelerationReferenceStandard"> + <dcc:name> + <dcc:content lang="de">Beschleunigungsaufnehmer-Bezugsnormal</dcc:content> + <dcc:content lang="en">Acceleration reference standard sensor</dcc:content> + </dcc:name> + <dcc:manufacturer> + <dcc:name> + <dcc:content>Brüel Kjær Dänemark</dcc:content> + </dcc:name> + </dcc:manufacturer> + <dcc:model>8305</dcc:model> + <dcc:identifications> + <dcc:identification> + <dcc:issuer>manufacturer</dcc:issuer> + <dcc:value>1842876</dcc:value> + <dcc:name> + <dcc:content lang="de">Serien Nr.</dcc:content> + <dcc:content lang="en">Serial no.</dcc:content> + </dcc:name> + </dcc:identification> + </dcc:identifications> + </dcc:item> + </dcc:items> + <dcc:calibrationLaboratory> + <dcc:contact> + <dcc:name> + <dcc:content>Physikalisch Technische Bundesanstalt AG 1.71 Darstellung Beschleunigung</dcc:content> + </dcc:name> + <dcc:eMail>acceleration@ptb.de</dcc:eMail> + <dcc:phone>+49 531 592 1223</dcc:phone> + <dcc:fax>+49 531 592 691227</dcc:fax> + <dcc:location> + <dcc:city>Braunschweig</dcc:city> + <dcc:countryCode>DE</dcc:countryCode> + <dcc:postCode>38116</dcc:postCode> + <dcc:street>Bundesallee</dcc:street> + <dcc:streetNo>100</dcc:streetNo> + </dcc:location> + </dcc:contact> + </dcc:calibrationLaboratory> + <dcc:respPersons> + <dcc:respPerson> + <dcc:person> + <dcc:name> + <dcc:content>Dr.-Ing. Leonard Klaus</dcc:content> + </dcc:name> + </dcc:person> + <dcc:mainSigner>true</dcc:mainSigner> + </dcc:respPerson> + <dcc:respPerson> + <dcc:person> + <dcc:name> + <dcc:content>Dipl.-Ing (FH) Denis Nordmann</dcc:content> + </dcc:name> + </dcc:person> + </dcc:respPerson> + </dcc:respPersons> + <dcc:customer> + <dcc:name> + <dcc:content>Accelerator Meas GmbH</dcc:content> + </dcc:name> + <dcc:eMail>info@acclemeas.de</dcc:eMail> + <dcc:location> + <dcc:city>Musterstadt</dcc:city> + <dcc:countryCode>DE</dcc:countryCode> + <dcc:postCode>42420</dcc:postCode> + <dcc:street>Hirschbach im Mühlkreis</dcc:street> + <dcc:streetNo>42</dcc:streetNo> + </dcc:location> + </dcc:customer> + <dcc:statements> + <dcc:statement> + <dcc:name> + <dcc:content lang="en">CIPM MRA</dcc:content> + <dcc:content lang="de">CIPM MRA</dcc:content> + </dcc:name> + <dcc:norm>DIN EN ISO/IEC 17025</dcc:norm> + <dcc:declaration> + <dcc:content lang="en">The Physikalisch-Technische Bundesanstalt (PTB) in Braunschweig and Berlin is the +National Metrology Institute and the supreme technical authority of the Federal Republic of Germany +for metrology. The PTB comes under the auspices of the Federal Ministry for Economic Affairs and +Climate Action. It meets the requirements for calibration and testing laboratories as defined in +DIN EN ISO/IEC 17025. +The central task of PTB is to realize, to maintain and to disseminate the legal units in compliance with +the International System of Units (SI). PTB thus is at the top of the metrological hierarchy in Germany. +The calibration certificates issued by PTB document a calibration traceable to national measurement +standards. +This certificate is consistent with the Calibration and Measurement Capabilities (CMCs) that are +included in Appendix C of the Mutual Recognition Arrangement (MRA) drawn up by the International +Committee for Weights and Measures (CIPM). Under the MRA, all participating institutes recognize +the validity of each other’s calibration and measurement certificates for the quantities, ranges and +measurement uncertainties specified in Appendix C (for details, see http://www.bipm.org). +The CIPM MRA Logo and this statement attest only to the measurement component of the certificate</dcc:content> + <dcc:content lang="de">Die Physikalisch-Technische Bundesanstalt (PTB) in Braunschweig und Berlin ist das +nationale Metrologieinstitut und die technische Oberbehörde der Bundesrepublik Deutschland für das +Messwesen. Die PTB gehört zum Geschäftsbereich des Bundesministeriums für Wirtschaft und +Klimaschutz. Sie erfüllt die Anforderungen an Kalibrier- und Prüflaboratorien auf der Grundlage der +DIN EN ISO/IEC 17025. +Zentrale Aufgabe der PTB ist es, die gesetzlichen Einheiten in Übereinstimmung mit dem +Internationalen Einheitensystem (SI) darzustellen, zu bewahren und weiterzugeben. Die PTB steht +damit an oberster Stelle der metrologischen Hierarchie in Deutschland. Die Kalibrierscheine der PTB +dokumentieren eine auf nationale Normale rückgeführte Kalibrierung. +Dieser Ergebnisbericht ist in Übereinstimmung mit den Kalibrier- und Messmöglichkeiten (CMCs), wie +sie im Anhang C des gegenseitigen Abkommens (MRA) des Internationalen Komitees für Maße und +Gewichte enthalten sind. Im Rahmen des MRA wird die Gültigkeit der Ergebnisberichte von allen +teilnehmenden Instituten für die im Anhang C spezifizierten Messgrößen, Messbereiche und +Messunsicherheiten gegenseitig anerkannt (nähere Informationen unter http://www.bipm.org). +Diese Aussage und das CIPM-MRA-Logo beziehen sich nur auf die Messergebnisse in diesem +Kalibrierschein.</dcc:content> + </dcc:declaration> + </dcc:statement> + </dcc:statements> + </dcc:administrativeData> + <dcc:measurementResults> + <dcc:measurementResult> + <dcc:name> + <dcc:content lang="de">Messergebnisse</dcc:content> + <dcc:content lang="en">Measurement results</dcc:content> + </dcc:name> + <dcc:usedMethods> + <dcc:usedMethod refType="basic_uncertainty"> + <dcc:name> + <dcc:content lang="de">Erweiterte Messunsicherheit</dcc:content> + <dcc:content lang="en">Expanded uncertainty</dcc:content> + </dcc:name> + <dcc:description> + <dcc:content lang="de">Angegeben ist die erweiterte Messunsicherheit, die sich aus der Standardmessunsicherheit durch Multiplikation mit dem Erweiterungsfaktor k=2 ergibt. Sie wurde gemäß dem „Guide to the Expression of Uncertainty in Measurement (GUM)“ ermittelt. Der Wert der Messgröße liegt dann im Regelfall mit einer Wahrscheinlichkeit von annähernd 95 % im zugeordneten Überdeckungsintervall.</dcc:content> + <dcc:content lang="en">The expanded uncertainty was calculated from the contributions of uncertainty originating from the standards used, from the weighings and the air buoyancy corrections. The reported uncertainty does not include an estimate of long-term variations.</dcc:content> + </dcc:description> + <dcc:norm>GUM</dcc:norm> + </dcc:usedMethod> + <dcc:usedMethod refType="vib_sineCalibration"> + <dcc:name> + <dcc:content lang="en">Primary vibration calibration with Laser Interferometry using Sine-approximation</dcc:content> + <dcc:content lang="de">Primäre Schwingungskalibration durch Laserinterferometrie unter Verwendung von Sinus Approximation</dcc:content> + </dcc:name> + <dcc:description> + <dcc:content lang="en">The accelerometer reference standard was excited to rectilinear sinusoidal vibrations on an electrodynamic vibration exciter equipped with an air-bearing oscillating section. The transducer was coupled as a single-ended transducer. The mounting surfaces were lightly greased, and the tightening torque was 2 N-m. +Vibration excitation was performed in the frequency range of 10 Hz to 10 kHz using the vibration exciter of the high-frequency acceleration standard measuring device (national standard). +The measurements of the vibration displacement amplitude and the phase shift Δφqa of the complex charge transfer coefficient Sqa were performed using the sinusoidal compensation method. In order to get from the instantaneous values of the vibration displacement to the amplitude and zero phase angle, the approximation is performed with a sinusoidal approach. A computerized transient recorder is used to synchronously acquire the two-channel interferometer output signal and the output signal of the normal measurement chain. +The calibration procedure is in accordance with the corresponding standard ISO 16063-11:1999. +The acceleration acting on the transducer was calculated from the measured vibration displacement amplitude and the known excitation frequency. +The magnitude Sqa of the complex charge transfer coefficient Sqa is obtained as the ratio of the amplitude of the output voltage of the normal electrode to the amplitude of the excitation acceleration. The output charge of the transducer was determined using a calibrated normal charge amplifier. +The phase shift Δφqa of the complex charge transfer coefficient Sqa is obtained as the difference between the zero phase angle of the output charge φq and the zero phase angle of the acceleration φa. +According to ISO 2041, the phase shift is defined as Δφqa = φq - φa. The acceleration vector is considered positive if it is directed from its mounting surface to the accelerometer.</dcc:content> + <dcc:content lang="de">Das Beschleunigungsaufnehmer-Bezugsnormal wurde auf einem elektrodynamischen Schwingungserreger, der mit einem luftgelagerten Schwingteil ausgerüstet ist, zu geradlinigen sinusförmigen Schwingungen angeregt. Der Aufnehmer wurde als Single-Ended-Aufnehmer angekoppelt. Die Montageflächen waren leicht eingefettet, das Anzugsdrehmoment betrug 2 N·m. +Die Schwingungsanregung erfolgte im Frequenzbereich von 10 Hz bis 10 kHz mit dem Schwingungserreger der Hochfrequenz-Beschleunigungs-Normalmesseinrichtung (nationales Normal). +Die Messungen der Schwingwegamplitude und der Phasenverschiebung Δφqa des komplexen Ladungsübertragungskoeffizienten Sqa wurden nach dem Sinusausgleichsverfahren ausgeführt. Um von den Augenblickswerten des Schwingwegs zu der Amplitude und dem Nullphasenwinkel zu gelangen, wird die Approximation mit einem Sinusansatz durchgeführt. Mit einem rechnergestützten Transientenrekorder werden das zweikanalige Interferometer-Ausgangssignal und das Ausgangssignal der Normal-Messkette synchron erfasst. +Das Kalibrierverfahren ist in Übereinstimmung mit der entsprechenden Norm ISO 16063-11:1999. +Die auf den Aufnehmer einwirkende Beschleunigung wurde aus der gemessenen Schwingwegamplitude und der bekannten Anregungsfrequenz berechnet. +Der Betrag Sqa des komplexen Ladungsübertragungskoeffizienten Sqa ergibt sich als Verhältnis der Amplitude der Ausgangsspannung der Normal-Messkette zur Amplitude der Anregungsbeschleunigung. Die Ausgangsladung des Aufnehmers wurde mit Hilfe eines kalibrierten Normal-Ladungsverstärkers bestimmt. +Die Phasenverschiebung Δφqa des komplexen Ladungsübertragungskoeffizienten Sqa ergibt sich als Differenz aus dem Nullphasenwinkel der Ausgangsladung φq und dem Nullphasenwinkel der Beschleunigung φa. +Entsprechend ISO 2041 ist die Phasenverschiebung definiert zu Δφqa = φq – φa. Der Beschleunigungsvektor wird als positiv betrachtet, wenn er von seiner Montagefläche zum Beschleunigungs-aufnehmer gerichtet ist.</dcc:content> + </dcc:description> + <dcc:norm>ISO 16063-11 1999</dcc:norm> + </dcc:usedMethod> + </dcc:usedMethods> + <dcc:measuringEquipments> + <dcc:measuringEquipment refType="vib_chargeAmplifier"> + <dcc:name> + <dcc:content lang="de">Ladungsverstärker</dcc:content> + <dcc:content lang="en">Charge amplifier</dcc:content> + </dcc:name> + <dcc:model>Brüel Kjær 2650</dcc:model> + <dcc:identifications> + <dcc:identification> + <dcc:issuer>manufacturer</dcc:issuer> + <dcc:value>1502245</dcc:value> + <dcc:name> + <dcc:content lang="de">Serien Nr.</dcc:content> + <dcc:content lang="en">Serial no.</dcc:content> + </dcc:name> + </dcc:identification> + </dcc:identifications> + </dcc:measuringEquipment> + <dcc:measuringEquipment refType="vib_vibrationExciter"> + <dcc:name> + <dcc:content lang="en">Vibration exciter</dcc:content> + <dcc:content lang="de">Schwingerreger</dcc:content> + </dcc:name> + <dcc:model>Spektra SE-09</dcc:model> + </dcc:measuringEquipment> + </dcc:measuringEquipments> + <dcc:influenceConditions> + <dcc:influenceCondition refType="vib_ambientTemperature"> + <dcc:name> + <dcc:content lang="de">Umgebungsbedingung Temperatur</dcc:content> + <dcc:content lang="en">Ambient condition temperature</dcc:content> + </dcc:name> + <dcc:description> + <dcc:content lang="de">Die Werte wurden während der Kalibrierung überwacht.</dcc:content> + <dcc:content lang="en">Values measured while calibration was performed.</dcc:content> + </dcc:description> + <dcc:data> + <dcc:quantity> + <dcc:name> + <dcc:content lang="en">Room temperature during calibration</dcc:content> + <dcc:content lang="de">Raumtemperatur währenden der Kalibration</dcc:content> + </dcc:name> + <si:real> + <si:value>296.15</si:value> + <si:unit>\kelvin</si:unit> + <si:expandedUnc> + <si:uncertainty>1</si:uncertainty> + <si:coverageFactor>1.73</si:coverageFactor> + <si:coverageProbability>1</si:coverageProbability> + <si:distribution>rectangular</si:distribution> + </si:expandedUnc> + </si:real> + </dcc:quantity> + </dcc:data> + </dcc:influenceCondition> + <dcc:influenceCondition refType="vib_ambientHumidityRelative"> + <dcc:name> + <dcc:content lang="de">Umgebungsbedingung relative Luftfeuchte</dcc:content> + <dcc:content lang="en">Ambient condition relative humidity</dcc:content> + </dcc:name> + <dcc:description> + <dcc:content lang="de">Die Werte wurden während der Kalibrierung überwacht.</dcc:content> + <dcc:content lang="en">Values measured while calibration was performed.</dcc:content> + </dcc:description> + <dcc:data> + <dcc:quantity> + <dcc:name> + <dcc:content lang="en">Ambient humidity</dcc:content> + <dcc:content lang="de">Ungebungsluftfeuchtigkeit</dcc:content> + </dcc:name> + <si:real> + <si:value>60</si:value> + <si:unit>\percent</si:unit> + <si:expandedUnc> + <si:uncertainty>15</si:uncertainty> + <si:coverageFactor>1.73</si:coverageFactor> + <si:coverageProbability>1</si:coverageProbability> + <si:distribution>rectangular</si:distribution> + </si:expandedUnc> + </si:real> + </dcc:quantity> + </dcc:data> + </dcc:influenceCondition> + <dcc:influenceCondition refType="vib_mountingConditions"> + <dcc:name> + <dcc:content lang="en">Sensor mounting conditions</dcc:content> + <dcc:content lang="de">Sensormontagebediungen</dcc:content> + </dcc:name> + <dcc:data> + <dcc:quantity refType="vib_mountingTorque"> + <dcc:name> + <dcc:content lang="en">mounting torque</dcc:content> + <dcc:content lang="de">Anzugdrehmoment</dcc:content> + </dcc:name> + <si:real> + <si:value>2</si:value> + <si:unit>\newton\metre</si:unit> + <si:expandedUnc> + <si:uncertainty>0.1</si:uncertainty> + <si:coverageFactor>2</si:coverageFactor> + <si:coverageProbability>0.95</si:coverageProbability> + <si:distribution>normal</si:distribution> + </si:expandedUnc> + </si:real> + </dcc:quantity> + <dcc:quantity refType="vib_mountingLaserPositiones"> + <dcc:name> + <dcc:content lang="en">Ankopplung des Aufnehmers und Laser-Positionen</dcc:content> + <dcc:content lang="de">Mounting of the sensore and laser spot position</dcc:content> + </dcc:name> + <dcc:noQuantity> + <dcc:content lang="en">The transducer was coupled as a single-ended transducer. The laser beams of the interferometers were adjusted symmetrically and diametrically next to the transducer on the oscillating part.</dcc:content> + <dcc:content lang="de">Die Ankopplung des Aufnehmers erfolgte als Single-Ended-Aufnehmer. Die Laserstrahlen der Interferometer wurden symmetrisch und diametral neben dem Aufnehmer auf das Schwingteil justiert.</dcc:content> + </dcc:noQuantity> + </dcc:quantity> + <dcc:quantity refType="vib_orientationInGravityfield vib_OrientationInGravityfieldVertical"> + <dcc:name> + <dcc:content lang="de">Ausrichtung im Erdschwerefeld</dcc:content> + <dcc:content lang="en">Orientation in the earth gravity field</dcc:content> + </dcc:name> + <dcc:noQuantity> + <dcc:name> + <dcc:content lang="en">Orientation in the earth gravity field</dcc:content> + <dcc:content lang="de">Ausrichtung im Erdschwerefeld</dcc:content> + </dcc:name> + <dcc:content lang="de">vertikal</dcc:content> + <dcc:content lang="en">vertical</dcc:content> + </dcc:noQuantity> + </dcc:quantity> + </dcc:data> + </dcc:influenceCondition> + <dcc:influenceCondition refType="vib_chargeAmplifyerConfSet" id="ConfigSet_a3548501"> + <dcc:name> + <dcc:content lang="de">Einstellungen des Ladungsverstärkers</dcc:content> + <dcc:content lang="en">Configuration of charge amplifier</dcc:content> + </dcc:name> + <dcc:data> + <dcc:quantity> + <dcc:name> + <dcc:content lang="de">Transducer Sensitivity</dcc:content> + </dcc:name> + <dcc:noQuantity> + <dcc:content>1,000 pC/Unit</dcc:content> + </dcc:noQuantity> + </dcc:quantity> + <dcc:quantity refType="vib_chargeAmplifierLPFreq"> + <dcc:name> + <dcc:content lang="de">Lower Frequency Limit</dcc:content> + </dcc:name> + <si:real> + <si:value>0.3</si:value> + <si:unit>\hertz</si:unit> + </si:real> + </dcc:quantity> + <dcc:quantity> + <dcc:name> + <dcc:content lang="de">Upper Frequency Limit</dcc:content> + </dcc:name> + <dcc:noQuantity> + <dcc:content>Lin</dcc:content> + </dcc:noQuantity> + </dcc:quantity> + <dcc:quantity> + <dcc:name> + <dcc:content lang="de">Transducer Range (1-11 pC/Unit)</dcc:content> + </dcc:name> + <dcc:noQuantity> + <dcc:content>0,1 V/Unit out</dcc:content> + </dcc:noQuantity> + </dcc:quantity> + </dcc:data> + </dcc:influenceCondition> + <dcc:influenceCondition refType="vib_sensorTemperature"> + <dcc:name> + <dcc:content lang="en">Sensor temperature during calibration</dcc:content> + <dcc:content lang="de">Temperatur des Aufnehmers während der Kalibrierung</dcc:content> + </dcc:name> + <dcc:data> + <dcc:quantity> + <dcc:name> + <dcc:content lang="en">Sensor temperature</dcc:content> + <dcc:content lang="de">Temperatur des Aufnehmers</dcc:content> + </dcc:name> + <si:real> + <si:value>296.15</si:value> + <si:unit>\kelvin</si:unit> + <si:expandedUnc> + <si:uncertainty>1</si:uncertainty> + <si:coverageFactor>1.73</si:coverageFactor> + <si:coverageProbability>1</si:coverageProbability> + <si:distribution>rectangular</si:distribution> + </si:expandedUnc> + </si:real> + </dcc:quantity> + </dcc:data> + </dcc:influenceCondition> + </dcc:influenceConditions> + <dcc:results> + <dcc:result refType="vib_chargeTransferFunction"> + <dcc:name> + <dcc:content lang="de">Ladungsübertragungsfunktion</dcc:content> + <dcc:content lang="en">Charge transfer function</dcc:content> + </dcc:name> + <dcc:data> + <dcc:list refType="basic_1IndexTable"> + <dcc:quantity refType="vib_frequency vib_nominalFrequency basic_TableIndex0"> + <dcc:name> + <dcc:content lang="de">Frequenz</dcc:content> + <dcc:content lang="en">Frequency</dcc:content> + </dcc:name> + <si:realListXMLList> + <si:valueXMLList>10.0 12.5 16.0 20.0 25.0 31.5 40.0 50.0 63.0 80.0 100.0 125.0 160.0 200.0 250.0 315.0 400.0 500.0 630.0 800.0 1000.0 1250.0 1600.0 2000.0 2500.0 3150.0 4000.0 5000.0 6300.0 8000.0 10000.0</si:valueXMLList> + <si:unitXMLList>\hertz</si:unitXMLList> + </si:realListXMLList> + </dcc:quantity> + <dcc:quantity refType="vib_nominalAccelerationAmplitude vib_accelerationAmplitude"> + <dcc:name> + <dcc:content lang="de">Beschleunigungsamplitude</dcc:content> + <dcc:content lang="en">Acceleration Amplitude</dcc:content> + </dcc:name> + <si:realListXMLList> + <si:valueXMLList>5.0 5.0 5.0 10.0 10.0 10.0 10.0 50.0 50.0 50.0 50.0 50.0 50.0 50.0 50.0 50.0 50.0 50.0 50.0 50.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0</si:valueXMLList> + <si:unitXMLList>\metre\per\second\tothe{2}</si:unitXMLList> + </si:realListXMLList> + </dcc:quantity> + <dcc:quantity refType="vib_magnitudeTransferCoefCharge"> + <dcc:name> + <dcc:content lang="de">Ladungsübertragungskoeffizent Betrag</dcc:content> + <dcc:content lang="en">Charge transfer coefficient magnitude</dcc:content> + </dcc:name> + <si:realListXMLList> + <si:valueXMLList>0.13017 0.13017 0.13016 0.13016 0.13017 0.13017 0.13017 0.13016 0.13013 0.13013 0.13012 0.13012 0.13012 0.13012 0.13014 0.13014 0.13015 0.13018 0.13017 0.13027 0.13033 0.13044 0.13058 0.13085 0.13124 0.13188 0.13300 0.13467 0.1375 0.1429 0.1497</si:valueXMLList> + <si:unitXMLList>\pico\coulomb\per\metre\second\tothe{-2}</si:unitXMLList> + <si:expandedUncXMLList> + <si:uncertaintyXMLList>0.00013 0.00013 0.00013 0.00013 0.00013 0.00013 0.00013 0.00013 0.00013 0.00013 0.00013 0.00013 0.00013 0.00013 0.00013 0.00013 0.00013 0.00013 0.00013 0.00013 0.00013 0.00013 0.00013 0.00013 0.00013 0.00013 0.00013 0.00013 0.0004 0.0004 0.0004</si:uncertaintyXMLList> + <si:coverageFactorXMLList>2.0</si:coverageFactorXMLList> + <si:coverageProbabilityXMLList>0.95</si:coverageProbabilityXMLList> + <si:distributionXMLList>normal</si:distributionXMLList> + </si:expandedUncXMLList> + </si:realListXMLList> + <dcc:relativeUncertainty> + <dcc:relativeUncertaintyXmlList> + <si:valueXMLList>0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.001 0.003 0.003 0.003</si:valueXMLList> + <si:unitXMLList>\one</si:unitXMLList> + </dcc:relativeUncertaintyXmlList> + </dcc:relativeUncertainty> + </dcc:quantity> + <dcc:quantity refType="vib_phase"> + <dcc:name> + <dcc:content lang="de">Phasenverzögerung</dcc:content> + <dcc:content lang="en">Phase delay</dcc:content> + </dcc:name> + <si:realListXMLList> + <si:valueXMLList>-0.0003 -0.0004 -0.0004 -0.0003 -0.0001 -0.0002 -0.0003 -0.0001 0.0000 0.0002 -0.0002 -0.0002 -0.0000 -0.0000 0.0001 0.0001 0.0000 0.0001 0.0005 0.0003 0.0001 -0.000 0.000 0.000 0.000 0.000 -0.000 -0.000 0.000 -0.034 -0.002</si:valueXMLList> + <si:unitXMLList>\radian</si:unitXMLList> + <si:expandedUncXMLList> + <si:uncertaintyXMLList>0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.009 0.009 0.009 0.009 0.009 0.009 0.009 0.009 0.009 0.009</si:uncertaintyXMLList> + <si:coverageFactorXMLList>2.0</si:coverageFactorXMLList> + <si:coverageProbabilityXMLList>0.95</si:coverageProbabilityXMLList> + <si:distributionXMLList>normal</si:distributionXMLList> + </si:expandedUncXMLList> + </si:realListXMLList> + </dcc:quantity> + </dcc:list> + </dcc:data> + </dcc:result> + </dcc:results> + </dcc:measurementResult> + </dcc:measurementResults> +</dcc:digitalCalibrationCertificate> diff --git a/tests/data/APITestJSONStub.json b/tests/data/APITestJSONStub.json new file mode 100644 index 0000000000000000000000000000000000000000..f410d9615d17920864e5bf4a5d4b192bd9de210d --- /dev/null +++ b/tests/data/APITestJSONStub.json @@ -0,0 +1,43 @@ +{ + "dcc:quantity": { + "@refType": "vib_phase", + "@_Comment": [ + " This is only an example ", + " This is an additional Comment " + ], + "dcc:name": { + "dcc:content": [ + { + "@lang": "de", + "#text": "Phasenverz\u00f6gerung" + }, + { + "@lang": "en", + "#text": "Phase delay" + } + ] + }, + "si:realListXMLList": { + "si:valueXMLList": { + "#text": "1.7 1.5 1.3 1.1 1.02 0.92 0.83 0.76 0.69 0.63 0.57 0.53 0.48 0.44 0.42 0.38 0.33 0.28 0.39 0.30 0.30 0.15 0.17 0.1 0.1 0.1 0.1" + }, + "si:unitXMLList": { + "#text": "\\degree" + }, + "si:expandedUncXMLList": { + "si:uncertaintyXMLList": { + "#text": "1.5 1.0 1.0 1.0 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 1.0 1.0 1.0 1.0" + }, + "si:coverageFactorXMLList": { + "#text": "2.0" + }, + "si:coverageProbabilityXMLList": { + "#text": "0.95" + }, + "si:distributionXMLList": { + "#text": "normal" + } + } + } + } +} \ No newline at end of file diff --git a/tests/data/APITestXMLStub.xml b/tests/data/APITestXMLStub.xml new file mode 100644 index 0000000000000000000000000000000000000000..48a356a64050772491f291202652e98110d5cc8b --- /dev/null +++ b/tests/data/APITestXMLStub.xml @@ -0,0 +1,18 @@ + <dcc:quantity refType="vib_phase"> + <!-- This is only an example --> + <dcc:name> + <dcc:content lang="de">Phasenverzögerung</dcc:content> + <dcc:content lang="en">Phase delay</dcc:content> + </dcc:name> + <!-- This is an additional Comment --> + <si:realListXMLList> + <si:valueXMLList>1.7 1.5 1.3 1.1 1.02 0.92 0.83 0.76 0.69 0.63 0.57 0.53 0.48 0.44 0.42 0.38 0.33 0.28 0.39 0.30 0.30 0.15 0.17 0.1 0.1 0.1 0.1</si:valueXMLList> + <si:unitXMLList>\degree</si:unitXMLList> + <si:expandedUncXMLList> + <si:uncertaintyXMLList>1.5 1.0 1.0 1.0 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 1.0 1.0 1.0 1.0</si:uncertaintyXMLList> + <si:coverageFactorXMLList>2.0</si:coverageFactorXMLList> + <si:coverageProbabilityXMLList>0.95</si:coverageProbabilityXMLList> + <si:distributionXMLList>normal</si:distributionXMLList> + </si:expandedUncXMLList> + </si:realListXMLList> + </dcc:quantity> \ No newline at end of file diff --git a/tests/data/unindentedJSONTOXMLRespons.xml b/tests/data/unindentedJSONTOXMLRespons.xml new file mode 100644 index 0000000000000000000000000000000000000000..ddb9f13a77d145f9220c6e41c47b9e6c2544f719 --- /dev/null +++ b/tests/data/unindentedJSONTOXMLRespons.xml @@ -0,0 +1,18 @@ +<dcc:quantity refType="vib_phase"> +<!-- This is only an example --> +<!-- This is an additional Comment --> +<dcc:name> +<dcc:content lang="de">Phasenverzögerung</dcc:content> +<dcc:content lang="en">Phase delay</dcc:content> +</dcc:name> +<si:realListXMLList> +<si:valueXMLList>1.7 1.5 1.3 1.1 1.02 0.92 0.83 0.76 0.69 0.63 0.57 0.53 0.48 0.44 0.42 0.38 0.33 0.28 0.39 0.30 0.30 0.15 0.17 0.1 0.1 0.1 0.1</si:valueXMLList> +<si:unitXMLList>\degree</si:unitXMLList> +<si:expandedUncXMLList> +<si:uncertaintyXMLList>1.5 1.0 1.0 1.0 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 1.0 1.0 1.0 1.0</si:uncertaintyXMLList> +<si:coverageFactorXMLList>2.0</si:coverageFactorXMLList> +<si:coverageProbabilityXMLList>0.95</si:coverageProbabilityXMLList> +<si:distributionXMLList>normal</si:distributionXMLList> +</si:expandedUncXMLList> +</si:realListXMLList> +</dcc:quantity> diff --git a/tests/test_RestServer.py b/tests/test_RestServer.py new file mode 100644 index 0000000000000000000000000000000000000000..305669fc0e5c67099e4b21ac2eb86de774b50ddf --- /dev/null +++ b/tests/test_RestServer.py @@ -0,0 +1,49 @@ +# This file is part of dcc-xmljsonconv (https://gitlab1.ptb.de/digitaldynamicmeasurement/dcc_XMLJSONConv) +# Copyright 2024 [Benedikt Seeger(PTB), Vanessa Stehr(PTB), Thomas Bruns (PTB)] +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program 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 Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +from fastapi.testclient import TestClient +from dccXMLJSONConv.dcc_server import app +import json +client = TestClient(app) + +def test_root(): + response = client.get("/") + assert response.status_code == 200 + assert response.json() == {"message": "Hello!\nBetter use the URL /json2dcc/?js={...} \n or /dcc2json (POST method)"} + +def test_dcc2json_with_valid_xml(): + xml_data = open("./data/APITestXMLStub.xml", "r").read() + response = client.post("/dcc2json/", json={"xml": xml_data}) + responsJsonDict=json.loads(response.text) + assert response.status_code == 200 + assert responsJsonDict==json.loads(open("./data/APITestJSONStub.json", "r").read()) + # Add more specific assertions here depending on the expected JSON output of your valid XML input + +def test_dcc2json_with_empty_xml(): + response = client.post("/dcc2json/", json={"xml": ""}) + assert response.status_code == 400 # returnes 400 Bad Request + +def test_json2dcc_with_valid_json(): + json_data = json.loads(open("./data/APITestJSONStub.json", "r").read()) + response = client.post("/json2dcc/", json={"js": json_data}) + assert response.status_code == 200 + assert response.text==open("./data/unindentedJSONTOXMLRespons.xml", "r").read() + # Add more specific assertions here depending on the expected XML output of your valid JSON input + + +def test_json2dcc_with_empty_json(): + response = client.post("/json2dcc/", json={"js": {}}) + assert response.status_code == 400 # Assuming it's not successful; adjust based on your error handling diff --git a/tests/test_dccXMLJSONConv.py b/tests/test_dccXMLJSONConv.py new file mode 100644 index 0000000000000000000000000000000000000000..842023474e79e86d034b2420ace8a280eaab5068 --- /dev/null +++ b/tests/test_dccXMLJSONConv.py @@ -0,0 +1,45 @@ +# This file is part of dcc-xmljsonconv (https://gitlab1.ptb.de/digitaldynamicmeasurement/dcc_XMLJSONConv) +# Copyright 2024 [Benedikt Seeger(PTB), Vanessa Stehr(PTB), Thomas Bruns (PTB)] +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program 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 Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. +import pytest +from dccXMLJSONConv.dcc_Conv import dcc +from dccXMLJSONConv.dcc_Conv import XMLToJson,JSONToXML +import os +import json +def test_XMLToJson(): + dccInst=dcc() + data_dir = os.path.join(os.path.dirname(__file__), 'data') + xml_file_path = os.path.join(data_dir, '20220708_8305_SN1842876.xml') + # Read the XML file + with open(xml_file_path, 'r') as xml_file: + xml_data = xml_file.read() + dccInst.read_dcc_string(str(xml_data)) + dccInst.xml2dict() + xmlDict=json.loads(dccInst.get_json()) + print(xmlDict) + print("DONE") + +def test_XMLToJsonFunction(): + data_dir = os.path.join(os.path.dirname(__file__), 'data') + xml_file_path = os.path.join(data_dir, '20220708_8305_SN1842876.xml') + # Read the XML file + with open(xml_file_path, 'r') as xml_file: + xml_data = xml_file.read() + dict=XMLToJson(str(xml_data)) + print(dict) + print("DONE") + xml=JSONToXML(dict) + print(xml) + print("DONE")