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

2010-07-06 Thread Michael Stone
  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
___
Sugar-devel mailing list
Sugar-devel@lists.sugarlabs.org
http://lists.sugarlabs.org/listinfo/sugar-devel


[Sugar-devel] [PATCH 1/3] Add models for detecting and parsing the providers DB.

2010-06-13 Thread Andrés Ambrois
has_providers_db() checks for the files needed and is used by the view
to decide whether to show the combo boxes.
The models are gtk.ListStore subclasses that parse the XML element they
receive as a parameter in their constructors.

Signed-off-by: Andrés Ambrois andresambr...@gmail.com

 mode change 100755 = 100644 extensions/cpsection/modemconfiguration/model.py

diff --git a/extensions/cpsection/modemconfiguration/model.py 
b/extensions/cpsection/modemconfiguration/model.py
old mode 100755
new mode 100644
index 2545ce1..42f7563
--- a/extensions/cpsection/modemconfiguration/model.py
+++ b/extensions/cpsection/modemconfiguration/model.py
@@ -15,11 +15,20 @@
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  US
 
 import gconf
+import gtk
+import os
+import locale
+from xml.etree.cElementTree import ElementTree
+from gettext import gettext as _
 
 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 = gconf.client_get_default()
 return client.get_string(GSM_USERNAME_PATH) or ''
@@ -68,3 +77,103 @@ def set_puk(puk):
 client = 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
+
+class CountryListStore(gtk.ListStore):
+COUNTRY_CODE = locale.getdefaultlocale()[0][3:5].lower()
+
+def __init__(self):
+gtk.ListStore.__init__(self, str, object)
+codes = {}
+with open(COUNTRY_CODES_PATH) as codes_file:
+for line in codes_file:
+if line.startswith('#'):
+continue
+code, name = line.split('\t')[:2]
+codes[code.lower()] = name.strip()
+etree = ElementTree(file=PROVIDERS_PATH).getroot()
+self._country_idx = None
+i = 0
+for elem in etree.findall('.//country'):
+code = elem.attrib['code']
+if code == self.COUNTRY_CODE:
+self._country_idx = i
+else:
+i += 1
+if code in codes:
+self.append((codes[code], elem))
+else:
+self.append((code, elem))
+
+def get_row_providers(self, row):
+return self[row][1]
+
+def guess_country_row(self):
+if self._country_idx is not None:
+return self._country_idx
+else:
+return -1
+
+class ProviderListStore(gtk.ListStore):
+def __init__(self, elem):
+gtk.ListStore.__init__(self, str, object)
+for provider_elem in elem.findall('.//provider'):
+apns = provider_elem.findall('.//apn')
+if not apns:
+# Skip carriers with CDMA entries only
+continue
+self.append((provider_elem.find('.//name').text, apns))
+
+def get_row_plans(self, row):
+return self[row][1]
+
+class PlanListStore(gtk.ListStore):
+LANG_NS_ATTR = '{http://www.w3.org/XML/1998/namespace}lang'
+LANG = locale.getdefaultlocale()[0][:2]
+DEFAULT_NUMBER = '*99#'
+
+def __init__(self, elems):
+gtk.ListStore.__init__(self, str, object)
+for apn_elem in elems:
+plan = {}
+names = apn_elem.findall('.//name')
+if names:
+for name in names:
+if name.get(self.LANG_NS_ATTR) is None:
+# serviceproviders.xml default value
+plan['name'] = name.text
+elif name.get(self.LANG_NS_ATTR) == self.LANG:
+# Great! We found a name value for our locale!
+plan['name'] = name.text
+break
+else:
+plan['name'] = _('Default')
+plan['apn'] = apn_elem.get('value')
+user = apn_elem.find('.//username')
+if user is not None:
+plan['username'] = user.text
+else:
+plan['username'] = ''
+passwd = apn_elem.find('.//password')
+if passwd is not None:
+plan['password'] = passwd.text
+else:
+plan['password'] = ''
+
+plan['number'] = self.DEFAULT_NUMBER
+
+self.append((plan['name'], plan))
+
+def get_row_plan(self, row):
+return