OPEN-NEXT/wp2.2_dev

Refactor checking of required directories into its own function

Closed this issue · 3 comments

As stated in the comments in start.py, code for checking whether the required directories (e.g. __DATA__ or for JSON and GraphML output) are repeated several times. Let's wrap this into one function call, probably at the start of start.py where it checks everything.

As a bonus, if the needed directories don't exist, either create them using sensible defaults or stop execution and ask the user to create and specify them then re-run.

jbon commented

As far as I can judge, the commit b9b0c34 in branch iss21 fixes the issue.

Added function:

def build_export_file_path(dir_path, filename):
    # checks if the dir_path exists and if not create it
    # then returns the (now valid) file path 
    if not os.path.isdir(dir_path):
        os.makedirs(dir_path)
    return os.path.join(
        dir_path, filename)

to replace the following snippet that occured 4 times in the script:

    # checks whether the export dir exists and if not creates it # TODO: this is a code snippet we use many times, we should make a function out of it
    output_dir_JSON = os.path.join(config["data_dir_path"], 'JSON_commits')
    if not os.path.isdir(output_dir_JSON):
        os.makedirs(output_dir_JSON)
    output_JSON = os.path.join(
        output_dir_JSON, username + '-' + repo + '.json')

The result is more condensed and less redundant:

    output_JSON = build_export_file_path(
        os.path.join(config["data_dir_path"], 'JSON_commits'), 
        username + '-' + repo + '.json') 
jbon commented

@penyuan: can you review, validate, eventually close the issue and merge the branch if you find the solution satisfactory?

Tested fine on my end!