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 load_from_path(cls, path): """ Loads a writer and all their solutions from a specified path """ # Check if writer directoroy exists. If not, return nothing if not fileops.exists(path, fileops.FileType.DIRECTORY): return None loadedWriter = Writer(writerPath=path) # Load the user data from the data file dataDictionary = fileops.get_json_dict(loadedWriter._get_datafile_path()) # Populate the data if available # Load name if cls.DATAFILE_NAME_FIELD in dataDictionary: loadedWriter.name = dataDictionary[cls.DATAFILE_NAME_FIELD] # Load email if cls.DATAFILE_EMAIL_FIELD in dataDictionary: loadedWriter.email = dataDictionary[cls.DATAFILE_EMAIL_FIELD] # Load languages if cls.DATAFILE_LANGS_FIELD in dataDictionary: for languageName in dataDictionary[cls.DATAFILE_LANGS_FIELD]: loadedWriter._add_known_language_from_name(languageName) # Load assigned if cls.DATAFILE_ASSIGNED_PROBLEMS in dataDictionary: for assignedProblem in dataDictionary[cls.DATAFILE_ASSIGNED_PROBLEMS]: loadedWriter._add_assigned_problem(assignedProblem[0], assignedProblem[1]) # Load all solutions for possibleSolution in fileops.get_files_in_dir(path): if Solution.is_solution_file(possibleSolution): if not Languages.is_prevalent_extension(fileops.get_extension(possibleSolution)): continue solutionObject = Solution.load_from_path(possibleSolution) loadedWriter._add_solution(solutionObject) return loadedWriter