def load_questions(question_dir=None, from_server=False, module_name=bfq.__name__): # type: (Optional[str], bool, str) -> None """Load questions from directory or batfish service. :param question_dir: Load questions from this local directory instead of remote questions from the batfish service. :type question_dir: str :param from_server: if true or `question_dir` is None, load questions from service. :type from_server: bool :param module_name: the name of the module where questions should be loaded. Default is :py:mod:`pybatfish.question.bfq` """ new_names = set() # type: Set[str] if not question_dir or from_server: remote_questions = _load_remote_questions_templates() _install_questions_in_module(remote_questions, module_name) new_names |= set(name for name, q in remote_questions) if question_dir: local_questions = load_dir_questions(question_dir, moduleName=module_name) over_written_questions = len(set(local_questions) & new_names) if over_written_questions > 0: bf_logger.info( "Overwrote {over_written_questions} remote question(s) with local question(s)".format( over_written_questions=over_written_questions))
def _load_questions_from_dir(question_dir): # type: (str) -> Dict[str, QuestionMeta] question_files = [] for dirpath, dirnames, filenames in os.walk(question_dir): for filename in filenames: if filename.endswith(".json"): question_files.append(os.path.join(dirpath, filename)) if len(question_files) == 0: bf_logger.warn( "WARNING: no .json files found in supplied question directory: {questionDir}".format( questionDir=question_dir)) return {} questions = {} for questionFile in question_files: try: (qname, qclass) = _load_question_disk(questionFile) questions[qname] = qclass except Exception as err: bf_logger.error( "Could not load question from {questionFile}:{err}".format( questionFile=questionFile, err=err)) bf_logger.info( "Successfully loaded {numQuestions}/{numQuestionFiles} question(s) from local directory".format( numQuestions=len(questions), numQuestionFiles=len(question_files))) return questions
def load_questions(question_dir=None, from_server=False, module_name=bfq.__name__): """Load questions from directory or batfish service. :param question_dir: Load questions from this local directory instead of remote questions from the batfish service. :type question_dir: str :param from_server: if true, also load questions from service. Ignored if `question_dir` is `None` :type from_server: bool :param module_name: the name of the module where questions should be loaded. Default is :py:mod:`pybatfish.question.bfq` """ questions = set() over_written_questions = 0 if not question_dir or from_server: remote_questions = _load_remote_questions_templates( moduleName=module_name) over_written_questions += _merge_questions(remote_questions, questions) if question_dir: local_questions = load_dir_questions(question_dir, moduleName=module_name) over_written_questions += _merge_questions(local_questions, questions) if over_written_questions > 0: bf_logger.info( "Overwrote {over_written_questions} remote question(s) with local question(s)" .format(overWrittenQuestions=over_written_questions))
def load_dir_questions(questionDir, moduleName=bfq.__name__): questionFiles = [] for dirpath, dirnames, filenames in os.walk(questionDir): for filename in filenames: if filename.endswith(".json"): questionFiles.append(os.path.join(dirpath, filename)) localQuestions = set([]) if len(questionFiles) == 0: bf_logger.warn( "WARNING: no .json files found in supplied question directory: {questionDir}" .format(questionDir=questionDir)) else: numQuestions = 0 for questionFile in questionFiles: try: localQuestions.add( _load_question_disk(questionFile, module_name=moduleName)) numQuestions += 1 except ValueError as err: bf_logger.error( "Could not load question from {questionFile}:{err}".format( questionFile=questionFile, err=err)) bf_logger.info( "Successfully loaded {numQuestions}/{numQuestionFiles} question(s) from local directory" .format(numQuestions=numQuestions, numQuestionFiles=len(questionFiles))) return localQuestions
def _load_remote_questions_templates(moduleName=bfq.__name__): numQuestions = 0 remoteQuestions = set([]) questionsDict = _bf_get_question_templates() for (key, value) in questionsDict.items(): try: remoteQuestions.add( _load_question_json(value, module_name=moduleName)) numQuestions += 1 except Exception as err: bf_logger.error("Could not load question {name} : {err}".format( name=key, err=err)) bf_logger.info( "Successfully loaded {numQuestions} questions from remote".format( numQuestions=numQuestions)) return remoteQuestions
def _load_remote_questions_templates(): # type: () -> Set[Tuple[str, QuestionMeta]] num_questions = 0 remote_questions = set() questions_dict = _bf_get_question_templates() for (key, value) in questions_dict.items(): try: remote_questions.add(_load_question_dict(json.loads(value))) num_questions += 1 except Exception as err: bf_logger.error( "Could not load question {name} : {err}".format(name=key, err=err)) bf_logger.info( "Successfully loaded {numQuestions} questions from remote".format( numQuestions=num_questions)) return remote_questions