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

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

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

    
99
main()
(7-7/40)