Project

General

Profile

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

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

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

    
11
import opts
12
import streams
13
import util
14

    
15
# Config
16
timeout = 20 # sec
17

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

    
52
def main():
53
    env_names = []
54
    def usage_err():
55
        raise SystemExit('Usage: '+opts.env_usage(env_names, True)+' '
56
            +sys.argv[0]+' 2>>log')
57
    
58
    # Get config from env vars
59
    url = opts.get_env_var('url', None, env_names)
60
    resource = opts.get_env_var('resource', None, env_names)
61
    start = util.cast(int, opts.get_env_var('start', 0, env_names))
62
    count = util.cast(int, opts.get_env_var('n', 1, env_names))
63
    if url == None or resource == None: usage_err()
64
    
65
    this_request_xml_template = (request_xml_template.replace('[url]', url)
66
        .replace('[resource]', resource).replace('[count]', str(count)))
67
    
68
    request_xml = this_request_xml_template.replace('[start]', str(start))
69
    this_url = url+'?'+urllib.urlencode({'request': request_xml})
70
    stream = streams.StreamIter(streams.TimeoutInputStream(
71
        urllib2.urlopen(this_url), timeout))
72
    
73
    try:
74
        # Copy lines
75
        for line in stream:
76
            sys.stdout.write(line)
77
    finally: # still run if break is called
78
        stream.close()
79

    
80
main()
(7-7/39)