Project

General

Profile

1
#!/usr/bin/env python
2
# A DiGIR client
3

    
4
import os
5
import os.path
6
import sys
7
import urllib
8
import urllib2
9

    
10
sys.path.append(os.path.dirname(__file__)+"/../lib")
11

    
12
import dates
13
import opts
14
import streams
15
import util
16

    
17
# Config
18
timeout = 20 # sec
19
default_schema = 'http://digir.net/schema/conceptual/darwin/full/2003/1.0/darwin2full.xsd'
20

    
21
request_xml_template = '''\
22
<?xml version="1.0" encoding="UTF-8"?>
23
<request
24
    xmlns="http://digir.net/schema/protocol/2003/1.0"
25
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
26
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
27
    xmlns:digir="http://digir.net/schema/protocol/2003/1.0"
28
    xmlns:darwin="http://digir.net/schema/conceptual/darwin/2003/1.0"
29
    xmlns:dwc="http://digir.net/schema/conceptual/darwin/2003/1.0"
30
    xsi:schemaLocation="http://digir.net/schema/protocol/2003/1.0 
31
      http://digir.sourceforge.net/schema/protocol/2003/1.0/digir.xsd 
32
      http://digir.net/schema/conceptual/darwin/2003/1.0 
33
      http://digir.sourceforge.net/schema/conceptual/darwin/2003/1.0/darwin2.xsd">
34
    <header>
35
        <version>1.0</version>
36
        <sendTime>[time]</sendTime>
37
        <source>[source]</source>
38
        <destination resource="[resource]">[url]</destination>
39
        <type>search</type>
40
    </header>
41
    <search>
42
        <filter>
43
            <equals>
44
                <darwin:Kingdom>plantae</darwin:Kingdom>
45
            </equals>
46
        </filter>
47
        <records limit="[count]" start="[start]">
48
            <structure schemaLocation="[schema]"/>
49
        </records>
50
        <count>true</count>
51
    </search>
52
</request>
53
'''
54

    
55
diags_start = '<diagnostics>'
56
diags_end = '</diagnostics>'
57

    
58
def main():
59
    env_names = []
60
    def usage_err():
61
        raise SystemExit('Usage: '+opts.env_usage(env_names, True)+' '
62
            +sys.argv[0]+' 2>>log')
63
    
64
    # Get config from env vars
65
    url = opts.get_env_var('url', None, env_names)
66
    resource = opts.get_env_var('resource', None, env_names)
67
    schema = opts.get_env_var('schema', default_schema, env_names)
68
    start = util.cast(int, opts.get_env_var('start', 0, env_names))
69
    count = util.cast(int, opts.get_env_var('n', 1, env_names))
70
    debug = opts.env_flag('debug', False, env_names)
71
    if url == None or resource == None: usage_err()
72
    
73
    def clear_line(): sys.stderr.write('\n')
74
    log_indent = 0
75
    def log(msg, line_ending='\n'):
76
        sys.stderr.write(('    '*log_indent)+msg+line_ending)
77
    
78
    self_dir = os.path.dirname(__file__)
79
    source = os.popen(self_dir+"/local_ip").read().strip()
80
    this_request_xml_template = (request_xml_template
81
        .replace('[source]', source)
82
        .replace('[url]', url)
83
        .replace('[resource]', resource)
84
        .replace('[schema]', schema)
85
        .replace('[count]', str(count))
86
        )
87
    
88
    time = dates.strftime('%Y-%m-%d %H:%M:%S %Z', dates.now())
89
    request_xml = (this_request_xml_template
90
        .replace('[start]', str(start))
91
        .replace('[time]', time)
92
        )
93
    if debug: sys.stderr.write(request_xml)
94
    this_url = url+'?'+urllib.urlencode({'request': request_xml})
95
    diag_capture = streams.CaptureStream(streams.TimeoutInputStream(
96
        urllib2.urlopen(this_url), timeout), diags_start, diags_end)
97
    stream = streams.StreamIter(diag_capture)
98
    
99
    try:
100
        # Copy lines
101
        for line in stream:
102
            sys.stdout.write(line)
103
    finally: # still run if break is called
104
        stream.close()
105
        
106
        if debug: sys.stderr.write(diag_capture.match)
107

    
108
main()
(7-7/40)