cover.py :  » Business-Application » hylaPEx » hylapex » library » ftp » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Business Application » hylaPEx 
hylaPEx » hylapex » library » ftp » cover.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys, tempfile

class CoverData(object):
    """ Class for rapresent cover data
    """
    def __init__(self, cover_path, **kw):
        """
        """
        
        if not os.path.exists(cover_path):
            raise ValueError("Cover %s doesn't exists!" % cover_path)
        self._cover_path = cover_path
        
        self._lst_vars = ("to", "to_company", "to_location", "to_voice_number",
            "to_fax_number", "from", "from_fax_number", "from_voice_number",
            "from_company", "from_location", "page_count", "todays_date",
            "regarding", "comments")
        
        lst_not_inside = filter(lambda x: x not in self._lst_vars, kw)
        if lst_not_inside:
            print "EEEEEEEEEEEEEEEE", lst_not_inside
        for v in self._lst_vars:
            setattr(self, v, kw.get(v, ""))
            
    
    def make_cover_fd(self):
        """
        """
    
    def make_cover_path(self):
        """ Create the fax cover and return the path
        """
        #create an empty cover
        fd, path = tempfile.mkstemp(prefix="hylapex_tmp", suffix=".ps.cover")
        f = os.fdopen(fd, "w")
        
        #add our data
        f.write(self._get_prolog_before())
        self._write_data_values(f)
        f.write(self._get_prolog_after())
        
        #and now wrote the real cover
        f.write( open(self._cover_path, "r").read() )
        f.close()
        
        #print "cover path", path
        return path
    
    def _get_cover_file_tmp(self):
        """
        """
    
    def _write_data_values(self, f):
        """ 
        """
        for attr_name in self._lst_vars:
            value = getattr(self, attr_name)
            attr_name = attr_name.replace("_", "-")
            data = "/%s (%s) def\n" % (attr_name, value)
            try:
                f.write(data)
            except UnicodeEncodeError:
                print repr(data)
                raise
            
    def _get_prolog_before(self):
        return ("""%!PS-Adobe-2.0 EPSF-2.0
%%Creator: faxcover
%%Title: HylaFAX Cover Sheet
%%CreationDate: Tue Sep 18 12:41:43 2007
%%Origin: 0 0
%%BoundingBox: 0 0 592 839
%%Pages: 1 +1
%%EndComments
%%BeginProlog
120 dict begin
/wordbreak ( ) def
/linebreak (\n) def
/doLine {
% <line> <width> <height> <x> <y> doLine <width> <height> <x> <y>
    2 copy moveto 5 -1 roll
    wordbreak
    {
        search {
            dup stringwidth pop currentpoint pop add 7 index 6 index add gt {
                6 3 roll 2 index sub 2 copy moveto 6 3 roll
            } if
            show wordbreak show
        }{
            dup stringwidth pop currentpoint pop add 5 index 4 index add gt {
                3 1 roll 3 index sub 2 copy moveto 3 -1 roll
            } if
            show exit
        } ifelse
    } loop
    2 index sub 2 copy moveto
} def
/BreakIntoLines {
% <width> <height> <x> <y> <text> BreakIntoLines
    linebreak
    {
         search {
             7 3 roll doLine 6 -2 roll
         }{
             5 1 roll doLine exit
         } ifelse
    } loop
    pop pop pop pop
} def
/BreakIntoCommentX {
% <maxlines> <text> BreakIntoCommentX -
    /cbuf (Comment ) def
    0 exch
    linebreak { search { 4 -1 roll 1 add 4 2 roll }{ exch 1 add exit } ifelse } loop
    dup dup 2 add 1 roll
    -1 1 { cbuf exch 7 exch 48 add put cbuf cvn exch def } for
    1 add exch 1 exch { cbuf exch 7 exch 48 add put cbuf cvn () def } for
} def
/XtoCommentsX {
% <X> XtoCommentsX <commentsX>
    3 string cvs (comments) dup length dup 4 1 roll
    2 index length add string dup 0 4 -1 roll
    putinterval dup 4 -2 roll putinterval
} def
/BreakIntoCommentsX {
% <maxlines> <text> BreakIntoCommentsX -
    exch 1 1 3 2 roll
    { XtoCommentsX cvn () def } for
    dup length string copy 0 1 index 0 4 1 roll
    {   linebreak 0 get eq {
            exch dup 0 3 index getinterval 4 -1 roll 1 add dup 5 1 roll
            XtoCommentsX cvn exch def dup length 2 index sub 1 sub
            2 index 1 add exch getinterval exch pop 0
        }{ 1 add } ifelse
        dup MaxLenComments gt {
            exch MaxLenComments 1 sub -1 0 {
                2 copy get wordbreak 0 get eq {
                    mark 4 1 roll
                    {   2 copy 1 add 1 index length 1 index 1 add sub
                        getinterval 5 -1 roll search { 3 -2 roll pop pop } if
                        length MaxLenComments gt { 4 -1 roll exec
                        }{ false } ifelse
                    }
                    { true }
                    5 1 roll linebreak 1 index wordbreak 7 3 roll exec
                    counttomark 1 add 4 roll cleartomark { pop exit } if
                    2 copy 1 add 0 exch getinterval 5 -1 roll
                    1 add dup 6 1 roll XtoCommentsX cvn exch def
                    2 copy 1 add 1 index length 1 index sub getinterval
                    3 -1 roll pop 3 -2 roll 1 add sub exch exit
                } if
                pop
            } for
            exch dup MaxLenComments gt {
                pop dup 0 MaxLenComments getinterval 3 -1 roll
                1 add dup 4 1 roll XtoCommentsX cvn exch def
                dup length MaxLenComments sub
                MaxLenComments exch getinterval 1
            } if
        }if
    } forall
    pop exch 1 add XtoCommentsX cvn exch def
} def
/pageWidth 209.00 def
/pageLength 296.00 def
""")


    def _get_prolog_after(self):
        return ("""/MaxComments 20 def
MaxComments comments BreakIntoCommentX
/MaxLenComments 35 def
MaxComments comments BreakIntoCommentsX
%%EndProlog
%%Page: "1" 1
%!
""")



"""
/to (%s) def
/to-company (%s) def
/to-location (%s) def
/to-voice-number (%s) def
/to-fax-number (%s) def
/from (%s) def
/from-fax-number (%s) def
/from-voice-number (%s) def
/from-company (%s) def
/from-location (%s) def
/page-count (%s) def
/todays-date (%s) def
/regarding (%s) def
/comments (%s) def
"""
        
        
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.