[Sugar-devel] [PATCH 1/3] Add models for detecting and parsing

Michael Stone michael at laptop.org
Tue Jul 6 11:39:19 EDT 2010


>>  from jarabe.model.network import GSM_USERNAME_PATH, GSM_PASSWORD_PATH, \
>>                                  GSM_NUMBER_PATH, GSM_APN_PATH, GSM_PIN_PATH, \
>>                                  GSM_PUK_PATH
>>
>> +from cpsection.modemconfiguration.config import PROVIDERS_PATH, \
>> +                                    PROVIDERS_FORMAT_SUPPORTED, \
>> +                                    COUNTRY_CODES_PATH
>> +
>>  def get_username():
>>     client =3D gconf.client_get_default()
>>     return client.get_string(GSM_USERNAME_PATH) or ''
>> @@ -68,3 +77,103 @@ def set_puk(puk):
>>     client =3D gconf.client_get_default()
>>     client.set_string(GSM_PUK_PATH, puk)
>>
>> +def has_providers_db():
>> +    if not os.path.isfile(COUNTRY_CODES_PATH):
>> +        return False
>> +    try:
>> +        tree = ElementTree(file=PROVIDERS_PATH)
>> +        elem = tree.getroot()
>> +        if elem is None or elem.get('format') != PROVIDERS_FORMAT_SUPPORTED:
>> +            return False
>> +        return True
>> +    except IOError:
>> +        return False
>
> Consider checking for file existence and readability with
> os.access(). 

As a general safety rule, it's better to open the file, catch any errors, and
do any further work via fstat(), openat(), and friends because doing so avoids
some annoying "time-of-check-to-time-of-use" (TOCTTOU) races without being any
more difficult. However, beware: when designing for a hostile environment, more
care is needed [1].

[1]: http://www.usenix.org/events/fast08/tech/tsafrir.html

>> +class CountryListStore(gtk.ListStore):
>> +    COUNTRY_CODE =3D locale.getdefaultlocale()[0][3:5].lower()
>> +
>> +    def __init__(self):
>> +        gtk.ListStore.__init__(self, str, object)
>> +        codes =3D {}
>> +        with open(COUNTRY_CODES_PATH) as codes_file:
>
> Using 'with' like that makes us depend on Python 2.6. 

Fortunately, using 'with' with a

   from __future__ import with_statement

import is Python-2.5 compatible.

Regards,

Michael


More information about the Sugar-devel mailing list