Esempio n. 1
0
def _attach_files(paths, gist_slug, version, output=False, exclude_files=None):
    """Helper functions to attach files & folders to a commit"""
    config = read_creds().get("DEFAULT_CONFIG", {})

    whitelist = config.get("EXTENSION_WHITELIST")
    upload_wd = config.get("UPLOAD_WORKING_DIRECTORY", False)

    if not isinstance(whitelist, list):
        whitelist = DEFAULT_EXTENSION_WHITELIST

    if not paths:
        if output or not upload_wd:
            return

        paths = [
            f for f in glob.glob('**/*', recursive=True)
            if get_file_extension(f) in whitelist
        ]

    if exclude_files:
        if not isinstance(exclude_files, list):
            exclude_files = [exclude_files]

        for filename in exclude_files:
            try:
                paths.remove(filename)
            except ValueError:
                pass

    log('Uploading additional ' + ('outputs' if output else 'files') + '...')

    # Convert single path to list
    if type(paths) == str:
        paths = [paths]

    for path in paths:
        if os.path.isdir(path):
            files = [
                f
                for f in glob.glob(os.path.join(path, '**/*'), recursive=True)
                if get_file_extension(f) in whitelist
            ]
            for file in files:
                _attach_file(file, gist_slug, version, output)
        elif os.path.exists(path):
            _attach_file(path, gist_slug, version, output)
        else:
            log('Ignoring "' + path + '" (not found)', error=True)
Esempio n. 2
0
def _list_ipynb_files(path):
    """Return list of ipynb files in a path"""
    if os.path.isfile(path) and get_file_extension(path) == ".ipynb":
        files = [os.path.normpath(path)]
    elif os.path.isdir(path):
        files = [
            os.path.normpath(f)
            for f in sorted(glob.glob(os.path.join(path, '*.ipynb')))
        ]
    else:
        files = []
    return files
Esempio n. 3
0
def _parse_filename(filename):
    """Perform the required checks and get the final filename"""
    # Get the filename of the notebook (if not provided)
    if filename is None:
        if in_script():
            filename = get_script_filename()
        elif in_notebook():
            filename = get_notebook_name()

    # Add the right extension to the filename
    elif get_file_extension(filename) not in ['py', 'ipynb']:
        filename += '.py' if in_script() else '.ipynb'
    return filename
Esempio n. 4
0
    def test_get_file_extension_malformed(self):
        file = 123
        expected_result = ''

        self.assertEqual(get_file_extension(file), expected_result)
Esempio n. 5
0
    def test_get_file_extension_normal(self):
        file = "users/siddhant/file.ipynb"
        expected_result = "ipynb"

        self.assertEqual(get_file_extension(file), expected_result)
Esempio n. 6
0
def test_get_file_extension(filename, expected_result):
    assert get_file_extension(filename) == expected_result