def _parse_version4(self, ksf_data): # Standardize line breaks by dropping the carriage return ksf_data = ksf_data.replace('\r','') for section in ksf_data.split('\n\n'): m = re.search('([\w]+)(?: )?=(?: )?(.+)', section, DOTALL) if m: # Format data strings to JSON-style raw = m.group(2).strip() raw = raw.replace('True', "'True'").replace('False', "'False'") raw = raw.replace('None', "''") section = m.group(1) json = JSON(strict=False) data = json.decode(raw) if section == 'Booleans': self.booleans = data # Make sure all values are True/False for key,value in self.booleans.items(): if str(value) == 1 or value == 'True': self.booleans[key] = True elif str(value) == 0 or value == 'False': self.booleans[key] = False elif section == 'CommonStyles': self.common_styles = data if self.parse_colors: for key,value in self.common_styles.items(): self._load_child_color(self.common_styles, key, 'fore') self._load_child_color(self.common_styles, key, 'back') elif section == 'LanguageStyles': self.lang_styles = data if self.parse_colors: for key,value in self.lang_styles.items(): for lang_key,lang_value in self.lang_styles[key].items(): self._load_child_color(self.lang_styles[key], lang_key, 'fore') self._load_child_color(self.lang_styles[key], lang_key, 'back') elif section == 'MiscLanguageSettings': self.misc_lang_settings = data elif section == 'Colors': self.colors = data if self.parse_colors: for key,value in self.colors.items(): self.colors[key] = KSFColor(self.colors[key]) elif section == 'Indicators': self.indicators = data if self.parse_colors: for key,value in self.indicators.items(): self._load_child_color(self.indicators, key, 'fore') self._load_child_color(self.indicators, key, 'back')
class GoogleDataSource(object): """ Parse Google data source to table >>> source = 'http://spreadsheets.google.com/tq?key=pCQbetd-CptGXxxQIG7VFIQ&range=B1:D11&pub=1' >>> ds = GoogleDataSource() >>> ds.set_source(source) >>> ds.is_ok() True >>> ds.get_version() '0.6' >>> ds.get_titles() ['Country code', 'Population', 'Population Density'] >>> ds.get_row(1) 'IN' """ def __init__(self): self.json = JSON() def set_source(self, source, headers=None): self.source = source if headers is None: f = urllib2.urlopen(source) else: r = urllib2.Request(source, headers=headers) f = urllib2.urlopen(r) s = f.readline() self.parse(s) def parse(self, string): string = strip_query(string) string = remove_dates(string) try: self.object = self.json.decode(string) except JSONDecodeError, e: logging.info("Decode error: " + str(e)) self.object = None