File: dime.py

package info (click to toggle)
hplip 3.16.11%2Brepack0-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 129,416 kB
  • ctags: 20,423
  • sloc: python: 78,229; ansic: 67,606; cpp: 59,753; sh: 11,285; perl: 4,353; makefile: 693
file content (111 lines) | stat: -rw-r--r-- 3,401 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# (c) Copyright 2003-2015 HP Development Company, L.P.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
# Author: Don Welch
#

# Std Lib
import struct

# Local
from .g import *

# DIME constants
TYPE_T_MIME = 0x01
TYPE_T_URI = 0x02
DIME_VERSION = 1
PAD_SIZE = 4


class Record(object):
    def __init__(self, id, typ, typ_code, payload):
        self.id = id
        self.typ = typ
        self.typ_code = typ_code
        self.payload = payload
        

class Message(object):
    def __init__(self):
        self.records = []

    def add_record(self, rec):
        self.records.append(rec)
        
    def generate(self, output): # output is a stream type
        for i, r in enumerate(self.records):
            log.debug("Processing record %d (%s)" % (i, r.id))
            mb = me = cf = 0
            if i == 0: mb = 1
            if i == len(self.records)-1: me = 1
                
            output.write(struct.pack("!B", ((DIME_VERSION & 0x1f) << 3 |
                                            (mb & 0x01) << 2 |
                                            (me & 0x01) << 1 |
                                            (cf & 0x01))))
                   
            output.write(struct.pack("!B", ((r.typ_code & 0xf) << 4) & 0xf0))
    
            output.write(struct.pack("!H", 0)) # Options length
            
            id_len = self.bytes_needed(len(r.id))
            output.write(struct.pack("!H", len(r.id))) # ID length
            
            typ_len = self.bytes_needed(len(r.typ))
            output.write(struct.pack("!H", len(r.typ))) # Type length
            
            data_len = self.bytes_needed(len(r.payload))
            output.write(struct.pack("!I", len(r.payload))) # Data length
            
            if id_len:
                output.write(struct.pack("%ds" % id_len, r.id))
                
            if typ_len:
                output.write(struct.pack("%ds" % typ_len, r.typ))
            
            if data_len:
                output.write(struct.pack("%ds" % data_len, r.payload))
        
    
    def bytes_needed(self, data_len, block_size=PAD_SIZE):
        if data_len % block_size == 0:
            return data_len
        else:
            return (int(data_len/block_size+1))*block_size
            
            


if __name__ == "__main__":
    log.set_level("debug")
    import io
    m = Message()
    m.add_record(Record("cid:id0", "http://schemas.xmlsoap.org/soap/envelope/", 
                        TYPE_T_URI, "<test>test</test>"))
    
    m.add_record(Record("test2", "text/xml", TYPE_T_MIME, "<test>test2</test>"))
    
    output = io.StringIO()
    
    m.generate(output)
    
    log.log_data(output.getvalue())