Completed
Push — master ( 3a5649...6a4ed1 )
by
unknown
42s
created

parse_request()   A

Complexity

Conditions 3

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
dl 0
loc 52
rs 9.4929
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
82
    obj = objectify.fromstring(request, parser)
83
84
    # Check if this is coming from update_engine, which handles machines not applications
85
    if obj.get('userid') == None and obj.app.get('machineid') != None:
86
        obj.set('userid', obj.app.get('machineid'))
87
88
    return obj
89
90
91
def get_channel(app):
92
    return app.get('tag') or app.get('ap') or DEFAULT_CHANNEL
93