Exemplo n.º 1
0
 def _data(self, filtered=True, excluded=False):
     """Unfiltered data"""
     if not self.input:
         return []
     result = []
     reader = csv.reader(StringIO(getattr(self, 'input', u'')))
     rows = list(reader)  # iterate over CSV
     for row in rows:
         note = uri = None  # default empty optional values
         if len(row) < 2:
             continue  # silently ignore
         try:
             key = row[0]
             if self.KEYTYPE == date:
                 key = normalize_usa_date(key)
             value = float(row[1])
         except ValueError:
             continue  # failed to type-cast value, ignore row
         if len(row) >= 3:
             note = row[2]
         if len(row) >= 4:
             uri = row[3]
         result.append(self.POINTCLS(key, value, note, uri))
     if filtered and not excluded:
         result = filter_data(self, result)
     if excluded:
         included = filter_data(self, result)
         result = [p for p in result if p not in included]
     return result
Exemplo n.º 2
0
 def _data(self, filtered=True, excluded=False):
     """Unfiltered data"""
     if not self.input:
         return []
     result = []
     reader = csv.reader(StringIO(getattr(self, 'input', u'')))
     rows = list(reader)  # iterate over CSV
     for row in rows:
         note = uri = sample_size = None  # default empty optional values
         if len(row) < 2:
             continue  # silently ignore
         try:
             key = row[0]
             if self.KEYTYPE == date:
                 key = normalize_usa_date(key)
             value = float(row[1])
         except ValueError:
             continue  # failed to type-cast value, ignore row
         if len(row) >= 3:
             note = row[2]
         if len(row) >= 4:
             uri = row[3]
         if len(row) >= 5:
             sample_size = int(row[4])
         result.append(self.POINTCLS(key, value, note, uri, sample_size))
     if filtered and not excluded:
         result = filter_data(self, result)
     if excluded:
         included = filter_data(self, result)
         result = [p for p in result if p not in included]
     return result
Exemplo n.º 3
0
 def _normalize_date_value(self, field, data):
     """
     Normalize from data a date field; if data contains a singular
     string value, try USA mm/dd/yyyy and ISO 8601 (assume mockup widget)
     to convert, otherwise, parse the individual values for day, month,
     year from stock collective.z3cform.datetimewidget widget.
     """
     v = data.get(field.__name__, None)
     if v is None:
         return None
     v = v.strip()
     # BBB: handle possibility of USA middle-endian date, in case of
     # javascript issues in entry widget re: pasted dates.
     usadate = normalize_usa_date(data.get(field.__name__))
     if usadate is not None:
         return usadate
     # assume ISO 8601, which should be the case with mockup2 widgets
     parts = v[:10].split('-')
     if not v or len(parts) != 3:
         return None
     try:
         year = int(parts[0])
         month = int(parts[1])
         day = int(parts[2])
     except (IndexError, ValueError, TypeError):
         return None
     return date(year, month, day)
Exemplo n.º 4
0
 def normalize_value(self, field, value):
     if fieldtypes.IBool.providedBy(field):
         if isinstance(value, bool):
             return value
         return True if value.lower() == 'yes' else False
     if fieldtypes.IDate.providedBy(field):
         if isinstance(value, basestring):
             usa_date = normalize_usa_date(value)
             if usa_date is not None:
                 return usa_date  # M/D/YYYY -> date
             return date(*(map(lambda v: int(v), value.split('-'))))  # ISO
     if fieldtypes.IInt.providedBy(field):
         return int(value)
     if fieldtypes.IFloat.providedBy(field):
         return float(value)
     if fieldtypes.ISet.providedBy(field):
         return set(value)   # list of selected to set
     return value
Exemplo n.º 5
0
 def normalize_value(self, field, value):
     if fieldtypes.IBool.providedBy(field):
         if isinstance(value, bool):
             return value
         return True if value.lower() == 'yes' else False
     if fieldtypes.IDate.providedBy(field):
         if isinstance(value, basestring):
             usa_date = normalize_usa_date(value)
             if usa_date is not None:
                 return usa_date  # M/D/YYYY -> date
             return date(*(map(lambda v: int(v), value.split('-'))))  # ISO
     if fieldtypes.IInt.providedBy(field):
         return int(value)
     if fieldtypes.IFloat.providedBy(field):
         return float(value)
     if fieldtypes.ISet.providedBy(field):
         return set(value)  # list of selected to set
     return value
Exemplo n.º 6
0
 def _normalize_date_value(self, field, data):
     """
     Normalize from data a date field; if data contains a singular
     string value, try USA mm/dd/yyyy and ISO 8601 (assume mockup widget)
     to convert, otherwise, parse the individual values for day, month,
     year from stock collective.z3cform.datetimewidget widget.
     """
     v = data.get(field.__name__, None)
     if v is None:
         return None
     v = v.strip()
     # TODO: we may consider removing non-ISO support altogether TBD:
     usadate = normalize_usa_date(data.get(field.__name__))
     if usadate is not None:
         return usadate
     # assume ISO 8601, which should be the case with mockup2 widgets
     parts = v[:10].split('-')
     if not v or len(parts) != 3:
         return None
     year = int(parts[0])
     month = int(parts[1])
     day = int(parts[2])
     return date(year, month, day)
Exemplo n.º 7
0
 def _normalize_date_value(self, field, data):
     """
     Normalize from data a date field; if data contains a singular
     string value, try USA mm/dd/yyyy and ISO 8601 (assume mockup widget)
     to convert, otherwise, parse the individual values for day, month,
     year from stock collective.z3cform.datetimewidget widget.
     """
     v = data.get(field.__name__, None)
     if v is None:
         return None
     v = v.strip()
     # BBB: handle possibility of USA middle-endian date, in case of
     # javascript issues in entry widget re: pasted dates.
     usadate = normalize_usa_date(data.get(field.__name__))
     if usadate is not None:
         return usadate
     # assume ISO 8601, which should be the case with mockup2 widgets
     parts = v[:10].split('-')
     if not v or len(parts) != 3:
         return None
     year = int(parts[0])
     month = int(parts[1])
     day = int(parts[2])
     return date(year, month, day)