#!/usr/bin/python # -*- coding: utf-8 -*- ######################################################################## ## Read wiki file as parameter and write a new file. Replace any ## SEARCH macro with a corresponding DBQUERY macro if possible. ## ## Currently supported are only query searches and the most common ## search parameters. If an unsupported parameter is detected, the ## search is left untouched and a corresponding message is logged. ## ## Search parameters are left untouched and should work in most cases. ## Beware that DBQUERY supports the ~ operator only as workaround. ## For example Foobar~'Topic*' will match for FirstTopic123, too. ## For a perfect match, ^ and $ need to be added to each RHS. ## This script does _not_ do that. ## ## Further modifications: ## - Table headers are modified and centered: |*cell*| -> | *cell* | ## - Something like the following is rewritten: ## Before: $percntFORMFIELD{"Foo" topic="$formfield(Bar)"}$percnt ## After: $expand(@Bar.Foo) ## ## The script should be used with caution and conversions should be ## verified by checking the messages and comparing some results before ## deactivating the testmode. Comment/uncomment log messages as needed. ## ## This script requires Python 2.7. ## ## Stefan Hamacher, zak256@mail.de, 2018-03-28, Version 1.0 ## ######################################################################## ## Requirements import os import sys import re import logging from collections import OrderedDict ######################################################################## ## Configuration and parameters # If set to False, move new file to actual file and overwrite the latter. testmode = True # Create backup file with extra suffix before overwriting file. # Only relevant with testmode=False. do_backup = True # If the main topic file has changed, the timestamp of the # corresponding rcs file should be updated as well. touch_rcs = True # Suffixes for files suffix_newfile = "-dbquery" suffix_backupfile = "-searchbackup" # Include topic names to ignore in this list blacklist = [] # Read parameter try: filename = sys.argv[1] except IndexError: print "Usage: {} ".format(__file__) sys.exit(1) # Regex for SEARCH macro pattern_search = re.compile( r'''(?P
.*)
    %SEARCH\{\s*
    "(?P.+?)"
    (?:\s+scope="(?P.+?)"
    |\s+web="(?P.+?)"
    |\s+topic="(?P.+?)"
    |\s+excludetopic="(?P.+?)"
    |\s+type="(?P.+?)"
    |\s+casesensitive="(?P.+?)"
    |\s+nototal="(?P.+?)"
    |\s+zeroresults="(?P.+?)"
    |\s+noempty="(?P.+?)"
    |\s+nosearch="(?P.+?)"
    |\s+noheader="(?P.+?)"
    |\s+nosummary="(?P.+?)"
    |\s+multiple="(?P.+?)"
    |\s+separator="(?P.+?)"
    |\s+nonoise="(?P.+?)"
    |\s+order="(?P.+?)"
    |\s+reverse="(?P.+?)"
    |\s+date="(?P.+?)"
    |\s+limit="(?P.+?)"
    |\s+header="(?P
.+?)" |\s+footer="(?P