def _parse_xml_element(dataclass_object: dataclass, xml_element: ET) -> dataclass: for key in dataclass_object.__annotations__: try: if dataclass_object.__annotations__[key] == int: dataclass_object.__dict__[key] = int( xml_element.findtext(key.replace("_", "-"))) elif dataclass_object.__annotations__[key] == Union[None, datetime]: dataclass_object.__dict__[key] = datetime.strptime( xml_element.findtext(key.replace("_", "-")), "%Y-%m-%d %H:%M:%S %Z", ) elif (dataclass_object.__annotations__[key] == Union[ None, IPv4Address, IPv6Address]): dataclass_object.__dict__[key] = ip_address( xml_element.findtext(key.replace("_", "-"))) else: dataclass_object.__dict__[key] = str( xml_element.findtext(key.replace("_", "-"))) except ValueError: continue except TypeError: continue return dataclass_object
def get_calendar_period( ctx: dataclasses.dataclass) -> ResultE[dataclasses.dataclass]: if ctx.base_date is None: ctx.base_date = timezone.localdate(timezone=pytz.timezone( ctx.house.timezone or cf.get_config('TIME_ZONE'))) ctx.start_date = ctx.base_date + relativedelta.relativedelta(days=-2) ctx.end_date = ctx.base_date + relativedelta.relativedelta(months=1, days=2) return Success(ctx)
def select_room( self, ctx: dataclasses.dataclass) -> ResultE[dataclasses.dataclass]: pk = cf.get_int_or_none(ctx.room_id) or 0 if pk <= 0: return self._error('Missed Room ID', ctx, self._case_errors.missed_room) try: data = self._rooms_repo.get(pk) except Exception as err: return self._error( f"Error select Room ID={pk} in House ID={ctx.house.id}", ctx, self._case_errors.error, exc=err) if data == Nothing: return self._error( f"Unknown Room ID={pk} in House ID={ctx.house.id}", ctx, self._case_errors.missed_room) ctx.room = data.unwrap() if ctx.room.house_id != ctx.house.id: return self._error( f"Unknown Room ID={pk} in House ID={ctx.house.id}", ctx, self._case_errors.missed_room) return Success(ctx)
def ensure_only_allowed_dataclass_keys_updated( dataclass: dataclasses.dataclass, allowed_keys: Iterable[str], ): """ Validate dataclass by raising an exception if any key not included in ``allowed_keys`` differs from the default value. Args: dataclass: Dict or dataclass to check. allowed_keys: dataclass attribute keys that can have a value different than the default one. """ default_data = dataclass.__class__() allowed_keys = set(allowed_keys) # These keys should not have been updated in the `dataclass` object prohibited_keys = set(default_data.__dict__) - allowed_keys bad_keys = [ key for key in prohibited_keys if dataclass.__dict__[key] != default_data.__dict__[key] ] if bad_keys: raise ValueError( f"Key(s) {bad_keys} are not allowed to be updated in the current context. " "Remove them from the dataclass.")
def open_source_meta(dc: dataclass, **kwargs) -> dataclass: """Open tiff given a dataclass""" full_path = dc.full_path.replace("S2", "S2metadata") full_path = full_path.rsplit(".", 1)[0] + ".geojson" dc.meta_data = open_file_from_bucket(full_path) return dc
def addValuesToDataClass(node: xml.etree.ElementTree.Element, cls: dataclass): for i in node.items(): if hasattr(cls, i[0]): setattr(cls, i[0], i[1]) if hasattr(cls, 'channel_info') and node.tag == 'CHANNEL': if cls.channel_info is None: cls.channel_info = list() chan = Channel() recurseNode(node, addValuesToDataClass, chan) cls.channel_info.append(chan)
def select_reservation(self, ctx: dataclasses.dataclass) -> ResultE[dataclasses.dataclass]: """Select reservation from Repository and check if it is acceptable""" pk = cf.get_int_or_none(ctx.pk) or 0 if pk <= 0: return self._error('Missed Reservation ID', ctx, ReservationErrors.missed_reservation) try: data = self._reservations_repo.get(pk) except Exception as err: return self._error( f"Error select Reservation ID={pk} in House ID={ctx.house.id}", ctx, ReservationErrors.error, exc=err ) if data == Nothing: return self._error( f"Unknown Reservation ID={pk} in House ID={ctx.house.id}", ctx, ReservationErrors.missed_reservation ) if hasattr(ctx, 'source'): ctx.source = data.unwrap() else: ctx.reservation = data.unwrap() return Success(ctx)
def setHeader(self, filename_root: str, header: dataclass): """ Writes out the header to the specified file Parameters ------------ filename_root : str A fully qualified path to a file with the relevant suffix at the end (e.g. ".set", ".pos" or whatever) header : dataclass See ephysiopy.dacq2py.axona_headers """ with open(filename_root, "w") as f: with redirect_stdout(f): header.print() f.write("data_start") f.write("\r\n") f.write("data_end") f.write("\r\n")
def _update_settings_from_file(opts: dataclass, argv: List[str]): """ Update given settings from a configuration file. """ config_parser = argparse.ArgumentParser(add_help=False) config_parser.add_argument('--config_file', help='Configuration file', dest='config_file', metavar='CONFIG_FILE', default='') parsed_args, next_argv = config_parser.parse_known_args(argv) if parsed_args.config_file: opts = opts.load(parsed_args.config_file) return (opts, next_argv, [config_parser]) # If no `config_file` arg, fallback to passthrough behavior. return (opts, argv, [])
def select_room_type( self, ctx: dataclasses.dataclass) -> ResultE[dataclasses.dataclass]: pk = cf.get_int_or_none(ctx.roomtype_id) or 0 if pk <= 0: return self._error('Wrong Room Type ID', ctx, self._case_errors.missed_roomtype) try: data = self._roomtypes_repo.get(ctx.house, pk, user=ctx.user) except Exception as err: return self._error(f"Error select Room Type ID={pk}", ctx, self._case_errors.error, exc=err) if data == Nothing: return self._error(f"Unknown Room Type ID={pk}", ctx, self._case_errors.missed_roomtype) ctx.room_type = data.unwrap() return Success(ctx)
def ensure_only_allowed_dataclass_keys_updated( dataclass: dataclasses.dataclass, allowed_keys: Iterable[str], ): """ Validate dataclass by raising an exception if any key not included in ``allowed_keys`` differs from the default value. A ``ValueError`` will also be raised if any of the ``allowed_keys`` is not present in ``dataclass.__dict__``. Args: dataclass: Dict or dataclass to check. allowed_keys: dataclass attribute keys that can have a value different than the default one. """ default_data = dataclass.__class__() allowed_keys = set(allowed_keys) # TODO: split keys_not_in_dict validation to a separate function. keys_not_in_dict = [key for key in allowed_keys if key not in default_data.__dict__] if keys_not_in_dict: raise ValueError( f"Key(s) {keys_not_in_dict} are not present in " f"{dataclass.__class__.__name__}. " "Remove them from `allowed_keys`. " f"Valid keys: {list(default_data.__dict__.keys())}" ) # These keys should not have been updated in the `dataclass` object prohibited_keys = set(default_data.__dict__) - allowed_keys bad_keys = [ key for key in prohibited_keys if dataclass.__dict__[key] != default_data.__dict__[key] ] if bad_keys: raise ValueError( f"Key(s) {bad_keys} are not allowed to be updated in the current context. " "Remove them from the dataclass." )
def select_house( self, ctx: dataclasses.dataclass) -> ResultE[dataclasses.dataclass]: """Select house from Repository""" house_id = cf.get_int_or_none(ctx.house_id) or 0 if house_id <= 0: return self._error('Missed House ID', ctx, self._case_errors.missed_house) try: data = self._houses_repo.get( house_id, company_id=ctx.user.company.id if hasattr(ctx, 'user') and ctx.user is not None else None) except Exception as err: return self._error(f"Error select House ID={house_id}", ctx, self._case_errors.error, exc=err) if data == Nothing: return self._error(f"Unknown House ID={house_id}", ctx, self._case_errors.missed_house) ctx.house = data.unwrap() return Success(ctx)
def select_rate_plan( self, ctx: dataclasses.dataclass) -> ResultE[dataclasses.dataclass]: plan_id = cf.get_int_or_none(ctx.plan_id) or 0 if plan_id <= 0: return self._error('Missed Rate Plan ID', ctx, self._case_errors.missed_rateplan) try: data = self._prices_repo.get_plan(ctx.house.odoo_id, plan_id, user=ctx.user) except Exception as err: return self._error( f"Error select Rate Plan ID={plan_id} for House ID={ctx.house.id}", ctx, self._case_errors.error, exc=err, ) if data == Nothing: return self._error( f"Unknown Rate Plan ID={plan_id} in House ID={ctx.house.id}", ctx, self._case_errors.missed_rateplan) ctx.rate_plan = data.unwrap() return Success(ctx)
def render_struct(struct: dataclass) -> Union[str, ConsoleRenderable]: if isinstance(struct, Player): table = Table(padding=(0, 1), expand=True, show_header=False, box=box.SIMPLE) table.add_column("Key", style="dim", justify="right") table.add_column("Value", justify="left") table.add_row("Name:", f"[green]{struct.name}") table.add_row("Description:", f"[i]{struct.description}") table.add_row("Influence Points:", f"[bold blue]{struct.influence_points}") table.add_row("Residence:", nil) return Panel.fit(table, title=struct.name, border_style="scope.border", padding=(0, 0)) if isinstance(struct, World): return render_struct(struct.territories) if isinstance(struct, dict): return render_struct(struct.values()) try: iterator = iter(struct) except TypeError: # The struct is not iterable, but also not an object we know how to parse. try: console.log( f"Unknown struct of type [b blue]{type(struct).__name__}[/b blue] received, attempting auto-render" ) return struct except NotRenderableError: raise NotImplementedError( f"Cannot render struct of type {type(struct)}") # The struct is a container from which we have fetched some item. # Build a table based on the item's type. # We will assume each instance in the collection to be of the same type. if not struct: return str(struct) if isinstance(next(iterator), Territory): title = "Territories" table = Table(padding=(0, 1), expand=True, show_header=True, box=box.SIMPLE) table.add_column("ID", style="dim", justify="right") table.add_column("Name", justify="right") table.add_column("Biome") table.add_column("Owner") table.add_column("Inhabitants") table.add_column("Neighbours", style="dim") territory: Territory for territory in struct: table.add_row( str(territory.id), territory.name or nil, territory.biome.render(), territory.owner.name if territory.owner else nil, f":kitchen_knife: {territory.id * 5:<2} :firecracker: 3" + (" :star:" if territory.id == 2 else ""), " ".join( sorted({ str(other.id) for other in territory.adjacent_territories }))) else: raise NotImplementedError(f"Cannot parse list of {type(struct[0])}") return Panel.fit(table, title=title, border_style="scope.border", padding=(0, 0))
def decorate(cls: dataclass): cls.validate = lambda self: tuple( f'Field {f.name} pattern does not match.' for f in fields(cls) if f.name in kwargs and not re.match(kwargs[f.name], getattr(self, f.name))) return cls
def open_source_tiff_meta(dc: dataclass, **kwargs) -> np.ndarray: """Open tiff given a dataclass""" with rasterio.open(dc.full_path) as f: dc.source_tiff_meta = f.meta # .tobytes() # dc.source_meta = f.meta.copy() return dc
def open_source_tiff(dc: dataclass, **kwargs) -> List: """Open tiff given a dataclass""" with rasterio.open(dc.full_path) as f: dc.source_tiff = f.read(**kwargs).tolist() # .tobytes() # dc.source_meta = f.meta.copy() return dc
def grab_dict(dc: dataclass, meta_dict: Dict) -> dataclass: """input metadata from the tiff""" dc.meta_data = meta_dict return dc
def create_dataclass_fullpath(dc: dataclass) -> dataclass: """Create the full path from the info within the dataclass""" full_path = "gs://" + dc.bucket_name + "/" + dc.filename dc.full_path = full_path return dc
def store_tiff_to_bytes(dc: dataclass) -> dataclass: """Create the full path from the info within the dataclass""" full_path = "gs://" + dc.bucket_name + "/" + dc.filename dc.full_path = full_path return dc