On 25/03/15 15:49, Denis Auroux wrote:
Hi Romano,

Actually the order of attributes does matter.


Ok, fair enough. Would be nice to remove the order dependencies for XML compliance, but it's not so urgent, I agree (I would love to be able to work on other things that would be especially nice...).

By the way, if anyone need it (it needs python 3.4 I think), the script to replace the bg is attached here (I hope it will go through the list). Run with -h to have the help and options.

Romano



--
--
Romano at http://www.rgtti.com/

#! /usr/bin/env python3
# -*- coding: utf8 -*-
#

import sys
import os
import re
import argparse

import gzip 

def warn(*what):
    if args.verbose:
        print("WARNING: ", *what, file=sys.stderr) 

def error(*what):
    print("ERROR: ", *what, file=sys.stderr)
    sys.exit(1)

# regular expressions 
page_element = r'<page.*>'
bg_element =   r'<background.*>'
bg_pdf =       r'type="pdf"'
has_pdf_file = r'filename=".+"'

# description 
parser = argparse.ArgumentParser(
        description="""change the background annotated PDF file in a Xournal file; 
        replace all PDF backgrounds with the first page of the given PDFfile. 
        It does not change any page with a non-PDF background, unless told with -a""")
# positional (mandatory) arguments
parser.add_argument('filein', metavar='filein',
                   help='Input file')
parser.add_argument('bgfile', metavar='bgfile',
                   help='Background file, must be a PDF')
parser.add_argument('fileout', metavar='fileout',
                   help='Output file')
#flag arguments
parser.add_argument("-v", "--verbose", dest='verbose', action="store_true",
                    help="increase output verbosity")
parser.add_argument("-o", "--overwrite", dest='overwrite', action="store_true", 
                    help="overwrite output file (stop otherwise)")
parser.add_argument("-a", "--all", dest='all', action="store_true", 
                    help="change all backgrounds, even native ones")

args=parser.parse_args()
full_bg_name=os.path.abspath(args.bgfile)

fin=gzip.open(args.filein, mode="rt", encoding="utf-8")
if (os.path.exists(args.fileout)):
    if not args.overwrite:
        error("File {} exists, aborted (use -o to override)".format(args.fileout))
    else:
        warn("Overwriting output file")
fout=gzip.open(args.fileout, mode="wt", encoding="utf-8")

for rline in fin:
    line=rline.strip()
    if re.match(bg_element, line) and (args.all or re.search(bg_pdf, line)):
        warn("BG  :", line)
        if re.search(has_pdf_file, line):
            newline = '<background type="pdf" domain="absolute" filename="{}" pageno="1" />'.format(full_bg_name)
        else:
            newline = '<background type="pdf" pageno="1" />'
        warn("->  :", newline)
        fout.write(newline)
        fout.write("\n")
    else:
        fout.write(rline)

fin.close()
fout.close()








------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the 
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Xournal-devel mailing list
Xournal-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/xournal-devel

Reply via email to