~evfool/software-center/lp927426

« back to all changes in this revision

Viewing changes to softwarecenter/region.py

  • Committer: Kiwinote
  • Date: 2012-03-15 22:36:31 UTC
  • mfrom: (2867 trunk)
  • mto: This revision was merged to the branch mainline in revision 2881.
  • Revision ID: kiwinote@gmail.com-20120315223631-lvea6t5sydpkkqni
mergeĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
from gettext import dgettext
29
29
from gettext import gettext as _
30
30
 
31
 
LOG=logging.getLogger(__name__)
 
31
LOG = logging.getLogger(__name__)
32
32
 
33
33
# warning displayed if region does not match
34
34
REGION_WARNING_STRING = _("Sorry, this software is not available in your "
39
39
# blacklist this region
40
40
REGION_BLACKLIST_TAG = "blacklist-iso3166::"
41
41
 
 
42
 
42
43
def get_region_name(countrycode):
43
44
    """ return translated region name from countrycode using iso3166 """
44
45
    # find translated name
45
46
    if countrycode:
46
47
        for iso in ["iso_3166", "iso_3166_2"]:
47
 
            path = os.path.join("/usr/share/xml/iso-codes/", iso+".xml")
 
48
            path = os.path.join("/usr/share/xml/iso-codes/", iso + ".xml")
48
49
            if os.path.exists(path):
49
50
                root = xml.etree.ElementTree.parse(path)
50
51
                xpath = ".//%s_entry[@alpha_2_code='%s']" % (iso, countrycode)
56
57
                    return dgettext(iso, name)
57
58
    return ""
58
59
 
 
60
 
59
61
# the first parameter of SetRequirements
60
62
class AccuracyLevel:
61
 
 NONE = 0
62
 
 COUNTRY = 1
63
 
 REGION = 2
64
 
 LOCALITY = 3
65
 
 POSTALCODE = 4
66
 
 STREET = 5
67
 
 DETAILED = 6
 
63
    NONE = 0
 
64
    COUNTRY = 1
 
65
    REGION = 2
 
66
    LOCALITY = 3
 
67
    POSTALCODE = 4
 
68
    STREET = 5
 
69
    DETAILED = 6
 
70
 
68
71
 
69
72
class AllowedResources:
70
73
    NONE = 0
71
74
    NETWORK = 1 << 0
72
75
    CELL = 1 << 1
73
76
    GPS = 1 << 2
74
 
    ALL = (1 << 10) -1
 
77
    ALL = (1 << 10) - 1
 
78
 
75
79
 
76
80
class RegionDiscover(object):
77
81
 
93
97
 
94
98
    def _get_region_dumb(self):
95
99
        """ return dict estimate about the current countrycode/country """
96
 
        res = { 'countrycode' : '',
97
 
                'country' : '',
98
 
                }
 
100
        res = {'countrycode': '',
 
101
               'country': '',
 
102
              }
99
103
        try:
100
104
            # use LC_MONETARY as the best guess
101
105
            loc = locale.getlocale(locale.LC_MONETARY)[0]
111
115
 
112
116
    def _get_region_geoclue(self):
113
117
        """ return the dict with at least countrycode,country from a geoclue
114
 
            provider 
 
118
            provider
115
119
        """
116
120
        bus = dbus.SessionBus()
117
121
        master = bus.get_object(
118
 
            'org.freedesktop.Geoclue.Master', '/org/freedesktop/Geoclue/Master')
 
122
            'org.freedesktop.Geoclue.Master',
 
123
            '/org/freedesktop/Geoclue/Master')
119
124
        client = bus.get_object(
120
125
            'org.freedesktop.Geoclue.Master', master.Create())
121
126
        client.SetRequirements(AccuracyLevel.COUNTRY,   # (i) accuracy_level
128
133
        time, address_res, accuracy = client.GetAddress()
129
134
        return address_res
130
135
 
 
136
 
131
137
my_region = None
 
138
 
 
139
 
132
140
def get_region_cached():
133
141
    global my_region
134
142
    if my_region is None: