def get_variables(exp: str) -> list: exp_a = regex.compile('([-]?[0-9]+)(?=x²)') exp_b = regex.compile('-?[0-9]+(?!x²)(?=x)') exp_c = regex.compile(r'(‒ |\+ )?[0-9]+(?!x)( |$)+') match_a = exp_a.search(exp) match_b = exp_b.search(exp) match_c = exp_c.search(exp) a = "" b = "" c = "" # a if match_a is None: if "x²" in exp: a = "1" else: a = None print("Coeficiente a não pode ser nulo") else: a = match_a.group() if a == "0": a = None elif is_nagative(exp, a): a = f'-{a}' # b if match_b is None: if "x" in exp.replace("x²", ""): b = "1" else: b = "0" else: b = match_b.group() if is_nagative(exp, b): b = f'-{b}' # c if match_c is None: c = "0" else: exp_c = match_c.group() if '‒' in exp_c: c = f'-{exp_c.replace("‒ ", "")}' elif '+' in exp_c: c = f'{exp_c.replace("+ ", "")}' else: c = exp_c return [a, b, c]
def test_charclass_transforms(pattern, good_match, bad_match): regex = js_regex.compile(pattern) assert regex.search(good_match) assert not regex.search(bad_match) if ord(bad_match) >= 128: # Non-ascii string is matched by Python 3, but not in JS mode assert re.compile(pattern).search(bad_match)
def get_compiled_regex(regex): try: regex_to_return = js_regex.compile(regex) except Exception as e: try: regex_to_return = re.compile(regex) except Exception as a: regex_to_return = re.compile('a^') return regex_to_return
def patternProperties(validator, patternProperties, instance, schema): if not validator.is_type(instance, "object"): return for pattern, subschema in iteritems(patternProperties): for k, v in iteritems(instance): if js_regex.compile(pattern).search(k): for error in validator.descend( v, subschema, path=k, schema_path=pattern, ): yield error
def find_additional_properties(instance, schema): """ Return the set of additional properties for the given ``instance``. Weeds out properties that should have been validated by ``properties`` and / or ``patternProperties``. Assumes ``instance`` is dict-like already. """ properties = schema.get("properties", {}) patterns = "|".join(sorted(schema.get("patternProperties", {}))) for property in instance: if property not in properties: if patterns and js_regex.compile(patterns).search(property): continue yield property
def notify(notifyBuffer): for user in dbGetCollection("users").find(): if (user["adminDB_en"] and user["isAHUser"]): app_log.info("User " + user["name"] + " is a valid alarm user") notifyEmailDict = {} notifySMSDict = {} notifyWhatsAppDict = {} notifySignalDict = {} name = user["name"] email = user["email"] mobile = user["mobile"] for notifyPV in user["notifyPVs"]: for area in notifyBuffer: for pvname in notifyBuffer[area]: app_log.info('##-START NOTIFY DEBUG-##') app_log.info(name) app_log.info(area) app_log.info(pvname) message = notifyBuffer[area][pvname] minorAlarm = False majorAlarm = False invalidAlarm = False disconnAlarm = False for entry in message: minorAlarm = minorAlarm or ("MINOR_ALARM" in entry["entry"]) majorAlarm = majorAlarm or ("MAJOR_ALARM" in entry["entry"]) invalidAlarm = invalidAlarm or ("INVALID_ALARM" in entry["entry"]) disconnAlarm = disconnAlarm or ("DISCONNECTED" in entry["entry"]) app_log.info("minorAlarm " + str(minorAlarm)) app_log.info("majorAlarm " + str(majorAlarm)) app_log.info("invalidAlarm " + str(invalidAlarm)) app_log.info("disconnAlarm " + str(disconnAlarm)) if (js_regex.compile( notifyPV["regEx"]).search(pvname)): # Passes regEx check app_log.info("Pass regEx " + notifyPV["regEx"]) notify = False notifyAlarmType = False notifyOnEmail = False notifyOnSMS = False notifyOnWhatsApp = False notifyOnSignal = False if (user["global"]): app_log.info("Using global profile") notify = notifyValid(user["globalSetup"]) notifyOnEmail = user["globalSetup"]["email"] notifyOnSMS = user["globalSetup"]["sms"] notifyOnWhatsApp = user["globalSetup"][ "whatsapp"] # Backwards compatible notifyOnSignal = user["globalSetup"][ "signal"] if ( "signal" in user["globalSetup"]) else False notifyMinorAlarm = user["globalSetup"][ "alarmMinor"] if ( "alarmMinor" in user["globalSetup"]) else True notifyMajorAlarm = user["globalSetup"][ "alarmMajor"] if ( "alarmMajor" in user["globalSetup"]) else True notifyInvalidAlarm = user["globalSetup"][ "alarmInvalid"] if ( "alarmInvalid" in user["globalSetup"]) else True notifyDisconnAlarm = user["globalSetup"][ "alarmDisconn"] if ( "alarmDisconn" in user["globalSetup"]) else True # else: app_log.info("Using unique profile") notify = notifyValid(notifyPV["notifySetup"]) notifyOnEmail = notifyPV["notifySetup"][ "email"] notifyOnSMS = notifyPV["notifySetup"]["sms"] notifyOnWhatsApp = notifyPV["notifySetup"][ "whatsapp"] # Backwards compatible notifyOnSignal = notifyPV["notifySetup"][ "signal"] if ( "signal" in notifyPV["notifySetup"]) else False notifyMinorAlarm = notifyPV["notifySetup"][ "alarmMinor"] if ( "alarmMinor" in notifyPV["notifySetup"]) else True notifyMajorAlarm = notifyPV["notifySetup"][ "alarmMajor"] if ( "alarmMajor" in notifyPV["notifySetup"]) else True notifyInvalidAlarm = notifyPV["notifySetup"][ "alarmInvalid"] if ( "alarmInvalid" in notifyPV["notifySetup"]) else True notifyDisconnAlarm = notifyPV["notifySetup"][ "alarmDisconn"] if ( "alarmDisconn" in notifyPV["notifySetup"]) else True # app_log.info("notifyMinorAlarm " + str(notifyMinorAlarm)) app_log.info("notifyMajorAlarm " + str(notifyMajorAlarm)) app_log.info("notifyInvalidAlarm " + str(notifyInvalidAlarm)) app_log.info("notifyDisconnAlarm " + str(notifyDisconnAlarm)) if (minorAlarm and notifyMinorAlarm): notifyAlarmType = True elif (majorAlarm and notifyMajorAlarm): notifyAlarmType = True elif (invalidAlarm and notifyInvalidAlarm): notifyAlarmType = True elif (disconnAlarm and notifyDisconnAlarm): notifyAlarmType = True if (notify and notifyAlarmType): # Passes notifyValid check app_log.info( "Pass notifyValid and alarm type checks") if (notifyOnEmail): # Notify via email app_log.info("Notify via email") if (area not in notifyEmailDict): notifyEmailDict[area] = {} notifyEmailDict[area][pvname] = message if (notifyOnSMS): # Notify via sms app_log.info("Notify via sms") if (area not in notifySMSDict): notifySMSDict[area] = {} notifySMSDict[area][pvname] = message if (notifyOnWhatsApp): # Notify via whatsapp app_log.info("Notify via whatsapp") if (area not in notifyWhatsAppDict): notifyWhatsAppDict[area] = {} notifyWhatsAppDict[area][pvname] = message if (notifyOnSignal): # Notify via signal app_log.info("Notify via Signal") if (area not in notifySignalDict): notifySignalDict[area] = {} notifySignalDict[area][pvname] = message else: app_log.info( "Fail notifyValid or alarm type check") else: app_log.info("Fail regEx " + notifyPV["regEx"]) app_log.info('###-END NOTIFY DEBUG-###') timestamp = datetime.now(utc).isoformat() if (notifyEmailDict): if (notifyEmail(timestamp, email, notifyEmailDict)): timestamp = datetime.now(utc).isoformat() entry = { "timestamp": timestamp, "entry": " ".join([name, "notified on email"]) } else: timestamp = datetime.now(utc).isoformat() entry = { "timestamp": timestamp, "entry": " ".join(["FAILED to notify", name, "on email!"]) } for area in notifyEmailDict: for pvname in notifyEmailDict[area]: dbUpdateHistory(area, entry, pvname) if (notifySMSDict): if (notifySMS(timestamp, mobile, notifySMSDict)): timestamp = datetime.now(utc).isoformat() entry = { "timestamp": timestamp, "entry": " ".join([name, "notified on SMS"]) } else: timestamp = datetime.now(utc).isoformat() entry = { "timestamp": timestamp, "entry": " ".join(["FAILED to notify", name, "on SMS!"]) } for area in notifySMSDict: for pvname in notifySMSDict[area]: dbUpdateHistory(area, entry, pvname) if (notifyWhatsAppDict): if (notifyWhatsApp(timestamp, mobile, notifyWhatsAppDict)): timestamp = datetime.now(utc).isoformat() entry = { "timestamp": timestamp, "entry": " ".join([name, "notified on WhatsApp"]) } else: timestamp = datetime.now(utc).isoformat() entry = { "timestamp": timestamp, "entry": " ".join(["FAILED to notify", name, "on WhatsApp!"]) } for area in notifyWhatsAppDict: for pvname in notifyWhatsAppDict[area]: dbUpdateHistory(area, entry, pvname) if (notifySignalDict): notifySignal(timestamp, mobile, name, notifySignalDict) else: app_log.info("User " + user["name"] + " is NOT a valid alarm user")
def pattern(validator, patrn, instance, schema): if (validator.is_type(instance, "string") and not js_regex.compile(patrn).search(instance)): yield ValidationError("%r does not match %r" % (instance, patrn))
def create_without_config(): print('\n' + tag + 'Create Without Config File\n' + 'Please enter values for the following configurations...\n' + 'For each entry, type ' + Fore.YELLOW + '!more' + Fore.RESET + ' for more information.') # RDS Instance Name check_rds_instance_name = False while not check_rds_instance_name: print('\n' + tag + 'RDS Instance Name:', end=' ') rds_name = input() if js_regex.compile( "^(?:[a-z]|[a-z][a-z0-9]{1,62}|[a-z](?:[a-z0-9]|\-(?!\-)){1,61}[a-z0-9])$" ).search(rds_name): check_rds_instance_name = True config['DBInstanceIdentifier'] = rds_name elif rds_name.lower().startswith('!more'): print( '\n' + tag + 'RDS Instance Name\n' + 'The DB instance identifier. This parameter is stored as a lowercase string.\n' + '\nConstraints:\n' + '- Must contain from 1 to 63 letters, numbers, or hyphens\n' + '- First character must be a letter\n' + '- Cannot end with a hypen or contain two consecutive hypens') else: print(Style.BRIGHT + 'Invalid entry. Type ' + Fore.YELLOW + '!more' + Fore.RESET + ' for more information.') # Database Engine check_db_engine = False while not check_db_engine: print('\n' + tag + 'Database Engine:', end=' ') db_engine = input() if (db_engine == "aurora" or db_engine == "aurora-mysql" or db_engine == "aurora-postgresql" or db_engine == "mariadb" or db_engine == "mysql" or db_engine == "oracle-ee" or db_engine == "oracle-se" or db_engine == "oracle-se1" or db_engine == "oracle-se2" or db_engine == "postgres" or db_engine == "sqlserver-ee" or db_engine == "sqlserver-se" or db_engine == "sqlserver-ex" or db_engine == "sqlserver-web"): check_db_engine = True config['Engine'] = db_engine elif db_engine.lower().startswith('!more'): print( '\n' + tag + 'Database Engine\n' + 'The name of the database engine to be used for this instance.\n' + 'Not every database engine is available for every AWS Region.\n' + '\nValid Values:\n' + '- aurora (for MySQL 5.6-compatible Aurora)\n' + '- aurora-mysql (for MySQL 5.7-compatible Aurora)\n' + '- aurora-postgresql\n' + '- mariadb\n' + '- mysql\n' + '- oracle-ee\n' + '- oracle-se\n' + '- oracle-se1\n' + '- oracle-se2\n' + '- postgres\n' + '- sqlserver-ee\n' + '- sqlserver-se\n' + '- sqlserver-ex\n' + '- sqlserver-web') else: print(Style.BRIGHT + 'Invalid entry. Type ' + Fore.YELLOW + '!more' + Fore.RESET + ' for more information.') # Database Name check_db_name = False while not check_db_name: print('\n' + tag + 'Database Name:', end=' ') db_name = input() if re.search(r'^[a-zA-Z0-9\_]{1,63}', db_name): check_db_name = True config['DBName'] = db_name elif db_name.lower().startswith('!more'): print( '\n' + tag + 'Database Name\n' + 'The name of the database to create when the DB instance is created.\n' + '\nConstraints:\n' + '- Must contain 1 to 63 letters, number, or underscores\n' + '- Must begin with a letter or an underscore\n' + '- Subsequent characters can be letters, underscores, or digits (0-9)\n' + '- Cannot be a word reserved by the specified database engine') else: print(Style.BRIGHT + 'Invalid entry. Type ' + Fore.YELLOW + '!more' + Fore.RESET + ' for more information.') # Database Port check_db_port = False while not check_db_port: print('\n' + tag + 'Database Port:', end=' ') db_port = input() if db_port.isdigit() and 1150 <= int(db_port) <= 65535: check_db_port = True config['Port'] = int(db_port) elif db_port.lower().startswith('!more'): print( '\n' + tag + 'Database Port\n' + 'The port number on which the database accepts connections.\n' + '\nValid Values:\n' + '- 1550-65535\n' + '\nDefault Values:\n' + '- mysql: 3306\n' + '- mariadb: 3306\n' + '- postgres: 5432\n' + '- oracle: 1521\n' + '- sqlserver: 1433\n' + '- aurora: 3306') else: print(Style.BRIGHT + 'Invalid entry. Type ' + Fore.YELLOW + '!more' + Fore.RESET + ' for more information.') # Database Class # db.m5 – Latest Generation Memory Optimized Instance Classes db_classes = [ 'db.m5.24xlarge', 'db.m5.16xlarge', 'db.m5.12xlarge', 'db.m5.8xlarge', 'db.m5.4xlarge', 'db.m5.2xlarge', 'db.m5.xlarge', 'db.m5.large', # db.m4 – Current Generation Standard Instance Classes 'db.m4.16xlarge', 'db.m4.10xlarge', 'db.m4.4xlarge', 'db.m4.2xlarge', 'db.m4.xlarge', 'db.m4.large', # db.r3 – Previous Generation Memory Optimized Instance Classes 'db.m3.2xlarge', 'db.m3.xlarge', 'db.m3.large', 'db.m3.medium', # db.r4 – Current Generation Memory Optimized Instance Classes 'db.r4.16xlarge', 'db.r4.8xlarge', 'db.r4.4xlarge', 'db.r4.2xlarge', 'db.r4.xlarge', 'db.r4.large', # db.t3 – Latest Generation Burstable Performance Instance Classes 'db.t3.2xlarge', 'db.t3.xlarge', 'db.t3.large', 'db.t3.medium', 'db.t3.small', 'db.t3.micro', # db.t2 – Current Generation Burstable Performance Instance Classes 'db.t2.2xlarge', 'db.t2.xlarge', 'db.t2.large', 'db.t2.medium', 'db.t2.small', 'db.t2.micro' ] check_db_class = False while not check_db_class: print('\n' + tag + 'Database Instance Class:', end=' ') db_class = input() if db_class in db_classes: check_db_class = True config['DBInstanceClass'] = db_class elif db_class.lower().startswith('!more'): print( '\n' + tag + 'The compute and memory capacity of the DB instance.\n' + 'Not all DB instance classes are available in all AWS Regions\n' + 'or for all database engines.\n' + '\nCurrent supported classes:\n' + Fore.CYAN + 'db.m5 – Latest Generation Memory Optimized Instance Classes:\n' + Fore.RESET + '- db.m5.24xlarge\n' + '- db.m5.16xlarge\n' + '- db.m5.12xlarge\n' + '- db.m5.8xlarge\n' + '- db.m5.4xlarge\n' + '- db.m5.2xlarge\n' + '- db.m5.xlarge\n' + '- db.m5.large\n' + Fore.CYAN + 'db.m4 – Current Generation Standard Instance Classes:\n' + Fore.RESET + '- db.m4.16xlarge\n' + '- db.m4.10xlarge\n' + '- db.m4.4xlarge\n' + '- db.m4.2xlarge\n' + '- db.m4.xlarge\n' + '- db.m4.large\n' + Fore.CYAN + 'db.r3 – Previous Generation Memory Optimized Instance Classes:\n' + Fore.RESET + '- db.m3.2xlarge\n' + '- db.m3.xlarge\n' + '- db.m3.large\n' + '- db.m3.medium\n' + Fore.CYAN + 'db.r4 – Current Generation Memory Optimized Instance Classes:\n' + Fore.RESET + '- db.r4.16xlarge\n' + '- db.r4.8xlarge\n' + '- db.r4.4xlarge\n' + '- db.r4.2xlarge\n' + '- db.r4.xlarge\n' + '- db.r4.large\n' + Fore.CYAN + 'db.t3 – Latest Generation Burstable Performance Instance Classes:\n' + Fore.RESET + '- db.t3.2xlarge\n' + '- db.t3.xlarge\n' + '- db.t3.large\n' + '- db.t3.medium\n' + '- db.t3.small\n' + '- db.t3.micro\n' + Fore.CYAN + 'db.t2 – Current Generation Burstable Performance Instance Classes:\n' + Fore.RESET + '- db.t2.2xlarge\n' + '- db.t2.xlarge\n' + '- db.t2.large\n' + '- db.t2.medium\n' + '- db.t2.small\n' + '- db.t2.micro') else: print(Style.BRIGHT + 'Invalid entry. Type ' + Fore.YELLOW + '!more' + Fore.RESET + ' for more information.') # Database Storage Type check_db_storage_type = False while not check_db_storage_type: print('\n' + tag + 'Storage Type [gp2][io1][standard]:', end=' ') db_storage_type = input() if db_storage_type == 'gp2' or db_storage_type == 'io1' or db_storage_type == 'standard': check_db_storage_type = True config['StorageType'] = db_storage_type elif db_storage_type.lower().startswith('!more'): print( '\n' + tag + 'Database Storage Type\n' + 'Specifies the storage type to be associated with the DB instance.\n' + '\nValid Values:\n' + '- gp2 (General Purpose SSD)\n' + '- io1 (Provisioned IOPS storage)\n' + '- standard (Magnetic storage)') else: print(Style.BRIGHT + 'Invalid entry. Type ' + Fore.YELLOW + '!more' + Fore.RESET + ' for more information.') # Database Storage Size check_db_storage_size = False while not check_db_storage_size: print('\n' + tag + 'Storage Size (GiB): ', end=' ') db_storage_size = input() if db_storage_size.isdigit() and (5 <= int(db_storage_size) <= 65536): check_db_storage_size = True config['AllocatedStorage'] = int(db_storage_size) elif db_storage_size.lower().startswith('!more'): print( '\n' + tag + 'Database Storage Size\n' + 'Constraints to the amount of storage (in Gibibytes) of each storage type are the following:\n' + '- General Purpose (SSD) storage (gp2): 20 to 65536\n' + '- Provisioned IOPS storage (io1): 100 to 65536\n' + '- Magnetic storage (standard): 5 to 3072') else: print(Style.BRIGHT + 'Invalid entry. Type ' + Fore.YELLOW + '!more' + Fore.RESET + ' for more information.') # IOPS Value check_iops_value = False while not check_iops_value: if db_storage_type != 'io1': check_iops_value = True else: print('\n' + tag + 'IOPS Value:', end=' ') iops_value = input() if iops_value.isdigit() and (1000 <= int(iops_value) <= 40000): check_iops_value = True config['Iops'] = int(iops_value) elif iops_value.lower().startswith('!more'): print( '\n' + tag + 'IOPS Value\n' + 'The amount of Provisioned IOPS (input/output operations per second) to be allocated for the DB instance.\n' + '\nConstraints: For mariadb, mysql, oracle, and postgres instances, IOPS must be a multiple between\n' + '0.5 and 50 of the storage amount for the DB instance.\n' + '\nValid Values:\n' + '- 1000 to 40000') else: print(Style.BRIGHT + 'Invalid entry. Type ' + Fore.YELLOW + '!more' + Fore.RESET + ' for more information.') # Database Master Username check_db_master_username = False while not check_db_master_username: print('\n' + tag + 'Database Master Username:'******' ') db_master_username = input() if re.search(r'^[a-zA-Z0-9]{1,20}', db_master_username): check_db_master_username = True config['MasterUsername'] = db_master_username elif db_master_username.lower().startswith('!more'): print('\n' + tag + 'Database Master Username\n' + 'The name for the master user.\n' + '\nConstraints:\n' + '- Must be 1 to 20 letters or numbers\n' + '- Cannot be a reserved word for the chosen database engine') else: print(Style.BRIGHT + 'Invalid entry. Type ' + Fore.YELLOW + '!more' + Fore.RESET + ' for more information.') # Database Master Password check_db_master_password = False while not check_db_master_password: print('\n' + tag + 'Database Master Password:'******' ') db_master_password = input() if re.search(r'[^\/\"\@]{8,128}', db_master_password): check_db_master_password = True config['MasterUserPassword'] = db_master_password elif db_master_password.lower().startswith('!more'): print( '\n' + tag + 'Database Master Password\n' + 'The password for the master user.\n' + '\nConstraints:\n' + '- Must be 8 to 128 ANSI characters (excluding \"/\", \'"\', \"@\")' ) else: print(Style.BRIGHT + 'Invalid entry. Type ' + Fore.YELLOW + '!more' + Fore.RESET + ' for more information.') # Configuration Confirmation print('\n' + tag + 'Configuration Confirmation') print(Style.BRIGHT + '- RDS Instance Name: ' + Fore.CYAN + rds_name + Fore.RESET) print(Style.BRIGHT + '- Database Name: ' + Fore.CYAN + db_name + Fore.RESET) print(Style.BRIGHT + '- Database Engine: ' + Fore.CYAN + db_engine + Fore.RESET) print(Style.BRIGHT + '- Database Port: ' + Fore.CYAN + db_port + Fore.RESET) print(Style.BRIGHT + '- Database Instance Class: ' + Fore.CYAN + db_class + Fore.RESET) print(Style.BRIGHT + '- Database Storage Type: ' + Fore.CYAN + db_storage_type + Fore.RESET) print(Style.BRIGHT + '- Database Storage Size: ' + Fore.CYAN + db_storage_size + ' GiB' + Fore.RESET) if db_storage_type == 'io1': print(Style.BRIGHT + '- IOPS Value: ' + Fore.CYAN + iops_value + Fore.RESET) else: config['Iops'] = 0 print(Style.BRIGHT + '- Database Master Username: '******'- Database Master Password: '******'\nConfirm this configuration:' + options) # 'y' answer if confirm_config.lower().startswith('y'): check_confirm_config = True # Call create_rds_instance() with empty auto_name and include config dict cri.create_rds_instance('', **config) # 'n' answer elif confirm_config.lower().startswith('n'): print('\n' + tag + 'Cancelled.' + Fore.RED + '\nExiting program:' + Fore.RESET + ' You have chosen to cancel the configuration.') sys.exit(0) # '!more' answer elif confirm_config.lower().startswith('!more'): print( tag + 'Configuration Confirmation\n' + 'If you choose to confirm the configuration (\'y\'), the RDS instance will be created with these values.\n' + 'If you choose to cancel the configuration (\'n\'), the program will exit and you must start over.' ) # Invalid answer else: print(tag + 'Invalid entry. Type ' + Fore.YELLOW + '!more' + Fore.RESET + ' for more information.')
def test_consistent_behaviour_is_consistent(pattern, string): # The main point of this test is to excercise the recursion in check_features convert = bool if PY2 else repr assert convert(re.search(pattern, string)) == convert( js_regex.compile(pattern).search(string))
def test_expected_transforms(pattern, good_match, bad_match): regex = js_regex.compile(pattern) assert regex.search(good_match) assert not regex.search(bad_match)
def test_flags_validation(flags, error): with pytest.raises(error): js_regex.compile("", flags=flags)
def test_pattern_validation(pattern, error): with pytest.raises(error): js_regex.compile(pattern)
def is_regex(instance): if not isinstance(instance, str_types): return True return js_regex.compile(instance)