Completed
Push — master ( 28ce01...2d2b32 )
by
unknown
01:25
created

get_channel()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 2
rs 10
1
# coding: utf8
2
3
"""
4
This software is licensed under the Apache 2 license, quoted below.
5
6
Copyright 2014 Crystalnix Limited
7
8
Licensed under the Apache License, Version 2.0 (the "License"); you may not
9
use this file except in compliance with the License. You may obtain a copy of
10
the License at
11
12
    http://www.apache.org/licenses/LICENSE-2.0
13
14
Unless required by applicable law or agreed to in writing, software
15
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17
License for the specific language governing permissions and limitations under
18
the License.
19
"""
20
21
import os
22
23
from lxml import etree, objectify
24
from omaha.settings import DEFAULT_CHANNEL
25
26
__all__ = ['parser', 'parse_request']
27
28
29
BASE_DIR = os.path.dirname(__file__)
30
31
with open(os.path.join(BASE_DIR, 'request.xsd')) as f:
32
    schema = etree.XMLSchema(file=f)
33
34
parser = objectify.makeparser(schema=schema)
35
36
37
def parse_request(request):
38
    """
39
        >>> request = b'''<?xml version="1.0" encoding="UTF-8"?>
40
        ... <request protocol="3.0"
41
        ...          version="1.3.23.0"
42
        ...          ismachine="0"
43
        ...          sessionid="{5FAD27D4-6BFA-4daa-A1B3-5A1F821FEE0F}"
44
        ...          userid="{D0BBD725-742D-44ae-8D46-0231E881D58E}"
45
        ...          installsource="scheduler"
46
        ...          testsource="ossdev"
47
        ...          requestid="{C8F6EDF3-B623-4ee6-B2DA-1D08A0B4C665}">
48
        ...     <os platform="win" version="6.1" sp="" arch="x64"/>
49
        ...     <app appid="{430FD4D0-B729-4F61-AA34-91526481799D}" version="1.2.23.0" nextversion="" lang="en" brand="GGLS"
50
        ...          client="someclientid" installage="39">
51
        ...         <updatecheck/>
52
        ...         <ping r="1"/>
53
        ...     </app>
54
        ...     <app appid="{D0AB2EBC-931B-4013-9FEB-C9C4C2225C8C}" version="2.2.2.0" nextversion="" lang="en" brand="GGLS"
55
        ...          client="" installage="6">
56
        ...         <updatecheck/>
57
        ...         <ping r="1"/>
58
        ...     </app>
59
        ... </request>'''
60
        >>> request_obj = parse_request(request)
61
        >>> request_obj.get('version')
62
        '1.3.23.0'
63
        >>> request_obj.os.get('platform')
64
        'win'
65
        >>> request_obj.app.get('appid')
66
        '{430FD4D0-B729-4F61-AA34-91526481799D}'
67
        >>> request_obj.app.find('updatecheck')
68
        ''
69
        >>> request_obj.keys()
70
        ['protocol', 'version', 'ismachine', 'sessionid', 'userid', 'installsource', 'testsource', 'requestid']
71
        >>> request_obj.values()
72
        ['3.0', '1.3.23.0', '0', '{5FAD27D4-6BFA-4daa-A1B3-5A1F821FEE0F}', '{D0BBD725-742D-44ae-8D46-0231E881D58E}', 'scheduler', 'ossdev', '{C8F6EDF3-B623-4ee6-B2DA-1D08A0B4C665}']
73
        >>> request_obj.tag
74
        'request'
75
        >>> for app in request_obj.find('app'):
76
        ...     app.get('appid')
77
        ...
78
        '{430FD4D0-B729-4F61-AA34-91526481799D}'
79
        '{D0AB2EBC-931B-4013-9FEB-C9C4C2225C8C}'
80
    """
81
    return objectify.fromstring(request, parser)
82
83
84
def get_channel(app):
85
    return app.get('tag') or app.get('ap') or DEFAULT_CHANNEL
86