• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / z3c.rml / 16098868126

14 Apr 2025 06:50AM UTC coverage: 87.385%. Remained the same
16098868126

push

github

icemac
Back to development: 5.1

561 of 792 branches covered (70.83%)

Branch coverage included in aggregate %.

3990 of 4416 relevant lines covered (90.35%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

10.53
/src/z3c/rml/rml2pdfscript.py
1
##############################################################################
2
#
3
# Copyright (c) 2007 Zope Foundation and Contributors.
4
# All Rights Reserved.
5
#
6
# This software is subject to the provisions of the Zope Public License,
7
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
# FOR A PARTICULAR PURPOSE.
12
#
13
##############################################################################
14
"""RML to PDF Converter
15
"""
16
import os
1✔
17
import subprocess
1✔
18
import sys
1✔
19

20

21
_fileOpen = None
1✔
22

23

24
def excecuteSubProcess(xmlInputName, outputFileName, testing=None):
1✔
25
    # set the sys path given from the parent process
26
    sysPath = os.environ['Z3CRMLSYSPATH']
×
27
    sys.path[:] = sysPath.split(';')
×
28

29
    # now it come the ugly thing, but we need to hook our test changes into
30
    # our subprocess.
31
    if testing is not None:
×
32

33
        # set some globals
34
        import z3c.rml.attr
×
35
        import z3c.rml.directive
×
36
        global _fileOpen
37
        _fileOpen = z3c.rml.attr.File.open
×
38

39
        def testOpen(img, filename):
×
40
            # cleanup win paths like:
41
            # ....\\input\\file:///D:\\trunk\\...
42
            if sys.platform[:3].lower() == "win":
×
43
                if filename.startswith('file:///'):
×
44
                    filename = filename[len('file:///'):]
×
45
            path = os.path.join(os.path.dirname(xmlInputName), filename)
×
46
            return open(path, 'rb')
×
47
        # override some testing stuff for our tests
48
        z3c.rml.attr.File.open = testOpen
×
49
        import z3c.rml.tests.module
×
50
        sys.modules['module'] = z3c.rml.tests.module
×
51
        sys.modules['mymodule'] = z3c.rml.tests.module
×
52

53
    # import rml and process the pdf
54
    from z3c.rml import rml2pdf
×
55
    rml2pdf.go(xmlInputName, outputFileName)
×
56

57
    if testing is not None:
×
58
        # reset some globals
59
        z3c.rml.attr.File.open = _fileOpen
×
60
        del sys.modules['module']
×
61
        del sys.modules['mymodule']
×
62

63

64
def goSubProcess(xmlInputName, outputFileName, testing=False):
1✔
65
    """Processes PDF rendering in a sub process.
66

67
    This method is much slower then the ``go`` method defined in rml2pdf.py
68
    because each PDF generation is done in a sub process. But this will make
69
    sure, that we do not run into problems. Note, the ReportLab lib is not
70
    threadsafe.
71

72
    Use this method from python and it will dispatch the pdf generation
73
    to a subprocess.
74

75
    Note: this method does not take care on how much process will started.
76
    Probably it's a good idea to use a queue or a global utility which only
77
    start a predefined amount of sub processes.
78
    """
79
    # get the sys path used for this python process
80
    env = os.environ
×
81
    sysPath = ';'.join(sys.path)
×
82
    # set the sys path as env var for the new sub process
83
    env['Z3CRMLSYSPATH'] = sysPath
×
84
    py = sys.executable
×
85

86
    # setup the cmd
87
    program = [
×
88
        py,
89
        __file__,
90
        'excecuteSubProcess',
91
        xmlInputName,
92
        outputFileName]
93
    if testing is True:
×
94
        program.append('testing=1')
×
95
    program = " ".join(program)
×
96

97
    # run the subprocess in the rml input file folder, this will make it easy
98
    # to include images. If this doesn't fit, feel free to add a additional
99
    # home argument, and let this be the default, ri
100
    os.chdir(os.path.dirname(xmlInputName))
×
101

102
    # start processing in a sub process, raise exception or return None
103
    try:
×
104
        p = subprocess.Popen(program, executable=py, env=env,
×
105
                             stdin=subprocess.PIPE, stdout=subprocess.PIPE,
106
                             stderr=subprocess.PIPE)
107
    except Exception as e:
×
108
        raise Exception("Subprocess error: %s" % e)
×
109

110
    # Do we need to improve the implementation and kill the subprocess which
111
    # will fail? ri
112
    stdout, stderr = p.communicate()
×
113
    error = stderr
×
114
    if error:
×
115
        raise Exception("Subprocess error: %s" % error)
×
116

117

118
if __name__ == '__main__':
119
    if len(sys.argv) == 5:
120
        # testing support
121
        canvas = excecuteSubProcess(sys.argv[2], sys.argv[3], sys.argv[4])
122
    else:
123
        canvas = excecuteSubProcess(sys.argv[2], sys.argv[3])
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc