Example #1
0
    def test14(self):
        "test the TimeStamp behaviour"
        oinvariant = rl_config.invariant
        sden = 'SOURCE_DATE_EPOCH'
        if sden in os.environ:
            sde = os.environ[sden]
            del os.environ[sden]
        else:
            sde = self

        try:
            rl_config.invariant = False
            t = time.time()
            ts = TimeStamp()
            self.assertTrue(abs(t - ts.t) < 1)
            ts = TimeStamp(invariant=True)
            self.assertEqual(ts.t, 946684800.0)
            self.assertEqual(ts.YMDhms, (2000, 1, 1, 0, 0, 0))
            self.assertEqual(ts.tzname, 'UTC')
            os.environ[sden] = '1490003100'
            ts = TimeStamp(
                invariant=True)  #debian variable takes precedence here
            self.assertEqual(ts.t, 1490003100)
            self.assertEqual(ts.YMDhms, (2017, 3, 20, 9, 45, 0))
            self.assertEqual(ts.tzname, 'UTC')
            rl_config.invariant = True
            ts = TimeStamp()  #still takes precedence
            self.assertEqual(ts.t, 1490003100)
            self.assertEqual(ts.YMDhms, (2017, 3, 20, 9, 45, 0))
            self.assertEqual(ts.tzname, 'UTC')
            del os.environ[sden]
            ts = TimeStamp()  #now rl_config takes precedence
            self.assertEqual(ts.t, 946684800.0)
            self.assertEqual(ts.YMDhms, (2000, 1, 1, 0, 0, 0))
            self.assertEqual(ts.tzname, 'UTC')
        finally:
            if sde is not self:
                os.environ[sden] = sde
            rl_config.invariant = oinvariant
Example #2
0
#Copyright ReportLab Europe Ltd. 2000-2017
#see license.txt for license details
__version__ = '$Id$'
from tools.docco.rl_doc_utils import *
from reportlab.platypus.tableofcontents import TableOfContents
import reportlab
from reportlab.lib.utils import TimeStamp

title("ReportLab PDF Library")
title("User Guide")
centred('ReportLab Version ' + reportlab.Version)
centred(TimeStamp().datetime.strftime(
    'Document generated on %Y/%m/%d %H:%M:%S %Z'))

nextTemplate("TOC")

headingTOC()

toc = TableOfContents()
PS = ParagraphStyle
toc.levelStyles = [
    PS(fontName='Times-Bold',
       fontSize=14,
       name='TOCHeading1',
       leftIndent=20,
       firstLineIndent=-20,
       spaceBefore=5,
       leading=16),
    PS(fontSize=12,
       name='TOCHeading2',
       leftIndent=40,
Example #3
0
    def makeStory():
        story = []
        a = story.append

        styleSheet = getSampleStyleSheet()
        h1 = styleSheet['Heading1']
        h1.pageBreakBefore = 1
        h1.keepWithNext = 1

        h2 = styleSheet['Heading2']
        h2.frameBreakBefore = 1
        h2.keepWithNext = 1

        h3 = styleSheet['Heading3']
        h3.backColor = colors.cyan
        h3.keepWithNext = 1

        bt = styleSheet['BodyText']
        btj = ParagraphStyle('bodyText1j', parent=bt, alignment=TA_JUSTIFY)
        btr = ParagraphStyle('bodyText1r', parent=bt, alignment=TA_RIGHT)
        btc = ParagraphStyle('bodyText1c', parent=bt, alignment=TA_CENTER)
        from reportlab.lib.utils import TimeStamp
        ts = TimeStamp()
        a(
            Paragraph(
                """
            <a name='top'/>Subsequent pages test pageBreakBefore, frameBreakBefore and
            keepTogether attributes.  Generated at %s.  The number in brackets
            at the end of each paragraph is its position in the story. (%d)"""
                % (ts.asctime, len(story)), bt))

        for i in range(10):
            a(
                Paragraph(
                    'Heading 1 always starts a new page (%d)' % len(story),
                    h1))
            for j in range(3):
                a(
                    Paragraph(
                        'Heading1 paragraphs should always'
                        'have a page break before.  Heading 2 on the other hand'
                        'should always have a FRAME break before (%d)' %
                        len(story), bt))
                a(
                    Paragraph(
                        'Heading 2 always starts a new frame (%d)' %
                        len(story), h2))
                a(
                    Paragraph(
                        'Heading1 paragraphs should always'
                        'have a page break before.  Heading 2 on the other hand'
                        'should always have a FRAME break before (%d)' %
                        len(story), bt))
                for j in range(3):
                    a(
                        Paragraph(
                            RT((i, j, 0), theme=PYTHON, sentences=2) +
                            ' (%d)' % len(story), bt))
                    a(
                        Paragraph(
                            'I should never be at the bottom of a frame (%d)' %
                            len(story), h3))
                    a(
                        Paragraph(
                            RT((i, j, 1), theme=PYTHON, sentences=1) +
                            ' (%d)' % len(story), bt))

        for align, bts in [('left', bt), ('JUSTIFIED', btj), ('RIGHT', btr),
                           ('CENTER', btc)]:
            a(Paragraph('Now we do &lt;br/&gt; tests(align=%s)' % align, h1))
            a(Paragraph('First off no br tags', h3))
            a(Paragraph(_text1, bts))
            a(Paragraph("&lt;br/&gt; after 'the' in line 4", h3))
            a(
                Paragraph(
                    _text1.replace('forms of the', 'forms of the<br/>', 1),
                    bts))
            a(Paragraph("2*&lt;br/&gt; after 'the' in line 4", h3))
            a(
                Paragraph(
                    _text1.replace('forms of the', 'forms of the<br/><br/>',
                                   1), bts))
            a(Paragraph("&lt;br/&gt; after 'I suggested ' in line 5", h3))
            a(
                Paragraph(
                    _text1.replace('I suggested ', 'I suggested<br/>', 1),
                    bts))
            a(Paragraph("2*&lt;br/&gt; after 'I suggested ' in line 5", h3))
            a(
                Paragraph(
                    _text1.replace('I suggested ', 'I suggested<br/><br/>', 1),
                    bts))
            a(Paragraph("&lt;br/&gt; at the end of the paragraph!", h3))
            a(Paragraph("""text one<br/>text two<br/>""", bts))
            a(
                Paragraph(
                    "Border with &lt;br/&gt; at the end of the paragraph!",
                    h3))
            bt1 = ParagraphStyle('bodyText1', bts)
            bt1.borderWidth = 0.5
            bt1.borderColor = colors.toColor('red')
            bt1.backColor = colors.pink
            bt1.borderRadius = 2
            bt1.borderPadding = 3
            a(Paragraph("""text one<br/>text two<br/>""", bt1))
            a(
                Paragraph("Border no &lt;br/&gt; at the end of the paragraph!",
                          h3))
            bt1 = ParagraphStyle('bodyText1', bts)
            bt1.borderWidth = 0.5
            bt1.borderColor = colors.toColor('red')
            bt1.backColor = colors.pink
            bt1.borderRadius = 2
            bt1.borderPadding = 3
            a(Paragraph("""text one<br/>text two""", bt1))
            a(Paragraph("Different border style!", h3))
            bt2 = ParagraphStyle('bodyText1', bt1)
            bt2.borderWidth = 1.5
            bt2.borderColor = colors.toColor('blue')
            bt2.backColor = colors.gray
            bt2.borderRadius = 3
            bt2.borderPadding = 3
            a(Paragraph("""text one<br/>text two<br/>""", bt2))
        for i in 0, 1, 2:
            P = Paragraph(
                """This is a paragraph with <font color='blue'><a href='#top'>with an incredibly
long and boring link in side of it that
contains lots and lots of stupidly boring and worthless information.
So that we can split the link and see if we get problems like Dinu's.
I hope we don't, but you never do Know.</a></font>""", bt)
            a(P)
        return story
    def _prolog(self, dviPreview=None):
        pColors = {}
        spColors = {}
        sCC = ''
        cmykCC = ''
        for c in self.cmykColors:
            if hasattr(c, 'spotName') and c.spotName:  # is not None.
                if c.spotName not in spColors:
                    spColors[c.spotName] = 1
                    if sCC == '':
                        sH0 = '%%DocumentCustomColors:'
                        sH1 = '%%CMYKCustomColor:'
                    else:
                        sH0 = '%%+'
                        sH1 = '%%+'
                    sCC = '%s%s (%s)\n' % (sCC, sH0, c.spotName)
                    cmykCC = '%s%s %s (%s)\n' % (cmykCC, sH1, fp_str(
                        c.cmyk()), c.spotName)
            else:
                c, m, y, k = c.cyan, c.magenta, c.yellow, c.black  #_cmyk
                if c: pColors['cyan'] = 1
                if m: pColors['magenta'] = 1
                if y: pColors['yellow'] = 1
                if k: pColors['black'] = 1
        if pColors != {}:
            pC = '%%DocumentProcessColors:'
            for c in list(pColors.keys()):
                pC = pC + ' ' + c
            pC = pC + '\n'
        else:
            pC = ''

        if self._fontsUsed:
            _fontsUsed = '%%DocumentFonts: ' + ('\n%%+ '.join(
                self._fontsUsed)) + '\n'
        else:
            _fontsUsed = ''

        if self.delayedFonts:
            from .ttf2ps import ttf2ps
            analyticFonts = '\n%%analytic fonts\n' + ('\n'.join(
                [ttf2ps(f, self) for f in self.delayedFonts]))
        else:
            analyticFonts = ''

        return '''\
%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 0 0 ''' + ("%d %d" % (self.width, self.height)) + '''
%%Title: (''' + self.title + ''')
''' + pC + sCC + cmykCC + _fontsUsed + '''
%%CreationDate: ''' + time.strftime('(%d/%b/%Y) (%H:%M:%S GMT)',
                                    time.gmtime(TimeStamp().t)) + '''
%%Creator: ReportLab+renderPS_SEP 1.01
%%For: (''' + self.dept + ''') (''' + self.company + ''')
%%Extensions: CMYK
%%EndComments
%%BeginProlog
''' + SEP_OPS + '''
''' + (dviPreview or '') + '''
%%EndProlog
%%BeginSetup
sep_ops begin
50 dict begin % temp dict for variable definitions
%%EndSetup

/pgsave save def
/m {moveto} def
/l {lineto} def
/c {curveto} bind def
''' + analyticFonts