-MSDS
-backup
-certgen.pyc
-chem_db.pyc
-chem_web.log
-chem_web.log.1
-chem_web.log.2
-chem_web.log.3
-chem_web.log.4
-chem_web.log.5
-chem_web.pid
-chem_web.pyc
-current
-daemon.pyc
-docs/door_data.tex
-docs/door_warning.pdf
-docs/inventory_data.tex
-docs/inventory.pdf
-docs/main.aux
-docs/main.log
-docs/main.pdf
-docs/main.tex
-docs/mp/NFPA.1
-docs/mp/NFPA.log
-docs/mp/NFPA.mp
-docs/mp/NFPA.mpx
-docs/mp/sample.aux
-docs/mp/sample.log
-docs/mp/sample.pdf
-mk_simple_certs.pyc
-origs
-restart.sh
-ssl
-templates
-text_db.pyc
+*.pyc
+chem_web.log*
+backup/
--- /dev/null
+ GNU GENERAL PUBLIC LICENSE
+ Version 3, 29 June 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+ Preamble
+
+ The GNU General Public License is a free, copyleft license for
+software and other kinds of works.
+
+ The licenses for most software and other practical works are designed
+to take away your freedom to share and change the works. By contrast,
+the GNU General Public License is intended to guarantee your freedom to
+share and change all versions of a program--to make sure it remains free
+software for all its users. We, the Free Software Foundation, use the
+GNU General Public License for most of our software; it applies also to
+any other work released this way by its authors. You can apply it to
+your programs, too.
+
+ When we speak of free software, we are referring to freedom, not
+price. Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+them if you wish), that you receive source code or can get it if you
+want it, that you can change the software or use pieces of it in new
+free programs, and that you know you can do these things.
+
+ To protect your rights, we need to prevent others from denying you
+these rights or asking you to surrender the rights. Therefore, you have
+certain responsibilities if you distribute copies of the software, or if
+you modify it: responsibilities to respect the freedom of others.
+
+ For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must pass on to the recipients the same
+freedoms that you received. You must make sure that they, too, receive
+or can get the source code. And you must show them these terms so they
+know their rights.
+
+ Developers that use the GNU GPL protect your rights with two steps:
+(1) assert copyright on the software, and (2) offer you this License
+giving you legal permission to copy, distribute and/or modify it.
+
+ For the developers' and authors' protection, the GPL clearly explains
+that there is no warranty for this free software. For both users' and
+authors' sake, the GPL requires that modified versions be marked as
+changed, so that their problems will not be attributed erroneously to
+authors of previous versions.
+
+ Some devices are designed to deny users access to install or run
+modified versions of the software inside them, although the manufacturer
+can do so. This is fundamentally incompatible with the aim of
+protecting users' freedom to change the software. The systematic
+pattern of such abuse occurs in the area of products for individuals to
+use, which is precisely where it is most unacceptable. Therefore, we
+have designed this version of the GPL to prohibit the practice for those
+products. If such problems arise substantially in other domains, we
+stand ready to extend this provision to those domains in future versions
+of the GPL, as needed to protect the freedom of users.
+
+ Finally, every program is threatened constantly by software patents.
+States should not allow patents to restrict development and use of
+software on general-purpose computers, but in those that do, we wish to
+avoid the special danger that patents applied to a free program could
+make it effectively proprietary. To prevent this, the GPL assures that
+patents cannot be used to render the program non-free.
+
+ The precise terms and conditions for copying, distribution and
+modification follow.
+
+ TERMS AND CONDITIONS
+
+ 0. Definitions.
+
+ "This License" refers to version 3 of the GNU General Public License.
+
+ "Copyright" also means copyright-like laws that apply to other kinds of
+works, such as semiconductor masks.
+
+ "The Program" refers to any copyrightable work licensed under this
+License. Each licensee is addressed as "you". "Licensees" and
+"recipients" may be individuals or organizations.
+
+ To "modify" a work means to copy from or adapt all or part of the work
+in a fashion requiring copyright permission, other than the making of an
+exact copy. The resulting work is called a "modified version" of the
+earlier work or a work "based on" the earlier work.
+
+ A "covered work" means either the unmodified Program or a work based
+on the Program.
+
+ To "propagate" a work means to do anything with it that, without
+permission, would make you directly or secondarily liable for
+infringement under applicable copyright law, except executing it on a
+computer or modifying a private copy. Propagation includes copying,
+distribution (with or without modification), making available to the
+public, and in some countries other activities as well.
+
+ To "convey" a work means any kind of propagation that enables other
+parties to make or receive copies. Mere interaction with a user through
+a computer network, with no transfer of a copy, is not conveying.
+
+ An interactive user interface displays "Appropriate Legal Notices"
+to the extent that it includes a convenient and prominently visible
+feature that (1) displays an appropriate copyright notice, and (2)
+tells the user that there is no warranty for the work (except to the
+extent that warranties are provided), that licensees may convey the
+work under this License, and how to view a copy of this License. If
+the interface presents a list of user commands or options, such as a
+menu, a prominent item in the list meets this criterion.
+
+ 1. Source Code.
+
+ The "source code" for a work means the preferred form of the work
+for making modifications to it. "Object code" means any non-source
+form of a work.
+
+ A "Standard Interface" means an interface that either is an official
+standard defined by a recognized standards body, or, in the case of
+interfaces specified for a particular programming language, one that
+is widely used among developers working in that language.
+
+ The "System Libraries" of an executable work include anything, other
+than the work as a whole, that (a) is included in the normal form of
+packaging a Major Component, but which is not part of that Major
+Component, and (b) serves only to enable use of the work with that
+Major Component, or to implement a Standard Interface for which an
+implementation is available to the public in source code form. A
+"Major Component", in this context, means a major essential component
+(kernel, window system, and so on) of the specific operating system
+(if any) on which the executable work runs, or a compiler used to
+produce the work, or an object code interpreter used to run it.
+
+ The "Corresponding Source" for a work in object code form means all
+the source code needed to generate, install, and (for an executable
+work) run the object code and to modify the work, including scripts to
+control those activities. However, it does not include the work's
+System Libraries, or general-purpose tools or generally available free
+programs which are used unmodified in performing those activities but
+which are not part of the work. For example, Corresponding Source
+includes interface definition files associated with source files for
+the work, and the source code for shared libraries and dynamically
+linked subprograms that the work is specifically designed to require,
+such as by intimate data communication or control flow between those
+subprograms and other parts of the work.
+
+ The Corresponding Source need not include anything that users
+can regenerate automatically from other parts of the Corresponding
+Source.
+
+ The Corresponding Source for a work in source code form is that
+same work.
+
+ 2. Basic Permissions.
+
+ All rights granted under this License are granted for the term of
+copyright on the Program, and are irrevocable provided the stated
+conditions are met. This License explicitly affirms your unlimited
+permission to run the unmodified Program. The output from running a
+covered work is covered by this License only if the output, given its
+content, constitutes a covered work. This License acknowledges your
+rights of fair use or other equivalent, as provided by copyright law.
+
+ You may make, run and propagate covered works that you do not
+convey, without conditions so long as your license otherwise remains
+in force. You may convey covered works to others for the sole purpose
+of having them make modifications exclusively for you, or provide you
+with facilities for running those works, provided that you comply with
+the terms of this License in conveying all material for which you do
+not control copyright. Those thus making or running the covered works
+for you must do so exclusively on your behalf, under your direction
+and control, on terms that prohibit them from making any copies of
+your copyrighted material outside their relationship with you.
+
+ Conveying under any other circumstances is permitted solely under
+the conditions stated below. Sublicensing is not allowed; section 10
+makes it unnecessary.
+
+ 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
+
+ No covered work shall be deemed part of an effective technological
+measure under any applicable law fulfilling obligations under article
+11 of the WIPO copyright treaty adopted on 20 December 1996, or
+similar laws prohibiting or restricting circumvention of such
+measures.
+
+ When you convey a covered work, you waive any legal power to forbid
+circumvention of technological measures to the extent such circumvention
+is effected by exercising rights under this License with respect to
+the covered work, and you disclaim any intention to limit operation or
+modification of the work as a means of enforcing, against the work's
+users, your or third parties' legal rights to forbid circumvention of
+technological measures.
+
+ 4. Conveying Verbatim Copies.
+
+ You may convey verbatim copies of the Program's source code as you
+receive it, in any medium, provided that you conspicuously and
+appropriately publish on each copy an appropriate copyright notice;
+keep intact all notices stating that this License and any
+non-permissive terms added in accord with section 7 apply to the code;
+keep intact all notices of the absence of any warranty; and give all
+recipients a copy of this License along with the Program.
+
+ You may charge any price or no price for each copy that you convey,
+and you may offer support or warranty protection for a fee.
+
+ 5. Conveying Modified Source Versions.
+
+ You may convey a work based on the Program, or the modifications to
+produce it from the Program, in the form of source code under the
+terms of section 4, provided that you also meet all of these conditions:
+
+ a) The work must carry prominent notices stating that you modified
+ it, and giving a relevant date.
+
+ b) The work must carry prominent notices stating that it is
+ released under this License and any conditions added under section
+ 7. This requirement modifies the requirement in section 4 to
+ "keep intact all notices".
+
+ c) You must license the entire work, as a whole, under this
+ License to anyone who comes into possession of a copy. This
+ License will therefore apply, along with any applicable section 7
+ additional terms, to the whole of the work, and all its parts,
+ regardless of how they are packaged. This License gives no
+ permission to license the work in any other way, but it does not
+ invalidate such permission if you have separately received it.
+
+ d) If the work has interactive user interfaces, each must display
+ Appropriate Legal Notices; however, if the Program has interactive
+ interfaces that do not display Appropriate Legal Notices, your
+ work need not make them do so.
+
+ A compilation of a covered work with other separate and independent
+works, which are not by their nature extensions of the covered work,
+and which are not combined with it such as to form a larger program,
+in or on a volume of a storage or distribution medium, is called an
+"aggregate" if the compilation and its resulting copyright are not
+used to limit the access or legal rights of the compilation's users
+beyond what the individual works permit. Inclusion of a covered work
+in an aggregate does not cause this License to apply to the other
+parts of the aggregate.
+
+ 6. Conveying Non-Source Forms.
+
+ You may convey a covered work in object code form under the terms
+of sections 4 and 5, provided that you also convey the
+machine-readable Corresponding Source under the terms of this License,
+in one of these ways:
+
+ a) Convey the object code in, or embodied in, a physical product
+ (including a physical distribution medium), accompanied by the
+ Corresponding Source fixed on a durable physical medium
+ customarily used for software interchange.
+
+ b) Convey the object code in, or embodied in, a physical product
+ (including a physical distribution medium), accompanied by a
+ written offer, valid for at least three years and valid for as
+ long as you offer spare parts or customer support for that product
+ model, to give anyone who possesses the object code either (1) a
+ copy of the Corresponding Source for all the software in the
+ product that is covered by this License, on a durable physical
+ medium customarily used for software interchange, for a price no
+ more than your reasonable cost of physically performing this
+ conveying of source, or (2) access to copy the
+ Corresponding Source from a network server at no charge.
+
+ c) Convey individual copies of the object code with a copy of the
+ written offer to provide the Corresponding Source. This
+ alternative is allowed only occasionally and noncommercially, and
+ only if you received the object code with such an offer, in accord
+ with subsection 6b.
+
+ d) Convey the object code by offering access from a designated
+ place (gratis or for a charge), and offer equivalent access to the
+ Corresponding Source in the same way through the same place at no
+ further charge. You need not require recipients to copy the
+ Corresponding Source along with the object code. If the place to
+ copy the object code is a network server, the Corresponding Source
+ may be on a different server (operated by you or a third party)
+ that supports equivalent copying facilities, provided you maintain
+ clear directions next to the object code saying where to find the
+ Corresponding Source. Regardless of what server hosts the
+ Corresponding Source, you remain obligated to ensure that it is
+ available for as long as needed to satisfy these requirements.
+
+ e) Convey the object code using peer-to-peer transmission, provided
+ you inform other peers where the object code and Corresponding
+ Source of the work are being offered to the general public at no
+ charge under subsection 6d.
+
+ A separable portion of the object code, whose source code is excluded
+from the Corresponding Source as a System Library, need not be
+included in conveying the object code work.
+
+ A "User Product" is either (1) a "consumer product", which means any
+tangible personal property which is normally used for personal, family,
+or household purposes, or (2) anything designed or sold for incorporation
+into a dwelling. In determining whether a product is a consumer product,
+doubtful cases shall be resolved in favor of coverage. For a particular
+product received by a particular user, "normally used" refers to a
+typical or common use of that class of product, regardless of the status
+of the particular user or of the way in which the particular user
+actually uses, or expects or is expected to use, the product. A product
+is a consumer product regardless of whether the product has substantial
+commercial, industrial or non-consumer uses, unless such uses represent
+the only significant mode of use of the product.
+
+ "Installation Information" for a User Product means any methods,
+procedures, authorization keys, or other information required to install
+and execute modified versions of a covered work in that User Product from
+a modified version of its Corresponding Source. The information must
+suffice to ensure that the continued functioning of the modified object
+code is in no case prevented or interfered with solely because
+modification has been made.
+
+ If you convey an object code work under this section in, or with, or
+specifically for use in, a User Product, and the conveying occurs as
+part of a transaction in which the right of possession and use of the
+User Product is transferred to the recipient in perpetuity or for a
+fixed term (regardless of how the transaction is characterized), the
+Corresponding Source conveyed under this section must be accompanied
+by the Installation Information. But this requirement does not apply
+if neither you nor any third party retains the ability to install
+modified object code on the User Product (for example, the work has
+been installed in ROM).
+
+ The requirement to provide Installation Information does not include a
+requirement to continue to provide support service, warranty, or updates
+for a work that has been modified or installed by the recipient, or for
+the User Product in which it has been modified or installed. Access to a
+network may be denied when the modification itself materially and
+adversely affects the operation of the network or violates the rules and
+protocols for communication across the network.
+
+ Corresponding Source conveyed, and Installation Information provided,
+in accord with this section must be in a format that is publicly
+documented (and with an implementation available to the public in
+source code form), and must require no special password or key for
+unpacking, reading or copying.
+
+ 7. Additional Terms.
+
+ "Additional permissions" are terms that supplement the terms of this
+License by making exceptions from one or more of its conditions.
+Additional permissions that are applicable to the entire Program shall
+be treated as though they were included in this License, to the extent
+that they are valid under applicable law. If additional permissions
+apply only to part of the Program, that part may be used separately
+under those permissions, but the entire Program remains governed by
+this License without regard to the additional permissions.
+
+ When you convey a copy of a covered work, you may at your option
+remove any additional permissions from that copy, or from any part of
+it. (Additional permissions may be written to require their own
+removal in certain cases when you modify the work.) You may place
+additional permissions on material, added by you to a covered work,
+for which you have or can give appropriate copyright permission.
+
+ Notwithstanding any other provision of this License, for material you
+add to a covered work, you may (if authorized by the copyright holders of
+that material) supplement the terms of this License with terms:
+
+ a) Disclaiming warranty or limiting liability differently from the
+ terms of sections 15 and 16 of this License; or
+
+ b) Requiring preservation of specified reasonable legal notices or
+ author attributions in that material or in the Appropriate Legal
+ Notices displayed by works containing it; or
+
+ c) Prohibiting misrepresentation of the origin of that material, or
+ requiring that modified versions of such material be marked in
+ reasonable ways as different from the original version; or
+
+ d) Limiting the use for publicity purposes of names of licensors or
+ authors of the material; or
+
+ e) Declining to grant rights under trademark law for use of some
+ trade names, trademarks, or service marks; or
+
+ f) Requiring indemnification of licensors and authors of that
+ material by anyone who conveys the material (or modified versions of
+ it) with contractual assumptions of liability to the recipient, for
+ any liability that these contractual assumptions directly impose on
+ those licensors and authors.
+
+ All other non-permissive additional terms are considered "further
+restrictions" within the meaning of section 10. If the Program as you
+received it, or any part of it, contains a notice stating that it is
+governed by this License along with a term that is a further
+restriction, you may remove that term. If a license document contains
+a further restriction but permits relicensing or conveying under this
+License, you may add to a covered work material governed by the terms
+of that license document, provided that the further restriction does
+not survive such relicensing or conveying.
+
+ If you add terms to a covered work in accord with this section, you
+must place, in the relevant source files, a statement of the
+additional terms that apply to those files, or a notice indicating
+where to find the applicable terms.
+
+ Additional terms, permissive or non-permissive, may be stated in the
+form of a separately written license, or stated as exceptions;
+the above requirements apply either way.
+
+ 8. Termination.
+
+ You may not propagate or modify a covered work except as expressly
+provided under this License. Any attempt otherwise to propagate or
+modify it is void, and will automatically terminate your rights under
+this License (including any patent licenses granted under the third
+paragraph of section 11).
+
+ However, if you cease all violation of this License, then your
+license from a particular copyright holder is reinstated (a)
+provisionally, unless and until the copyright holder explicitly and
+finally terminates your license, and (b) permanently, if the copyright
+holder fails to notify you of the violation by some reasonable means
+prior to 60 days after the cessation.
+
+ Moreover, your license from a particular copyright holder is
+reinstated permanently if the copyright holder notifies you of the
+violation by some reasonable means, this is the first time you have
+received notice of violation of this License (for any work) from that
+copyright holder, and you cure the violation prior to 30 days after
+your receipt of the notice.
+
+ Termination of your rights under this section does not terminate the
+licenses of parties who have received copies or rights from you under
+this License. If your rights have been terminated and not permanently
+reinstated, you do not qualify to receive new licenses for the same
+material under section 10.
+
+ 9. Acceptance Not Required for Having Copies.
+
+ You are not required to accept this License in order to receive or
+run a copy of the Program. Ancillary propagation of a covered work
+occurring solely as a consequence of using peer-to-peer transmission
+to receive a copy likewise does not require acceptance. However,
+nothing other than this License grants you permission to propagate or
+modify any covered work. These actions infringe copyright if you do
+not accept this License. Therefore, by modifying or propagating a
+covered work, you indicate your acceptance of this License to do so.
+
+ 10. Automatic Licensing of Downstream Recipients.
+
+ Each time you convey a covered work, the recipient automatically
+receives a license from the original licensors, to run, modify and
+propagate that work, subject to this License. You are not responsible
+for enforcing compliance by third parties with this License.
+
+ An "entity transaction" is a transaction transferring control of an
+organization, or substantially all assets of one, or subdividing an
+organization, or merging organizations. If propagation of a covered
+work results from an entity transaction, each party to that
+transaction who receives a copy of the work also receives whatever
+licenses to the work the party's predecessor in interest had or could
+give under the previous paragraph, plus a right to possession of the
+Corresponding Source of the work from the predecessor in interest, if
+the predecessor has it or can get it with reasonable efforts.
+
+ You may not impose any further restrictions on the exercise of the
+rights granted or affirmed under this License. For example, you may
+not impose a license fee, royalty, or other charge for exercise of
+rights granted under this License, and you may not initiate litigation
+(including a cross-claim or counterclaim in a lawsuit) alleging that
+any patent claim is infringed by making, using, selling, offering for
+sale, or importing the Program or any portion of it.
+
+ 11. Patents.
+
+ A "contributor" is a copyright holder who authorizes use under this
+License of the Program or a work on which the Program is based. The
+work thus licensed is called the contributor's "contributor version".
+
+ A contributor's "essential patent claims" are all patent claims
+owned or controlled by the contributor, whether already acquired or
+hereafter acquired, that would be infringed by some manner, permitted
+by this License, of making, using, or selling its contributor version,
+but do not include claims that would be infringed only as a
+consequence of further modification of the contributor version. For
+purposes of this definition, "control" includes the right to grant
+patent sublicenses in a manner consistent with the requirements of
+this License.
+
+ Each contributor grants you a non-exclusive, worldwide, royalty-free
+patent license under the contributor's essential patent claims, to
+make, use, sell, offer for sale, import and otherwise run, modify and
+propagate the contents of its contributor version.
+
+ In the following three paragraphs, a "patent license" is any express
+agreement or commitment, however denominated, not to enforce a patent
+(such as an express permission to practice a patent or covenant not to
+sue for patent infringement). To "grant" such a patent license to a
+party means to make such an agreement or commitment not to enforce a
+patent against the party.
+
+ If you convey a covered work, knowingly relying on a patent license,
+and the Corresponding Source of the work is not available for anyone
+to copy, free of charge and under the terms of this License, through a
+publicly available network server or other readily accessible means,
+then you must either (1) cause the Corresponding Source to be so
+available, or (2) arrange to deprive yourself of the benefit of the
+patent license for this particular work, or (3) arrange, in a manner
+consistent with the requirements of this License, to extend the patent
+license to downstream recipients. "Knowingly relying" means you have
+actual knowledge that, but for the patent license, your conveying the
+covered work in a country, or your recipient's use of the covered work
+in a country, would infringe one or more identifiable patents in that
+country that you have reason to believe are valid.
+
+ If, pursuant to or in connection with a single transaction or
+arrangement, you convey, or propagate by procuring conveyance of, a
+covered work, and grant a patent license to some of the parties
+receiving the covered work authorizing them to use, propagate, modify
+or convey a specific copy of the covered work, then the patent license
+you grant is automatically extended to all recipients of the covered
+work and works based on it.
+
+ A patent license is "discriminatory" if it does not include within
+the scope of its coverage, prohibits the exercise of, or is
+conditioned on the non-exercise of one or more of the rights that are
+specifically granted under this License. You may not convey a covered
+work if you are a party to an arrangement with a third party that is
+in the business of distributing software, under which you make payment
+to the third party based on the extent of your activity of conveying
+the work, and under which the third party grants, to any of the
+parties who would receive the covered work from you, a discriminatory
+patent license (a) in connection with copies of the covered work
+conveyed by you (or copies made from those copies), or (b) primarily
+for and in connection with specific products or compilations that
+contain the covered work, unless you entered into that arrangement,
+or that patent license was granted, prior to 28 March 2007.
+
+ Nothing in this License shall be construed as excluding or limiting
+any implied license or other defenses to infringement that may
+otherwise be available to you under applicable patent law.
+
+ 12. No Surrender of Others' Freedom.
+
+ If 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 convey a
+covered work so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you may
+not convey it at all. For example, if you agree to terms that obligate you
+to collect a royalty for further conveying from those to whom you convey
+the Program, the only way you could satisfy both those terms and this
+License would be to refrain entirely from conveying the Program.
+
+ 13. Use with the GNU Affero General Public License.
+
+ Notwithstanding any other provision of this License, you have
+permission to link or combine any covered work with a work licensed
+under version 3 of the GNU Affero General Public License into a single
+combined work, and to convey the resulting work. The terms of this
+License will continue to apply to the part which is the covered work,
+but the special requirements of the GNU Affero General Public License,
+section 13, concerning interaction through a network will apply to the
+combination as such.
+
+ 14. Revised Versions of this License.
+
+ The Free Software Foundation may publish revised and/or new versions of
+the GNU 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
+Program specifies that a certain numbered version of the GNU General
+Public License "or any later version" applies to it, you have the
+option of following the terms and conditions either of that numbered
+version or of any later version published by the Free Software
+Foundation. If the Program does not specify a version number of the
+GNU General Public License, you may choose any version ever published
+by the Free Software Foundation.
+
+ If the Program specifies that a proxy can decide which future
+versions of the GNU General Public License can be used, that proxy's
+public statement of acceptance of a version permanently authorizes you
+to choose that version for the Program.
+
+ Later license versions may give you additional or different
+permissions. However, no additional obligations are imposed on any
+author or copyright holder as a result of your choosing to follow a
+later version.
+
+ 15. Disclaimer of Warranty.
+
+ THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
+APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
+HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
+OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
+THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
+IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
+ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+ 16. Limitation of Liability.
+
+ IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
+THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
+GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
+USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
+DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
+PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
+EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGES.
+
+ 17. Interpretation of Sections 15 and 16.
+
+ If the disclaimer of warranty and limitation of liability provided
+above cannot be given local legal effect according to their terms,
+reviewing courts shall apply local law that most closely approximates
+an absolute waiver of all civil liability in connection with the
+Program, unless a warranty or assumption of liability accompanies a
+copy of the Program in return for a fee.
+
+ END OF TERMS AND CONDITIONS
+
+ How to Apply These Terms to Your New Programs
+
+ If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+ To do so, attach the following notices to the program. It is safest
+to attach them to the start of each source file to most effectively
+state 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 program's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+Also add information on how to contact you by electronic and paper mail.
+
+ If the program does terminal interaction, make it output a short
+notice like this when it starts in an interactive mode:
+
+ <program> Copyright (C) <year> <name of author>
+ This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+ This is free software, and you are welcome to redistribute it
+ under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License. Of course, your program's commands
+might be different; for a GUI interface, you would use an "about box".
+
+ You should also get your employer (if you work as a programmer) or school,
+if any, to sign a "copyright disclaimer" for the program, if necessary.
+For more information on this, and how to apply and follow the GNU GPL, see
+<http://www.gnu.org/licenses/>.
+
+ The GNU General Public License does not permit incorporating your program
+into proprietary programs. If your program is a subroutine library, you
+may consider it more useful to permit linking proprietary applications with
+the library. If this is what you want to do, use the GNU Lesser General
+Public License instead of this License. But first, please read
+<http://www.gnu.org/philosophy/why-not-lgpl.html>.
-------------------------------Python dependencies------------------------------
+Python dependencies
+===================
-STANDARD MODULES
+Standard modules
+----------------
-Modules included with a standard Python 2.5 distribution:
+Modules included with a standard Python 2.6 distribution:
Documentation available at
- http://docs.python.org/lib/module-<module-name>.html
-
-copy
-doctest
-logging
-optparse
-os
-os.path
-re
-shutil
-stat
-string
-sys
-time
-types
-
-
-EXTERNAL MODULES
-
-OpenSSL http://pyopenssl.sourceforge.net/
- Python wrapper on OpenSSL library.
-web http://webpy.org/
- A simple web framework. I think the Debain package was too
- old a version. I installed mine from source, version 0.23.
- Patched httpserver.py to support HTTPS.
- Patch submitted upstream:
- https://bugs.launchpad.net/webpy/+bug/262495
-markdown Implements a version of markdown syntax. webpy bundles a
- version of this in their `tools' subdirectory.
-
-------------------------------System dependencies------------------------------
-
-You need latex and metapost to compile the documents. The
-texlive-base, texlive-metapost, and context Debian packages should
-cover you. You can avoid the context dependency with some legwork,
-see
- http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=465107
+ http://docs.python.org/library/<module-name>.html
+External modules
+----------------
+* `OpenSSL`_, a Python wrapper on OpenSSL library.
+* `CherryPy`_, a simple web framework.
+* `Jinja2`_, a template engine.
+
+.. _OpenSSL: http://pyopenssl.sourceforge.net/
+.. _CherryPy: http://www.cherrypy.org/
+.. _Jinja2: http://jinja.pocoo.org/2/
+
+System dependencies
+===================
+
+You need `LaTeX`_ and `MetaPost`_ to compile the documents. The
+`texlive-base`, `texlive-metapost`, and `context` `Debian`_ packages
+should cover you. You can avoid the context dependency with some
+`legwork`_.
+
+.. _LaTeX: http://www.latex-project.org/
+.. _MetaPost: http://tug.org/metapost.html
+.. _Debian: http://www.debian.org/
+.. _legwork: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=465107
Web, python, and command-line interfaces for managing a chemical inventory.
-COMMANDS
+Commands
+========
The web interface (chem_web.py) is a web-daemon using web.py and it's build in htmlserver.
-Standard command for starting the daemon
- $ chem_web.py -a 192.168.1.2:55555
-Standard command for stopping the daemon
- $ chem_web.py --stop
+Standard command for starting the daemon::
-From the command line, you can validate CAS#s (and possibly other fields) with
- $ python chem_db.py -f current/inventory.db -V
+ $ chem_web.py -a 192.168.1.2:55555
+Standard command for stopping the daemon::
-DATABASE FORMAT
+ $ chem_web.py --stop
+
+From the command line, you can validate CAS#s (and possibly other fields) with::
+
+ $ python chem_db.py -f current/inventory.db -V
+
+
+Database format
+===============
text_db.py provides the python interface to this format. The basic
idea was to produce and use files which were M$-Excel-compatible,
since future users might not want to maintain this interface. A brief
example inventory.db file is included in the examples directory.
-Tab-delimited ('\t') fields
-Endline-delimited ('\n') records
-The first line is the header (starts with a pound sign '#').
-The header should be a tab-delimited list of short field names.
-If the second line also begins with a pound sign,
-it contains a tab-delimiteded list of long field names.
-Blank lines are ignored.
+* Tab-delimited ('\t') fields
+* Endline-delimited ('\n') records
+* The first line is the header (starts with a pound sign '#').
+* The header should be a tab-delimited list of short field names.
+* If the second line also begins with a pound sign,
+ it contains a tab-delimiteded list of long field names.
+* Blank lines are ignored.
The fields H, F, R, and S are the NFPA Health, Fire, Reactivity, and Special Hazards (NFPA diamond).
-Blue: Health Hazard
+* Blue: Health Hazard
0 Hazard no greater than ordinary material
1 May cause irritation; minimal residual injury
2 Intense or prolonged exposure may cause incapacitation;
Residual injury may occur if not treated
3 Exposure could cause serious injury even if treated
4 Exposure may cause death
-Red: Fire Hazard
+* Red: Fire Hazard
0 Will not burn
1 Must be preheated for ignition; flashpoint above 200°F (93°C)
2 Must be moderately heated for ignition, flashpoint above 100°F (38°C)
Flashpoint below 100°F (38°C)
4 Extremely flammable and will readily disperse through air under standard
conditions, flashpoint below 73°F (23°C)
-Reactivity hazards:
+* Reactivity hazards:
0 Stable
1 May become unstable at elevated temperatures and pressures.
May be mildly water reactive
May form explosive mixtures with water
3 Detonates with strong ignition source
4 Readily detonates
-Special Hazards have the following codes:
- OX strong oxidizer
- -W- water reactive
- SA simple ascphyxiants
- (The only gases for which this symbol is permitted are nitrogen, helium,
- neon, argon, krypton, and xenon..)
-Non-official Special Hazard codes:
- ACID acid
- ALK base
- COR corrosive
- BIO Biohazard
- POI Poison
- CRY Cyrogenic
-
-
-CONTENTS
-
-README this file
-text_db.py python interface to the above db format
-chem_db.py chem-inventory specific functionality such as
- * CAS # validation,
+* Special Hazards have the following codes:
+ * OX strong oxidizer
+ * -W- water reactive
+ * SA simple ascphyxiants
+ (The only gases for which this symbol is permitted are nitrogen, helium,
+ neon, argon, krypton, and xenon..)
+* Unofficial Special Hazard codes:
+ * ACID acid
+ * ALK base
+ * COR corrosive
+ * BIO Biohazard
+ * POI Poison
+ * CRY Cyrogenic
+
+
+Contents
+========
+
+* README this file
+* DEPENDENCIES list of dependencies (libraries, python modules, etc.)
+* COPYING GNU General Public License, version 3
+* text_db.py python interface to the above db format
+* chem_db.py chem-inventory specific functionality such as
+ * CAS # validation,
* a Material Saftey Data Sheet (MSDS) manager,
- * document-generation drivers, and
- * a simple command line interface.
-current store the current database file.
-backup store previous (timestamped) database files.
- You may want to clean this out occasionally.
-MSDS store Material Saftey Data Sheets by database index number
-docs latex (and metapost) sources for document generation.
-chem_web.py daemonized web bindings to chem_web.py
-ssl Secure Socket Layer (SSL) key and certificate generation tests.
+ * document-generation drivers, and
+ * a simple command line interface.
+* current store the current database file.
+* backup store previous (timestamped) database files.
+ You may want to clean this out occasionally.
+* MSDS store Material Saftey Data Sheets by database index number
+* docs latex (and metapost) sources for document generation.
+* chem_web.py daemonized web bindings to chem_web.py
+* ssl Secure Socket Layer (SSL) key and certificate generation tests.
-GENERATED FILES AND DIRECTORIES
+Generated files and directories
+-------------------------------
+============ =======================================================================
templates quasi-HTML templates for pages generated chem_web.py
chem_web.pid store the Process ID (PID) for a daemonized chem_web.py process
chem_web.log log chem_web activity (maintained between runs, so remove periodically)
-
+============ =======================================================================
TODO
+====
-Security:
+Security
+--------
I can create my own self-signed certificate.
Protects against eavesdropping, but not man-in-the-middle attacks.
Solution for now: hide from external world, trust everyone inside, backup before any change.
-Print columns in a particular order from the database with
- $ awk -F ' ' '{print $1, "\t", $9, "\t", , "\t", $2}' current/inventory.db | less
+History
+=======
+
+The original web fron-end was based on Adam Bachman's simplewiki.py_.
+
+.. simplewiki.py: http://bachman.infogami.com/another_simple_wiki
-HISTORY
+License
+=======
-The web front-end on this database is an adaptation of Adam Bachman's
-simplewiki.py
- http://bachman.infogami.com/another_simple_wiki
+ChemDB is released under the GNU General Public License version 3.
+See :file:`COPYING` for details.
--- /dev/null
+#!/usr/bin/python
+
+# Copyright
+
+from sys import stdout, stdin, stderr
+
+import chemdb.chemdb
+
+from chemdb.db.text import TextDB, DBPrettyPrinter
+
+
+def open_IOfiles(ifilename=None, ofilename=None, debug=False):
+ if ifilename:
+ if debug: print >> stderr, "open input file '%s'" % ifilename
+ ifile = file(ifilename, 'r')
+ else:
+ ifile = stdin
+ if ofilename:
+ if debug: print >> stderr, "open output file '%s'" % ofilename
+ ofile = file(ofilename, 'w')
+ else:
+ ofile = stdout
+ return (ifile, ofile)
+
+def close_IOfiles(ifilename=None, ifile=stdin,
+ ofilename=None, ofile=stdout,
+ debug=False):
+ if ifilename:
+ if debug: print >> stderr, "close input file '%s'" % ifilename
+ ifile.close()
+ if ofilename:
+ if debug: print >> stderr, "close output file '%s'" % ofilename
+ ofile.close()
+
+if __name__ == '__main__':
+ from optparse import OptionParser
+
+ parser = OptionParser(usage='usage: %prog [options]', version='%prog 0.1')
+
+ parser.add_option('-f', '--input-file', dest='ifilename',
+ help='Read input from FILE (default stdin)',
+ type='string', metavar='FILE')
+ parser.add_option('-o', '--output-file', dest='ofilename',
+ help='Write output to FILE (default stdout)',
+ type='string', metavar='FILE')
+ parser.add_option('-d', '--delimiter', dest='FS', # field seperator
+ help="Set field delimiter (default '%default')",
+ type='string', metavar='DELIM', default='\t')
+ parser.add_option('-p', '--print-fields', dest='print_fields',
+ help='Only print certain fields (e.g. 0,3,4,2)',
+ type='string', metavar='FIELDS')
+ parser.add_option('-r', '--print-records', dest='print_records',
+ help='Only print certain records (e.g. 0:3)',
+ type='string', metavar='RECORDS')
+ parser.add_option('-w', '--column-width', dest='width',
+ help='Set column width for short-format output.',
+ type='string', metavar='WIDTH')
+ parser.add_option('-L', '--long-format', dest='long_format',
+ help='Print long format (several lines per record)',
+ action='store_true', default=False)
+ parser.add_option('-l', '--short-format', dest='long_format',
+ help='Print short format (default) (one lines per record)',
+ action='store_false', default=False)
+ parser.add_option('--valid-record', dest='valid_record',
+ help="Select fields where True == lambda r : eval(EXPRESSION). default '%default'",
+ type='string', metavar='EXPRESSION', default="r['Disposed'] == ''")
+ parser.add_option('--sort-field', dest='sort_field',
+ help="Sort matching records by FIELD (defauly '%default')",
+ type='string', metavar='FIELD', default='db_id')
+ parser.add_option('--pdf-title', dest='pdf_title',
+ help='Override the default PDF title',
+ type='string', metavar='TITLE')
+ parser.add_option('--inventory', dest='inventory',
+ help='Output a PDF inventory of matching records',
+ action='store_true', default=False)
+ parser.add_option('--door-warning', dest='door_warning',
+ help='Output a PDF door warning of matching records',
+ action='store_true', default=False)
+ parser.add_option('-t', '--test', dest='test',
+ help='Run docutils tests on db.py',
+ action='store_true', default=False)
+ parser.add_option('--list-locations', dest='locations',
+ help='List all currently used locations (no other output)',
+ action='store_true', default=False)
+ parser.add_option('-V', '--validate', dest='validate',
+ help='Validate CAS#s (no other output)',
+ action='store_true', default=False)
+ parser.add_option('-A', '--audit', dest='audit',
+ help='Search for troublesome entries (no other output)',
+ action='store_true', default=False)
+ parser.add_option('-v', '--verbose', dest='verbose',
+ help='Print lots of debugging information',
+ action='store_true', default=False)
+
+ (options, args) = parser.parse_args()
+ parser.destroy()
+
+ ifile,ofile = open_IOfiles(options.ifilename, options.ofilename,
+ options.verbose)
+
+ if options.test:
+ _test()
+ elif options.locations:
+ db = TextDB(filename=None)
+ pp = DBPrettyPrinter(db)
+
+ # read in and parse the file
+ db._parse(ifile.read())
+
+ locations = []
+ for record in db.records():
+ if len(record['Location']) > 0 and record['Location'] not in locations:
+ locations.append(record['Location'])
+ locations.sort()
+ print >> ofile, '\n'.join(locations)
+ elif options.validate:
+ db = TextDB(filename=None)
+ pp = DBPrettyPrinter(db)
+
+ # read in and parse the file
+ db._parse(ifile.read())
+
+ CAS_DELIM = ',' # seperate CAS entries for chemicals with multiple CAS numbers
+ PERCENT_DELIM = ':' # seperate CAS number from ingredient percentage
+ for record in db.records():
+ valid = True
+ cas = record['CAS#']
+ if len(cas.split(CAS_DELIM)) == 0 : # cas = 'N...N-NN-N'
+ if not valid_CASno(cas, options.verbose):
+ valid = False
+ print >> ofile, "Invalid CAS# in record: '%s'" % cas
+ else : # cas = 'N...N-NN-N:X%,N...N-NN-N:Y%,...'
+ for casterm in cas.split(CAS_DELIM) : # casterm = 'N...N-NN-N:X%'
+ c = casterm.split(PERCENT_DELIM)[0] # c = 'N...N-NN-N'
+ if not valid_CASno(c, options.verbose):
+ valid = False
+ print >> ofile, "Invalid CAS* in record: '%s'" % c
+ if not valid:
+ print >> ofile, (
+ "in record %s: %s" % (record['ID'], record['Name']))
+ #pp.full_record_string(record)
+ elif options.audit:
+ db = TextDB(filename=None)
+ pp = DBPrettyPrinter(db)
+
+ # read in and parse the file
+ db._parse(ifile.read())
+
+ for record in db.records():
+ # check for extra spaces
+ for key,value in record.items():
+ if (isinstance(value, types.StringTypes)
+ and value.strip() != value):
+ print >> ofile, (
+ "Extra whitespace for %s - %s field %s : '%s'"
+ % (record['ID'], record['Name'], key, value))
+ # make sure we know the location of all current chemicals
+ if len(record['Disposed']) == 0 and len(record['Location']) == 0:
+ print >> ofile, (
+ "Misplaced record: %s - %s"
+ % (record['ID'], record['Name']))
+ elif options.inventory:
+ db = TextDB(filename=None)
+ pp = DBPrettyPrinter(db)
+
+ # read in and parse the file
+ db._parse(ifile.read())
+
+ dgen = docgen(db)
+ def valid_record(r):
+ return eval(options.valid_record, # expression
+ {'__builtins__':None}, # globals
+ {'r':r}) # locals
+ path = dgen.inventory(title=options.pdf_title,
+ namewidth=40,
+ sort_field=options.sort_field,
+ valid_record=valid_record)
+ print >> ofile, '\n', path
+ elif options.door_warning:
+ db = TextDB(filename=None)
+ pp = DBPrettyPrinter(db)
+
+ # read in and parse the file
+ db._parse(ifile.read())
+
+ dgen = docgen(db)
+ def valid_record(r):
+ return eval(options.valid_record, # expression
+ {'__builtins__':None}, # globals
+ {'r':r}) # locals
+ path = dgen.door_warning(valid_record=valid_record)
+ print >> ofile, '\n', path
+ else:
+ db = TextDB(filename=None)
+
+ # read in and parse the file
+ db._parse(ifile.read())
+ pp = DBPrettyPrinter(db)
+ if options.long_format:
+ for id in pp._norm_record_ids(options.print_records):
+ string = pp.full_record_string_id(id)
+ else:
+ # pythonize the width option
+ if options.width == None or options.width == 'a':
+ width = options.width
+ elif len(options.width.split(':')) == 1:
+ width = int(options.width)
+ elif len(options.width.split(':')) > 1:
+ width = {}
+ for kv in options.width.split(','):
+ spl = kv.split(':')
+ assert len(spl) == 2, 'invalid width "%s" in "%s"' % (kv, options.width)
+ if spl[1] == 'a':
+ width[spl[0]] = spl[1]
+ else:
+ width[spl[0]] = int(spl[1])
+
+ string = pp.multi_record_string(options.print_records,
+ options.print_fields,
+ width,
+ options.FS)
+ print >> ofile, string,
+
+ close_IOfiles(options.ifilename, ifile,
+ options.ofilename, ofile, options.verbose)
--- /dev/null
+#!/usr/bin/python
+
+# Copyright
+
+from chemdb.server import ServerDaemon
+
+
+if __name__=="__main__":
+ ServerDaemon().main()
+++ /dev/null
-#!/usr/bin/python
-
-"""
-Extend text_db with a CAS# validator, a command line interface, and document generation.
-"""
-
-from text_db import *
-import re
-import os
-import os.path
-import time
-import types
-
-def valid_CASno(cas_string, debug=False):
- """
- Check N..NN-NN-N format, and the checksum digit for valid CAS number structure.
- see http://www.cas.org/expertise/cascontent/registry/checkdig.html
- for N_n .. N_4 N_3 - N_2 N_1 - R
- R = remainder([sum_{i=1}^n i N_i ] / 10 )
- Ignores 'na' and '+secret-non-hazardous'
- >>> valid_CASno('107-07-3')
- True
- >>> valid_CASno('107-08-3')
- False
- >>> valid_CASno('107-083')
- False
- """
- for string in ['na', '+secret-non-hazardous']:
- # the first marks a non-existent CAS#
- # the last marks items with secret, non-hazardous ingredients for which we have no CAS#
- if cas_string == string:
- return True
- # check format,
- # ^ matches the start of the string
- # \Z matches the end of the string
- regexp = re.compile('^[0-9]{2,}[-][0-9]{2}[-][0-9]\Z')
- if regexp.match(cas_string) == None:
- if debug : print >> stderr, "invalid CAS# format: '%s'" % cas_string
- return False
- # generate check digit
- casdigs = "".join(cas_string.split('-')) # remove '-'
- sumdigs = list(casdigs[:-1])
- sumdigs.reverse()
- sum=0
- for i in range(len(sumdigs)) :
- sum += (i+1)*int(sumdigs[i])
- check = sum % 10
- if int(casdigs[-1]) == check :
- return True
- else :
- if debug : print >> stderr, "invalid CAS# check: '%s' (expected %d)" % (cas_string, check)
- return False
-
-class MSDS_manager (object) :
- """
- Manage Material Saftey Data Sheets (MSDSs)
- """
- def __init__(self, db, dir="./MSDS/") :
- self.db = db
- self.dir = dir
- self.MIMEs = ['application/pdf',
- 'text/html',
- 'text/plain']
- self.MIME_exts = ['pdf', 'html', 'txt']
- self.check_dir()
- def check_dir(self) :
- "Create the MSDS directory if it's missing"
- if os.path.isdir(self.dir) :
- return # all set to go
- elif os.path.exists(self.dir) :
- raise Exception, "Error: a non-directory file exists at %s" % self.dir
- else :
- os.mkdir(self.dir)
- def basepath(self, id) :
- assert type(id) == types.IntType, 'id must be an integer, not %s (%s)' \
- % (type(id), str(id))
- return os.path.join(self.dir, "%d" % id)
- def local_basepath(self, id) : # for symbolic links
- assert type(id) == types.IntType, 'id must be an integer, not %s (%s)' \
- % (type(id), str(id))
- return "./%d" % id
- def MIME_ext(self, mime) :
- assert mime in self.MIMEs, \
- "invalid MIME type '%s'\nshould be one of %s" % (mime, self.MIMEs)
- i = self.MIMEs.index(mime)
- ext = self.MIME_exts[i]
- return ext
- def path(self, id, mime) :
- return "%s.%s" % (self.basepath(id), self.MIME_ext(mime))
- def local_path(self, id, mime) :
- return "%s.%s" % (self.local_basepath(id), self.MIME_ext(mime))
- def save(self, id, filetext, mime='application/pdf') :
- "Save the binary byte string FILE to the path for ID"
- print >> file(self.path(id, mime), 'wb'), filetext,
- def link(self, id, target_id) :
- # target_id already exists, create a symlink to it for id.
- target_mime = self.get_MSDS_MIME(target_id)
- target_path = self.local_path(target_id, target_mime)
- path = self.path(id, target_mime)
- #os.link(self.path(target_id), self.path(id)) # hard link...
- os.symlink(target_path, path) # ... or soft link
- def has_MSDS_MIME(self, id, mime) :
- """
- >>> m = MSDS_manager(db=None)
- >>> print m.has_MSDS_type(102, 'pdf') # test on html
- False
- >>> print m.has_MSDS_type(102, 'html') # test on html
- True
- >>> print m.has_MSDS_type(6, 'pdf') # test on pdf symlink
- True
- """
- return os.path.exists(self.path(id, mime))
- def get_MSDS_path(self, id) :
- """
- >>> m = MSDS_manager(db=None)
- >>> print m.get_MSDS_path(102) # test on html
- ./MSDS/102.html
- >>> print m.get_MSDS_path(1) # test on pdf
- ./MSDS/1.pdf
- >>> print m.get_MSDS_path(6) # test on pdf symlink
- ./MSDS/6.pdf
- """
- for mime in self.MIMEs :
- if self.has_MSDS_MIME(id, mime) :
- return self.path(id, mime)
- return None
- def get_MSDS_MIME(self, id) :
- """
- >>> m = MSDS_manager(db=None)
- >>> print m.get_MSDS_MIME(102) # test on html
- text/html
- >>> print m.get_MSDS_MIME(1) # test on pdf
- application/pdf
- >>> print m.get_MSDS_MIME(6) # test on pdf symlink
- application/pdf
- """
- for mime in self.MIMEs :
- if self.has_MSDS_MIME(id, mime) :
- return mime
- return None
- def has_MSDS(self, id) :
- if self.get_MSDS_path(id) == None :
- return False
- return True
- def get_all(self, simlinks=True) :
- ret = []
- for record in self.db.records() :
- p = self.get_MSDS_path( int(record['ID']) )
- if p != None :
- if simlinks == False and os.path.islink( p ) :
- continue # ignore the symbolic link
- ret.append({'ID':record['ID'], 'Name':record['Name']})
- return ret
-
-class docgen (object) :
- "Generate the officially required documents"
- def __init__(self, db) :
- self.db = db
- def _latex_safe(self, string):
- string = string.replace('%', '\%')
- string = string.replace('>', '$>$')
- string = string.replace('<', '$<$')
- return string
- def _set_main_target(self, target):
- print >> file('./docs/main.tex', 'w'), \
- """\documentclass[letterpaper]{article}
-
-\input{%s}
-""" % target
- def _make_pdf(self, target_file):
- os.system('cd ./docs && make pdf')
- path = os.path.join('./docs/', target_file)
- os.system('cp ./docs/main.pdf %s' % path)
- return path
- def inventory(self, title=None,
- namewidth='a', sort_field='db_id',
- valid_record=lambda r: r['Disposed'] == '') :
- """Create a pdf list of all maching chemicals. The default is to
- match all currently owned chemicals. Matching chemicals can be sorted
- by any field (defaults to 'ID')."""
- if title == None:
- title == 'Inventory'
- pp = db_pretty_printer(self.db)
- active_ids = []
- for record in self.db.records() :
- if valid_record(record) : # get ids for matching chemicals
- active_ids.append(record['db_id'])
- active_ids.sort(cmp=lambda a,b: cmp(self.db.record(a)[sort_field],
- self.db.record(b)[sort_field]))
- active_fields = ['ID', 'Name', 'Amount',
- 'H', 'F', 'R', 'O', 'M', 'C', 'T']
- width = {}
- for field in active_fields :
- width[field] = 'a'
- width['Name'] = namewidth
- ## Plain text method
- #string = "Chemical inventory:\t\tGenerated on %s\n\n" \
- # % time.strftime('%Y-%m-%d')
- #string += pp.multi_record_string(active_ids, active_fields,
- # width=width, FS=' ')
- # return string
- ## Latex method
- string = "\\begin{longtable}{l l l c c c c c c c}\n"
- string += ('%% The header for the remaining page(s) of the table...\n'
- 'ID & Name & Amount & H & F & R & O & M & C & T \\\\\n'
- '\\hline\n'
- '\\endhead\n')
- for db_id in active_ids :
- record = self.db.record(db_id)
- string += " %s & %s & %s & %s & %s & %s & %s & %s & %s & %s \\\\\n" \
- % (self._latex_safe(record['ID']),
- self._latex_safe(record['Name']),
- self._latex_safe(record['Amount']),
- self._latex_safe(record['H']),
- self._latex_safe(record['F']),
- self._latex_safe(record['R']),
- self._latex_safe(record['O']),
- self._latex_safe(record['M']),
- self._latex_safe(record['C']),
- self._latex_safe(record['T']))
- string += "\\end{longtable}\n"
- print >> file('./docs/inventory_title.tex', 'w'), title
- print >> file('./docs/inventory_data.tex', 'w'), string
- ## alter main.tex to point to the inventory template.
- self._set_main_target('inventory_template')
- ## run latex
- path = self._make_pdf('inventory.pdf')
- return path
- def door_warning(self,
- valid_record=lambda r: r['Disposed'] == '') :
- """create a warning NFPA diamond and list of the most dangerous
- chemicals for which valid_record(record) is true. For
- example, to generate a door warning for the front door use
- door_warning(lambda r: r['Disposed'] == '')
- or to generate the warning for the fridge
- door_warning(lambda r: r['Location'] == 'Refrigerator')
- Note that valid_record defaults to the first example.
- """
- pp = db_pretty_printer(self.db)
- all_ids = range(self.db.len_records())
-
- # Search the database to find the nasties
- NFPA_maxs = {'H':0, 'F':0, 'R':0, 'O':[]}
- Mutagens = []
- Carcinogens = []
- Teratogens = []
- Healths = []
- Fires = []
- Reactivities = []
- Others = []
- for record in self.db.records() :
- if valid_record(record) :
- for field in ['H', 'F', 'R', 'O'] :
- r = record[field]
- if r != '' and r != '?' :
- if field != 'O' and int(r) > NFPA_maxs[field] :
- NFPA_maxs[field] = int(r)
- elif field == 'O' and not r in NFPA_maxs['O'] :
- NFPA_maxs[field].append(r)
- for field,array in zip(['M','C','T'],
- [Mutagens,
- Carcinogens,
- Teratogens]) :
- if record[field] != '' and record[field] != '?':
- array.append(record['db_id'])
- # now that we've found the max NFPAs,
- # find all the chemicals at those levels
- for record in self.db.records() :
- if valid_record(record) :
- for field,array in zip(['H', 'F', 'R', 'O'],
- [Healths, Fires,
- Reactivities, Others]) :
- r = record[field]
- if r != '' and r != '?' :
- if field != 'O' and int(r) == NFPA_maxs[field] :
- array.append(record['db_id'])
- elif field == 'O' and r in NFPA_maxs['O'] :
- array.append(record['db_id'])
-
- ## generate the output
- # first, update the NFPA grapic code
- if 'OX' in NFPA_maxs['O'] : OX = 'y'
- else : OX = 'n'
- if 'W' in NFPA_maxs['O'] : W = 'y'
- else : W = 'n'
- os.system('./docs/mp/gen_NFPA.sh %d %d %d %s %s > ./docs/mp/NFPA.mp'
- % (NFPA_maxs['H'], NFPA_maxs['F'], NFPA_maxs['R'], OX, W))
- # now generate a list of the nasties ( Amount & ID & Name )
- string = "\\begin{tabular}{r r l}\n"
- for field,name,array in zip(['H', 'F', 'R', 'O'],
- ['Health', 'Fire',
- 'Reactivity', 'Other'],
- [Healths, Fires,
- Reactivities, Others]) :
- if (not hasattr(NFPA_maxs[field], '__len__')) \
- or len(NFPA_maxs[field]) > 0 :
- string += " \multicolumn{3}{c}{\Tstrut %s : %s} \\\\\n" \
- % (name, NFPA_maxs[field])
- else : # Print "Other" instead of "Other : []"
- string += " \multicolumn{3}{c}{\Tstrut %s} \\\\\n" \
- % (name)
- for db_id in array :
- record = self.db.record(db_id)
- string += " %s & %s & %s \\\\\n" \
- % (self._latex_safe(record['Amount']),
- self._latex_safe(record['ID']),
- self._latex_safe(record['Name']))
- if len(array) == 0 :
- string += " \multicolumn{3}{c}{ --- } \\\\\n"
- for hazard,array in zip(['Mutagens','Carcinogens','Teratogens'],
- [Mutagens, Carcinogens, Teratogens]) :
- string += " \multicolumn{3}{c}{\Tstrut %s} \\\\\n" % (hazard)
- for db_id in array :
- record = self.db.record(db_id)
- string += " %s & %s & %s \\\\\n" \
- % (self._latex_safe(record['Amount']),
- self._latex_safe(record['ID']),
- self._latex_safe(record['Name']))
- if len(array) == 0 :
- string += " \multicolumn{3}{c}{ --- } \\\\\n"
- string += "\\end{tabular}\n"
- print >> file('./docs/door_data.tex', 'w'), string
- ## alter main.tex to point to the door template.
- self._set_main_target('door_template')
- ## run latex
- path = self._make_pdf('door_warning.pdf')
- return path
-
-def _test():
- import doctest
- doctest.testmod()
-
-def open_IOfiles(ifilename=None, ofilename=None, debug=False):
- if ifilename :
- if debug : print >> stderr, "open input file '%s'" % ifilename
- ifile = file(ifilename, 'r')
- else :
- ifile = stdin
- if ofilename :
- if debug : print >> stderr, "open output file '%s'" % ofilename
- ofile = file(ofilename, 'w')
- else :
- ofile = stdout
- return (ifile, ofile)
-
-def close_IOfiles(ifilename=None, ifile=stdin,
- ofilename=None, ofile=stdout,
- debug=False):
- if ifilename :
- if debug : print >> stderr, "close input file '%s'" % ifilename
- ifile.close()
- if ofilename :
- if debug : print >> stderr, "close output file '%s'" % ofilename
- ofile.close()
-
-
-if __name__ == '__main__' :
- from optparse import OptionParser
-
- parser = OptionParser(usage='usage: %prog [options]', version='%prog 0.1')
-
- parser.add_option('-f', '--input-file', dest='ifilename',
- help='Read input from FILE (default stdin)',
- type='string', metavar='FILE')
- parser.add_option('-o', '--output-file', dest='ofilename',
- help='Write output to FILE (default stdout)',
- type='string', metavar='FILE')
- parser.add_option('-d', '--delimiter', dest='FS', # field seperator
- help="Set field delimiter (default '%default')",
- type='string', metavar='DELIM', default='\t')
- parser.add_option('-p', '--print-fields', dest='print_fields',
- help='Only print certain fields (e.g. 0,3,4,2)',
- type='string', metavar='FIELDS')
- parser.add_option('-r', '--print-records', dest='print_records',
- help='Only print certain records (e.g. 0:3)',
- type='string', metavar='RECORDS')
- parser.add_option('-w', '--column-width', dest='width',
- help='Set column width for short-format output.',
- type='string', metavar='WIDTH')
- parser.add_option('-L', '--long-format', dest='long_format',
- help='Print long format (several lines per record)',
- action='store_true', default=False)
- parser.add_option('-l', '--short-format', dest='long_format',
- help='Print short format (default) (one lines per record)',
- action='store_false', default=False)
- parser.add_option('--valid-record', dest='valid_record',
- help="Select fields where True == lambda r : eval(EXPRESSION). default '%default'",
- type='string', metavar='EXPRESSION', default="r['Disposed'] == ''")
- parser.add_option('--sort-field', dest='sort_field',
- help="Sort matching records by FIELD (defauly '%default')",
- type='string', metavar='FIELD', default='db_id')
- parser.add_option('--pdf-title', dest='pdf_title',
- help='Override the default PDF title',
- type='string', metavar='TITLE')
- parser.add_option('--inventory', dest='inventory',
- help='Output a PDF inventory of matching records',
- action='store_true', default=False)
- parser.add_option('--door-warning', dest='door_warning',
- help='Output a PDF door warning of matching records',
- action='store_true', default=False)
- parser.add_option('-t', '--test', dest='test',
- help='Run docutils tests on db.py',
- action='store_true', default=False)
- parser.add_option('--list-locations', dest='locations',
- help='List all currently used locations (no other output)',
- action='store_true', default=False)
- parser.add_option('-V', '--validate', dest='validate',
- help='Validate CAS#s (no other output)',
- action='store_true', default=False)
- parser.add_option('-A', '--audit', dest='audit',
- help='Search for troublesome entries (no other output)',
- action='store_true', default=False)
- parser.add_option('-v', '--verbose', dest='verbose',
- help='Print lots of debugging information',
- action='store_true', default=False)
-
- (options, args) = parser.parse_args()
- parser.destroy()
-
- ifile,ofile = open_IOfiles(options.ifilename, options.ofilename,
- options.verbose)
-
- if options.test :
- _test()
- elif options.locations :
- db = text_db(filename=None)
- pp = db_pretty_printer(db)
-
- # read in and parse the file
- db._parse(ifile.read())
-
- locations = []
- for record in db.records():
- if len(record['Location']) > 0 and record['Location'] not in locations:
- locations.append(record['Location'])
- locations.sort()
- print >> ofile, '\n'.join(locations)
- elif options.validate :
- db = text_db(filename=None)
- pp = db_pretty_printer(db)
-
- # read in and parse the file
- db._parse(ifile.read())
-
- CAS_DELIM = ',' # seperate CAS entries for chemicals with multiple CAS numbers
- PERCENT_DELIM = ':' # seperate CAS number from ingredient percentage
- for record in db.records() :
- valid = True
- cas = record['CAS#']
- if len(cas.split(CAS_DELIM)) == 0 : # cas = 'N...N-NN-N'
- if not valid_CASno(cas, options.verbose) :
- valid = False
- print >> ofile, "Invalid CAS# in record: '%s'" % cas
- else : # cas = 'N...N-NN-N:X%,N...N-NN-N:Y%,...'
- for casterm in cas.split(CAS_DELIM) : # casterm = 'N...N-NN-N:X%'
- c = casterm.split(PERCENT_DELIM)[0] # c = 'N...N-NN-N'
- if not valid_CASno(c, options.verbose) :
- valid = False
- print >> ofile, "Invalid CAS* in record: '%s'" % c
- if not valid :
- print >> ofile, "in record %s: %s" % (record['ID'], record['Name'])
- #pp.full_record_string(record)
- elif options.audit :
- db = text_db(filename=None)
- pp = db_pretty_printer(db)
-
- # read in and parse the file
- db._parse(ifile.read())
-
- for record in db.records():
- # check for extra spaces
- for key,value in record.items():
- if type(value) in types.StringTypes and value.strip() != value:
- print >> ofile, "Extra whitespace for %s - %s field %s : '%s'" % (record['ID'], record['Name'], key, value)
- # make sure we know the location of all current chemicals
- if len(record['Disposed']) == 0 and len(record['Location']) == 0:
- print >> ofile, "Misplaced record: %s - %s" % (record['ID'], record['Name'])
- elif options.inventory:
- db = text_db(filename=None)
- pp = db_pretty_printer(db)
-
- # read in and parse the file
- db._parse(ifile.read())
-
- dgen = docgen(db)
- def valid_record(r) :
- return eval(options.valid_record, # expression
- {'__builtins__':None}, # globals
- {'r':r}) # locals
- path = dgen.inventory(title=options.pdf_title,
- namewidth=40,
- sort_field=options.sort_field,
- valid_record=valid_record)
- print >> ofile, '\n', path
- elif options.door_warning:
- db = text_db(filename=None)
- pp = db_pretty_printer(db)
-
- # read in and parse the file
- db._parse(ifile.read())
-
- dgen = docgen(db)
- def valid_record(r) :
- return eval(options.valid_record, # expression
- {'__builtins__':None}, # globals
- {'r':r}) # locals
- path = dgen.door_warning(valid_record=valid_record)
- print >> ofile, '\n', path
- else :
- db = text_db(filename=None)
-
- # read in and parse the file
- db._parse(ifile.read())
- pp = db_pretty_printer(db)
- if options.long_format :
- for id in pp._norm_record_ids(options.print_records) :
- string = pp.full_record_string_id(id)
- else :
- # pythonize the width option
- if options.width == None or options.width == 'a':
- width = options.width
- elif len(options.width.split(':')) == 1 :
- width = int(options.width)
- elif len(options.width.split(':')) > 1 :
- width = {}
- for kv in options.width.split(',') :
- spl = kv.split(':')
- assert len(spl) == 2, 'invalid width "%s" in "%s"' % (kv, options.width)
- if spl[1] == 'a' :
- width[spl[0]] = spl[1]
- else :
- width[spl[0]] = int(spl[1])
-
- string = pp.multi_record_string(options.print_records,
- options.print_fields,
- width,
- options.FS)
- print >> ofile, string,
-
- close_IOfiles(options.ifilename, ifile,
- options.ofilename, ofile, options.verbose)
+++ /dev/null
-#!/usr/bin/python
-#
-# Web application for posting additions/modifications to the chem-inventory
-# derived from simplewiki and wiki_py, by Adam Bachman
-# ( http://bachman.infogami.com/ )
-# Authentication following Snakelets
-# http://snakelets.sourceforge.net/manual/authorization.html
-
-# Remember to delete the templates directory after making alterations,
-# because this program only generates templates if they are missing.
-
-
-## Bring in some useful goodies:
-# web, because we're running a web server, and generating html;
-# time, for timestamping backups;
-# os, for creating the template directories; and
-# re, for replacing internal node links with proper markup
-import web, os, re
-# md5, for hashing passwords
-#import md5
-
-# database must support the following methods :
-# field_list() : return an ordered list of available fields (fields unique)
-# long_field() : return an dict of long field names (keyed by field names)
-# record(id) : return a record dict (keyed by field names)
-# records() : return an ordered list of available records.
-# backup() : save a copy of the current database somehow.
-# set_record(i, newvals) : set a record by overwriting any preexisting data
-# : with data from the field-name-keyed dict NEWVALS
-import text_db
-database = text_db.text_db
-from chem_db import valid_CASno, MSDS_manager, docgen
-DB_FILE = 'inventory.db'
-import sys, daemon, logging
-PID_FILE = './chem_web.pid'
-LOG_FILE = './chem_web.log'
-
-## Also import a markup language, and associated regexp functions
-from markdown import markdown
-## for converting the first character in record names, and stripping |
-from string import upper, rstrip
-
-## for SSL
-SSL = False
-if SSL :
- from mk_simple_certs import get_cert_filenames
- server_name = 'chem_web'
-
-__version__ = "0.3"
-
-## For web.py, when a URL matches '/view', handle with class 'view'
-# when it matches '/edit', handle with class 'edit',
-# and when it matches '/login', handle with class 'login',
-# Note, there are no .html extension.
-#
-# link to edit pages using db_id
-# link to MSDSs with ID
-urls = ('/(view)?', 'view',
- '/edit/([0-9]+)', 'edit',
- '/MSDS/([0-9]+)', 'MSDS',
- '/docs/([a-z._]*)', 'docs',
- '/login', 'login')
-
-# limit the display width of selected fields
-WIDTH_MAX = {'CAS#':12}
-
-# set the display width of entry fields
-ENTRY_WIDTH = 50
-
-class view (object) :
- "Print the database"
- # when the server recieves the command "GET '/view', call view.get('view')
- def GET(self, name):
- """
- Render the view.html template using name and the previous text.
- If the file doesn't exist, use default text '%s doesn't exist'.
- """
- if name == None :
- name = 'view'
- db._refresh()
- recs = []
- for record in db.records() :
- id = int(record['ID'])
- record['ID'] = '<a href="/edit/%d">%s</a>' \
- % (id, record['ID'])
- if MSDSman.has_MSDS(id) : # link the id to the MSDS
- record['Name'] = '<a href="/MSDS/%d">%s</a>' \
- % (record['db_id'], record['Name'])
- recs.append(record)
- print render.view('Chemical inventory',
- db.len_records(), db.field_list(), recs)
-
-class edit (object) :
- "Provide methods for handling requests related to edit pages"
- def GET(self, name):
- """
- Render the edit.html template using the specified record number.
- """
- db._refresh()
- db_id = int(name)
- if db_id >= db.len_records() :
- logging.info("new record")
- assert db_id == db.len_records(), \
- 'invalid id: %d (max %d)' % (db_id, db.len_records())
- record = db.new_record()
- record['ID'] = str(record['db_id'])
- db.set_record(db_id, record)
- if MSDSman.has_MSDS(db_id) :
- MSDSs = None
- else :
- # only ask for an MSDS if we still need one
- MSDSs = MSDSman.get_all(simlinks=False)
- print render.edit(int(name), db.field_list(),
- db.long_fields(), db.record(int(name)), MSDSs)
- def POST(self, name):
- """
- Read the form input and update the database accordingly.
- Then redirect to /view.
- """
- db._refresh()
- record = db.record(int(name))
- ## Generate a new record from the form input
- # 'MSDS={}' to storify MSDS as a FieldStorage (dict-like) instance,
- # otherwise it's saved as a string, and we loose info about it.
- # e.g. newvals['MSDS'].type = 'text/html'
- # The contents of the file are saved to newvals['MSDS'].value
- newvals = web.input(MSDS={})
- update = False
- for field in db.field_list() :
- if newvals[field] != record[field] :
- # TODO: add validation!
- update=True
- record[field] = newvals[field]
- if 'MSDS source' in newvals :
- # Handle any MSDS file actions
- if newvals['MSDS source'] == 'upload' :
- if len(newvals['MSDS'].filename) > 0 and len(newvals['MSDS'].value) > 0:
- # only save if there is a file there to save ;)
- #print >> stderr, web.ctx.env['CONTENT_TYPE']
- #print >> stderr, web.ctx.keys()
- #print >> stderr, dir(newvals['MSDS'])
- #print >> stderr, type(newvals['MSDS'])
- #print >> stderr, newvals['MSDS'].filename
- #print >> stderr, newvals['MSDS'].type
- MSDSman.save(int(name), newvals['MSDS'].value,
- newvals['MSDS'].type)
- else :
- logging.info('linking MSDS %d to %d' \
- % (int(record['ID']),
- int(newvals['MSDS share'])) )
- MSDSman.link(int(record['ID']),
- int(newvals['MSDS share']))
- if update :
- db.set_record(int(name), record, backup=True)
- # redirect to view all
- web.seeother('/view')
-
-class MSDS (object) :
- "Serve MSDS files by ID"
- def GET(self, name):
- "Serve MSDS files by ID"
- id = int(name)
- if MSDSman.has_MSDS(id) :
- mime = MSDSman.get_MSDS_MIME(id)
- web.header("Content-Type", mime)
- print file(MSDSman.get_MSDS_path(id), 'rb').read() ,
- else :
- print render.error(id)
-
-class docs (object) :
- "Generate and serve assorted official documents"
- def GET(self, name):
- """
- List the available documents.
- """
- db._refresh()
- if name == '' :
- print render.docs()
- if name == 'inventory.pdf' :
- path = dgen.inventory(namewidth=40)
- print file(path, 'rb').read() ,
- if name == 'door_warning.pdf' :
- path = dgen.door_warning()
- print file(path, 'rb').read() ,
- def POST(self, name):
- """
- Read the form input and print the appropriate file.
- """
- db._refresh()
- formdata = web.input()
- user_regexp = formdata['regexp']
- regexp = re.compile(user_regexp, re.I) # Case insensitive
- path = dgen.door_warning(lambda r: regexp.match(r['Location']))
- print file(path, 'rb').read() ,
-
-class login (object) :
- "Print an alphabetized index of all existing pages"
- def GET(self):
- print "Not yet implemented"
- #print render.stat('login')
-
-class markup (object) :
- "Convert text to html, using Markdown with a bit of preformatting"
- def __init__(self) :
- # [[optional display text|name with or_without_spaces]]
- linkregexp = ('\[\['
- +'([^\|\[\]]*\|)?'
- +'([a-zA-Z0-9-_ ]*)'
- +'\]\]')
- #print linkregexp
- self.linkexp = re.compile(linkregexp)
- self.linebreak = ' \n'
- self.uscore = re.compile('_')
- self.space = re.compile(' ')
- def htmlize(self, text) :
- return text
- pre_markup = self._preformat(text)
- #print "Preformatted '"+pre_markup+"'"
- return self._markup( pre_markup )
- def _markup(self, text) :
- # markdown() implements the text->html Markdown semantics
- # the str() builtin ensures a nice, printable string
- return str(markdown(text))
- def _preformat(self, text) :
- #print "Preformatting '"+text+"'"
- return self._autolink(text)
- def _autolink(self, text) :
- "Replace linkexps in text with Markdown syntax versions"
- # sub passes a match object to link2markup for each match
- # see http://docs.python.org/lib/match-objects.html
- return self.linkexp.sub(self.matchlink2markup, text)
- def matchlink2markup(self, match) :
- # The name is the first (and only) matched region
- #print match.groups()
- name = match.groups()[1]
- text = match.groups()[0]
- return self.link2markup(text, name)
- def linklist(self, text) :
- linklist = [] # empty list
- for m in self.linkexp.finditer(text) :
- linklist.append( self.str2name(m.groups()[1]) )
- #print linklist
- return linklist
- def link2markup(self, text, name) :
- """
- usage: string = link2markup(text, target)
- takes the string name of a wiki page,
- and returns a string that will be markuped into a link to that page
- displaying the text text.
- """
- # convert to Markdown pretty link syntax
- if text : # remove trailing |
- text = text.rstrip('|')
- else :
- text = self.name2str(name)
- name = self.str2name(name)
- return str('['+text+'](/'+name+')')
- def name2str(self, name) :
- """
- usage: string = name2str(name)
- takes the string name of a wiki page,
- and converts it to display format
- See str2name()
- """
- w_spaces = self.uscore.sub(' ', name)
- return w_spaces
- def str2name(self, string) :
- """
- usage: string = name2str(name)
- Converts strings to the relevent wiki page name.
- See str2name()
- """
- wo_spaces = self.space.sub('_', string)
- cap_first = upper(wo_spaces[0]) + wo_spaces[1:]
- return cap_first
- def make_add_button(self, next_db_id) :
- string = '<form action="/edit/%d" method="get">\n' % next_db_id
- string += ' <input type="submit" value="Add entry">\n'
- string += '</form><br>\n'
- return string
- def make_table(self, fields, records, width_max=WIDTH_MAX,
- raw_fields=()):
- """
- >>> print make_table(['A','B','C'],[{'A':'a','B':'b'},{'A':'d','B':'e','C':'f'}]),
- <table>
- <tr><td><b>A</b></td><td><b>B</b></td><td><b>C</b></td></tr>
- <tr><td>a</td><td>b</td><td></td></tr>
- <tr><td>d</td><td>e</td><td>f</td></tr>
- </table>
- """
- # open the table
- string = '<table>\n'
- # add the header
- string += '<tr>'
- for field in fields :
- string += '<td><b>%s<b></td>' % self.htmlize(field)
- string += '</tr>\n'
- # add the records
- for record in records :
- string += '<tr>'
- for field in fields :
- rstring = record[field]
- # truncate if the width is regulated...
- if field in width_max :
- w = width_max[field]
- # ... and the full string is too long
- if len(rstring) > w :
- rstring = "%s..." % rstring[:w]
- # if the field isn't raw, protect special chars
- if not field in raw_fields :
- rstring = self.htmlize(rstring)
- string += '<td>%s</td>' % rstring
- string += '</tr>\n'
- # close the table
- string += '</table>\n'
- return string
- def record_form_entries(self, index, fields, lfields, record, MSDSs=None, entry_width=ENTRY_WIDTH):
- """
- >>> print record_form_entries(4,['A','B','MSDS'],{'A':'AA','B':'BB','MSDS'},{'A':'a','B':'b'}),
- <table>
- <tr><td><label for="A">AA</label></td><td>:</td>
- <td><input type="text" size="50" id="A" name="A" value="a"></td></tr>
- <tr><td><label for="B">BB</label></td><td>:</td>
- <td><input type="text" size="50" id="B" name="B" value="b"></td></tr>
- <tr><td><label for="MSDS">MSDS</label></td><td>:</td>
- <td><input type="file" size="50" id="MSDS" name="MSDS" value=""></td></tr>
- </table>
- """
- # open the table
- string = '<table>\n'
- # add the record fields
- for field in fields :
- # get the record string
- rstring = self.htmlize(record[field])
- fstring = self.htmlize(field)
- lfstring = self.htmlize(lfields[field])
- string += ' <tr><td><label for="%s">%s</label></td><td>:</td>\n' % (fstring, lfstring)
- string += ' <td><input type="text" size="%d" id="%s" name="%s" value="%s"></td></tr>\n' \
- % (entry_width, fstring, fstring, rstring)
- if MSDSs != None :
- ## add an MSDS radio, share menu, and file upload fields
- # radio
- lfstring = fstring = 'MSDS source'
- string += ' <tr><td><label for="%s">%s</label></td><td>:</td>\n' \
- % (fstring, lfstring)
- string += ' <td><input type="radio" id="%s" name="%s" value="upload" checked>Upload\n' \
- % (fstring, fstring)
- string += ' <input type="radio" id="%s" name="%s" value="share">Share</td></tr>\n' \
- % (fstring, fstring)
-
- # file upload
- fstring = 'MSDS'
- lfstring = 'Upload MSDS'
- string += ' <tr><td><label for="%s">%s</label></td><td>:</td>\n' \
- % (fstring, lfstring)
- string += ' <td><input type="file" size="%d" id="%s" name="%s" accept="text/plain,text/html,application/pdf" value=""></td></tr>\n' \
- % (entry_width, fstring, fstring)
-
- # file share menu
- fstring = 'MSDS share'
- lfstring = 'Use an already uploaded MSDS'
- string += ' <tr><td><label for="%s">%s</label></td><td>:</td>\n' \
- % (fstring, lfstring)
- string += ' <td><select size="1" id="%s" name="%s">\n' \
- % (fstring, fstring)
- for MSDS in MSDSs :
- lfstring = self.htmlize(MSDS['Name'])
- id = int(MSDS['ID'])
- string += ' <option value="%d">%d: %s</option>\n' \
- % (id, id, lfstring)
- string += ' </select></td></tr>\n'
- # close the table
- string += '</table><br>\n'
- return string
-
-class templt (object) :
- "Handle some template wrapping"
- def __init__(self) :
- self.dir = 'templates/'
- if not os.path.exists(self.dir): os.mkdir(self.dir)
- ## Write simple template html pages if they don't already exist.
- # see ( http://webpy.org/templetor ) for the syntax.
- html=('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"\n'
- ' "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">\n'
- '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">\n')
- if not os.path.exists(self.dir+'view.html') :
- view=('$def with (name, next_db_id, fields, records)\n'
- +html+
- ' <head>\n'
- ' <title>\n'
- ' $:name2str(name) \n'
- ' </title>\n'
- ' </head>\n'
- ' <body>\n'
- ' <h1>\n'
- ' $:name2str(name)\n'
- ' </h1>\n'
- ' See \n'
- ' <a href="http://www.drexelsafetyandhealth.com/lab-chem.htm">the rules</a>\n'
- ' for more information.\n'
- ' See the <a href="/docs/">docs</a> page to generate required documents.<br>\n'
- ' <br>\n'
- ' $:make_add_button(next_db_id)\n'
- ' $:make_table(fields, records)\n'
- ' $:make_add_button(next_db_id)\n'
- ' </body>\n'
- '</html>\n')
- file(self.dir+'view.html', 'w').write(view)
- if not os.path.exists(self.dir+'edit.html') :
- # note:
- # the form encoding type 'enctype="multipart/form-data">\n'
- # is only required because of the MSDS file-upload field.
- edit=('$def with (index, fields, lfields, record, MSDSs)\n'
- +html+
- ' <head>\n'
- ' <title>\n'
- ' $:htmlize(record["Name"]) \n'
- ' </title>\n'
- ' </head>\n'
- ' <body>\n'
- ' <a style="cursor:pointer;" onclick="history.back()"><h1>\n'
- ' Editing: $:htmlize(record["Name"]) \n'
- ' </h1></a>\n'
- ' <form action="" method="post" enctype="multipart/form-data">\n'
- ' $:record_form(index, fields, lfields, record, MSDSs)<br />\n'
- ' <input type="submit" value="Submit" />\n'
- ' </form>\n'
- ' </body>\n'
- '</html>\n')
- file(self.dir+'edit.html', 'w').write(edit)
- if not os.path.exists(self.dir+'error.html') :
- # like view, but no edit ability
- # since the content is generated,
- # or backlink list, since there would be so many.
- stat=('$def with (id)\n'
- +html+
- ' <head>\n'
- ' </head>\n'
- ' <body>\n'
- ' <h1>Error</h1>\n'
- ' There is currently no MSDS file for \n'
- ' $:htmlize(id)\n'
- ' </body>\n'
- '</html>\n')
- file(self.dir+'error.html', 'w').write(stat)
- if not os.path.exists(self.dir+'docs.html') :
- docs=(html +
- ' <head>\n'
- ' </head>\n'
- ' <body>\n'
- ' <p>\n'
- ' <a href="/docs/inventory.pdf">Inventory</a>\n'
- ' in accordance with the \n'
- ' <a href="http://www.drexelsafetyandhealth.com/lab-chem.htm#chpe7">\n'
- ' Chemical Hygiene Plan Section E-7</a>.<br>\n'
- ' <a href="/docs/door_warning.pdf">Door warning</a>\n'
- ' in accordance with the Chemical Hygiene Plan Sections\n'
- ' <a href="http://www.drexelsafetyandhealth.com/lab-chem.htm#chpe7">E-7</a>\n'
- ' and \n'
- ' <a href="http://www.drexelsafetyandhealth.com/lab-chem.htm#chpe10">E-10</a>\n'
- ' .<br>\n'
- ' </p>\n'
- ' <p>\n'
- ' For door warnings for subsections of the whole room,\n'
- ' please give a location regexp in the form below.\n'
- ' For example: ".*liquids" or "refrigerator".\n'
- ' </p>\n'
- ' <form action="" method="post" enctype="multipart/form-data">\n'
- ' <table>\n'
- ' <tr><td>Location regexp</td><td>:</td>\n'
- ' <td><input type="text" size="50" id="regexp" name="regexp"></td></tr>\n'
- ' </table>\n'
- ' <input type="submit" value="Submit" />\n'
- ' </form>\n'
- ' </body>\n'
- '</html>\n')
- file(self.dir+'docs.html', 'w').write(docs)
-
-
-class chem_web_daemon (daemon.Daemon) :
- gid = None
- uid = None
- pidfile = PID_FILE
- logfile = LOG_FILE
- loglevel = 'INFO'
- def run(self) :
- if self.pkey_file == None or self.cert_file == None :
- logging.info("http://%s:%d/" % web.validip(self.options.ip))
- else :
- logging.info("https://%s:%d/" % web.validip(self.options.ip))
-
- ## web.py should give detailed error messages, not just `internal error'
- web.internalerror = web.debugerror
-
- ## How we'd start webpy server if we didn't need command line args or SSL
- # Pass web my URL regexps, and the globals from this script
- # You can also pass it web.profiler to help optimize for speed
- #web.run(urls, globals())
- ## How we have to start it now
- webpy_func = web.webpyfunc(urls, globals(), False)
- wsgi_func = web.wsgifunc(webpy_func)
- web.httpserver.runsimple(wsgi_func,
- web.validip(self.options.ip),
- ssl_private_key_filename=self.pkey_file,
- ssl_certificate_filename=self.cert_file)
-
- def read_basic_config(self) :
- pass
- def parse_options(self):
- from optparse import OptionParser
-
- usage_string = ('%prog [options]\n'
- '\n'
- '2008, W. Trevor King.\n')
- version_string = '%%prog %s' % __version__
- parser = OptionParser(usage=usage_string, version=version_string)
-
- # Daemon options
- parser.add_option('--start', dest='action',
- action='store_const', const='start', default='start',
- help='Start the daemon (the default action)')
- parser.add_option('--stop', dest='action',
- action='store_const', const='stop', default='start',
- help='Stop the daemon')
- parser.add_option('-n', '--nodaemon', dest='daemonize',
- action='store_false', default=True,
- help='Run in the foreground')
-
- # Server options
- parser.add_option('-a', '--ip-address', dest="ip",
- help="IP address (default '%default')",
- type='string', metavar="IP", default='0.0.0.0:8080')
- parser.add_option('-s', '--secure', dest="secure",
- help="Run in secure (HTTPS) mode.",
- type='string', metavar="PKEY_FILE:CERT_FILE")
- parser.add_option('-v', '--verbose', dest="verbose", action="store_true",
- help="Print lots of debugging information",
- default=False)
-
- self.options, self.args = parser.parse_args()
- parser.destroy()
-
- if self.options.verbose :
- self.loglevel = 'DEBUG'
- # get options for httpserver
- if self.options.secure != None :
- split = self.options.secure.split(':')
- assert len(split) == 2, "Invalid secure argument '%s'"
- self.pkey_file = split[0]
- self.cert_file = split[1]
- else :
- self.pkey_file = None
- self.cert_file = None
-
-### the following instances and definitions must have
-## global scope. because they are called from inside web
-# create the database
-db = database(filename=DB_FILE)
-MSDSman = MSDS_manager(db)
-dgen = docgen(db)
-
-## set up the templates
-tmplt = templt()
-# Tell templator where to look for templates
-# to provide a framework for the generated webpages
-render = web.template.render(tmplt.dir)
-
-## Define markup functions
-mkup = markup()
-htmlize = lambda t : mkup.htmlize(t)
-name2str = lambda n : mkup.name2str(n)
-mktable = lambda h,r : mkup.make_table(h,r,WIDTH_MAX,raw_fields=('ID','Name'))
-mkadd = lambda ni : mkup.make_add_button(ni)
-record_form = lambda i,f,l,r,M : mkup.record_form_entries(i,f,l,r,M)
-## Give templates access to the htmlize, make_table, and name2str functions
-# ( see http://webpy.org/templetor and our view.html )
-web.template.Template.globals['htmlize'] = htmlize
-web.template.Template.globals['make_table'] = mktable
-web.template.Template.globals['make_add_button'] = mkadd
-web.template.Template.globals['name2str'] = name2str
-web.template.Template.globals['record_form'] = record_form
-
-
-## If this script is run from the command line,
-# use the tools we've just defined to host the chemical inventory
-if __name__=="__main__":
- chem_web_daemon().main()
--- /dev/null
+# Copyright
--- /dev/null
+# Copyright
+
+"""Utilities for chemical inventories.
+
+Includes a CAS number validator and document generation.
+"""
+
+import re
+import os
+import os.path
+import time
+import types
+from sys import stdin, stdout, stderr
+
+from .db.text import DBPrettyPrinter
+
+
+def valid_CASno(cas_string, debug=False):
+ """Validate CAS numbers.
+
+ Check `N..NN-NN-N` format, and the `checksum digit`_ for valid CAS
+ number structure. for
+
+ .. math::
+ N_n .. N_4 N_3 - N_2 N_1 - R
+
+ The checksum digit is
+
+ .. math::
+ R = remainder([sum_{i=1}^n i N_i ] / 10 )
+
+ .. _checksum digit:
+ http://www.cas.org/expertise/cascontent/registry/checkdig.html
+
+ >>> valid_CASno('107-07-3')
+ True
+ >>> valid_CASno('107-08-3')
+ False
+ >>> valid_CASno('107-083')
+ False
+
+ Sometimes we don't have a CAS number, or a product will contain
+ secret, non-hazardous ingredients. Therefore we treat the strings
+ `na` and `+secret-non-hazardous` as valid CAS numbers.
+
+ >>> valid_CASno('na')
+ True
+ >>> valid_CASno('+secret-non-hazardous')
+ True
+ """
+ if cas_string in ['na', '+secret-non-hazardous']:
+ return True
+ # check format,
+ # \A matches the start of the string
+ # \Z matches the end of the string
+ regexp = re.compile('\A[0-9]{2,}[-][0-9]{2}[-][0-9]\Z')
+ if regexp.match(cas_string) == None:
+ if debug: print >> stderr, "invalid CAS# format: '%s'" % cas_string
+ return False
+ # generate check digit
+ casdigs = [int(d) for d in ''.join(cas_string.split('-'))]
+ sumdigs = casdigs[:-1]
+ sumdigs.reverse()
+ s = sum([(i+1)*d for i,d in enumerate(sumdigs)])
+ check = s % 10
+ if check == casdigs[-1]:
+ return True
+ elif debug:
+ print >> stderr, (
+ "invalid CAS# check: '%s' (expected %d)" % (cas_string, check))
+ return False
+
+class MSDSManager (object):
+ """Manage Material Saftey Data Sheets (MSDSs).
+ """
+ def __init__(self, db, dir="./MSDS/"):
+ self.db = db
+ self.dir = dir
+ self.MIMEs = {
+ 'application/pdf': ['pdf'],
+ 'text/html': ['html'],
+ 'text/plain': ['txt'],
+ }
+ self.check_dir()
+
+ def check_dir(self):
+ "Create the MSDS directory if it's missing."
+ if os.path.isdir(self.dir):
+ return # all set to go
+ elif os.path.exists(self.dir):
+ raise Exception, (
+ 'Error: a non-directory file exists at %s' % self.dir)
+ else:
+ os.mkdir(self.dir)
+
+ def basepath(self, id):
+ assert isinstance(id, int), (
+ 'id must be an integer, not %s (%s)' % (type(id), str(id)))
+ return os.path.join(self.dir, "%d" % id)
+
+ def local_basepath(self, id) : # for symbolic links
+ assert isinstance(id, int), (
+ 'id must be an integer, not %s (%s)' % (type(id), str(id)))
+ return "./%d" % id
+
+ def MIME_ext(self, mime):
+ if mime in self.MIMEs.keys():
+ return self.MIMEs[mime][0]
+ for values in self.MIMEs.values():
+ if mime in values:
+ return mime
+ raise ValueError(
+ "invalid MIME type '%s'\nshould be one of %s" % (mime, self.MIMEs))
+
+ def path(self, id, mime):
+ return "%s.%s" % (self.basepath(id), self.MIME_ext(mime))
+
+ def local_path(self, id, mime):
+ return "%s.%s" % (self.local_basepath(id), self.MIME_ext(mime))
+
+ def save(self, id, filetext, mime='application/pdf'):
+ "Save the binary byte string FILE to the path for ID"
+ print >> file(self.path(id, mime), 'wb'), filetext,
+
+ def link(self, id, target_id):
+ # target_id already exists, create a symlink to it for id.
+ target_mime = self.get_MSDS_MIME(target_id)
+ target_path = self.local_path(target_id, target_mime)
+ path = self.path(id, target_mime)
+ #os.link(self.path(target_id), self.path(id)) # hard link...
+ os.symlink(target_path, path) # ... or soft link
+
+ def has_MSDS_MIME(self, id, mime):
+ """
+ >>> m = MSDSManager(db=None)
+ >>> print m.has_MSDS_MIME(102, 'pdf') # test on html
+ False
+ >>> print m.has_MSDS_MIME(102, 'html') # test on html
+ True
+ >>> print m.has_MSDS_MIME(6, 'pdf') # test on pdf symlink
+ True
+ """
+ return os.path.exists(self.path(id, mime))
+
+ def get_MSDS_path(self, id):
+ """
+ >>> m = MSDSManager(db=None)
+ >>> print m.get_MSDS_path(102) # test on html
+ ./MSDS/102.html
+ >>> print m.get_MSDS_path(1) # test on pdf
+ ./MSDS/1.pdf
+ >>> print m.get_MSDS_path(6) # test on pdf symlink
+ ./MSDS/6.pdf
+ """
+ for mime in self.MIMEs:
+ if self.has_MSDS_MIME(id, mime):
+ return self.path(id, mime)
+ return None
+
+ def get_MSDS_MIME(self, id):
+ """
+ >>> m = MSDSManager(db=None)
+ >>> print m.get_MSDS_MIME(102) # test on html
+ text/html
+ >>> print m.get_MSDS_MIME(1) # test on pdf
+ application/pdf
+ >>> print m.get_MSDS_MIME(6) # test on pdf symlink
+ application/pdf
+ """
+ for mime in self.MIMEs:
+ if self.has_MSDS_MIME(id, mime):
+ return mime
+ return None
+
+ def has_MSDS(self, id):
+ if self.get_MSDS_path(id) == None:
+ return False
+ return True
+
+ def get_all(self, simlinks=True):
+ ret = []
+ for record in self.db.records():
+ p = self.get_MSDS_path( int(record['ID']) )
+ if p != None:
+ if simlinks == False and os.path.islink( p ):
+ continue # ignore the symbolic link
+ ret.append({'ID':record['ID'], 'Name':record['Name']})
+ return ret
+
+
+class DocGen (object):
+ "Generate the officially required documents"
+ def __init__(self, db, doc_root=os.path.join('template', 'doc')):
+ self.db = db
+ self.doc_root = doc_root
+
+ def _latex_safe(self, string):
+ string = string.replace('%', '\%')
+ string = string.replace('>', '$>$')
+ string = string.replace('<', '$<$')
+ return string
+
+ def _set_main_target(self, target):
+ print >> file(os.path.join(self.doc_root, 'main.tex'), 'w'), (
+ """\documentclass[letterpaper]{article}
+
+\input{%s}
+""" % target)
+
+ def _make_pdf(self, target_file):
+ os.system('cd %s && make pdf' % self.doc_root)
+ path = os.path.join(self.doc_root, target_file)
+ os.system('cp %s %s' % (os.path.join(self.doc_root, 'main.pdf'), path))
+ return path
+
+ def inventory(self, title=None,
+ namewidth='a', sort_field='db_id',
+ valid_record=lambda r: r['Disposed'] == ''):
+ """Create a pdf list of all maching chemicals. The default is to
+ match all currently owned chemicals. Matching chemicals can be sorted
+ by any field (defaults to 'ID')."""
+ if title == None:
+ title == 'Inventory'
+ pp = DBPrettyPrinter(self.db)
+ active_ids = []
+ for record in self.db.records():
+ if valid_record(record) : # get ids for matching chemicals
+ active_ids.append(record['db_id'])
+ active_ids.sort(cmp=lambda a,b: cmp(self.db.record(a)[sort_field],
+ self.db.record(b)[sort_field]))
+ active_fields = ['ID', 'Name', 'Amount',
+ 'H', 'F', 'R', 'O', 'M', 'C', 'T']
+ width = {}
+ for field in active_fields:
+ width[field] = 'a'
+ width['Name'] = namewidth
+ ## Plain text method
+ #string = "Chemical inventory:\t\tGenerated on %s\n\n" \
+ # % time.strftime('%Y-%m-%d')
+ #string += pp.multi_record_string(active_ids, active_fields,
+ # width=width, FS=' ')
+ # return string
+ ## Latex method
+ string = "\\begin{longtable}{l l l c c c c c c c}\n"
+ string += ('%% The header for the remaining page(s) of the table...\n'
+ 'ID & Name & Amount & H & F & R & O & M & C & T \\\\\n'
+ '\\hline\n'
+ '\\endhead\n')
+ for db_id in active_ids:
+ record = self.db.record(db_id)
+ string += " %s & %s & %s & %s & %s & %s & %s & %s & %s & %s \\\\\n" \
+ % (self._latex_safe(record['ID']),
+ self._latex_safe(record['Name']),
+ self._latex_safe(record['Amount']),
+ self._latex_safe(record['H']),
+ self._latex_safe(record['F']),
+ self._latex_safe(record['R']),
+ self._latex_safe(record['O']),
+ self._latex_safe(record['M']),
+ self._latex_safe(record['C']),
+ self._latex_safe(record['T']))
+ string += "\\end{longtable}\n"
+ print >> file(os.path.join(self.doc_root, 'inventory_title.tex'), 'w'), title
+ print >> file(os.path.join(self.doc_root, 'inventory_data.tex'), 'w'), string
+ ## alter main.tex to point to the inventory template.
+ self._set_main_target('inventory_template')
+ ## run latex
+ path = self._make_pdf('inventory.pdf')
+ return path
+
+ def door_warning(self,
+ valid_record=lambda r: r['Disposed'] == ''):
+ """create a warning NFPA diamond and list of the most dangerous
+ chemicals for which valid_record(record) is true. For
+ example, to generate a door warning for the front door use
+ door_warning(lambda r: r['Disposed'] == '')
+ or to generate the warning for the fridge
+ door_warning(lambda r: r['Location'] == 'Refrigerator')
+ Note that valid_record defaults to the first example.
+ """
+ pp = DBPrettyPrinter(self.db)
+ all_ids = range(self.db.len_records())
+
+ # Search the database to find the nasties
+ NFPA_maxs = {'H':0, 'F':0, 'R':0, 'O':[]}
+ Mutagens = []
+ Carcinogens = []
+ Teratogens = []
+ Healths = []
+ Fires = []
+ Reactivities = []
+ Others = []
+ for record in self.db.records():
+ if valid_record(record):
+ for field in ['H', 'F', 'R', 'O']:
+ r = record[field]
+ if r != '' and r != '?':
+ if field != 'O' and int(r) > NFPA_maxs[field]:
+ NFPA_maxs[field] = int(r)
+ elif field == 'O' and not r in NFPA_maxs['O']:
+ NFPA_maxs[field].append(r)
+ for field,array in zip(['M','C','T'],
+ [Mutagens,
+ Carcinogens,
+ Teratogens]):
+ if record[field] != '' and record[field] != '?':
+ array.append(record['db_id'])
+ # now that we've found the max NFPAs,
+ # find all the chemicals at those levels
+ for record in self.db.records():
+ if valid_record(record):
+ for field,array in zip(['H', 'F', 'R', 'O'],
+ [Healths, Fires,
+ Reactivities, Others]):
+ r = record[field]
+ if r != '' and r != '?':
+ if field != 'O' and int(r) == NFPA_maxs[field]:
+ array.append(record['db_id'])
+ elif field == 'O' and r in NFPA_maxs['O']:
+ array.append(record['db_id'])
+
+ ## generate the output
+ # first, update the NFPA grapic code
+ if 'OX' in NFPA_maxs['O'] : OX = 'y'
+ else : OX = 'n'
+ if 'W' in NFPA_maxs['O'] : W = 'y'
+ else : W = 'n'
+ os.system('%s %d %d %d %s %s > %s'
+ % (os.path.join(self.doc_root, 'gen_NFPA.sh'),
+ NFPA_maxs['H'], NFPA_maxs['F'], NFPA_maxs['R'], OX, W,
+ os.path.join(self.doc_root, 'mp', 'NFPA.mp')))
+ # now generate a list of the nasties ( Amount & ID & Name )
+ string = "\\begin{tabular}{r r l}\n"
+ for field,name,array in zip(['H', 'F', 'R', 'O'],
+ ['Health', 'Fire',
+ 'Reactivity', 'Other'],
+ [Healths, Fires,
+ Reactivities, Others]):
+ if (not hasattr(NFPA_maxs[field], '__len__')) \
+ or len(NFPA_maxs[field]) > 0:
+ string += " \multicolumn{3}{c}{\Tstrut %s : %s} \\\\\n" \
+ % (name, NFPA_maxs[field])
+ else : # Print "Other" instead of "Other : []"
+ string += " \multicolumn{3}{c}{\Tstrut %s} \\\\\n" \
+ % (name)
+ for db_id in array:
+ record = self.db.record(db_id)
+ string += " %s & %s & %s \\\\\n" \
+ % (self._latex_safe(record['Amount']),
+ self._latex_safe(record['ID']),
+ self._latex_safe(record['Name']))
+ if len(array) == 0:
+ string += " \multicolumn{3}{c}{ --- } \\\\\n"
+ for hazard,array in zip(['Mutagens','Carcinogens','Teratogens'],
+ [Mutagens, Carcinogens, Teratogens]):
+ string += " \multicolumn{3}{c}{\Tstrut %s} \\\\\n" % (hazard)
+ for db_id in array:
+ record = self.db.record(db_id)
+ string += " %s & %s & %s \\\\\n" \
+ % (self._latex_safe(record['Amount']),
+ self._latex_safe(record['ID']),
+ self._latex_safe(record['Name']))
+ if len(array) == 0:
+ string += " \multicolumn{3}{c}{ --- } \\\\\n"
+ string += "\\end{tabular}\n"
+ print >> file(os.path.join(self.doc_root, 'door_data.tex'), 'w'), string
+ ## alter main.tex to point to the door template.
+ self._set_main_target('door_template')
+ ## run latex
+ path = self._make_pdf('door_warning.pdf')
+ return path
+
+
+def _test():
+ import doctest
+ doctest.testmod()
+
+if __name__ == "__main__":
+ _test()
--- /dev/null
+# Copyright
-#!/usr/bin/python
-"""
-Simple database-style interface to text-delimited, single files.
-Use this if, for example, your coworkers insist on keeping data compatible with M$ Excel.
+# Copyright
+
+"""Database-style interface with a text-delimited, single files.
+
+Use this if, for example, your coworkers insist on keeping data
+compatible with M$ Excel.
"""
import copy
+import os
+import os.path
+import shutil
+import stat
from sys import stdin, stdout, stderr
+import time
+import types
-# import os, shutil, and chem_db for managing the database files
-import os, shutil, stat, time
-import os.path
FILE = 'default.db'
STANDARD_TAB = 8
-class fieldError (Exception) :
+
+class IndexStringError (Exception):
+ "invalid index string format"
+ pass
+
+
+class FieldError (Exception):
"database fields are not unique"
pass
-class text_db (object) :
- """
- Define a simple database interface for a spread-sheet style database file.
-
+
+class MissingDatabaseFile (Exception):
+ "Specified database file does not exist"
+ def __init__(self, path):
+ msg = "Missing database file %s" % path
+ Exception.__init__(self, msg)
+ self.path = path
+
+
+class TextDB (object):
+ """Simple database interface for a spread-sheet style database file.
+
field_list() : return an ordered list of available fields (fields unique)
long_fields() : return an dict of long field names (keyed by field names)
record(id) : return a record dict (keyed by field names)
new_record() : add a blank record (use set_record to change its values)
"""
def __init__(self, filename=FILE, COM_CHAR='#', FS='\t', RS='\n',
- current_dir='./current/', backup_dir='./backup/' ) :
+ current_dir='./current/', backup_dir='./backup/' ):
self.filename = filename
self.COM_CHAR = COM_CHAR # comment character (also signals header row)
self.FS = FS # field seperator
# define directories used by the database
self.cur = current_dir
self.bak = backup_dir
-
- if self.filename == None :
+
+ if self.filename == None:
return # for testing, don't touch the file system
-
+
## Generate the neccessary directory structure if neccessary
- for d in [self.cur,self.bak] :
+ for d in [self.cur, self.bak]:
self.check_dir(d)
self._open()
-
+
# directory and file IO operations
- def check_dir(self, dir) :
+
+ def check_dir(self, dir):
"Create the database directory if it's missing"
- if os.path.isdir(dir) :
+ if os.path.isdir(dir):
return # all set to go
- elif os.path.exists(dir) :
+ elif os.path.exists(dir):
raise Exception, "Error: a non-directory file exists at %s" % dir
- else :
+ else:
os.mkdir(dir)
- def curpath(self) :
+
+ def curpath(self):
"Return the path to the current database file."
return os.path.join(self.cur, self.filename)
- def _get_mtime(self) :
+
+ def _get_mtime(self):
"Get the timestamp of the last modification to the database."
s = os.stat(self.curpath())
return s[stat.ST_MTIME]
- def exists(self) :
+
+ def exists(self):
"Check if the database exists"
- # for some reason, my system's os.path.exists
- # returns false for valid symbolic links...
return os.path.exists(self.curpath())
- def _assert_exists(self) :
- """
- Assert that the database exists on disk.
+
+ def _assert_exists(self):
+ """Assert that the database exists on disk.
+
Print a reasonable error if it does not.
"""
if not self.exists():
- raise missingDatabaseFile(self.curpath())
- def _open(self) :
+ raise MissingDatabaseFile(self.curpath())
+
+ def _open(self):
"Load the database from disk"
self._assert_exists()
- # precedence AND > OR
fulltext = file(self.curpath(), 'r').read()
self._mtime = self._get_mtime()
self._parse(fulltext)
- def iscurrent(self) :
+
+ def iscurrent(self):
"Check if our memory-space db is still syncd with the disk-space db."
return self._mtime == self._get_mtime()
- def _refresh(self) :
+
+ def _refresh(self):
"If neccessary, reload the database from disk."
- if not self.iscurrent() :
+ if not self.iscurrent():
self._open()
- def _save(self) :
+
+ def _save(self):
"Create a new database file from a header and list of records."
- # save the new text
fid = file(self.curpath(), 'w')
fid.write( self._file_header_string(self._header) )
- if self._long_header :
+ if self._long_header:
fid.write( self._file_header_string(self._long_header) )
- for record in self._records :
+ for record in self._records:
fid.write( self._file_record_string(record) )
fid.close()
- def backup(self) :
+
+ def backup(self):
"Back up database file"
if not self.exists(): return None # nothing to back up.
# Append a timestamp to the file & copy to self.bak
- # the str() builtin ensures a nice, printable string
- tname = self.filename+'.'+str(int(time.time()))
+ tname = '%s.%d' % (self.filename, time.time())
tpath = os.path.join(self.bak, tname)
spath = self.curpath()
shutil.copy(spath, tpath)
-
+
+
# file-text to memory operations
+
def _get_header(self, head_line, assert_unique=False,
- assert_no_db_id_field=True) :
- """
- Parse a header line (starts with the comment character COM_CHAR).
-
+ assert_no_db_id_field=True):
+ """Parse a header line (starts with the comment character COM_CHAR).
+
Because doctest doesn't play well with tabs, use colons as field seps.
- >>> db = text_db(FS=':', filename=None)
+ >>> db = TextDB(FS=':', filename=None)
+
>>> print db._get_header("#Name:Field 1:ID: another field")
['Name', 'Field 1', 'ID', ' another field']
- >>> try :
+ >>> try:
... x = db._get_header("#Name:Field 1:Name: another field", assert_unique=True)
- ... except fieldError, s :
+ ... except FieldError, s:
... print s
fields 0 and 2 both 'Name'
"""
assert len(head_line) > 0, 'empty header'
assert head_line[0] == self.COM_CHAR, 'bad header: "%s"' % head_line
fields = head_line[1:].split(self.FS)
- if assert_unique :
- for i in range(len(fields)) :
- for j in range(i+1,len(fields)) :
- if fields[i] == fields[j] :
- raise fieldError, "fields %d and %d both '%s'" \
+ if assert_unique:
+ for i in range(len(fields)):
+ for j in range(i+1,len(fields)):
+ if fields[i] == fields[j]:
+ raise FieldError, "fields %d and %d both '%s'" \
% (i,j,fields[i])
- if assert_no_db_id_field :
- for i in range(len(fields)) :
- if fields[i] == self.db_id_field :
- raise fieldError, "fields %d uses db_id field '%s'" \
+ if assert_no_db_id_field:
+ for i in range(len(fields)):
+ if fields[i] == self.db_id_field:
+ raise FieldError, "fields %d uses db_id field '%s'" \
% (i,fields[i])
return fields
- def _get_fields(self, line, num_fields=None) :
- """
- Parse a record line.
-
+
+ def _get_fields(self, line, num_fields=None):
+ """Parse a record line.
+
Because doctest doesn't play well with tabs, use colons as field seps.
- >>> db = text_db(FS=':', filename=None)
+ >>> db = TextDB(FS=':', filename=None)
+
>>> print db._get_fields("2-Propanol:4 L:67-63-0:Fisher:6/6/2004",7)
['2-Propanol', '4 L', '67-63-0', 'Fisher', '6/6/2004', '', '']
"""
vals = line.split(self.FS)
- if num_fields != None :
+ if num_fields != None:
assert len(vals) <= num_fields, "Too many values in '%s'" % line
- for i in range(len(vals), num_fields) :
+ for i in range(len(vals), num_fields):
vals.append('') # pad with empty strings if neccessary
return vals
- def _parse(self, text) :
+
+ def _parse(self, text):
reclines = text.split(self.RS)
assert len(reclines) > 0, "Empty database file"
- self._header = self._get_header(reclines[0], assert_unique=True)
+ self._header = self._get_header(reclines.pop(0), assert_unique=True)
self._long_header = None
self._records = []
- if len(reclines) == 1 :
+ if len(reclines) == 0:
return # Only a header
# check for a long-header line
- if len(reclines[1]) > 0 and reclines[1][0] == self.COM_CHAR :
- self._long_header = self._get_header(reclines[1])
- startline = 2
- else :
- startline = 1
- for recline in reclines[startline:] :
- if len(recline) == 0 :
- continue # ignore blank lines
+ if len(reclines[0]) > 0 and reclines[0].startswith(self.COM_CHAR):
+ self._long_header = self._get_header(reclines.pop(0))
+ for recline in reclines:
+ if len(recline) == 0 or recline.startswith(self.COM_CHAR):
+ continue # ignore blank lines and comments
self._records.append(self._get_fields(recline, len(self._header)))
-
-
+
# memory to file-text operations
- def _file_record_string(self, record) :
- """
- Format record for creating a new database file.
-
+
+ def _file_record_string(self, record):
+ """Format record for creating a new database file.
+
Because doctest doesn't play well with tabs, use colons as field seps.
- >>> db = text_db(FS=':', RS=';', filename=None)
- >>> rs="2-Propanol:4 L:67-63-0:BPA426P-4:Fisher:6/6/2004:2:3:0"
- >>> print db._file_record_string( db._get_fields(rs)) == (rs+";")
- True
+ >>> db = TextDB(FS=':', RS=';', filename=None)
+
+ >>> rs = '2-Propanol:4 L:67-63-0:Fisher:6/6/2004:2:3:0'
+ >>> db._file_record_string(db._get_fields(rs))
+ '2-Propanol:4 L:67-63-0:Fisher:6/6/2004:2:3:0;'
"""
return "%s%s" % (self.FS.join(record), self.RS)
- def _file_header_string(self, header) :
- """
- Format header for creating a new database file.
- """
+
+ def _file_header_string(self, header):
+ "Format header for creating a new database file."
return "%s%s%s" % (self.COM_CHAR, self.FS.join(header), self.RS)
-
- # nice, stable api for our users
- def field_list(self) :
- "return an ordered list of available fields (fields unique)"
+
+ # nice, stable API for our users
+
+ def field_list(self) :
+ "Return an ordered list of available fields (fields unique)"
return copy.copy(self._header)
- def long_fields(self) :
- "return an dict of long field names (keyed by field names)"
- if self._long_header :
+
+ def long_fields(self):
+ "Return a dict of long field names (keyed by field names)"
+ if self._long_header:
return dict(zip(self._header, self._long_header))
else : # default to the standard field names
return dict(zip(self._header, self._header))
- def record(self, db_id) :
- "return a record dict (keyed by field names)"
- assert type(db_id) == type(1), "id %s not an int!" % str(db_id)
- assert db_id < len(self._records), "record %d does not exist" % db_id
+
+ def record(self, db_id):
+ "Return a record dict (keyed by field names)"
+ assert isinstance(db_id, int), "id %s not an int!" % str(db_id)
+ assert db_id >= 0 and db_id < len(self._records), (
+ "record %d does not exist" % db_id)
d = dict(zip(self._header, self._records[db_id]))
d['db_id'] = db_id
return d
- def records(self) :
- "return an ordered list of available records."
+
+ def records(self):
+ "Return an ordered list of available records."
ret = []
- for id in range(len(self._records)) :
+ for id in range(len(self._records)):
ret.append(self.record(id))
return ret
- def len_records(self) :
- "return len(self.records()), but more efficiently"
+
+ def len_records(self):
+ "Return len(self.records()), but more efficiently"
return len(self._records)
- def set_record(self, db_id, newvals, backup=True) :
- """
- set a record by overwriting any preexisting data
- with data from the field-name-keyed dict NEWVALS
+
+ def set_record(self, db_id, newvals, backup=True):
+ """Set a record with data from the field-name-keyed dict `newvals`.
+
+ Overwrites any preexisting data.
"""
- if backup :
+ if backup:
self.backup()
- for k,v in newvals.items() :
- if k == self.db_id_field :
+ for k,v in newvals.items():
+ if k == self.db_id_field:
assert int(v) == db_id, \
"don't set the db_id field! (attempted %d -> %d)" \
% (db_id, int(v))
# overwrite the record value
self._records[db_id][fi] = v
self._save()
- def new_record(self, db_id=None) :
- """
- create a blank new record and return it.
+
+ def new_record(self, db_id=None):
+ """Create a blank new record and return it.
"""
record = {}
- for field in self._header :
- record[field] = ""
+ for field in self._header:
+ record[field] = ''
record[self.db_id_field] = len(self._records)
self._records.append(['']*len(self._header))
return record
-class missingDatabaseFile (Exception) :
- "Specified database file does not exist"
- def __init__(self, path):
- msg = "Missing database file %s" % path
- Exception.__init__(self, msg)
- self.path = path
-
-class indexStringError (Exception) :
- "invalid index string format"
- pass
-
-class db_pretty_printer (object) :
- """
- Define some pretty-print functions for text_db objects.
+class DBPrettyPrinter (object):
+ """Define some pretty-print functions for :class:`TextDB` objects.
"""
- def __init__(self, db) :
+ def __init__(self, db):
self.db = db
- def _norm_active_fields(self, active_fields_in=None) :
- """
- Normalize the active field parameter
-
- >>> db = text_db(FS=':', RS=' ; ', filename=None)
- >>> pp = db_pretty_printer(db)
+
+ def _norm_active_fields(self, active_fields_in=None):
+ """Normalize the active field parameter
+
+ >>> from pprint import pprint
+ >>> db = TextDB(FS=':', RS=' ; ', filename=None)
+ >>> pp = DBPrettyPrinter(db)
>>> db._parse("#Name:Amount:CAS#:Vendor ; 2-Propanol:4 L:67-63-0:Fisher")
- >>> print pp._norm_active_fields(None) == {'Name':True, 'Amount':True, 'CAS#':True, 'Vendor':True}
- True
- >>> print pp._norm_active_fields(['Vendor', 'Amount']) == {'Name':False, 'Amount':True, 'CAS#':False, 'Vendor':True}
- True
- >>> print pp._norm_active_fields('1:3') == {'Name':False, 'Amount':True, 'CAS#':True, 'Vendor':False}
- True
+
+ >>> pprint(pp._norm_active_fields(None))
+ {'Amount': True, 'CAS#': True, 'Name': True, 'Vendor': True}
+ >>> pprint(pp._norm_active_fields(['Vendor', 'Amount']))
+ {'Amount': True, 'CAS#': False, 'Name': False, 'Vendor': True}
+ >>> pprint(pp._norm_active_fields('1:3'))
+ {'Amount': True, 'CAS#': True, 'Name': False, 'Vendor': False}
"""
- if active_fields_in == None :
+ if active_fields_in == None:
active_fields = {}
- for field in self.db.field_list() :
+ for field in self.db.field_list():
active_fields[field] = True
return active_fields
- elif type(active_fields_in) == type('') :
+ elif isinstance(active_fields_in, types.StringTypes):
active_i = self._istr2ilist(active_fields_in)
active_fields = {}
fields = self.db.field_list()
- for i in range(len(fields)) :
- if i in active_i :
+ for i in range(len(fields)):
+ if i in active_i:
active_fields[fields[i]] = True
- else :
+ else:
active_fields[fields[i]] = False
- else :
- if type(active_fields_in) == type([]) :
+ else:
+ if isinstance(active_fields_in, list):
active_fields = {}
- for field in active_fields_in :
+ for field in active_fields_in:
active_fields[field] = True
- elif type(active_fields_in) == type({}) :
+ elif isinstance(active_fields_in, dict):
active_fields = active_fields_in
- assert type(active_fields) == type({}), 'by this point, should be a dict'
- for field in self.db.field_list() :
- if not field in active_fields :
+ assert isinstance(active_fields, dict), 'by this point, should be a dict'
+ for field in self.db.field_list():
+ if not field in active_fields:
active_fields[field] = False
return active_fields
- def full_record_string(self, record, active_fields=None) :
+
+ def full_record_string(self, record, active_fields=None):
"""
Because doctest doesn't play well with tabs, use colons as field seps.
- >>> db = text_db(FS=':', RS=' ; ', filename=None)
- >>> pp = db_pretty_printer(db)
+ >>> db = TextDB(FS=':', RS=' ; ', filename=None)
+ >>> pp = DBPrettyPrinter(db)
>>> db._parse("#Name:Amount:CAS#:Vendor ; 2-Propanol:4 L:67-63-0:Fisher")
>>> print pp.full_record_string( db.record(0) ),
Name : 2-Propanol
active_fields = self._norm_active_fields(active_fields)
# scan through and determine the width of the largest field
w = 1
- for field in fields :
- if active_fields[field] and len(field) > w :
+ for field in fields:
+ if active_fields[field] and len(field) > w:
w = len(field)
# generate the pretty-print string
string = ""
- for field in fields :
- if field in active_fields and active_fields[field] :
+ for field in fields:
+ if field in active_fields and active_fields[field]:
string += "%*.*s : %s\n" \
% (w, w, long_fields[field], record[field])
return string
- def full_record_string_id(self, id, active_fields=None) :
+
+ def full_record_string_id(self, id, active_fields=None):
record = self.db.record(id)
return self.full_record_string(record, active_fields)
- def _istr2ilist(self, index_string) :
- """
- Generate index lists from assorted string formats.
-
+
+ def _istr2ilist(self, index_string):
+ """Generate index lists from assorted string formats.
+
Parse index strings
- >>> pp = db_pretty_printer('dummy')
- >>> print pp._istr2ilist('0,2,89,4')
+ >>> pp = DBPrettyPrinter('dummy')
+ >>> pp._istr2ilist('0,2,89,4')
[0, 2, 89, 4]
- >>> print pp._istr2ilist('1:6')
+ >>> pp._istr2ilist('1:6')
[1, 2, 3, 4, 5]
- >>> print pp._istr2ilist('0,3,6:9,2')
+ >>> pp._istr2ilist('0,3,6:9,2')
[0, 3, 6, 7, 8, 2]
"""
ret = []
- for spl in index_string.split(',') :
+ for spl in index_string.split(','):
s = spl.split(':')
- if len(s) == 1 :
+ if len(s) == 1:
ret.append(int(spl))
- elif len(s) == 2 :
- for i in range(int(s[0]),int(s[1])) :
+ elif len(s) == 2:
+ for i in range(int(s[0]),int(s[1])):
ret.append(i)
- else :
- raise indexStringError, "unrecognized index '%s'" % spl
+ else:
+ raise IndexStringError, "unrecognized index '%s'" % spl
return ret
- def _norm_width(self, width_in=None, active_fields=None, skinny=True) :
+
+ def _norm_width(self, width_in=None, active_fields=None, skinny=True):
"Normalize the width parameter"
active_fields = self._norm_active_fields(active_fields)
- if type(width_in) == type(1) or width_in == 'a' : # constant width
+ if isinstance(width_in, int) or width_in == 'a' : # constant width
width = {} # set all fields to this width
- for field in active_fields.keys() :
+ for field in active_fields.keys():
width[field] = width_in
- else :
- if width_in == None :
+ else:
+ if width_in == None:
width_in = {}
width = {}
- for field in active_fields.keys() :
- # fill in the gaps in the current width
- if field in width_in :
+ for field in active_fields.keys():
+ # fill in the gaps in the current width
+ if field in width_in:
width[field] = width_in[field]
else : # field doesn't exist
if skinny : # set to a fixed width
else : # set to automatic
width[field] = 'a'
return width
- def _norm_record_ids(self, record_ids=None) :
+
+ def _norm_record_ids(self, record_ids=None):
"Normalize the record_ids parameter"
- if record_ids == None :
+ if record_ids == None:
record_ids = range(len(self.db.records()))
- if type(record_ids) == type('') :
+ if isinstance(record_ids, types.StringTypes):
record_ids = self._istr2ilist(record_ids)
stderr.flush()
return record_ids
+
def _line_record_string(self, record, width=None, active_fields=None,
- FS=None, RS=None, TRUNC_STRING=None) :
+ FS=None, RS=None, TRUNC_STRING=None):
"""
+
Because doctest doesn't play well with tabs, use colons as field seps.
- >>> db = text_db(FS=':', RS=' ; ', filename=None)
- >>> pp = db_pretty_printer(db)
+ >>> db = TextDB(FS=':', RS=' ; ', filename=None)
+
+ >>> pp = DBPrettyPrinter(db)
>>> db._parse("#Name:Amount:CAS#:Vendor ; 2-Propanol:4 L:67-63-0:Fisher")
- >>> print pp._line_record_string_id(0)
- 2-Propa: 4 L:67-63-0: Fisher ;
+ >>> pp._line_record_string_id(0)
+ '2-Propa: 4 L:67-63-0: Fisher ; '
"""
fields = self.db.field_list()
active_fields = self._norm_active_fields(active_fields)
width = self._norm_width(width)
- if FS == None :
+ if FS == None:
FS = self.db.FS
- if RS == None :
+ if RS == None:
RS = self.db.RS
- for field in fields :
- if field in active_fields and active_fields[field] :
+ for field in fields:
+ if field in active_fields and active_fields[field]:
lastfield = field
# generate the pretty-print string
string = ""
- for field in fields :
- if field in active_fields and active_fields[field] :
+ for field in fields:
+ if field in active_fields and active_fields[field]:
w = width[field]
string += "%*.*s" % (w, w, record[field])
- if field != lastfield :
+ if field != lastfield:
string += "%s" % (FS)
string += RS
return string
+
def _line_record_string_id(self, id, width=None, active_fields=None,
- FS=None, RS=None, TRUNC_STRING=None) :
+ FS=None, RS=None, TRUNC_STRING=None):
return self._line_record_string(self.db.record(id),
width, active_fields, FS, RS,
TRUNC_STRING)
- def _get_field_width(self, record_ids, field) :
- """
- Return the width of the longest value in FIELD
- for all the records with db_ids in record_ids.
+
+ def _get_field_width(self, record_ids, field):
+ """Return the width of the longest value in FIELD for all the
+ records with db_ids in record_ids.
"""
width = 1
- for i in record_ids :
+ for i in record_ids:
w = len(self.db.record(i)[field])
- if w > width :
+ if w > width:
width = w
return width
- def _get_width(self, width_in, active_fields=None, record_ids=None) :
- """
- Return the width of the largest value in FIELD
- for all the records with db_ids in record_ids.
+ def _get_width(self, width_in, active_fields=None, record_ids=None):
+ """Return the width of the largest value in FIELD for all the
+ records with db_ids in record_ids.
"""
active_fields = self._norm_active_fields(active_fields)
width = self._norm_width(width_in, active_fields)
record_ids = self._norm_record_ids(record_ids)
- for field in active_fields :
- if width[field] == 'a' :
+ for field in active_fields:
+ if width[field] == 'a':
width[field] = self._get_field_width(record_ids, field)
return width
+
def multi_record_string(self, record_ids=None, active_fields=None,
width=None, FS=None, RS=None, COM_CHAR=None,
- TRUNC_STRING=None) :
+ TRUNC_STRING=None):
"""
+
Because doctest doesn't play well with tabs, use colons as field seps.
- >>> db = text_db(FS=':', RS=' ; ', filename=None)
- >>> pp = db_pretty_printer(db)
+ >>> db = TextDB(FS=':', RS=' ; ', filename=None)
+
+ >>> pp = DBPrettyPrinter(db)
>>> db._parse("#Name:Amount:CAS#:Vendor ; 2-Propanol:4 L:67-63-0:Fisher")
- >>> print pp.multi_record_string('0'),
- Name: Amount: CAS#: Vendor ; 2-Propa: 4 L:67-63-0: Fisher ;
+ >>> pp.multi_record_string('0')
+ ' Name: Amount: CAS#: Vendor ; 2-Propa: 4 L:67-63-0: Fisher ; '
"""
- if FS == None :
+ if FS == None:
FS = self.db.FS
- if RS == None :
+ if RS == None:
RS = self.db.RS
- if COM_CHAR == None :
+ if COM_CHAR == None:
COM_CHAR = self.db.COM_CHAR
active_fields = self._norm_active_fields(active_fields)
record_ids = self._norm_record_ids(record_ids)
active_fields, FS, RS,
TRUNC_STRING=TRUNC_STRING)
# print the records
- for id in record_ids :
+ for id in record_ids:
string += self._line_record_string_id(id, width,
active_fields, FS, RS,
TRUNC_STRING=TRUNC_STRING)
def _test():
import doctest
doctest.testmod()
-
-if __name__ == "__main__" :
+
+if __name__ == "__main__":
_test()
--- /dev/null
+# Copyright
+
+"""Chemical inventory web interface.
+"""
+
+from __future__ import with_statement
+
+from cgi import escape
+import os
+import os.path
+import re
+import sys, logging
+
+import cherrypy
+from jinja2 import Environment, FileSystemLoader
+
+from .daemon import Daemon
+from .db.text import TextDB
+from .chemdb import valid_CASno, MSDSManager, DocGen
+
+
+__version__ = "0.4"
+
+
+class Server (object):
+ """ChemDB web interface."""
+ def __init__(self, db=None, MSDS_manager=None, docs=None,
+ template_root='template', static_dir='static',
+ static_url='static'):
+ self.db = db
+ self.MSDS_manager = MSDS_manager
+ self.docs = docs
+ self.static_dir = static_dir
+ self.static_url = static_url
+ self.env = Environment(loader=FileSystemLoader(template_root))
+ self.width_max = {'CAS#':12}
+ self.entry_width = 50 # display width of entry fields
+ self.raw_fields = ['ID','Name']
+ self.MSDS_content_types = [
+ 'text/plain', 'text/html', 'application/pdf']
+
+ @cherrypy.expose
+ def index(self, id=None):
+ """Chemical index page.
+ """
+ self.db._refresh()
+ if id == None:
+ return self._index_page()
+ else:
+ return self._view_page(id=id)
+
+ def _index_page(self):
+ records = self.db.records()
+ for record in records:
+ for field in self.db.field_list():
+ rstring = record[field]
+ if field in self.width_max: # truncate if width is regulated...
+ w = self.width_max[field]
+ if len(rstring) > w: # ... and full string is too long.
+ rstring = "%s..." % rstring[:w-3]
+ # if the field isn't raw, protect special chars
+ if not field in self.raw_fields:
+ rstring = escape(rstring)
+ if self.MSDS_manager.has_MSDS(record['db_id']):
+ # link the id to the MSDS
+ record['Name'] = (
+ '<a href="%s">%s</a>'
+ % (self._MSDS_path(record['db_id']), record['Name']))
+ template = self.env.get_template('index.html')
+ return template.render(fields=self.db.field_list(), records=records)
+
+ def _view_page(self, id):
+ db_id = int(id)
+ record = self.db.record(db_id)
+ MSDS_link = None
+ if self.MSDS_manager.has_MSDS(db_id):
+ path = self._MSDS_path(db_id)
+ MSDS_link = '<a href="%s">%s</a>' % (
+ path, os.path.basename(path))
+ template = self.env.get_template('record.html')
+ return template.render(fields=self.db.field_list(),
+ long_fields=self.db.long_fields(),
+ record=record,
+ MSDS=MSDS_link,
+ escape=escape)
+
+ def _MSDS_path(self, db_id):
+ path = self.MSDS_manager.get_MSDS_path(db_id)
+ path = os.path.relpath(path, self.static_dir)
+ path = os.path.join(self.static_url, path)
+ return path
+
+ @cherrypy.expose
+ def edit(self, id=None, **kwargs):
+ """Render the edit-record.html template for the specified record.
+ """
+ self.db._refresh()
+ if kwargs:
+ self._update_record(id=id, **kwargs)
+ raise cherrypy.HTTPRedirect(u'.?id=%s' % id, status=303)
+ MSDSs = self.MSDS_manager.get_all(simlinks=False)
+ if id in [None, '-1']:
+ record = dict([(field,'') for field in self.db.field_list()])
+ record['db_id'] = '-1'
+ record['ID'] = str(record['db_id'])
+ else:
+ db_id = int(id)
+ record = self.db.record(db_id)
+ if self.MSDS_manager.has_MSDS(db_id):
+ MSDSs = None # only ask for an MSDS if we still need one
+ template = self.env.get_template('edit-record.html')
+ return template.render(fields=self.db.field_list(),
+ long_fields=self.db.long_fields(),
+ record=record,
+ MSDSs=MSDSs,
+ entry_width=self.entry_width,
+ escape=escape)
+
+ def _update_record(self, id=None, **kwargs):
+ """Update a record with form input.
+ """
+ self.db._refresh()
+ if id in [None, '-1']:
+ record = self.db.new_record()
+ logging.info('new record %s' % record['db_id'])
+ else:
+ db_id = int(id)
+ record = self.db.record(db_id)
+ update = False
+ for field in self.db.field_list():
+ if kwargs[field] != record[field]:
+ # TODO: add validation!
+ update = True
+ record[field] = kwargs[field]
+ if kwargs.get('MSDS source', None) == 'upload':
+ # Handle any MSDS file actions
+ f = kwargs['MSDS upload']
+ contents = f.file.read()
+ if len(contents) > 0 and f.type in self.MSDS_content_types:
+ self.MSDS_manager.save(int(record['ID']), contents, f.type)
+ elif kwargs.get('MSDS source', None) == 'share':
+ logging.info('linking MSDS %d to %d'
+ % (int(record['ID']),
+ int(kwargs['MSDS share'])) )
+ self.MSDS_manager.link(int(record['ID']),
+ int(kwargs['MSDS share']))
+ if update:
+ self.db.set_record(record['db_id'], record, backup=True)
+
+
+class Docs (object):
+ "Generate and serve assorted official documents."
+ def __init__(self, db=None, docgen=None, template_root='template'):
+ self.db = db
+ self.docgen = docgen
+ self.namewidth = 40
+ self.env = Environment(loader=FileSystemLoader(template_root))
+
+ @cherrypy.expose
+ def index(self):
+ """List the available documents.
+ """
+ template = self.env.get_template('docs.html')
+ return template.render()
+
+ @cherrypy.expose
+ def inventory_pdf(self):
+ self.db._refresh()
+ path = self.docgen.inventory(namewidth=self.namewidth)
+ cherrypy.response.headers['Content-Type'] = 'application/pdf'
+ return file(path, 'rb').read()
+
+ @cherrypy.expose
+ def door_warning_pdf(self, location=None):
+ self.db._refresh()
+ regexp = re.compile(location, re.I) # Case insensitive
+ path = dgen.door_warning(lambda r: regexp.match(r['Location']))
+ cherrypy.response.headers['Content-Type'] = 'application/pdf'
+ return file(path, 'rb').read()
+
+
+class ServerDaemon (Daemon):
+ def __init__(self):
+ super(ServerDaemon, self).__init__()
+ self.gid = None
+ self.uid = None
+ self.pidfile = './chem_web.pid'
+ self.logfile = './chem_web.log'
+ self.loglevel = 'INFO'
+
+ def run(self):
+ if cherrypy.__version__.startswith('3.'):
+ cherrypy.quickstart(root=self.server, config=self.app_config)
+ elif cherrypy.__version__.startswith('2.'):
+ cherrypy.server.start()
+
+# if self.pkey_file == None or self.cert_file == None:
+# logging.info("http://%s:%d/" % web.validip(self.ip_address))
+# else:
+# logging.info("https://%s:%d/" % web.validip(self.ip_address))
+#
+# webpy_func = web.webpyfunc(urls, globals(), False)
+# wsgi_func = web.wsgifunc(webpy_func)
+# web.httpserver.runsimple(wsgi_func,
+# web.validip(self.ip_address),
+# ssl_private_key_filename=self.pkey_file,
+# ssl_certificate_filename=self.cert_file)
+
+ def read_basic_config(self):
+ pass
+
+ def parse_options(self):
+ from optparse import OptionParser
+
+ usage_string = ('%prog [options]\n'
+ '\n'
+ '2008, W. Trevor King.\n')
+ version_string = '%%prog %s' % __version__
+ p = OptionParser(usage=usage_string, version=version_string)
+
+ # Non-server options
+ p.add_option('-t', '--test', dest='test', default=False,
+ action='store_true', help='Run internal tests and exit.')
+
+ # Daemon options
+ p.add_option('--start', dest='action',
+ action='store_const', const='start', default='start',
+ help='Start the daemon (the default action).')
+ p.add_option('--stop', dest='action',
+ action='store_const', const='stop', default='start',
+ help='Stop the daemon.')
+ p.add_option('-n', '--nodaemon', dest='daemonize',
+ action='store_false', default=True,
+ help='Run in the foreground.')
+
+ # Server options
+ p.add_option('-a', '--address', dest='address', default='127.0.0.1',
+ metavar='ADDR',
+ help='Address that the server will bind to (%default).')
+ p.add_option('-p', '--port', dest='port', default='8080',
+ metavar='PORT',
+ help='Port that the server will listen on (%default).')
+ p.add_option('-s', '--secure', dest="secure",
+ help="Run in secure (HTTPS) mode.",
+ type='string', metavar="PKEY_FILE:CERT_FILE")
+ p.add_option('-v', '--verbose', dest="verbose", action="store_true",
+ help="Print lots of debugging information.",
+ default=False)
+ p.add_option('--static', dest='static', metavar='PATH',
+ help="Path to the directory of static files (%default).",
+ default=os.path.join('example', 'static'))
+ p.add_option('--template', dest='template', metavar='PATH',
+ help="Path to the directory of template files (%default).",
+ default=os.path.join('template', 'web'))
+ p.add_option('--doc', dest='doc', metavar='PATH',
+ help="Path to the directory of document generation files (%default).",
+ default=os.path.join('template', 'doc'))
+ p.add_option('--htaccess', dest='htaccess', metavar='FILE',
+ help="Path to the htaccess file (%default).",
+ default='.htaccess')
+
+ # Database options
+ p.add_option('--database', dest='database', metavar='FILE',
+ help="Path to the database file (%default).",
+ default=os.path.join('example', 'inventory.db'))
+
+ self.options, args = p.parse_args()
+ p.destroy()
+
+ if self.options.test == True:
+ _test()
+ sys.exit(0)
+
+ if self.options.verbose:
+ self.loglevel = 'DEBUG'
+
+ # get self.options for httpserver
+ if self.options.secure != None:
+ split = self.options.secure.split(':', 1)
+ assert len(split) == 2, (
+ "Invalid secure argument '%s'" % self.options.secure)
+ self.pkey_file = split[0]
+ self.cert_file = split[1]
+
+ # HACK! to ensure we *always* get utf-8 output
+ #reload(sys)
+ #sys.setdefaultencoding('utf-8')
+
+ dirname,filename = os.path.split(
+ os.path.abspath(self.options.database))
+ static_dir = os.path.abspath(self.options.static)
+ MSDS_dir = os.path.join(static_dir, 'MSDS')
+ template_dir = os.path.abspath(self.options.template)
+ doc_dir = os.path.abspath(self.options.doc)
+ db = TextDB(filename=filename, current_dir=dirname)
+ MSDS_manager = MSDSManager(db=db, dir=MSDS_dir)
+ docgen = DocGen(db=db, doc_root=doc_dir)
+ docs = Docs(db=db, docgen=docgen, template_root=template_dir)
+ server = Server(db=db, MSDS_manager=MSDS_manager, docs=docs,
+ template_root=template_dir, static_dir=static_dir,
+ static_url='/static')
+
+ if cherrypy.__version__.startswith('3.'):
+ cherrypy.config.update({ # http://www.cherrypy.org/wiki/ConfigAPI
+ 'server.socket_host': self.options.address,
+ 'server.socket_port': int(self.options.port),
+ 'tools.decode.on': True,
+ 'tools.encode.on': True,
+ 'tools.encode.encoding': 'utf8',
+ 'tools.staticdir.root': static_dir,
+ })
+ digest_auth = None
+ if cherrypy.__version__.startswith('3.2'):
+ try:
+ get_ha1 = cherrypy.lib.auth_digest.get_ha1_file_htdigest(
+ self.options.htaccess)
+ except IOError:
+ pass
+ else:
+ digest_auth = {
+ 'tools.auth_digest.on': True,
+ 'tools.auth_digest.realm': 'cookbook',
+ 'tools.auth_digest.get_ha1': get_ha1,
+ 'tools.auth_digest.key': str(uuid.uuid4()),
+ }
+ else:
+ passwds = {}
+ try:
+ with open(self.options.htaccess, 'r') as f:
+ for line in f:
+ user,realm,ha1 = line.strip().split(':')
+ passwds[user] = ha1 # use the ha1 as the password
+ except IOError:
+ pass
+ else:
+ digest_auth = {
+ 'tools.digest_auth.on': True,
+ 'tools.digest_auth.realm': 'cookbook',
+ 'tools.digest_auth.users': passwds,
+ }
+ app_config = {
+ '/static': {
+ 'tools.staticdir.on': True,
+ 'tools.staticdir.dir': '',
+ },
+ }
+ if digest_auth != None:
+ for url in ['edit']:
+ app_config[url] = digest_auth
+ self.server = server
+ self.app_config = app_config
+ elif cherrypy.__version__.startswith('2.'):
+ cherrypy.root = server
+ cherrypy.config.update({
+ 'server.environment': 'production',
+ 'server.socket_host': self.options.address,
+ 'server.socket_port': int(self.options.port),
+ 'decoding_filter.on': True,
+ 'encoding_filter.on': True,
+ 'encodinf_filter.encoding': 'utf8',
+ 'static_filter.on': True,
+ 'static_filter.dir': static_dir,
+ })
+ cherrypy.server.start()
+ else:
+ raise NotImplementedError(
+ 'Unsupported CherryPy version %s' % cherrypy.__version__)
+
+
+def _test():
+ import doctest
+ doctest.testmod()
+#!/usr/bin/python
"""
From pyOpenSSL examples with a bit of wrapping.
Create certificates and private keys for the 'simple' example.
"""
from OpenSSL import crypto
-from certgen import * # yes yes, I know, I'm lazy
+import certgen
+
def get_cert_filenames(server_name) :
"""
"""
pkey_file,cert_file = get_cert_filenames(server_name)
- cakey = createKeyPair(TYPE_RSA, 1024)
- careq = createCertRequest(cakey, CN='Certificate Authority')
- cacert = createCertificate(careq, (careq, cakey), 0, (0, 60*60*24*365*5)) # five years
+ cakey = certgen.createKeyPair(certgen.TYPE_RSA, 1024)
+ careq = certgen.createCertRequest(cakey, CN='Certificate Authority')
+ cacert = certgen.createCertificate(careq, (careq, cakey), 0, (0, 60*60*24*365*5)) # five years
open(pkey_file, 'w').write(crypto.dump_privatekey(crypto.FILETYPE_PEM, cakey))
open(cert_file, 'w').write(crypto.dump_certificate(crypto.FILETYPE_PEM, cacert))
--- /dev/null
+<html>
+ <head>
+ </head>
+ <body>
+ Acetic Acid MSDS.
+ </body>
+</html>
--- /dev/null
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
+ xml:lang="en">
+
+<html>
+ <head>
+ <title>ChemDB</title>
+<!--
+ <link rel="stylesheet" type="text/css" media="screen"
+ href="/static/style.css"/>
+-->
+ </head>
+
+ <body>
+ <div id="main-pane">
+ <div id="header" class="inside-main-pane">
+ <h1><a href="./">ChemDB</a></h1>
+ </div>
+ <div id="content-pane" class="inside-main-pane">
+ <h1>{% block page_title %}{% endblock %}</h1>
+ {% block content %}{% endblock %}
+ </div>
+ <div id="footer" class="inside-main-pane">
+ <p>
+ Powered by
+ <a href="http://www.physics.drexel.edu/">Trevor King's</a>
+ <a href="http://www.physics.drexel.edu/~wking/code/git/git.php?p=chemdb.git">ChemDB</a>.
+ Built using <a href="http://cherrypy.org/">CherryPy</a>
+ and <a href="http://jinja.pocoo.org/2/">Jinja2</a>.
+ </p>
+ </div>
+ </div>
+ </body>
+</html>
--- /dev/null
+{% extends "base.html" %}
+
+{% block page_title %}
+ Documents
+{% endblock %}
+
+{% block content %}
+<ul>
+ <li>
+ <a href="/docs/inventory.pdf">Inventory</a>
+ in accordance with the
+ <a href="http://www.drexelsafetyandhealth.com/lab-chem.htm#chpe7">
+ Chemical Hygiene Plan Section E-7</a>.
+ </li>
+ <li>
+ <a href="/docs/door_warning.pdf">Door warning</a>
+ in accordance with the Chemical Hygiene Plan Sections
+ <a href="http://www.drexelsafetyandhealth.com/lab-chem.htm#chpe7">E-7</a>
+ and
+ <a href="http://www.drexelsafetyandhealth.com/lab-chem.htm#chpe10">E-10</a>.
+ </li>
+</ul>
+<p>
+ For door warnings for subsections of the whole room, please give a
+ location regexp in the form below. For example: ".*liquids" or
+ "refrigerator".
+</p>
+<form action="door_warning" method="post">
+ <table>
+ <tr><td>Location regexp</td><td>:</td>
+ <td><input type="text" size="50" id="location" name="location"></td></tr>
+ </table>
+ <input type="submit" value="Submit" />
+</form>
+{% endblock %}
--- /dev/null
+{% extends "base.html" %}
+
+{% block page_title %}
+ Edit {{ record['ID'] }}
+{% endblock %}
+
+{% block content %}
+ <h1>Editing: {{ record['ID'] }}</h1>
+ <!-- the form encoding type 'enctype="multipart/form-data">
+ is only required because of the MSDS file-upload field. -->
+ <form action="" method="post" enctype="multipart/form-data">
+ <table>
+ <tr>
+ <th>Field</td><td>Value</td>
+ </tr>
+ {% for field in fields %}
+ <tr>
+ <td><label for="{{ escape(field, quote=True) }}">
+ {{ escape(long_fields[field]) }}</label></td>
+ <td><input type="text" size="{{ entry_width }}"
+ id="{{ escape(field, quote=True) }}"
+ name="{{ escape(field, quote=True) }}"
+ value="{{ escape(record[field], quote=True) }}">
+ </td>
+ </tr>
+ {% endfor %}
+ {% if MSDSs %}
+ <tr>
+ <td><label for="MSDS source">MSDS source</label></td>
+ <td>
+ <input type="radio" name="MSDS source" value="upload"
+ checkked="checked">Upload
+ <input type="radio" name="MSDS source" value="share">Share
+ </td>
+ </tr>
+ <tr>
+ <td><label for="MSDS upload">Upload file</label></td>
+ <td><input type="file" size="{{ entry_width }}"
+ id="MSDS upload" name="MSDS upload"
+ accept="text/plain,text/html,application/pdf" value="">
+ </td>
+ </tr>
+ <tr>
+ <td><label for="MSDS share">Use an already uploaded MSDS</label></td>
+ <td>
+ <select size="1" id="MSDS share" name="MSDS share">
+ {% for MSDS in MSDSs %}
+ <option value="{{ MSDS['ID'] }}">
+ {{ MSDS['ID'] }}: {{ escape(MSDS['Name']) }}</option>
+ {% endfor %}
+ </select>
+ </td>
+ </tr>
+ {% endif %}
+ </table>
+ <input type="submit" value="Submit" />
+ </form>
+{% endblock %}
--- /dev/null
+{% extends "base.html" %}
+
+{% block page_title %}
+ Index
+{% endblock %}
+
+{% block content %}
+ <p>See <a href="http://www.drexelsafetyandhealth.com/lab-chem.htm">the
+ rules</a> for more information. See
+ the <a href="/docs/">docs</a> page to generate required
+ documents.
+ </p>
+ <p><a href="edit">Add entry</a></p>
+ {% if records %}
+ <table id="record-list">
+ <tr>
+ {% for field in fields %}
+ <td>{{ field }}</td>
+ {% endfor %}
+ </tr>
+ {% for record in records %}
+ <tr>
+ {% for field in fields %}
+ {% if field == 'ID' %}
+ <td>
+ <a href="?id={{ record['ID'] }}">
+ {{ record[field] }}
+ </a>
+ </td>
+ {% else %}
+ <td>{{ record[field] }}</td>
+ {% endif %}
+ {% endfor %}
+ </tr>
+ {% endfor %}
+ </table>
+ {% endif %}
+ <p><a href="edit">Add entry</a></p>
+{% endblock %}
--- /dev/null
+{% extends "base.html" %}
+
+{% block page_title %}
+ {{ record['ID'] }}
+{% endblock %}
+
+{% block content %}
+ <h1>{{ record['ID'] }}</h1>
+ <table>
+ <tr>
+ <th>Field</td><td>Value</td>
+ </tr>
+ {% for field in fields %}
+ <tr>
+ <td>{{ escape(long_fields[field]) }}</td>
+ <td>{{ escape(record[field]) }}</td>
+ </tr>
+ {% endfor %}
+ <tr>
+ <td><label for="MSDS">MSDS</label></td>
+ <td>{{ MSDS }}</td>
+ </tr>
+ </table>
+ <p><a href="edit?id={{ record['ID'] }}">
+ Edit record</a></p>
+{% endblock %}
--- /dev/null
+#!/usr/bin/python
+#
+# Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
+#
+# This file is part of Cookbook.
+#
+# Cookbook is free software: you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the
+# Free Software Foundation, either version 3 of the License, or (at your
+# option) any later version.
+#
+# Cookbook is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Cookbook. If not, see <http://www.gnu.org/licenses/>.
+
+"""Automatically update copyright boilerplate.
+
+This script is adapted from one written for `Bugs Everywhere`_.
+
+.. _Bugs Everywhere: http://bugseverywhere.org/
+"""
+
+import difflib
+import email.utils
+import os
+import os.path
+import re
+import sys
+import time
+
+
+PROJECT_INFO = {
+ 'project': 'ChemDB',
+ 'vcs': 'Git',
+ }
+
+# Break "copyright" into "copy" and "right" to avoid matching the
+# REGEXP.
+COPY_RIGHT_TEXT="""
+This file is part of %(project)s.
+
+%(project)s is free software: you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation, either version 3 of the License, or (at your
+option) any later version.
+
+%(project)s is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with %(project)s. If not, see <http://www.gnu.org/licenses/>.
+""".strip()
+
+COPY_RIGHT_TAG='-xyz-COPY' + '-RIGHT-zyx-' # unlikely to occur in the wild :p
+
+# Convert author names to canonical forms.
+# ALIASES[<canonical name>] = <list of aliases>
+# for example,
+# ALIASES = {
+# 'John Doe <jdoe@a.com>':
+# ['John Doe', 'jdoe', 'J. Doe <j@doe.net>'],
+# }
+# Git-based projects are encouraged to use .mailmap instead of
+# ALIASES. See git-shortlog(1) for details.
+ALIASES = {}
+
+# List of paths that should not be scanned for copyright updates.
+# IGNORED_PATHS = ['./.git/']
+IGNORED_PATHS = ['./.git/', './template/']
+# List of files that should not be scanned for copyright updates.
+# IGNORED_FILES = ['COPYING']
+IGNORED_FILES = ['COPYING', 'certgen.py', 'mk_simple_certs.py',
+ 'daemon.py']
+
+# Work around missing author holes in the VCS history.
+# AUTHOR_HACKS[<path tuple>] = [<missing authors]
+# for example, if John Doe contributed to module.py but wasn't listed
+# in the VCS history of that file:
+# AUTHOR_HACKS = {
+# ('path', 'to', 'module.py'):['John Doe'],
+# }
+AUTHOR_HACKS = {}
+
+# Work around missing year holes in the VCS history.
+# YEAR_HACKS[<path tuple>] = <original year>
+# for example, if module.py was published in 2008 but the VCS history
+# only goes back to 2010:
+# YEAR_HACKS = {
+# ('path', 'to', 'module.py'):2008,
+# }
+YEAR_HACKS = {}
+
+# Helpers for VCS-specific commands
+
+def splitpath(path):
+ """Recursively split a path into elements.
+
+ Examples
+ --------
+
+ >>> splitpath(os.path.join('a', 'b', 'c'))
+ ('a', 'b', 'c')
+ >>> splitpath(os.path.join('.', 'a', 'b', 'c'))
+ ('a', 'b', 'c')
+ """
+ path = os.path.normpath(path)
+ elements = []
+ while True:
+ dirname,basename = os.path.split(path)
+ elements.insert(0,basename)
+ if dirname in ['', '.']:
+ break
+ path = dirname
+ return tuple(elements)
+
+# VCS-specific commands
+
+if PROJECT_INFO['vcs'] == 'Git':
+
+ import subprocess
+
+ _MSWINDOWS = sys.platform == 'win32'
+ _POSIX = not _MSWINDOWS
+
+ def invoke(args, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, expect=(0,)):
+ """
+ expect should be a tuple of allowed exit codes.
+ """
+ try :
+ if _POSIX:
+ q = subprocess.Popen(args, stdin=subprocess.PIPE,
+ stdout=stdout, stderr=stderr)
+ else:
+ assert _MSWINDOWS == True, 'invalid platform'
+ # win32 don't have os.execvp() so run the command in a shell
+ q = subprocess.Popen(args, stdin=subprocess.PIPE,
+ stdout=stdout, stderr=stderr, shell=True)
+ except OSError, e:
+ raise ValueError([args, e])
+ stdout,stderr = q.communicate(input=stdin)
+ status = q.wait()
+ if status not in expect:
+ raise ValueError([args, status, stdout, stderr])
+ return status, stdout, stderr
+
+ def git_cmd(*args):
+ status,stdout,stderr = invoke(['git'] + list(args))
+ return stdout.rstrip('\n')
+
+ def original_year(filename, year_hacks=YEAR_HACKS):
+ output = git_cmd('log', '--follow',
+ '--format=format:%ad', # Author date
+ '--date=short', # YYYY-MM-DD
+ filename)
+ years = [int(line.split('-', 1)[0]) for line in output.splitlines()]
+ if splitpath(filename) in year_hacks:
+ years.append(year_hacks[splitpath(filename)])
+ years.sort()
+ return years[0]
+
+ def authors(filename, author_hacks=AUTHOR_HACKS):
+ output = git_cmd('log', '--follow', '--format=format:%aN <%aE>',
+ filename) # Author name <author email>
+ ret = list(set(output.splitlines()))
+ if splitpath(filename) in author_hacks:
+ ret.extend(author_hacks[splitpath(filename)])
+ return ret
+
+ def authors_list(author_hacks=AUTHOR_HACKS):
+ output = git_cmd('log', '--format=format:%aN <%aE>')
+ ret = list(set(output.splitlines()))
+ for path,authors in author_hacks.items():
+ ret.extend(authors)
+ return ret
+
+ def is_versioned(filename):
+ output = git_cmd('log', '--follow', filename)
+ if len(output) == 0:
+ return False
+ return True
+
+elif PROJECT_INFO['vcs'] == 'Mercurial':
+
+ import StringIO
+ import mercurial
+ import mercurial.dispatch
+
+ def mercurial_cmd(*args):
+ cwd = os.getcwd()
+ stdout = sys.stdout
+ stderr = sys.stderr
+ tmp_stdout = StringIO.StringIO()
+ tmp_stderr = StringIO.StringIO()
+ sys.stdout = tmp_stdout
+ sys.stderr = tmp_stderr
+ try:
+ mercurial.dispatch.dispatch(list(args))
+ finally:
+ os.chdir(cwd)
+ sys.stdout = stdout
+ sys.stderr = stderr
+ return (tmp_stdout.getvalue().rstrip('\n'),
+ tmp_stderr.getvalue().rstrip('\n'))
+
+ def original_year(filename, year_hacks=YEAR_HACKS):
+ # shortdate filter: YEAR-MONTH-DAY
+ output,error = mercurial_cmd('log', '--follow',
+ '--template', '{date|shortdate}\n',
+ filename)
+ years = [int(line.split('-', 1)[0]) for line in output.splitlines()]
+ if splitpath(filename) in year_hacks:
+ years.append(year_hacks[splitpath(filename)])
+ years.sort()
+ return years[0]
+
+ def authors(filename, author_hacks=AUTHOR_HACKS):
+ output,error = mercurial_cmd('log', '--follow',
+ '--template', '{author}\n',
+ filename)
+ ret = list(set(output.splitlines()))
+ if splitpath(filename) in author_hacks:
+ ret.extend(author_hacks[splitpath(filename)])
+ return ret
+
+ def authors_list(author_hacks=AUTHOR_HACKS):
+ output,error = mercurial_cmd('log', '--template', '{author}\n')
+ ret = list(set(output.splitlines()))
+ for path,authors in author_hacks.items():
+ ret.extend(authors)
+ return ret
+
+ def is_versioned(filename):
+ output,error = mercurial_cmd('log', '--follow', filename)
+ if len(error) > 0:
+ return False
+ return True
+
+elif PROJECT_INFO['vcs'] == 'Bazaar':
+
+ import StringIO
+ import bzrlib
+ import bzrlib.builtins
+ import bzrlib.log
+
+ class LogFormatter (bzrlib.log.LogFormatter):
+ supports_merge_revisions = True
+ preferred_levels = 0
+ supports_deta = False
+ supports_tags = False
+ supports_diff = False
+
+ def log_revision(self, revision):
+ raise NotImplementedError
+
+ class YearLogFormatter (LogFormatter):
+ def log_revision(self, revision):
+ self.to_file.write(
+ time.strftime('%Y', time.gmtime(revision.rev.timestamp))
+ +'\n')
+
+ class AuthorLogFormatter (LogFormatter):
+ def log_revision(self, revision):
+ authors = revision.rev.get_apparent_authors()
+ self.to_file.write('\n'.join(authors)+'\n')
+
+ def original_year(filename, year_hacks=YEAR_HACKS):
+ cmd = bzrlib.builtins.cmd_log()
+ cmd.outf = StringIO.StringIO()
+ cmd.run(file_list=[filename], log_format=YearLogFormatter, levels=0)
+ years = [int(year) for year in set(cmd.outf.getvalue().splitlines())]
+ if splitpath(filename) in year_hacks:
+ years.append(year_hacks[splitpath(filename)])
+ years.sort()
+ return years[0]
+
+ def authors(filename, author_hacks=AUTHOR_HACKS):
+ cmd = bzrlib.builtins.cmd_log()
+ cmd.outf = StringIO.StringIO()
+ cmd.run(file_list=[filename], log_format=AuthorLogFormatter, levels=0)
+ ret = list(set(cmd.outf.getvalue().splitlines()))
+ if splitpath(filename) in author_hacks:
+ ret.extend(author_hacks[splitpath(filename)])
+ return ret
+
+ def authors_list(author_hacks=AUTHOR_HACKS):
+ cmd = bzrlib.builtins.cmd_log()
+ cmd.outf = StringIO.StringIO()
+ cmd.run(log_format=AuthorLogFormatter, levels=0)
+ output = cmd.outf.getvalue()
+ ret = list(set(cmd.outf.getvalue().splitlines()))
+ for path,authors in author_hacks.items():
+ ret.extend(authors)
+ return ret
+
+ def is_versioned(filename):
+ cmd = bzrlib.builtins.cmd_log()
+ cmd.outf = StringIO.StringIO()
+ cmd.run(file_list=[filename])
+ return True
+
+else:
+ raise NotImplementedError('Unrecognized VCS: %(vcs)s' % PROJECT_INFO)
+
+# General utility commands
+
+def _strip_email(*args):
+ """Remove email addresses from a series of names.
+
+ Examples
+ --------
+
+ >>> _strip_email('J Doe <jdoe@a.com>')
+ ['J Doe']
+ >>> _strip_email('J Doe <jdoe@a.com>', 'JJJ Smith <jjjs@a.com>')
+ ['J Doe', 'JJJ Smith']
+ """
+ args = list(args)
+ for i,arg in enumerate(args):
+ if arg == None:
+ continue
+ author,addr = email.utils.parseaddr(arg)
+ args[i] = author
+ return args
+
+def _reverse_aliases(aliases):
+ """Reverse an `aliases` dict.
+
+ Input: key: canonical name, value: list of aliases
+ Output: key: alias, value: canonical name
+
+ Examples
+ --------
+
+ >>> aliases = {
+ ... 'J Doe <jdoe@a.com>':['Johnny <jdoe@b.edu>', 'J'],
+ ... 'JJJ Smith <jjjs@a.com>':['Jingly <jjjs@b.edu>'],
+ ... None:['Anonymous <a@a.com>'],
+ ... }
+ >>> r = _reverse_aliases(aliases)
+ >>> for item in sorted(r.items()):
+ ... print item
+ ('Anonymous <a@a.com>', None)
+ ('J', 'J Doe <jdoe@a.com>')
+ ('Jingly <jjjs@b.edu>', 'JJJ Smith <jjjs@a.com>')
+ ('Johnny <jdoe@b.edu>', 'J Doe <jdoe@a.com>')
+ """
+ output = {}
+ for canonical_name,_aliases in aliases.items():
+ for alias in _aliases:
+ output[alias] = canonical_name
+ return output
+
+def _replace_aliases(authors, with_email=True, aliases=None):
+ """Consolidate and sort `authors`.
+
+ Make the replacements listed in the `aliases` dict (key: canonical
+ name, value: list of aliases). If `aliases` is ``None``, default
+ to ``ALIASES``.
+
+ >>> aliases = {
+ ... 'J Doe <jdoe@a.com>':['Johnny <jdoe@b.edu>'],
+ ... 'JJJ Smith <jjjs@a.com>':['Jingly <jjjs@b.edu>'],
+ ... None:['Anonymous <a@a.com>'],
+ ... }
+ >>> _replace_aliases(['JJJ Smith <jjjs@a.com>', 'Johnny <jdoe@b.edu>',
+ ... 'Jingly <jjjs@b.edu>', 'Anonymous <a@a.com>'],
+ ... with_email=True, aliases=aliases)
+ ['J Doe <jdoe@a.com>', 'JJJ Smith <jjjs@a.com>']
+ >>> _replace_aliases(['JJJ Smith', 'Johnny', 'Jingly', 'Anonymous'],
+ ... with_email=False, aliases=aliases)
+ ['J Doe', 'JJJ Smith']
+ >>> _replace_aliases(['JJJ Smith <jjjs@a.com>', 'Johnny <jdoe@b.edu>',
+ ... 'Jingly <jjjs@b.edu>', 'J Doe <jdoe@a.com>'],
+ ... with_email=True, aliases=aliases)
+ ['J Doe <jdoe@a.com>', 'JJJ Smith <jjjs@a.com>']
+ """
+ if aliases == None:
+ aliases = ALIASES
+ if with_email == False:
+ aliases = dict([(_strip_email(author)[0], _strip_email(*_aliases))
+ for author,_aliases in aliases.items()])
+ rev_aliases = _reverse_aliases(aliases)
+ for i,author in enumerate(authors):
+ if author in rev_aliases:
+ authors[i] = rev_aliases[author]
+ authors = sorted(list(set(authors)))
+ if None in authors:
+ authors.remove(None)
+ return authors
+
+def _copyright_string(original_year, final_year, authors, prefix=''):
+ """
+ >>> print _copyright_string(original_year=2005,
+ ... final_year=2005,
+ ... authors=['A <a@a.com>', 'B <b@b.edu>'],
+ ... prefix='# '
+ ... ) # doctest: +ELLIPSIS
+ # Copyright (C) 2005 A <a@a.com>
+ # B <b@b.edu>
+ #
+ # This file...
+ >>> print _copyright_string(original_year=2005,
+ ... final_year=2009,
+ ... authors=['A <a@a.com>', 'B <b@b.edu>']
+ ... ) # doctest: +ELLIPSIS
+ Copyright (C) 2005-2009 A <a@a.com>
+ B <b@b.edu>
+ <BLANKLINE>
+ This file...
+ """
+ if original_year == final_year:
+ date_range = '%s' % original_year
+ else:
+ date_range = '%s-%s' % (original_year, final_year)
+ lines = ['Copyright (C) %s %s' % (date_range, authors[0])]
+ for author in authors[1:]:
+ lines.append(' '*(len('Copyright (C) ')+len(date_range)+1) +
+ author)
+ lines.append('')
+ lines.extend((COPY_RIGHT_TEXT % PROJECT_INFO).splitlines())
+ for i,line in enumerate(lines):
+ lines[i] = (prefix + line).rstrip()
+ return '\n'.join(lines)
+
+def _tag_copyright(contents):
+ """
+ >>> contents = '''Some file
+ ... bla bla
+ ... # Copyright (copyright begins)
+ ... # (copyright continues)
+ ... # bla bla bla
+ ... (copyright ends)
+ ... bla bla bla
+ ... '''
+ >>> print _tag_copyright(contents).replace('COPY-RIGHT', 'CR')
+ Some file
+ bla bla
+ -xyz-CR-zyx-
+ (copyright ends)
+ bla bla bla
+ <BLANKLINE>
+ """
+ lines = []
+ incopy = False
+ for line in contents.splitlines():
+ if incopy == False and line.startswith('# Copyright'):
+ incopy = True
+ lines.append(COPY_RIGHT_TAG)
+ elif incopy == True and not line.startswith('#'):
+ incopy = False
+ if incopy == False:
+ lines.append(line.rstrip('\n'))
+ return '\n'.join(lines)+'\n'
+
+def _update_copyright(contents, original_year, authors):
+ """
+ >>> contents = '''Some file
+ ... bla bla
+ ... # Copyright (copyright begins)
+ ... # (copyright continues)
+ ... # bla bla bla
+ ... (copyright ends)
+ ... bla bla bla
+ ... '''
+ >>> print _update_copyright(contents, 2008, ['Jack', 'Jill']
+ ... ) # doctest: +ELLIPSIS, +REPORT_UDIFF
+ Some file
+ bla bla
+ # Copyright (C) 2008-... Jack
+ # Jill
+ #
+ # This file...
+ (copyright ends)
+ bla bla bla
+ <BLANKLINE>
+ """
+ current_year = time.gmtime()[0]
+ copyright_string = _copyright_string(
+ original_year, current_year, authors, prefix='# ')
+ contents = _tag_copyright(contents)
+ return contents.replace(COPY_RIGHT_TAG, copyright_string)
+
+def ignored_file(filename, ignored_paths=None, ignored_files=None,
+ check_disk=True, check_vcs=True):
+ """
+ >>> ignored_paths = ['./a/', './b/']
+ >>> ignored_files = ['x', 'y']
+ >>> ignored_file('./a/z', ignored_paths, ignored_files, False, False)
+ True
+ >>> ignored_file('./ab/z', ignored_paths, ignored_files, False, False)
+ False
+ >>> ignored_file('./ab/x', ignored_paths, ignored_files, False, False)
+ True
+ >>> ignored_file('./ab/xy', ignored_paths, ignored_files, False, False)
+ False
+ >>> ignored_file('./z', ignored_paths, ignored_files, False, False)
+ False
+ """
+ if ignored_paths == None:
+ ignored_paths = IGNORED_PATHS
+ if ignored_files == None:
+ ignored_files = IGNORED_FILES
+ if check_disk == True and os.path.isfile(filename) == False:
+ return True
+ for path in ignored_paths:
+ if filename.startswith(path):
+ return True
+ if os.path.basename(filename) in ignored_files:
+ return True
+ if check_vcs == True and is_versioned(filename) == False:
+ return True
+ return False
+
+def _set_contents(filename, contents, original_contents=None, dry_run=False,
+ verbose=0):
+ if original_contents == None and os.path.isfile(filename):
+ f = open(filename, 'r')
+ original_contents = f.read()
+ f.close()
+ if verbose > 0:
+ print "checking %s ... " % filename,
+ if contents != original_contents:
+ if verbose > 0:
+ if original_contents == None:
+ print "[creating]"
+ else:
+ print "[updating]"
+ if verbose > 1 and original_contents != None:
+ print '\n'.join(
+ difflib.unified_diff(
+ original_contents.splitlines(), contents.splitlines(),
+ fromfile=os.path.normpath(os.path.join('a', filename)),
+ tofile=os.path.normpath(os.path.join('b', filename)),
+ n=3, lineterm=''))
+ if dry_run == False:
+ f = file(filename, 'w')
+ f.write(contents)
+ f.close()
+ elif verbose > 0:
+ print "[no change]"
+
+# Update commands
+
+def update_authors(authors_fn=authors_list, dry_run=False, verbose=0):
+ authors = authors_fn()
+ authors = _replace_aliases(authors, with_email=True, aliases=ALIASES)
+ new_contents = '%s was written by:\n%s\n' % (
+ PROJECT_INFO['project'],
+ '\n'.join(authors)
+ )
+ _set_contents('AUTHORS', new_contents, dry_run=dry_run, verbose=verbose)
+
+def update_file(filename, original_year_fn=original_year, authors_fn=authors,
+ dry_run=False, verbose=0):
+ f = file(filename, 'r')
+ contents = f.read()
+ f.close()
+
+ original_year = original_year_fn(filename)
+ authors = authors_fn(filename)
+ authors = _replace_aliases(authors, with_email=True, aliases=ALIASES)
+
+ new_contents = _update_copyright(contents, original_year, authors)
+ _set_contents(filename, contents=new_contents, original_contents=contents,
+ dry_run=dry_run, verbose=verbose)
+
+def update_files(files=None, dry_run=False, verbose=0):
+ if files == None or len(files) == 0:
+ files = []
+ for dirpath,dirnames,filenames in os.walk('.'):
+ for filename in filenames:
+ files.append(os.path.join(dirpath, filename))
+
+ for filename in files:
+ if ignored_file(filename) == True:
+ continue
+ update_file(filename, dry_run=dry_run, verbose=verbose)
+
+def test():
+ import doctest
+ doctest.testmod()
+
+if __name__ == '__main__':
+ import optparse
+ import sys
+
+ usage = """%%prog [options] [file ...]
+
+Update copyright information in source code with information from
+the %(vcs)s repository. Run from the %(project)s repository root.
+
+Replaces every line starting with '^# Copyright' and continuing with
+'^#' with an auto-generated copyright blurb. If you want to add
+#-commented material after a copyright blurb, please insert a blank
+line between the blurb and your comment, so the next run of
+``update_copyright.py`` doesn't clobber your comment.
+
+If no files are given, a list of files to update is generated
+automatically.
+""" % PROJECT_INFO
+ p = optparse.OptionParser(usage)
+ p.add_option('--test', dest='test', default=False,
+ action='store_true', help='Run internal tests and exit')
+ p.add_option('--dry-run', dest='dry_run', default=False,
+ action='store_true', help="Don't make any changes")
+ p.add_option('-v', '--verbose', dest='verbose', default=0,
+ action='count', help='Increment verbosity')
+ options,args = p.parse_args()
+
+ if options.test == True:
+ test()
+ sys.exit(0)
+
+ update_authors(dry_run=options.dry_run, verbose=options.verbose)
+ update_files(files=args, dry_run=options.dry_run, verbose=options.verbose)