def to_string(caseType: int) -> str: """ Converts the casetype enum int into a readable string """ if caseType == CaseType.SAMPLE: return Definitions.get_value(CaseType.SAMPLE_STRING_KEY) elif caseType == CaseType.CORNER_CASE: return Definitions.get_value(CaseType.CORNER_STRING_KEY) else: return Definitions.get_value(CaseType.GENERATED_STRING_KEY)
def test_load_definitions(self, mocked_fileops, mocked_get_def_filepath): """ Ensure Definitions.load_definitions properly receives fileops output """ mocked_get_def_filepath.return_value = 'fakepath' mocked_fileops.get_json_dict.return_value = {'hello' : 'hi'} Definitions.load_definitions() self.assertEqual(Definitions._definitionsDict, {'hello' : 'hi'}) mocked_fileops.get_json_dict.assert_called_with('fakepath')
def from_string(string: str) -> int: """ Converts the CaseType strings defined in the definitions file to their respective enum int values. If invalid string, assumed generated :param string: The string to convert to enum """ if string == Definitions.get_value(CaseType.SAMPLE_STRING_KEY): return CaseType.SAMPLE elif string == Definitions.get_value(CaseType.CORNER_STRING_KEY): return CaseType.CORNER_CASE else: return CaseType.GENERATED
def test_get_definitions_filepath(self, mocked_pathmapper_get_conf_path): """ Ensure Definitions.get_definitions_filepath works as expected """ mocked_pathmapper_get_conf_path.return_value = 'configPath' self.assertEqual(Definitions.get_definitions_filepath(), os.path.join('configPath', Definitions.DEFINITIONS_FILE))
def test_get_definitions_filepath(self, mocked_fileops, mocked_pathmapper_get_conf_path): """ Ensure Definitions.get_definitions_filepath properly delegates to fileops """ mocked_fileops.join_path.return_value = 'hi' mocked_pathmapper_get_conf_path.return_value = 'lol' self.assertEqual(Definitions.get_definitions_filepath(), 'hi') mocked_fileops.join_path.assert_called_with('lol', Definitions.DEFINITIONS_FILE)
def get_all_cases(problemNumber=None): """ Resolves the cases directory from the definitions file and delegates to _get_all_cases :return: {problemNumber: [Case]} """ return _get_all_cases(fileops.join_path(PathMapper._rootPath, Definitions.get_value('test_directory')), problemNumber=problemNumber)
def test_get_value_unit(self, mocked_load_definitions): """ Ensure Definitions.get_value functions properly as a unit """ # Before doing anything, definitionsDict should be None and load called def setDefDict(): Definitions._definitionsDict = {} mocked_load_definitions.side_effect = setDefDict self.assertEqual(Definitions.get_value('invalidKey'), None) mocked_load_definitions.assert_called_with() # With a good definitions dict, ensure a key inside the dict returns val mocked_load_definitions.side_effect = None Definitions._definitionsDict = { 'key1' : 'val1', 'key2' : 'val2' } self.assertEqual(Definitions.get_value('key1'), 'val1') self.assertEqual(Definitions.get_value('nonkey'), None) Definitions._definitionsDict = None
def load_from_path(path): """ Creates a new Solution object from a file at path and returns it """ newSolution = Solution(solutionPath=path) filename = fileops.get_basename_less_extension(path) filenameMatcher = Definitions.get_value_matcher(Solution.NAMING_DEFINITION_KEY) newSolution.problemNumber = filenameMatcher.get_variable_value( filename, Variables.get_variable_key_name(Variables.NAME_PROBLEM_NUMBER) ) from util.writer import Writer newSolution.solutionWriter = fileops.get_basename(fileops.get_parent_dir(path)) newSolution.solutionLanguage = Languages.get_language_from_extension(fileops.get_extension(path)) return newSolution
def _get_file_problemnumber_type_tuple(path): """ Extracts the problem number and case type from the file name at path :returns: (problemnumber:int, casetype:int) """ filename = fileops.get_basename_less_extension(path) # Extract problem number filenameMatcher = Definitions.get_value_matcher(Case.NAMING_DEFINITION_KEY) problemNumber = filenameMatcher.get_variable_value(filename, Variables.get_variable_key_name(Variables.NAME_PROBLEM_NUMBER)) # Extract case type caseType = filenameMatcher.get_variable_value(filename, Variables.get_variable_key_name(Variables.NAME_CASE_TYPE)) if problemNumber is None: return None # Return the tuple return (int(problemNumber), CaseType.from_string(caseType))
def package_into_path(path: str, layoutDict: dict): """ Follows the users provided configuration file to package all cases into the provided path. Delegates most functionality to other functions. Arguments: path: str - The path to package into layoutDict: dict - The dict provided by the user's configuration """ # Look through all cases... for problemNumber, caseList in case.get_all_cases().items(): # Make the directories for the problem numbers problemDirName = Definitions.get_value('solution_naming').format( **dict(problem=problemNumber)) problemPath = fileops.join_path(path, problemDirName) fileops.make(problemPath, FileType.DIRECTORY) # Go through each type in the config file and package it for packageTypeName, packageType in layoutDict[TYPES_KEY].items(): package_type(problemPath, caseList, packageType, _get_compression_from_layout_dict(layoutDict), packageTypeName)
def operate(args): """ Takes the passed in args and delegates to proper functionality. This is set as the executable function when the `writers assign` subparser is used. Arguments: args: Namespace - The arguments passed via CLI """ # If the user specified a problem list, use that as the list of problems. problemParser = NumberParse() if not args.problems is None: specifiedProblems = problemParser.str_list_to_uniq_range(args.problems) else: specifiedProblems = problemParser.str_list_to_uniq_range(['1+']) specifiedLanguages = [] if not args.language is None: for languageName in args.language: loadedLanguage = Languages.get_language_by_name(languageName) if loadedLanguage is None: raise PyCException('Error: {} is an invalid language'.format(languageName)) specifiedLanguages.append(loadedLanguage) else: specifiedLanguages = [Languages.get_language_by_name(name) for name in Languages.get_all_language_names()] specifiedWriters = [] if not args.full and not args.writer_names is None and len(args.writer_names) > 0: for writerName in args.writer_names: loadedWriter = Writer.load_from_folder(writerName) if loadedWriter is None: raise PyCException('Error: {} is an invalid writer'.format(writerName)) specifiedWriters.append(loadedWriter) else: specifiedWriters = Writers.get_all_writers() allocator = AssignmentAllocator(specifiedProblems, specifiedLanguages, specifiedWriters, fromScratch=args.full) allocator.do_assignment(Definitions.get_value('complete_threshold'), overflow=args.overallocate)
def validate_defined_case_dir(): testDir = Definitions.get_value('test_directory') validate_casefiles(fileops.get_files_in_dir(testDir))
def is_solution_file(path): """ Checks to see if basename of file matches solution naming scheme from definitions """ filename = fileops.get_basename_less_extension(path) return Definitions.get_value_matcher(Solution.NAMING_DEFINITION_KEY).matches(filename)
def test_get_value_matcher(self, mocked_matcher_from_var_string, mocked_get_value): mocked_get_value.return_value = 'hello' mocked_matcher_from_var_string.return_value = 'hi' self.assertEqual(Definitions.get_value_matcher('key'), 'hi') mocked_get_value.assert_called_with('key') mocked_matcher_from_var_string.assert_called_with('hello')
def str_to_list_range(self, string): lowerBound = 1 upperBound = Definitions.get_value('problem_count') return self.str_to_list_range_with_bounds(string, upperBound, lowerBound)