def reorder_po(path, encoding='utf-8'): p = Path(path) if p.is_dir(): for path in p.glob('**.po'): _reorder_one(str(path), encoding=encoding) else: _reorder_one(str(path), encoding=encoding)
def _check_simulation_dates(self): """ Check simulation start and end dates or timesteps Check simulation start and end dates/timesteps to be later than begin date (CalendarStartDay). If dates are used for binding[start] and binding[end], it substitutes dates with time step numbers. :return int_start, int_end :rtype tuple :raise LisfloodError if dates are not compatible """ begin = calendar(self.binding['CalendarDayStart'], self.binding['calendar_type']) int_start, str_start = datetoint(self.binding['StepStart'], self.binding) int_end, str_end = datetoint(self.binding['StepEnd'], self.binding) # test if start and end > begin if (int_start < 0) or (int_end < 0) or ((int_end - int_start) < 0): str_begin = begin.strftime("%d/%m/%Y %H:%M") msg = "Simulation start date and/or simulation end date are wrong or do not match CalendarStartDate!\n" + \ "CalendarStartDay: " + str_begin + "\n" + \ "Simulation start: " + str_start + " - " + str(int_start) + "\n" + \ "Simulation end: " + str_end + " - " + str(int_end) raise LisfloodError(msg) self.binding['StepStartInt'] = int_start self.binding['StepEndInt'] = int_end return int_start, int_end
def get_binding(self, dom): binding = {} # built-in user variables user = { 'ProjectDir': project_dir, 'ProjectPath': project_dir, 'SettingsDir': self.settings_path, 'SettingsPath': self.settings_path, } lfuse = dom.getElementsByTagName("lfuser")[0] for userset in lfuse.getElementsByTagName("textvar"): user[userset.attributes['name'].value] = str(userset.attributes['value'].value) binding[userset.attributes['name'].value] = str(userset.attributes['value'].value) # get all the binding in the last part of the settingsfile = lfbinding bind_elem = dom.getElementsByTagName("lfbinding")[0] for textvar_elem in bind_elem.getElementsByTagName("textvar"): binding[textvar_elem.attributes['name'].value] = str(textvar_elem.attributes['value'].value) # replace/add the information from lfuser to lfbinding for i in binding: expr = binding[i] while expr.find('$(') > -1: a1 = expr.find('$(') a2 = expr.find(')') try: s2 = user[expr[a1 + 2:a2]] except KeyError: print('no ', expr[a1 + 2:a2], ' in lfuser defined') else: expr = expr.replace(expr[a1:a2 + 1], s2) binding[i] = expr return user, binding
def test_raise_plus_with_an_operand(self): try: XMLTemplate('<x>${"ciao" + }</x>') assert False, 'must raise' except XMLTemplateCompileError as e: assert 'detected an invalid python expression' in str(e), e assert '"ciao" +' in str(e), e
def find_dups(directory='.', files='*.jpg', callbacks=[]): '''Given a ``directory``, goes through all files that pass through the filter ``files``, and for each one that is a duplicate, calls a number of ``callbacks``. Returns a dictionary containing the duplicates found. Example usage:: d = find_dups('some/directory', callbacks=[print_dups, KeepLarger()]) The signature for writing callbacks is (existing, dup, m), where ``existing`` and ``dup`` are paths and ``m`` is the FileExistenceManager instance. ''' from pathlib import Path store = GdbmStorageStrategy() m = FileExistenceManager(store) dups = {} for p in Path(directory).glob(files): with open(str(p), 'rb') as stream: existing = m.try_add_file(stream, str(p)) if existing: existing = existing.decode('utf-8') dups[str(p)] = existing for function in callbacks: function(Path(existing), p, m) m.close() return dups
def checkdate(start, end): """ Check simulation start and end dates or timesteps Check simulation start and end dates/timesteps to be later than begin date (CalendarStartDay). If dates are used for binding[start] and binding[end], it substitutes dates with time step numbers. :param start: start date for model run (# or date as string) :param end: end date for model run (# or date as string) """ from . import LisSettings settings = LisSettings.instance() binding = settings.binding # CM: calendar date start (CalendarDayStart) begin = calendar(binding['CalendarDayStart']) int_start, str_start = date_to_int(binding[start], True) # CM mod # CM overwrite date with time step binding[start] = int_start int_end, str_end = date_to_int(binding[end], True) # CM mod binding[end] = int_end # test if start and end > begin if int_start < 0 or int_end < 0 or (int_end - int_start) < 0: str_begin = begin.strftime("%d/%m/%Y %H:%M") msg = "Simulation start date and/or simulation end date are wrong or do not match CalendarStartDate!\n" + \ "CalendarStartDay: " + str_begin + "\n" + \ "Simulation start: " + str_start + " - " + str(int_start) + "\n" + \ "Simulation end: " + str_end + " - " + str(int_end) raise LisfloodError(msg) return
def __init__(self, model): header = "\n\n ========================== LISFLOOD Simulation Information and Setting =============================\n" msg = '' settings = LisSettings.instance() option = settings.options out_dir = settings.output_dir ens_members = settings.ens_members[0] nr_cores = settings.ncores[0] steps = len(settings.filter_steps) mode = self.modes[model.__class__.__name__] msg += "\t[X] LISFLOOD is used in the {}\n".format(mode) if option['InitLisflood']: msg += "\t[X] INITIALISATION RUN\n" if mode in (self.modes['EnsKalmanFilterFramework'], self.modes['MonteCarloFramework']) and ens_members > 1: msg += "\t[X] It uses {} ensemble members for the simulation\n".format( str(ens_members)) if mode in (self.modes['EnsKalmanFilterFramework']) and steps > 1: msg += "\t[X] The model will be updated at {} time step during the simulation\n".format( str(steps)) if mode in (self.modes['EnsKalmanFilterFramework'], self.modes['MonteCarloFramework']) and nr_cores > 1: msg += "\t[X] The simulation will try to use {} processors simultaneous\n".format( str(nr_cores)) msg += "\t[X] The simulation output as specified in the settings file can be found in {}\n".format( out_dir) self._msg = '{}{}'.format(header, msg)
def test_expr_multiline_and_IndentationError(self): try: XMLTemplate("""<div>Hello, ${ 'pippo' + 'baudo'}</div>""")().render() except XMLTemplateCompileError as e: assert "`'pippo' +\n 'baudo'`" in str(e), str(e) assert 'Hello' in str(e) assert 'baudo' in str(e)
def clone(self): templ = self.serialize() templ['name'] = self.name templ['uuid'] = str(uuid.uuid4()) for inp in templ['inputs']: inp['uuid'] = str(uuid.uuid4()) for out in templ['outputs']: out['uuid'] = str(uuid.uuid4()) new_node = self.canvasRef().createNode(templ) return new_node
def _collect(self, it): result = [] for part in it: if part is None: continue if isinstance(part, flattener): result.append(str(part.accumulate_str())) else: result.append(str(part)) if result: return ''.join(result) else: return None
def pick_one_of(options, prompt='Pick one: '): '''Lets the user pick an item by number.''' alist = options if isinstance(options, list) else list(options) c = 0 for o in alist: c += 1 print(str(c).rjust(2) + ". " + str(o)) while True: try: opt = int(input(prompt)) except ValueError: continue return alist[opt - 1]
def ChangeFloatingStyle(self, state): if not state: self.box.setStyleSheet(editableStyleSheet().getStyleSheet()) else: self.box.setStyleSheet( """QGroupBox{ background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 %s, stop: 0.6 %s, stop: 1.0 %s);}""" % ("rgba%s" % str(editableStyleSheet().ButtonsColor.getRgb()), "rgba%s" % str(editableStyleSheet().BgColorBright.getRgb()), "rgba%s" % str(editableStyleSheet().BgColorBright.getRgb())))
def linkedTo(self): """store connection from pins from left hand side to right hand side .. code-block:: python { "lhsNodeName": "", "outPinId": 0, "rhsNodeName": "", "inPinId": 0 } where pin id is order in which pin was added to node :returns: Serialized connections :rtype: list(dict) """ result = list() if self.direction == PinDirection.Output: for i in getConnectedPins(self): connection = { "lhsNodeName": "", "outPinId": 0, "rhsNodeName": "", "inPinId": 0 } connection["lhsNodeName"] = self.owningNode().getName() connection["lhsNodeUid"] = str(self.owningNode().uid) connection["outPinId"] = self.pinIndex connection["rhsNodeName"] = i.owningNode().getName() connection["rhsNodeUid"] = str(i.owningNode().uid) connection["inPinId"] = i.pinIndex result.append(connection) if self.direction == PinDirection.Input: for i in getConnectedPins(self): connection = { "lhsNodeName": "", "outPinId": 0, "rhsNodeName": "", "inPinId": 0 } connection["lhsNodeName"] = i.owningNode().getName() connection["lhsNodeUid"] = str(i.owningNode().uid) connection["outPinId"] = i.pinIndex connection["rhsNodeName"] = self.owningNode().getName() connection["rhsNodeUid"] = str(self.owningNode().uid) connection["inPinId"] = self.pinIndex result.append(connection) return result
def test_dtd(self): dtd = DocumentTypeDeclaration.by_uri[''] assert dtd.name == 'html5' assert str(dtd) == '<!DOCTYPE html>', str(dtd) assert dtd.rendering_mode == 'html5' dtd = DocumentTypeDeclaration.by_uri[None] assert dtd.name == 'xhtml5' assert str(dtd) == '<!DOCTYPE html>', str(dtd) assert dtd.rendering_mode == 'xml' dtd = DocumentTypeDeclaration.by_uri[ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"] assert dtd.name == 'xhtml1transitional' assert str(dtd) == XHTML1 assert dtd.rendering_mode == 'xml'
def regex_validator(node, value): '''Validator that ensures a regular expression can be compiled.''' try: re.compile(value) except Exception as e: raise c.Invalid(node, _("Invalid regular expression: {}") .format(str(e)))
def test_raise_unclosed_string(self): try: XMLTemplate('<x>${"ciao}</x>') assert False, 'must raise' except XMLTemplateCompileError as e: # assert "can't compile" in str(e), e # different between pypy and cpython assert '"ciao' in str(e), e
def test_multiple_nodes(self): try: XMLTemplate('<!-- a --><x>${1+1}</x><y>${1+1}</y>') except XMLTemplateParseError as e: assert 'junk after document element' in str(e), e else: assert False, 'should have raised'
def test_only_comment(self): try: XMLTemplate('<!-- a -->') except XMLTemplateParseError as e: assert 'no element found' in str(e), e else: assert False, 'should have raised'
def output(self, encoding='utf-8'): '''Returns the final Python code with the fixture functions.''' return TEMPLATE.format( encoding=encoding, when=str(datetime.utcnow())[:16], imports='\n'.join(self.imports), pk=self.pk, the_fixtures='\n'.join(self.lines), )
def addWidget(self, label=None, widget=None, maxLabelWidth=None, group=None): if widget is None or isinstance(widget, CollapsibleWidget): return False if group is not None and group != "": if group in self.groups: groupW = self.groups[group] else: groupW = CollapSibleGoupBox(group) self.groups[group] = groupW self.propertyNames[label] = widget entry = PropertyEntry(str(label), widget, hideLabel=self.hideLabels, maxLabelWidth=maxLabelWidth, toolTip=widget.toolTip()) self.entryNames[label] = entry if group is None or group == "": self.Layout.addWidget(entry) else: groupW.addWidget(entry) self.Layout.addWidget(groupW) return True
def loadmap(name): """ :param name: Variable name as defined in XML settings or a filename of a netCDF or PCRaster map load a static map either value or pcraster map or netcdf """ settings = LisSettings.instance() value = settings.binding[name] filename = value res = None flagmap = False # Try first to load the value from settings try: res = float(value) flagmap = False load = True except ValueError: try: # try to read a pcraster map res = pcraster.readmap(value) flagmap = True load = True except: load = False if not load: # read a netcdf (single one not a stack) filename = '{}.{}'.format(os.path.splitext(value)[0], 'nc') # get mapextend of netcdf map # and calculate the cutting cut0, cut1, cut2, cut3 = CutMap.get_cuts(filename) # load netcdf map but only the rectangle needed nf1 = Dataset(filename, 'r') value = listitems(nf1.variables)[-1][0] # get the last variable name mapnp = nf1.variables[value][cut2:cut3, cut0:cut1] nf1.close() # check if integer map (like outlets, lakes etc) checkint = str(mapnp.dtype) if checkint == "int16" or checkint == "int32": mapnp[mapnp.mask] = -9999 res = numpy_operations.numpy2pcr(Nominal, mapnp, -9999) elif checkint == "int8": res = numpy_operations.numpy2pcr(Nominal, mapnp, 0) else: mapnp[np.isnan(mapnp)] = -9999 res = numpy_operations.numpy2pcr(Scalar, mapnp, -9999) # if the map is a ldd if value.split('.')[0][-3:] == 'ldd': # FIXME weak...filename must contain 'ldd' string res = operations.ldd(operations.nominal(res)) flagmap = True if settings.flags['checkfiles']: checkmap(name, filename, res, flagmap, 0) return res
def replace_kwargs(value, kwargs): """Replace any keyword arguments in ``value`` with the value specified in ``kwargs``. If the keyword argument does not exist in ``kwargs``, replace with an empty string. The function can handle both strings and dictionaries. In the case of dictionaries, both the keys and values are replaced. :params value: The value to replace :type value: ``str`` or ``dict`` :param kwargs: The replacement values :type kwargs: ``dict`` :return: The value with all keyword arguments replaced """ if isinstance(value, dict): new_value = {} for key, dict_value in list(value.items()): new_value[replace_kwargs(key, kwargs)] = replace_kwargs( dict_value, kwargs) return new_value else: kwarg = re.match(KWARG_PATTERN, value) while kwarg: if kwarg.group(1) in kwargs: value = value.replace(kwarg.group(0), str(kwargs[kwarg.group(1)])) else: value = value.replace(kwarg.group(0), '') kwarg = re.match(KWARG_PATTERN, value) return value
def plot(self): """Prints all data to console. May be useful for debugging """ root = self.findRootGraph() print("Active graph: {0}".format(str(self.activeGraph().name)), "All graphs:", [g.name for g in self._graphs.values()]) root.plot()
def date_to_int(date_in, both=False): """ Get number of steps between dateIn and CalendarDayStart. Get the number of steps between dateIn and CalendarDayStart and return it as integer number. It can now compute the number of sub-daily steps. dateIn can be either a date or a number. If dateIn is a number, it must be the number of steps between dateIn and CalendarDayStart. :param date_in: date as string or number :param both: if true it returns both the number of steps as integer and the input date as string. If false only the number of steps as integer is returned :return: number of steps as integer and input date as string """ from lisvap.utils import LisSettings settings = LisSettings.instance() binding = settings.binding # CM: get reference date to be used with step numbers from 'CalendarDayStart' in Settings.xml file date1 = calendar(date_in) begin = calendar(binding['CalendarDayStart']) # CM: get model time step as float form 'DtSec' in Settings.xml file DtSec = float(binding['DtSec']) # CM: compute fraction of day corresponding to model time step as float # DtDay = float(DtSec / 86400) # Time step, expressed as fraction of day (same as self.var.DtSec and self.var.DtDay) if type(date1) is datetime.datetime: str1 = date1.strftime("%d/%m/%Y %H:%M") # CM: get total number of seconds corresponding to the time interval between dateIn and CalendarDayStart timeinterval_in_sec = int((date1 - begin).total_seconds()) # CM: get total number of steps between dateIn and CalendarDayStart int1 = int(timeinterval_in_sec / DtSec + 1) else: int1 = int(date1) str1 = str(date1) return int1, str1 if both else int1
def __iter__(self): """We convert the chunk to string because it can be of any type -- after all, the template supports expressions such as ${x+y}. Here, ``chunk`` can be the computed expression result. """ for chunk in self.__main__(): yield str(chunk)
def __init__(self, parent=None): super(PropertiesWidget, self).__init__(parent) self.setWindowTitle("Properties view") self.mainLayout = QtWidgets.QVBoxLayout(self) self.mainLayout.setObjectName("propertiesMainLayout") self.mainLayout.setContentsMargins(2, 2, 2, 2) self.searchBox = QtWidgets.QLineEdit(self) self.searchBox.setObjectName("lineEdit") self.searchBox.setPlaceholderText(str("search...")) self.searchBox.textChanged.connect(self.searchTextChanged) self.searchBoxWidget = QtWidgets.QWidget() self.searchBoxLayout = QtWidgets.QHBoxLayout(self.searchBoxWidget) self.searchBoxLayout.setContentsMargins(1, 1, 1, 1) self.searchBoxLayout.addWidget(self.searchBox) self.lockCheckBox = QtWidgets.QCheckBox() self.lockCheckBox.setStyleSheet(lockUnlockCheckboxStyle) self.searchBoxLayout.addWidget(self.lockCheckBox) self.tearOffCopy = QtWidgets.QPushButton() self.tearOffCopy.setStyleSheet("") self.tearOffCopy.setFlat(True) self.tearOffCopy.setIcon(QtGui.QIcon(RESOURCES_DIR + "/tear_off_copy.png")) self.tearOffCopy.clicked.connect(self.spawnDuplicate.emit) self.searchBoxLayout.addWidget(self.tearOffCopy) self.mainLayout.addWidget(self.searchBoxWidget) self.searchBoxWidget.hide() self.contentLayout = QtWidgets.QVBoxLayout() self.contentLayout.setSizeConstraint(QtWidgets.QLayout.SetMinAndMaxSize) self.mainLayout.addLayout(self.contentLayout) self.spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) self.mainLayout.addItem(self.spacerItem) self.mainLayout.setSizeConstraint(QtWidgets.QLayout.SetMinAndMaxSize) self.setSizePolicy(QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding))
def serialize(self): storable = self.optionEnabled(PinOptions.Storable) serializedData = None if not self.dataType == "AnyPin": if storable: serializedData = json.dumps(self.currentData(), cls=self.jsonEncoderClass()) else: serializedData = json.dumps(self.defaultValue(), cls=self.jsonEncoderClass()) data = { 'name': self.name, 'fullName': self.getName(), 'dataType': self.__class__.__name__, 'direction': int(self.direction), 'value': serializedData, 'uuid': str(self.uid), 'bDirty': self.dirty, 'linkedTo': list(self.linkedTo), 'options': [i.value for i in PinOptions if self.optionEnabled(i)], 'structure': int(self._currStructure), 'alwaysList': self._alwaysList, 'alwaysSingle': self._alwaysSingle } # Wrapper class can subscribe to this signal and return # UI specific data which will be considered on serialization # Blinker returns a tuple (receiver, return val) wrapperData = self.serializationHook.send(self) if wrapperData is not None: if len(wrapperData) > 0: # We take return value from one wrapper data['wrapper'] = wrapperData[0][1] return data
def test_request2(self): deps = self.PageDeps() deps.lib('jquery.ui') deps.lib('jquery.ui') # requiring twice should have no effect SCRIPTS_OUT = '<script type="text/javascript" ' \ 'src="/static/lib/jquery-1.7.1.min.js"></script>\n' \ '<script type="text/javascript" ' \ 'src="/static/lib/jquery-ui-1.8.16.min.js"></script>' self.assertEqual(deps.lib.tags, SCRIPTS_OUT) deps.css('deform') CSS_OUT = '<link rel="stylesheet" ' \ 'type="text/css" href="http://jquery.css" />\n' \ '<link rel="stylesheet" type="text/css" '\ 'href="http://jquery.ui.css" />\n' \ '<link rel="stylesheet" type="text/css" ' \ 'href="http://deform.css" />' self.assertEqual(deps.css.tags, CSS_OUT) ALERT = 'alert("Bruhaha");' deps.script(ALERT) deps.script(ALERT) # Repeating should have no effect ALERT_OUT = '<script type="text/javascript">' \ '\nalert("Bruhaha");\n</script>\n' self.assertEqual(deps.script.tags, ALERT_OUT) self.assertEqual(deps.top_output, CSS_OUT) self.assertEqual(deps.bottom_output, SCRIPTS_OUT + '\n' + ALERT_OUT) self.assertEqual(str(deps), '\n'.join([ CSS_OUT, SCRIPTS_OUT, ALERT_OUT]))
def test_request2(self): deps = self.PageDeps() deps.lib('jquery.ui') deps.lib('jquery.ui') # requiring twice should have no effect SCRIPTS_OUT = '<script type="text/javascript" ' \ 'src="/static/lib/jquery-1.7.1.min.js"></script>\n' \ '<script type="text/javascript" ' \ 'src="/static/lib/jquery-ui-1.8.16.min.js"></script>' self.assertEqual(deps.lib.tags, SCRIPTS_OUT) deps.css('deform') CSS_OUT = '<link rel="stylesheet" ' \ 'type="text/css" href="http://jquery.css" />\n' \ '<link rel="stylesheet" type="text/css" '\ 'href="http://jquery.ui.css" />\n' \ '<link rel="stylesheet" type="text/css" ' \ 'href="http://deform.css" />' self.assertEqual(deps.css.tags, CSS_OUT) ALERT = 'alert("Bruhaha");' deps.script(ALERT) deps.script(ALERT) # Repeating should have no effect ALERT_OUT = '<script type="text/javascript">' \ '\nalert("Bruhaha");\n</script>\n' self.assertEqual(deps.script.tags, ALERT_OUT) self.assertEqual(deps.top_output, CSS_OUT) self.assertEqual(deps.bottom_output, SCRIPTS_OUT + '\n' + ALERT_OUT) self.assertEqual(str(deps), '\n'.join([CSS_OUT, SCRIPTS_OUT, ALERT_OUT]))
def createPropertiesWidget(self, propertiesWidget): self.propertyEditor = weakref.ref(propertiesWidget) baseCategory = CollapsibleFormWidget(headName="Base") le_name = QLineEdit(self.getName()) le_name.setReadOnly(True) baseCategory.addWidget("Name", le_name) leUid = QLineEdit(str(self._rawNode.graph().name)) leUid.setReadOnly(True) baseCategory.addWidget("Owning graph", leUid) text = "{0}".format(self.packageName) if self._rawNode.lib: text += " | {0}".format(self._rawNode.lib) text += " | {0}".format(self._rawNode.__class__.__name__) leType = QLineEdit(text) leType.setReadOnly(True) baseCategory.addWidget("Type", leType) self.propertyEditor().addWidget(baseCategory) self.createInputWidgets(self.propertyEditor()) Info = CollapsibleFormWidget(headName="Info", collapsed=True) doc = QTextBrowser() doc.setOpenExternalLinks(True) doc.setHtml(self.description()) Info.addWidget(widget=doc) self.propertyEditor().addWidget(Info)
def setError(self, err): """Marks this pin as invalid by setting error message to it. Also fires event :param err: Error message :type err: str """ self._lastError = str(err) self.errorOccured.send(self._lastError)
def pick_one_of(options, prompt='Pick one: ', to_str=None): """Let the user choose an item (from a sequence of options) by number. ``to_str()`` is a callback that must take an item as argument and must return a corresponding string to be displayed. """ alist = options if isinstance(options, list) else list(options) c = 0 for o in alist: c += 1 print(str(c).rjust(2) + ". " + to_str(o) if to_str else str(o)) while True: try: opt = int(input(prompt)) except ValueError: continue return alist[opt - 1]
def test_syntax_error(self): for strip_text in (False, True): try: perform('<div py:for="i i range(1, 2)">${i}</div>', '', strip_text=strip_text) except KajikiSyntaxError as exc: assert '--> for i i range(1, 2):' in str(exc), exc else: assert False
def createVariable(self, dataType=str('AnyPin'), accessLevel=AccessLevel.public, uid=None): rawVariable = self.canvas.graphManager.activeGraph().createVariable( dataType=dataType, accessLevel=accessLevel, uid=uid) uiVariable = self.createVariableWrapperAndAddToList(rawVariable) return uiVariable
def excel_reader(stream, worksheet_name=None, required_headers=[]): '''Reads an XLSX file (from ``stream``) and yields objects so you can access the values conveniently. You can pass in the ``worksheet_name`` to be read. If not passed in or not present in the file, the first worksheet will be read. In addition, you may pass a sequence of *required_headers*, and if they aren't all present, KeyError is raised. Let's see an example. Suppose you are reading some Excel file and all you know is it contains the columns "E-mail", "Full Name" and "Gender", not necessarily in that order:: reader = excel_reader( open('contacts.xlsx', mode='rb'), worksheet_name='Mailing', required_headers=['E-mail', 'Full Name', 'Gender']) for o in reader: print(o.full_name, o.e_mail, o.gender) ''' try: wb = load_workbook(stream, data_only=True) except (BadZipFile, InvalidFileException) as e: raise Problem( _('That is not an XLSX file.'), error_title=_('Unable to read the XLSX file'), error_debug=str(e)) # Grab either the worksheet named "Assets", or simply the first one if worksheet_name and worksheet_name in wb: sheet = wb[worksheet_name] else: sheet = wb[wb.sheetnames[0]] this_is_the_first_row = True for row in sheet.rows: if this_is_the_first_row: # Read and validate the headers this_is_the_first_row = False headers = [cell.value for cell in row] raise_if_missing_required_headers(headers, required_headers) vars = get_corresponding_variable_names(headers, required_headers) index_of_var = {var: i for i, var in enumerate(vars)} class SpreadsheetRow(object): '''View on a spreadsheet row so you can access data as if they were instance variables. ''' __slots__ = ('__cells',) def __init__(self, cells): self.__cells = cells def __getattr__(self, attr): content = self.__cells[index_of_var[attr]].value return content else: yield SpreadsheetRow(row)
def test_leading_opening_brace(self): if sys.version_info[:2] == (2, 6): raise SkipTest('Python 2.6 compiler raises a different kind of error') try: XMLTemplate('<x>${{"a", "b"}</x>') assert False, 'must raise' except XMLTemplateCompileError as e: assert 'Braced expression not terminated' in str(e), e
def createVariable(self, dataType=str('BoolPin'), accessLevel=AccessLevel.public, uid=None): rawVariable = self.pyFlowInstance.graphManager.get().activeGraph( ).createVariable(dataType=dataType, accessLevel=accessLevel, uid=uid) uiVariable = self.createVariableWrapperAndAddToList(rawVariable) EditorHistory().saveState("Create variable") return uiVariable
def test_extract_python_inside_invalid(self): src = '''<xml><div>${_('hi' +)}</div></xml>''' try: x = list(i18n.extract(BytesIO(src.encode('utf-8')), [], None, { 'extract_python': True })) except XMLTemplateCompileError as e: assert "_('hi' +)" in str(e) else: assert False, 'Should have raised'
def _collect(self, it): result = [] for part in it: if part is None: continue result.append(str(part)) if result: return ''.join(result) else: return None
def content_of(paths, encoding='utf-8', sep='\n'): if isinstance(paths, Path): paths = [str(paths)] elif isinstance(paths, basestring): paths = [paths] content = [] for path in paths: with codecs.open(path, encoding=encoding) as stream: content.append(stream.read()) return sep.join(content)
def rewind(self): say = self.log.critical say('\n' + screen_header('ROLLBACK', decor='*')) # Some steps offer a warning because they cannot be rolled back. say('\n'.join(self.non_rewindable)) # Display them. if not self.rewindable: say('No steps to roll back, but the release process FAILED.') return steps = list(reversed(self.rewindable)) print('I am about to roll back the following steps:\n{0}'.format( ', '.join([str(step) for step in steps]))) if not bool_input('Continue?', default=True): return for step in steps: self.log.critical(screen_header('ROLLBACK {0}'.format(step))) try: step.rollback() except Exception as e: self.log.error('Could not roll back step {0}:\n{1}' .format(step, str(e)))
def __call__(self, existing, dup, m): if self.dups_dir is None: self.dups_dir = dup.parent / 'dups' if dup.stat().st_size > existing.stat().st_size: # Keep *dup* since it is the larger file existing.rename(self.dups_dir / dup.name) # Move the old file with open(dup, 'rb') as stream: # Update the database m.add_or_replace_file(stream, str(dup)) else: # Move *dup* since it is the shorter file dup.rename(self.dups_dir / dup.name)
def deserialize(self, node, cstruct): if cstruct in ('<colander.null>', c.null): return c.null try: result = str(cstruct) except: raise c.Invalid(node, _('${val} is not a string', mapping={'val': cstruct})) result = result.lower() if result in ('false', '0'): return False return True
def test_switch_div(self): try: tpl = perform(''' <div class="test" py:switch="5 == 3"> <p py:case="True">True</p> <p py:else="">False</p> </div>''', '<div><div>False</div></div>') except XMLTemplateCompileError as e: self.assertTrue( 'py:switch directive can only contain py:case and py:else nodes' in str(e) ) else: self.assertTrue(False, msg='Should have raised XMLTemplateParseError')
def test_package1(self): deps = self.PageDeps() deps.package('jquery.ui') deps.package('jquery.ui') # Repeating should have no effect self.assertEqual(str(deps), ''' <link rel="stylesheet" type="text/css" href="http://jquery.css" /> <link rel="stylesheet" type="text/css" href="http://jquery.ui.css" /> <script type="text/javascript" src="/static/lib/jquery-1.7.1.min.js"></script> <script type="text/javascript" src="/static/lib/jquery-ui-1.8.16.min.js">\ </script> <script type="text/javascript"> alert("JQuery UI spam!"); </script>\n'''.lstrip())
def content_of(paths, encoding='utf-8', sep='\n'): """Read, join and return the contents of ``paths``. Makes it easy to read one or many files. """ if isinstance(paths, Path): paths = [str(paths)] elif isinstance(paths, basestring): paths = [paths] content = [] for path in paths: with codecs.open(path, encoding=encoding) as stream: content.append(stream.read()) return sep.join(content)
def url(self, fragment='', **kw): """Given a dictionary, generate an actual URL from the template.""" astr = self.url_templ for param in self.params: key = ':' + param if key in self.url_templ: value = str(kw.pop(param)) astr = astr.replace(key, value) # The remaining params in kw make the query parameters if kw: astr += '?' + urlencode(kw) if fragment: astr += '#' + fragment return astr
def test_package2(self): deps = self.PageDeps() deps.package('deform') self.assertEqual(str(deps), ''' <link rel="stylesheet" type="text/css" href="http://jquery.css" /> <link rel="stylesheet" type="text/css" href="http://jquery.ui.css" /> <link rel="stylesheet" type="text/css" href="http://deform.css" /> <script type="text/javascript" src="/static/lib/jquery-1.7.1.min.js"></script> <script type="text/javascript" src="/static/lib/jquery-ui-1.8.16.min.js">\ </script> <script type="text/javascript" src="/static/lib/deform.js"></script> <script type="text/javascript"> alert("JQuery UI spam!"); alert("Deform spam!"); </script>\n'''.lstrip())
def screen_header(text, decor='=', max=79): '''Returns a header to be displayed on screen, by decorating ``text``.''' text = str(text) available = max - len(text) if available > 3: text = ' ' + text + ' ' available -= 4 else: return text req_space = len(decor) * 2 while available >= req_space: text = decor + text + decor available -= req_space if len(text) == available - len(decor): # Add just one more = text += decor # in order to fill the whole screen available -= len(decor) return text