def initialize_database(autoinitialize=True): db_url = Configuration.database_url() if autoinitialize: SessionManager.initialize(db_url) session_factory = SessionManager.sessionmaker(db_url) _db = flask_scoped_session(session_factory, app) app._db = _db Configuration.load(_db) testing = 'TESTING' in os.environ log_level = LogConfiguration.initialize(_db, testing=testing) if app.debug is None: debug = log_level == 'DEBUG' app.debug = debug else: debug = app.debug app.config['DEBUG'] = debug _db.commit() app.log = logging.getLogger("Metadata web app") app.log.info("Application debug mode: %r", app.debug) for logger in logging.getLogger().handlers: app.log.info("Logs are going to %r", logger) # Register an error handler that logs exceptions through the # normal logging process and tries to turn them into Problem # Detail Documents. h = ErrorHandler(app, app.config['DEBUG']) @app.errorhandler(Exception) def exception_handler(exception): return h.handle(exception)
def load(cls): CoreConfiguration.load() config = CoreConfiguration.instance if not config.get(cls.POLICIES): config[cls.POLICIES] = {} if not config[cls.POLICIES].get(cls.ANALYTICS_POLICY): config[cls.POLICIES][cls.ANALYTICS_POLICY] = Analytics.initialize(["api.local_analytics_provuder"], config) cls.instance = config
class TestConfigurableOptions(ConfigurableOptions): name_one = "value1" name_two = "value2" options = [ Configuration(_id=name_one, _type=ConfigDataTypes.STRING, label=name_one), Configuration(_id=name_two, _type=ConfigDataTypes.STRING, label=name_two) ]
class SimpleService(ConfigService): name: str = "Simple" group: str = "SimpleGroup" directories: List[str] = ["/etc/quagga", "/usr/local/lib"] files: List[str] = ["test1.sh", "test2.sh"] executables: List[str] = [] dependencies: List[str] = [] startup: List[str] = [] validate: List[str] = [] shutdown: List[str] = [] validation_mode: ConfigServiceMode = ConfigServiceMode.BLOCKING default_configs: List[Configuration] = [ Configuration(_id="value1", _type=ConfigDataTypes.STRING, label="Text"), Configuration(_id="value2", _type=ConfigDataTypes.BOOL, label="Boolean"), Configuration( _id="value3", _type=ConfigDataTypes.STRING, label="Multiple Choice", options=["value1", "value2", "value3"], ), ] modes: Dict[str, Dict[str, str]] = { "mode1": { "value1": "value1", "value2": "0", "value3": "value2" }, "mode2": { "value1": "value2", "value2": "1", "value3": "value3" }, "mode3": { "value1": "value3", "value2": "0", "value3": "value1" }, } def get_text_template(self, name: str) -> str: if name == "test1.sh": return """ # sample script 1 # node id(${node.id}) name(${node.name}) # config: ${config} echo hello """ elif name == "test2.sh": return """
def put_annotations( cls, segment: Optional[Segment], seg_type: Optional[str] = None ): if seg_type is not None: segment.put_annotation("type", seg_type) for env, value in os.environ.items(): if env.startswith(cls.XRAY_ENV_ANNOTATE): name = env.replace(cls.XRAY_ENV_ANNOTATE, "").lower() segment.put_annotation(name, value) if Configuration.app_version() != Configuration.NO_APP_VERSION_FOUND: segment.put_annotation("version", Configuration.app_version())
class EmaneGlobalModel(EmaneModel): """ Global EMANE configuration options. """ _DEFAULT_DEV = "ctrl0" name = "emane" emulator_xml = "/usr/share/emane/manifest/nemmanager.xml" emulator_defaults = { "eventservicedevice": _DEFAULT_DEV, "eventservicegroup": "224.1.2.8:45703", "otamanagerdevice": _DEFAULT_DEV, "otamanagergroup": "224.1.2.8:45702" } emulator_config = emanemanifest.parse(emulator_xml, emulator_defaults) emulator_config.insert( 0, Configuration(_id="platform_id_start", _type=ConfigDataTypes.INT32, default="1", label="Starting Platform ID (core)")) nem_config = [ Configuration(_id="nem_id_start", _type=ConfigDataTypes.INT32, default="1", label="Starting NEM ID (core)") ] @classmethod def configurations(cls): return cls.emulator_config + cls.nem_config @classmethod def config_groups(cls): emulator_len = len(cls.emulator_config) config_len = len(cls.configurations()) return [ ConfigGroup("Platform Attributes", 1, emulator_len), ConfigGroup("NEM Parameters", emulator_len + 1, config_len) ] def __init__(self, session, _id=None): super(EmaneGlobalModel, self).__init__(session, _id) def build_xml_files(self, config, interface=None): raise NotImplementedError
def test_when_init_then_config_object_returned(): config = Configuration() assert isinstance(config, Configuration) assert hasattr(config, "paths") assert hasattr(config.paths, "repo_path") assert hasattr(config.paths, "code_path") assert hasattr(config, "version")
def __init__(self, args): self._args = args self._logger = logging.getLogger(__name__) self._config = Configuration() self._ofx_parser = OFXParser(self._config) self._db = Database() self._fs = FileSystem()
class EmaneBypassModel(emanemodel.EmaneModel): name = "emane_bypass" # values to ignore, when writing xml files config_ignore = {"none"} # mac definitions mac_library = "bypassmaclayer" mac_config = [ Configuration( _id="none", _type=ConfigDataTypes.BOOL, default="0", label="There are no parameters for the bypass model.", ) ] # phy definitions phy_library = "bypassphylayer" phy_config = [] @classmethod def load(cls, emane_prefix: str) -> None: # ignore default logic pass
class EmaneBypassModel(emanemodel.EmaneModel): name = "emane_bypass" # values to ignore, when writing xml files config_ignore = {"none"} # mac definitions mac_library = "bypassmaclayer" mac_config = [ Configuration( _id="none", _type=ConfigDataTypes.BOOL, default="0", options=["True", "False"], label="There are no parameters for the bypass model." ) ] # phy definitions phy_library = "bypassphylayer" phy_config = [] @classmethod def load(cls, emane_prefix): # ignore default logic pass # override config groups @classmethod def config_groups(cls): return [ ConfigGroup("Bypass Parameters", 1, 1), ]
def __init__(self, args): self._args = args self._config = Configuration() self._logger = self._configure_logger( self._config.paths.logs_path, self._config.version, self._args.task_type, self._args.runtime, )
class VpnServer(ConfigService): name = "VPNServer" group = GROUP_NAME directories = [] files = ["vpnserver.sh"] executables = ["openvpn", "ip", "killall"] dependencies = [] startup = ["sh vpnserver.sh"] validate = ["pidof openvpn"] shutdown = ["killall openvpn"] validation_mode = ConfigServiceMode.BLOCKING default_configs = [ Configuration( _id="keydir", _type=ConfigDataTypes.STRING, label="Key Dir", default="/etc/core/keys", ), Configuration( _id="keyname", _type=ConfigDataTypes.STRING, label="Key Name", default="server", ), Configuration( _id="subnet", _type=ConfigDataTypes.STRING, label="Subnet", default="10.0.200.0", ), ] modes = {} def data(self) -> Dict[str, Any]: address = None for ifc in self.node.netifs(): if getattr(ifc, "control", False): continue for x in ifc.addrlist: addr = x.split("/")[0] if netaddr.valid_ipv4(addr): address = addr return dict(address=address)
def parse(manifest_path, defaults): """ Parses a valid emane manifest file and converts the provided configuration values into ones used by core. :param str manifest_path: absolute manifest file path :param dict defaults: used to override default values for configurations :return: list of core configuration values :rtype: list """ # no results when emane bindings are not present if not manifest: return [] # load configuration file manifest_file = manifest.Manifest(manifest_path) manifest_configurations = manifest_file.getAllConfiguration() configurations = [] for config_name in sorted(manifest_configurations): config_info = manifest_file.getConfigurationInfo(config_name) # map type to internal config data type value for core config_type = config_info.get("numeric") if not config_type: config_type = config_info.get("nonnumeric") config_type_name = config_type["type"] config_type_value = _type_value(config_type_name) # get default values, using provided defaults if config_name in defaults: config_default = defaults[config_name] else: config_value = config_info["values"] config_default = _get_default(config_type_name, config_value) # map to possible values used as options within the gui config_regex = config_info.get("regex") possible = _get_possible(config_type_name, config_regex) # define description and account for gui quirks config_descriptions = config_name if config_name.endswith("uri"): config_descriptions = "%s file" % config_descriptions configuration = Configuration( _id=config_name, _type=config_type_value, default=config_default, options=possible, label=config_descriptions, ) configurations.append(configuration) return configurations
def step_impl(context, db_name, state_folder): """ :type context: behave.runner.Context """ config = Configuration() path = os.sep.join( [config.paths.db_path, state_folder, "{}*db".format(db_name)]) db_files = glob.glob(path) assert len(db_files) > 0 assert os.path.isfile(db_files[0])
class VpnServer(ConfigService): name: str = "VPNServer" group: str = GROUP_NAME directories: List[str] = [] files: List[str] = ["vpnserver.sh"] executables: List[str] = ["openvpn", "ip", "killall"] dependencies: List[str] = [] startup: List[str] = ["bash vpnserver.sh"] validate: List[str] = ["pidof openvpn"] shutdown: List[str] = ["killall openvpn"] validation_mode: ConfigServiceMode = ConfigServiceMode.BLOCKING default_configs: List[Configuration] = [ Configuration( _id="keydir", _type=ConfigDataTypes.STRING, label="Key Dir", default="/etc/core/keys", ), Configuration( _id="keyname", _type=ConfigDataTypes.STRING, label="Key Name", default="server", ), Configuration( _id="subnet", _type=ConfigDataTypes.STRING, label="Subnet", default="10.0.200.0", ), ] modes: Dict[str, Dict[str, str]] = {} def data(self) -> Dict[str, Any]: address = None for iface in self.node.get_ifaces(control=False): ip4 = iface.get_ip4() if ip4: address = str(ip4.ip) break return dict(address=address)
def load(cls, emane_prefix): cls.mac_defaults["pcrcurveuri"] = os.path.join( emane_prefix, "share/emane/xml/models/mac/tdmaeventscheduler/tdmabasemodelpcr.xml" ) super(EmaneTdmaModel, cls).load(emane_prefix) cls.mac_config.insert( 0, Configuration(_id=cls.schedule_name, _type=ConfigDataTypes.STRING, default=cls.default_schedule, label="TDMA schedule file (core)"))
def __init__(self, session: "Session") -> None: self.session = session self.nem_config = [ Configuration( _id="nem_id_start", _type=ConfigDataTypes.INT32, default="1", label="Starting NEM ID (core)", ) ] self.emulator_config = None self.parse_config()
def step_impl(context, resource_name, state_folder, db_type, db_name): config = Configuration() source_path = os.sep.join([ config.paths.test_path, "resources", context.feature.name, "database_files", db_type, resource_name, ]) dest_path = os.sep.join( [config.paths.resources_path, "databases", state_folder, db_name]) copy_file(source_path, dest_path)
def step_impl(context, input_filename, input_folder, state_folder): """ :param input_folder: the folder to look for the log file in :type context: behave.runner.Context """ config = Configuration() path = os.sep.join([ config.paths.input_path, input_folder, state_folder, "{}*".format(input_filename), ]) assert len(glob.glob(path)) > 0
def __init__(self, db, mirror, user_id=None, password=None, uploader=None, soap_client=None): self._db = db self.mirror = mirror if self.mirror: self.scaler = ImageScaler(db, [self.mirror], uploader=uploader) else: self.scaler = None integration = Configuration.integration("Content Cafe") self.user_id = user_id or integration['username'] self.password = password or integration['password'] self.log = logging.getLogger("Content Cafe API") self.soap_client = ( soap_client or ContentCafeSOAPClient(self.user_id, self.password) )
def step_impl(context, input_type, state_folder): """ THis behave step implementation will delete all files for the spcified input folder to clean up the test environment before a test is run :param input_type: represents the input folder to delete files from :type context: behave.runner.Context """ config = Configuration() input_path = os.sep.join( [config.paths.input_path, input_type, state_folder, "*"]) files = find_all_files(input_path) for filepath in files: delete_file(filepath)
def step_impl(context, db_type, state_folder): """ This behave step implementation will delete all of the databases type specified from the state folder specified to clean up the test environment before a test is run :param db_type: represents the DB type that needs to be removed :param state_folder: represents the state folder to remove the db from. must be either ["current", "backup"] :type context: behave.runner.Context """ config = Configuration() db_name = "{}*".format(db_type) path = os.sep.join([config.paths.db_path, state_folder, db_name]) files = find_all_files(path) for filepath in files: delete_file(filepath)
def __init__(self, _db, content_server=None): self._db = _db if not content_server: content_server_url = Configuration.integration_url( Configuration.CONTENT_SERVER_INTEGRATION, required=True) content_server = SimplifiedOPDSLookup(content_server_url) self.content_server = content_server self.importer = OPDSImporter(self._db, DataSource.OA_CONTENT_SERVER) input_identifier_types = [Identifier.GUTENBERG_ID, Identifier.URI] output_source = DataSource.lookup( self._db, DataSource.OA_CONTENT_SERVER ) super(ContentServerCoverageProvider, self).__init__( "OA Content Server Coverage Provider", input_identifier_types, output_source, batch_size=10 )
def parse_config(self) -> None: emane_prefix = self.session.options.get_config( "emane_prefix", default=DEFAULT_EMANE_PREFIX ) emulator_xml = os.path.join(emane_prefix, "share/emane/manifest/nemmanager.xml") emulator_defaults = { "eventservicedevice": DEFAULT_DEV, "eventservicegroup": "224.1.2.8:45703", "otamanagerdevice": DEFAULT_DEV, "otamanagergroup": "224.1.2.8:45702", } self.emulator_config = emanemanifest.parse(emulator_xml, emulator_defaults) self.emulator_config.insert( 0, Configuration( _id="platform_id_start", _type=ConfigDataTypes.INT32, default="1", label="Starting Platform ID (core)", ), )
def step_impl(context, filename, input_type, state_folder): """ This behave step implementation will place the required test resource file in the spcified input folder as part of the test setup phase :param filename: the name of the resource to load from the test/resources folder :param input_type: represents the input folder to place the file resource in :type context: behave.runner.Context """ config = Configuration() source_path = os.sep.join([ config.paths.test_path, "resources", context.feature.name, "input_files", filename, ]) dest_path = os.sep.join( [config.paths.input_path, input_type, state_folder, filename]) copy_file(source_path, dest_path)
def __init__(self, session: "Session") -> None: self.session: "Session" = session self.core_config: List[Configuration] = [ Configuration( _id="platform_id_start", _type=ConfigDataTypes.INT32, default="1", label="Starting Platform ID", ), Configuration( _id="nem_id_start", _type=ConfigDataTypes.INT32, default="1", label="Starting NEM ID", ), Configuration( _id="link_enabled", _type=ConfigDataTypes.BOOL, default="1", label="Enable Links?", ), Configuration( _id="loss_threshold", _type=ConfigDataTypes.INT32, default="30", label="Link Loss Threshold (%)", ), Configuration( _id="link_interval", _type=ConfigDataTypes.INT32, default="1", label="Link Check Interval (sec)", ), Configuration( _id="link_timeout", _type=ConfigDataTypes.INT32, default="4", label="Link Timeout (sec)", ), ] self.emulator_config = None self.parse_config()
# mean_valid_df = norm(mean_valid_df) # Converting to numpy mice_train_data = mice_train_df.to_numpy() # mice_valid_data = mice_valid_df.to_numpy() mice_test_data = mice_test_df.to_numpy() knn_train_data = knn_train_df.to_numpy() # knn_valid_data = knn_valid_df.to_numpy() knn_test_data = knn_test_df.to_numpy() mean_train_data = mean_train_df.to_numpy() # mean_valid_data = mean_valid_df.to_numpy() mean_test_data = mean_test_df.to_numpy() conf = Configuration() mice_train_dataset = prepare_dataset(mice_train_data, mice_train_labels, conf) # mice_valid_dataset = prepare_dataset(mice_valid_data, mice_valid_labels, conf) knn_train_dataset = prepare_dataset(knn_train_data, knn_train_labels, conf) # knn_valid_dataset = prepare_dataset(knn_valid_data, knn_valid_labels, conf) mean_train_dataset = prepare_dataset(mean_train_data, mean_train_labels, conf) # mean_valid_dataset = prepare_dataset(mean_valid_data, mice_valid_labels, conf) # datasets = [ # [mice_train_dataset, mice_valid_dataset, mice_test_data, 'mice_model'], # [knn_train_dataset, knn_valid_dataset, knn_test_data, 'knn_model'], # [mean_train_dataset, mean_valid_dataset, mean_test_data, 'mean_model'] # ]
def load(cls): CoreConfiguration.load() cls.instance = CoreConfiguration.instance
def from_config(cls, _db): config = Configuration.integration(Configuration.NOVELIST_INTEGRATION) profile, password = cls.values() if not (profile and password): raise ValueError("No NoveList client configured.") return cls(_db, profile, password)
class EmaneModel(WirelessModel): """ EMANE models inherit from this parent class, which takes care of handling configuration messages based on the list of configurable parameters. Helper functions also live here. """ # default mac configuration settings mac_library = None mac_xml = None mac_defaults = {} mac_config = [] # default phy configuration settings, using the universal model phy_library = None phy_xml = "emanephy.xml" phy_defaults = { "subid": "1", "propagationmodel": "2ray", "noisemode": "none" } phy_config = [] # support for external configurations external_config = [ Configuration("external", ConfigDataTypes.BOOL, default="0"), Configuration("platformendpoint", ConfigDataTypes.STRING, default="127.0.0.1:40001"), Configuration("transportendpoint", ConfigDataTypes.STRING, default="127.0.0.1:50002"), ] config_ignore = set() @classmethod def load(cls, emane_prefix): """ Called after being loaded within the EmaneManager. Provides configured emane_prefix for parsing xml files. :param str emane_prefix: configured emane prefix path :return: nothing """ manifest_path = "share/emane/manifest" # load mac configuration mac_xml_path = os.path.join(emane_prefix, manifest_path, cls.mac_xml) cls.mac_config = emanemanifest.parse(mac_xml_path, cls.mac_defaults) # load phy configuration phy_xml_path = os.path.join(emane_prefix, manifest_path, cls.phy_xml) cls.phy_config = emanemanifest.parse(phy_xml_path, cls.phy_defaults) @classmethod def configurations(cls): """ Returns the combination all all configurations (mac, phy, and external). :return: all configurations :rtype: list[Configuration] """ return cls.mac_config + cls.phy_config + cls.external_config @classmethod def config_groups(cls): """ Returns the defined configuration groups. :return: list of configuration groups. :rtype: list[ConfigGroup] """ mac_len = len(cls.mac_config) phy_len = len(cls.phy_config) + mac_len config_len = len(cls.configurations()) return [ ConfigGroup("MAC Parameters", 1, mac_len), ConfigGroup("PHY Parameters", mac_len + 1, phy_len), ConfigGroup("External Parameters", phy_len + 1, config_len), ] def build_xml_files(self, config, interface=None): """ Builds xml files for this emane model. Creates a nem.xml file that points to both mac.xml and phy.xml definitions. :param dict config: emane model configuration for the node and interface :param interface: interface for the emane node :return: nothing """ nem_name = emanexml.nem_file_name(self, interface) mac_name = emanexml.mac_file_name(self, interface) phy_name = emanexml.phy_file_name(self, interface) # check if this is external transport_type = "virtual" if interface and interface.transport_type == "raw": transport_type = "raw" transport_name = emanexml.transport_file_name(self.id, transport_type) # create nem xml file nem_file = os.path.join(self.session.session_dir, nem_name) emanexml.create_nem_xml(self, config, nem_file, transport_name, mac_name, phy_name) # create mac xml file mac_file = os.path.join(self.session.session_dir, mac_name) emanexml.create_mac_xml(self, config, mac_file) # create phy xml file phy_file = os.path.join(self.session.session_dir, phy_name) emanexml.create_phy_xml(self, config, phy_file) def post_startup(self): """ Logic to execute after the emane manager is finished with startup. :return: nothing """ logging.debug("emane model(%s) has no post setup tasks", self.name) def update(self, moved, moved_netifs): """ Invoked from MobilityModel when nodes are moved; this causes emane location events to be generated for the nodes in the moved list, making EmaneModels compatible with Ns2ScriptedMobility. :param bool moved: were nodes moved :param list moved_netifs: interfaces that were moved :return: """ try: wlan = self.session.get_node(self.id) wlan.setnempositions(moved_netifs) except CoreError: logging.exception("error during update") def linkconfig( self, netif, bw=None, delay=None, loss=None, duplicate=None, jitter=None, netif2=None, ): """ Invoked when a Link Message is received. Default is unimplemented. :param core.nodes.interface.Veth netif: interface one :param bw: bandwidth to set to :param delay: packet delay to set to :param loss: packet loss to set to :param duplicate: duplicate percentage to set to :param jitter: jitter to set to :param core.netns.vif.Veth netif2: interface two :return: nothing """ logging.warning("emane model(%s) does not support link configuration", self.name)
def browser(request): browser = request.config.getoption('--browser') if not browser: browser = config.get_config('BROWSER') return browser
#!/usr/bin/env python from core.config import Configuration from core.weather import Weather import core.signals as signals import sys if __name__ == '__main__': signals.setHandler() config = Configuration(path='./config') weather = Weather(config.getLocation(),config.getKey(),False) try: interfaces = config.getType() except Exception: print "Wrong delimeter in config file: [screen] type. Should be ','" sys.exit(1) if interfaces.find("kindle") != -1: from interfaces.kindle.remote import RemoteDisplay from interfaces.kindle.screen import Screen dimensions = config.getDimensions() font = config.getFont() screen = Screen(weather,dimensions,font=font) kindle = RemoteDisplay(screen.path) kindle.auto() if interfaces.find("web") != -1: pass
class BasicRangeModel(WirelessModel): """ Basic Range wireless model, calculates range between nodes and links and unlinks nodes based on this distance. This was formerly done from the GUI. """ name: str = "basic_range" options: List[Configuration] = [ Configuration( _id="range", _type=ConfigDataTypes.UINT32, default="275", label="wireless range (pixels)", ), Configuration( _id="bandwidth", _type=ConfigDataTypes.UINT64, default="54000000", label="bandwidth (bps)", ), Configuration( _id="jitter", _type=ConfigDataTypes.UINT64, default="0", label="transmission jitter (usec)", ), Configuration( _id="delay", _type=ConfigDataTypes.UINT64, default="5000", label="transmission delay (usec)", ), Configuration( _id="error", _type=ConfigDataTypes.STRING, default="0", label="loss (%)" ), ] @classmethod def config_groups(cls): return [ConfigGroup("Basic Range Parameters", 1, len(cls.configurations()))] def __init__(self, session: "Session", _id: int) -> None: """ Create a BasicRangeModel instance. :param session: related core session :param _id: object id """ super().__init__(session, _id) self.session: "Session" = session self.wlan: WlanNode = session.get_node(_id, WlanNode) self._netifs: Dict[CoreInterface, Tuple[float, float, float]] = {} self._netifslock: threading.Lock = threading.Lock() self.range: int = 0 self.bw: Optional[int] = None self.delay: Optional[int] = None self.loss: Optional[float] = None self.jitter: Optional[int] = None def _get_config(self, current_value: int, config: Dict[str, str], name: str) -> int: """ Convenience for updating value to use from a provided configuration. :param current_value: current config value to use when one is not provided :param config: config to get values from :param name: name of config value to get :return: current config value when not provided, new value otherwise """ value = config.get(name) if value is not None: if value == "": value = None else: value = int(float(value)) else: value = current_value return value def setlinkparams(self) -> None: """ Apply link parameters to all interfaces. This is invoked from WlanNode.setmodel() after the position callback has been set. """ with self._netifslock: for netif in self._netifs: options = LinkOptions( bandwidth=self.bw, delay=self.delay, per=self.loss, jitter=self.jitter, ) self.wlan.linkconfig(netif, options) def get_position(self, netif: CoreInterface) -> Tuple[float, float, float]: """ Retrieve network interface position. :param netif: network interface position to retrieve :return: network interface position """ with self._netifslock: return self._netifs[netif] def set_position(self, netif: CoreInterface) -> None: """ A node has moved; given an interface, a new (x,y,z) position has been set; calculate the new distance between other nodes and link or unlink node pairs based on the configured range. :param netif: network interface to set position for :return: nothing """ x, y, z = netif.node.position.get() self._netifslock.acquire() self._netifs[netif] = (x, y, z) if x is None or y is None: self._netifslock.release() return for netif2 in self._netifs: self.calclink(netif, netif2) self._netifslock.release() position_callback = set_position def update(self, moved: List[CoreNode], moved_netifs: List[CoreInterface]) -> None: """ Node positions have changed without recalc. Update positions from node.position, then re-calculate links for those that have moved. Assumes bidirectional links, with one calculation per node pair, where one of the nodes has moved. :param moved: moved nodes :param moved_netifs: moved network interfaces :return: nothing """ with self._netifslock: while len(moved_netifs): netif = moved_netifs.pop() nx, ny, nz = netif.node.getposition() if netif in self._netifs: self._netifs[netif] = (nx, ny, nz) for netif2 in self._netifs: if netif2 in moved_netifs: continue self.calclink(netif, netif2) def calclink(self, netif: CoreInterface, netif2: CoreInterface) -> None: """ Helper used by set_position() and update() to calculate distance between two interfaces and perform linking/unlinking. Sends link/unlink messages and updates the WlanNode's linked dict. :param netif: interface one :param netif2: interface two :return: nothing """ if netif == netif2: return try: x, y, z = self._netifs[netif] x2, y2, z2 = self._netifs[netif2] if x2 is None or y2 is None: return d = self.calcdistance((x, y, z), (x2, y2, z2)) # ordering is important, to keep the wlan._linked dict organized a = min(netif, netif2) b = max(netif, netif2) with self.wlan._linked_lock: linked = self.wlan.linked(a, b) if d > self.range: if linked: logging.debug("was linked, unlinking") self.wlan.unlink(a, b) self.sendlinkmsg(a, b, unlink=True) else: if not linked: logging.debug("was not linked, linking") self.wlan.link(a, b) self.sendlinkmsg(a, b) except KeyError: logging.exception("error getting interfaces during calclinkS") @staticmethod def calcdistance( p1: Tuple[float, float, float], p2: Tuple[float, float, float] ) -> float: """ Calculate the distance between two three-dimensional points. :param p1: point one :param p2: point two :return: distance petween the points """ a = p1[0] - p2[0] b = p1[1] - p2[1] c = 0 if p1[2] is not None and p2[2] is not None: c = p1[2] - p2[2] return math.hypot(math.hypot(a, b), c) def update_config(self, config: Dict[str, str]) -> None: """ Configuration has changed during runtime. :param config: values to update configuration :return: nothing """ self.range = self._get_config(self.range, config, "range") if self.range is None: self.range = 0 logging.debug("wlan %s set range to %s", self.wlan.name, self.range) self.bw = self._get_config(self.bw, config, "bandwidth") self.delay = self._get_config(self.delay, config, "delay") self.loss = self._get_config(self.loss, config, "error") self.jitter = self._get_config(self.jitter, config, "jitter") self.setlinkparams() def create_link_data( self, interface1: CoreInterface, interface2: CoreInterface, message_type: MessageFlags, ) -> LinkData: """ Create a wireless link/unlink data message. :param interface1: interface one :param interface2: interface two :param message_type: link message type :return: link data """ color = self.session.get_link_color(self.wlan.id) return LinkData( message_type=message_type, node1_id=interface1.node.id, node2_id=interface2.node.id, network_id=self.wlan.id, link_type=LinkTypes.WIRELESS, color=color, ) def sendlinkmsg( self, netif: CoreInterface, netif2: CoreInterface, unlink: bool = False ) -> None: """ Send a wireless link/unlink API message to the GUI. :param netif: interface one :param netif2: interface two :param unlink: unlink or not :return: nothing """ message_type = MessageFlags.DELETE if unlink else MessageFlags.ADD link_data = self.create_link_data(netif, netif2, message_type) self.session.broadcast_link(link_data) def all_link_data(self, flags: MessageFlags = MessageFlags.NONE) -> List[LinkData]: """ Return a list of wireless link messages for when the GUI reconnects. :param flags: link flags :return: all link data """ all_links = [] with self.wlan._linked_lock: for a in self.wlan._linked: for b in self.wlan._linked[a]: if self.wlan._linked[a][b]: all_links.append(self.create_link_data(a, b, flags)) return all_links
def values(cls): config = Configuration.integration(Configuration.NOVELIST_INTEGRATION) profile = config.get(Configuration.NOVELIST_PROFILE) password = config.get(Configuration.NOVELIST_PASSWORD) return (profile, password)
def __init__(self): self.base_url = Configuration.integration_url(self.SERVICE_NAME)
def __init__(self, _db): data_dir = Configuration.data_directory() self.fast = FASTNames.from_data_directory(data_dir) self.lcsh = LCSHNames.from_data_directory(data_dir) self.fast = self.lcsh = {} super(FASTAwareSubjectAssignmentMonitor, self).__init__(_db)
def load(cls, _db=None): CoreConfiguration.load(_db) cls.instance = CoreConfiguration.instance return cls.instance
class BasicRangeModel(WirelessModel): """ Basic Range wireless model, calculates range between nodes and links and unlinks nodes based on this distance. This was formerly done from the GUI. """ name = "basic_range" options = [ Configuration(_id="range", _type=ConfigDataTypes.UINT32, default="275", label="wireless range (pixels)"), Configuration(_id="bandwidth", _type=ConfigDataTypes.UINT64, default="54000000", label="bandwidth (bps)"), Configuration(_id="jitter", _type=ConfigDataTypes.UINT64, default="0", label="transmission jitter (usec)"), Configuration(_id="delay", _type=ConfigDataTypes.UINT64, default="5000", label="transmission delay (usec)"), Configuration(_id="error", _type=ConfigDataTypes.STRING, default="0", label="error rate (%)") ] @classmethod def config_groups(cls): return [ ConfigGroup("Basic Range Parameters", 1, len(cls.configurations())) ] def __init__(self, session, _id): """ Create a BasicRangeModel instance. :param core.session.Session session: related core session :param int _id: object id :param dict config: values """ super(BasicRangeModel, self).__init__(session=session, _id=_id) self.session = session self.wlan = session.get_node(_id) self._netifs = {} self._netifslock = threading.Lock() self.range = None self.bw = None self.delay = None self.loss = None self.jitter = None def values_from_config(self, config): """ Values to convert to link parameters. :param dict config: values to convert :return: nothing """ self.range = int(float(config["range"])) logging.info("basic range model configured for WLAN %d using range %d", self.wlan.id, self.range) self.bw = int(config["bandwidth"]) if self.bw == 0: self.bw = None self.delay = int(config["delay"]) if self.delay == 0: self.delay = None self.loss = int(config["error"]) if self.loss == 0: self.loss = None self.jitter = int(config["jitter"]) if self.jitter == 0: self.jitter = None def setlinkparams(self): """ Apply link parameters to all interfaces. This is invoked from WlanNode.setmodel() after the position callback has been set. """ with self._netifslock: for netif in self._netifs: self.wlan.linkconfig(netif, bw=self.bw, delay=self.delay, loss=self.loss, duplicate=None, jitter=self.jitter) def get_position(self, netif): """ Retrieve network interface position. :param netif: network interface position to retrieve :return: network interface position """ with self._netifslock: return self._netifs[netif] def set_position(self, netif, x=None, y=None, z=None): """ A node has moved; given an interface, a new (x,y,z) position has been set; calculate the new distance between other nodes and link or unlink node pairs based on the configured range. :param netif: network interface to set position for :param x: x position :param y: y position :param z: z position :return: nothing """ self._netifslock.acquire() self._netifs[netif] = (x, y, z) if x is None or y is None: self._netifslock.release() return for netif2 in self._netifs: self.calclink(netif, netif2) self._netifslock.release() position_callback = set_position def update(self, moved, moved_netifs): """ Node positions have changed without recalc. Update positions from node.position, then re-calculate links for those that have moved. Assumes bidirectional links, with one calculation per node pair, where one of the nodes has moved. :param bool moved: flag is it was moved :param list moved_netifs: moved network interfaces :return: nothing """ with self._netifslock: while len(moved_netifs): netif = moved_netifs.pop() nx, ny, nz = netif.node.getposition() if netif in self._netifs: self._netifs[netif] = (nx, ny, nz) for netif2 in self._netifs: if netif2 in moved_netifs: continue self.calclink(netif, netif2) def calclink(self, netif, netif2): """ Helper used by set_position() and update() to calculate distance between two interfaces and perform linking/unlinking. Sends link/unlink messages and updates the WlanNode's linked dict. :param netif: interface one :param netif2: interface two :return: nothing """ if netif == netif2: return try: x, y, z = self._netifs[netif] x2, y2, z2 = self._netifs[netif2] if x2 is None or y2 is None: return d = self.calcdistance((x, y, z), (x2, y2, z2)) # ordering is important, to keep the wlan._linked dict organized a = min(netif, netif2) b = max(netif, netif2) with self.wlan._linked_lock: linked = self.wlan.linked(a, b) if d > self.range: if linked: logging.debug("was linked, unlinking") self.wlan.unlink(a, b) self.sendlinkmsg(a, b, unlink=True) else: if not linked: logging.debug("was not linked, linking") self.wlan.link(a, b) self.sendlinkmsg(a, b) except KeyError: logging.exception("error getting interfaces during calclinkS") @staticmethod def calcdistance(p1, p2): """ Calculate the distance between two three-dimensional points. :param tuple p1: point one :param tuple p2: point two :return: distance petween the points :rtype: float """ a = p1[0] - p2[0] b = p1[1] - p2[1] c = 0 if p1[2] is not None and p2[2] is not None: c = p1[2] - p2[2] return math.hypot(math.hypot(a, b), c) def update_config(self, config): """ Configuration has changed during runtime. :param dict config: values to update configuration :return: nothing """ self.values_from_config(config) self.setlinkparams() return True def create_link_data(self, interface1, interface2, message_type): """ Create a wireless link/unlink data message. :param core.coreobj.PyCoreNetIf interface1: interface one :param core.coreobj.PyCoreNetIf interface2: interface two :param message_type: link message type :return: link data :rtype: LinkData """ return LinkData(message_type=message_type, node1_id=interface1.node.id, node2_id=interface2.node.id, network_id=self.wlan.id, link_type=LinkTypes.WIRELESS.value) def sendlinkmsg(self, netif, netif2, unlink=False): """ Send a wireless link/unlink API message to the GUI. :param core.nodes.interface.CoreInterface netif: interface one :param core.nodes.interface.CoreInterface netif2: interface two :param bool unlink: unlink or not :return: nothing """ if unlink: message_type = MessageFlags.DELETE.value else: message_type = MessageFlags.ADD.value link_data = self.create_link_data(netif, netif2, message_type) self.session.broadcast_link(link_data) def all_link_data(self, flags): """ Return a list of wireless link messages for when the GUI reconnects. :param flags: link flags :return: all link data :rtype: list """ all_links = [] with self.wlan._linked_lock: for a in self.wlan._linked: for b in self.wlan._linked[a]: if self.wlan._linked[a][b]: all_links.append(self.create_link_data(a, b, flags)) return all_links
from core.model import ( ConfigurationSetting, ExternalIntegration as EI, get_one_or_create, production_session, ) log = logging.getLogger(name="Metadata Wrangler configuration import") def log_import(integration_or_setting): log.info("CREATED: %r" % integration_or_setting) _db = production_session() try: Configuration.load() shadowcat_conf = Configuration.integration('Shadowcat') if shadowcat_conf and shadowcat_conf.get('url'): shadowcat = EI( name=EI.NYPL_SHADOWCAT, protocol=EI.NYPL_SHADOWCAT, goal=EI.METADATA_GOAL ) _db.add(shadowcat) shadowcat.url = shadowcat_conf.get('url') log_import(shadowcat) content_cafe_conf = Configuration.integration('Content Cafe') if content_cafe_conf: content_cafe = EI(
class Ns2ScriptedMobility(WayPointMobility): """ Handles the ns-2 script format, generated by scengen/setdest or BonnMotion. """ name: str = "ns2script" options: List[Configuration] = [ Configuration( _id="file", _type=ConfigDataTypes.STRING, label="mobility script file" ), Configuration( _id="refresh_ms", _type=ConfigDataTypes.UINT32, default="50", label="refresh time (ms)", ), Configuration( _id="loop", _type=ConfigDataTypes.BOOL, default="1", label="loop" ), Configuration( _id="autostart", _type=ConfigDataTypes.STRING, label="auto-start seconds (0.0 for runtime)", ), Configuration( _id="map", _type=ConfigDataTypes.STRING, label="node mapping (optional, e.g. 0:1,1:2,2:3)", ), Configuration( _id="script_start", _type=ConfigDataTypes.STRING, label="script file to run upon start", ), Configuration( _id="script_pause", _type=ConfigDataTypes.STRING, label="script file to run upon pause", ), Configuration( _id="script_stop", _type=ConfigDataTypes.STRING, label="script file to run upon stop", ), ] @classmethod def config_groups(cls) -> List[ConfigGroup]: return [ ConfigGroup("ns-2 Mobility Script Parameters", 1, len(cls.configurations())) ] def __init__(self, session: "Session", _id: int) -> None: """ Creates a Ns2ScriptedMobility instance. :param session: CORE session instance :param _id: object id """ super().__init__(session, _id) self.file: Optional[str] = None self.refresh_ms: Optional[int] = None self.loop: Optional[bool] = None self.autostart: Optional[str] = None self.nodemap: Dict[int, int] = {} self.script_start: Optional[str] = None self.script_pause: Optional[str] = None self.script_stop: Optional[str] = None def update_config(self, config: Dict[str, str]) -> None: self.file = config["file"] logging.info( "ns-2 scripted mobility configured for WLAN %d using file: %s", self.id, self.file, ) self.refresh_ms = int(config["refresh_ms"]) self.loop = config["loop"].lower() == "on" self.autostart = config["autostart"] self.parsemap(config["map"]) self.script_start = config["script_start"] self.script_pause = config["script_pause"] self.script_stop = config["script_stop"] self.readscriptfile() self.copywaypoints() self.setendtime() def readscriptfile(self) -> None: """ Read in mobility script from a file. This adds waypoints to a priority queue, sorted by waypoint time. Initial waypoints are stored in a separate dict. :return: nothing """ filename = self.findfile(self.file) try: f = open(filename, "r") except IOError: logging.exception( "ns-2 scripted mobility failed to load file: %s", self.file ) return logging.info("reading ns-2 script file: %s", filename) ln = 0 ix = iy = iz = None inodenum = None for line in f: ln += 1 if line[:2] != "$n": continue try: if line[:8] == "$ns_ at ": if ix is not None and iy is not None: self.addinitial(self.map(inodenum), ix, iy, iz) ix = iy = iz = None # waypoints: # $ns_ at 1.00 "$node_(6) setdest 500.0 178.0 25.0" parts = line.split() time = float(parts[2]) nodenum = parts[3][1 + parts[3].index("(") : parts[3].index(")")] x = float(parts[5]) y = float(parts[6]) z = None speed = float(parts[7].strip('"')) self.addwaypoint(time, self.map(nodenum), x, y, z, speed) elif line[:7] == "$node_(": # initial position (time=0, speed=0): # $node_(6) set X_ 780.0 parts = line.split() nodenum = parts[0][1 + parts[0].index("(") : parts[0].index(")")] if parts[2] == "X_": if ix is not None and iy is not None: self.addinitial(self.map(inodenum), ix, iy, iz) ix = iy = iz = None ix = float(parts[3]) elif parts[2] == "Y_": iy = float(parts[3]) elif parts[2] == "Z_": iz = float(parts[3]) self.addinitial(self.map(nodenum), ix, iy, iz) ix = iy = iz = None inodenum = nodenum else: raise ValueError except ValueError: logging.exception( "skipping line %d of file %s '%s'", ln, self.file, line ) continue if ix is not None and iy is not None: self.addinitial(self.map(inodenum), ix, iy, iz) def findfile(self, file_name: str) -> str: """ Locate a script file. If the specified file doesn't exist, look in the same directory as the scenario file, or in the default configs directory (~/.core/configs). This allows for sample files without absolute path names. :param file_name: file name to find :return: absolute path to the file """ if os.path.exists(file_name): return file_name if self.session.file_name is not None: d = os.path.dirname(self.session.file_name) sessfn = os.path.join(d, file_name) if os.path.exists(sessfn): return sessfn if self.session.user is not None: userfn = os.path.join( "/home", self.session.user, ".core", "configs", file_name ) if os.path.exists(userfn): return userfn return file_name def parsemap(self, mapstr: str) -> None: """ Parse a node mapping string, given as a configuration parameter. :param mapstr: mapping string to parse :return: nothing """ self.nodemap = {} if mapstr.strip() == "": return for pair in mapstr.split(","): parts = pair.split(":") try: if len(parts) != 2: raise ValueError self.nodemap[int(parts[0])] = int(parts[1]) except ValueError: logging.exception("ns-2 mobility node map error") def map(self, nodenum: str) -> int: """ Map one node number (from a script file) to another. :param nodenum: node id to map :return: mapped value or the node id itself """ nodenum = int(nodenum) return self.nodemap.get(nodenum, nodenum) def startup(self) -> None: """ Start running the script if autostart is enabled. Move node to initial positions when any autostart time is specified. Ignore the script if autostart is an empty string (can still be started via GUI controls). :return: nothing """ if self.autostart == "": logging.info("not auto-starting ns-2 script for %s", self.wlan.name) return try: t = float(self.autostart) except ValueError: logging.exception( "Invalid auto-start seconds specified '%s' for %s", self.autostart, self.wlan.name, ) return self.movenodesinitial() logging.info("scheduling ns-2 script for %s autostart at %s", self.wlan.name, t) self.state = self.STATE_RUNNING self.session.event_loop.add_event(t, self.run) def start(self) -> None: """ Handle the case when un-paused. :return: nothing """ logging.info("starting script") laststate = self.state super().start() if laststate == self.STATE_PAUSED: self.statescript("unpause") def run(self) -> None: """ Start is pressed or autostart is triggered. :return: nothing """ super().run() self.statescript("run") def pause(self) -> None: """ Pause the mobility script. :return: nothing """ super().pause() self.statescript("pause") def stop(self, move_initial: bool = True) -> None: """ Stop the mobility script. :param move_initial: flag to check if we should move node to initial position :return: nothing """ super().stop(move_initial=move_initial) self.statescript("stop") def statescript(self, typestr: str) -> None: """ State of the mobility script. :param typestr: state type string :return: nothing """ filename = None if typestr == "run" or typestr == "unpause": filename = self.script_start elif typestr == "pause": filename = self.script_pause elif typestr == "stop": filename = self.script_stop if filename is None or filename == "": return filename = self.findfile(filename) args = f"/bin/sh {filename} {typestr}" utils.cmd( args, cwd=self.session.session_dir, env=self.session.get_environment() )
return CanonicalizationController(Conf.db).canonicalize_author_name() @app.route('/updates') @requires_auth def updates(): return CollectionController(Conf.db).updates_feed() @app.route('/remove', methods=['POST']) @requires_auth def remove(): return CollectionController(Conf.db).remove_items() if __name__ == '__main__': debug = True url = Configuration.integration_url( Configuration.METADATA_WRANGLER_INTEGRATION, required=True) scheme, netloc, path, parameters, query, fragment = urlparse.urlparse(url) if ':' in netloc: host, port = netloc.split(':') port = int(port) else: host = netloc port = 80 # Workaround for a "Resource temporarily unavailable" error when # running in debug mode with the global socket timeout set by isbnlib if debug: import socket socket.setdefaulttimeout(None) Conf.log.info("Starting app on %s:%s", host, port)