From 5ee6084019aa7a8a955e6254b37d382eab683f74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dion=20H=C3=A4fner?= Date: Wed, 21 Sep 2016 13:38:48 +0200 Subject: [PATCH] fixed python 2 support --- python/CMakeLists.txt | 2 +- .../dorie/parscraper/match_parameters.py | 2 ++ .../dorie/parscraper/readers/xml.py | 2 ++ .../dorie/parscraper/wrapper/__init__.py | 0 .../dorie/parscraper/wrapper/scrape_folder.py | 4 ++++ .../dorie/parscraper/writers/html.py | 2 ++ .../dorie/parscraper/writers/ini.py | 2 ++ .../dorie/parscraper/writers/rst.py | 22 +++++++++---------- python/parscraper/wrapper/scrape_folder.py | 2 +- 9 files changed, 24 insertions(+), 14 deletions(-) create mode 100644 python/parscraper/dorie/parscraper/wrapper/__init__.py diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index bffc1f74..265ab3c0 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -1,6 +1,6 @@ function(install_python_package curdir packagelist) message(STATUS "Installing python package dorie.${curdir}") - dune_install_python_package(PATH ${curdir} ADDITIONAL_PIP_PARAMS "-r ${CMAKE_CURRENT_SOURCE_DIR}/${curdir}/requirements.txt -q") + dune_install_python_package(PATH ${curdir} ADDITIONAL_PIP_PARAMS -r ${CMAKE_CURRENT_SOURCE_DIR}/${curdir}/requirements.txt -q) set(${packagelist} ${${packagelist}} ${CMAKE_CURRENT_SOURCE_DIR}/${curdir} PARENT_SCOPE) endfunction() diff --git a/python/parscraper/dorie/parscraper/match_parameters.py b/python/parscraper/dorie/parscraper/match_parameters.py index 487c867e..2ec84c88 100644 --- a/python/parscraper/dorie/parscraper/match_parameters.py +++ b/python/parscraper/dorie/parscraper/match_parameters.py @@ -1,3 +1,5 @@ +from __future__ import absolute_import + import os from warnings import warn diff --git a/python/parscraper/dorie/parscraper/readers/xml.py b/python/parscraper/dorie/parscraper/readers/xml.py index cce02d2f..b0a6d428 100644 --- a/python/parscraper/dorie/parscraper/readers/xml.py +++ b/python/parscraper/dorie/parscraper/readers/xml.py @@ -1,3 +1,5 @@ +from __future__ import absolute_import + import xml.etree.ElementTree as ET from warnings import warn from collections import OrderedDict diff --git a/python/parscraper/dorie/parscraper/wrapper/__init__.py b/python/parscraper/dorie/parscraper/wrapper/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/python/parscraper/dorie/parscraper/wrapper/scrape_folder.py b/python/parscraper/dorie/parscraper/wrapper/scrape_folder.py index d6ee944f..96283d00 100644 --- a/python/parscraper/dorie/parscraper/wrapper/scrape_folder.py +++ b/python/parscraper/dorie/parscraper/wrapper/scrape_folder.py @@ -2,6 +2,7 @@ import os import sys import warnings import argparse +import traceback from dorie.parscraper import readers, writers, match_parameters from dorie.parscraper.warnings import OutputWarning @@ -45,6 +46,9 @@ def scrape(xml_file,source_folder,out,css=None,debug=False): raise else: warnings.warn("Output failed for file {0} with error:\n{1}".format(o,repr(e)), OutputWarning) + if debug: + exc_type, exc_value, exc_traceback = sys.exc_info() + traceback.print_exception(exc_type, exc_value, exc_traceback) else: warnings.warn("Unknown output format: .{}. Skipping output".format(file_suffix), OutputWarning) diff --git a/python/parscraper/dorie/parscraper/writers/html.py b/python/parscraper/dorie/parscraper/writers/html.py index 3094058b..ad2ce6af 100644 --- a/python/parscraper/dorie/parscraper/writers/html.py +++ b/python/parscraper/dorie/parscraper/writers/html.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import datetime import os import re diff --git a/python/parscraper/dorie/parscraper/writers/ini.py b/python/parscraper/dorie/parscraper/writers/ini.py index cb7e5ffe..fdc9ad8d 100644 --- a/python/parscraper/dorie/parscraper/writers/ini.py +++ b/python/parscraper/dorie/parscraper/writers/ini.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import datetime import os import re diff --git a/python/parscraper/dorie/parscraper/writers/rst.py b/python/parscraper/dorie/parscraper/writers/rst.py index 8cc680de..0afd0b6b 100644 --- a/python/parscraper/dorie/parscraper/writers/rst.py +++ b/python/parscraper/dorie/parscraper/writers/rst.py @@ -1,7 +1,14 @@ +from __future__ import unicode_literals + import datetime import os import re +try: + str = unicode +except NameError: + pass + from dorie.utilities.check_path import check_path from dorie.parscraper.parameter import Parameter @@ -35,10 +42,10 @@ def write(parameters,out,path_base,*args,**kwargs): check_path(out) with open(out,"w") as output: rst_document = _format_rst(parameters, headings, widths, table_content) - output.write(rst_document) + output.write(rst_document.encode('utf-8')) def _format_rst(parameters,headers,widths,content_function): - rst = "" + rst = u"" for category in parameters: rst += _format_heading(category) table_content = [content_function(p) for p in parameters[category]] @@ -49,7 +56,7 @@ def _format_table(content,headers,widths): tab = " " delimiter = "," - table = "" + table = u"" table += ".. csv-table::\n" table += tab + ":header: {}\n".format(", ".join(headers)) table += tab + ":delim: {}\n".format(delimiter) @@ -72,15 +79,6 @@ def _format_heading(text): sep = "+"*len(text) return text + "\n" + sep + "\n\n" -def _format_footer(): - # UNUSED - tab = " " - footer = "" - footer += ".. class:: center" - footer += tab + "Automatically created by the :mod:`DORiE parameter scraper `\n\n" - footer += tab + "{0:%d-%m-%Y, %H:%M}".format(datetime.datetime.today()) - return footer - def _sources(p,path_base): """ Assembles the cell text for the sources of a parameter. Since the diff --git a/python/parscraper/wrapper/scrape_folder.py b/python/parscraper/wrapper/scrape_folder.py index c3422830..0a9e4522 100644 --- a/python/parscraper/wrapper/scrape_folder.py +++ b/python/parscraper/wrapper/scrape_folder.py @@ -9,7 +9,7 @@ Script invoking the parameter scraper on a folder given via command line. """ if __name__ == "__main__": - try: # catch all exceptions we we can output an error message + try: # catch all exceptions so we can output an error message with warnings.catch_warnings(record=True) as warn: # catch all warnings so we can count them # PARSE COMMAND LINE parser = argparse.ArgumentParser() -- GitLab