예제 #1
0
    def is_valid(self) -> bool:
        """ is directory path, vrd filepath, url, name valid """

        return is_valid_filepath(self.directory, platform='auto') and \
               is_valid_filepath(self.vrd_filename, platform='auto') and \
               is_valid_filepath(self.url_path, platform='posix') and \
               self.url_path.startswith('/') and \
               '' != self.name
예제 #2
0
def setUser(parent, changesample: bool = True):
    # Make sure the base path exists and set it if it doesn't
    if not os.path.exists(parent.basepath):
        parent.basepath = setBasepath(parent)
        
    # Prompt user to enter a username
    entry, accepted = QtWidgets.QInputDialog().getText(
                            parent, 
                            'Change User', 
                            'Enter user name:')
    
    # Make sure the chosen username is valid
    testpath = os.path.join(parent.basepath, entry, '')
    if accepted and entry != '' and pathvalidate.is_valid_filepath(
                                        testpath, 
                                        platform='Windows'):
        userpath = testpath
        parent.user = entry

    # If the username is invalid, retry creation
    elif accepted and not pathvalidate.is_valid_filepath(
                                testpath,
                                platform='Windows'):
        QtWidgets.QMessageBox.warning(parent, 
              'Alert', 
              'User name contains invalid characters for folder creation.')
        setUser(parent, False)
        
    # If no username is entered, exit
    elif entry == '':
        setUser(parent, False)
    
    userpath = os.path.join(parent.basepath, parent.user, '')
    if not os.path.exists(userpath):
        os.makedirs(userpath)
        
    # Update parent variables
    parent.user = entry
    
    # Update user label text
    parent.userLabel.setText(entry)
    
    # Update the config file
    parent.config['Default']['user'] = entry
    with open(parent.configfile, 'w') as cfg:
        parent.config.write(cfg)
        
    # Run the sample entry dialog if changing samples
    if changesample:
        setSample(parent)
    else:
        samplepath = os.path.join(parent.basepath, parent.user, parent.user, '')
        if not os.path.exists(samplepath) and pathvalidate.is_valid_filepath(
                                                testpath, 
                                                platform='Windows'):
            os.makedirs(samplepath)
예제 #3
0
def setSample(parent):
    # Make sure basepath is valid and exists
    if not os.path.exists(parent.basepath) or not pathvalidate.is_valid_filepath(
                                                      parent.basepath,
                                                      platform='Windows'):
        setBasepath(parent)
        
    # Make sure userpath is valid and exists
    userpath = os.path.join(parent.basepath, parent.user, '')
    if not os.path.exists(userpath) or not pathvalidate.is_valid_filepath(
                                                      userpath,
                                                      platform='Windows'):
        setUser(parent, False)
        
    # Prompt user to enter sample name
    entry, accepted = QtWidgets.QInputDialog().getText(
        parent, 
        'Change Sample', 
        'Enter sample name:')
    testpath = os.path.join(userpath, entry, '')
    if accepted and entry != '' and pathvalidate.is_valid_filepath(
                                        testpath, 
                                        platform='Windows'):
        samplepath = testpath
    elif accepted:
        QtWidgets.QMessageBox.warning(
            parent, 
            'Alert', 
            'Sample name contains invalid characters for folder creation.')
        setSample(parent)
        
    elif not accepted and os.path.exists(os.path.join(userpath, 
                                                          parent.sample)):
        return
    elif not accepted:
        setSample(parent)
    else:
        return
            
    # Make the sample directory if it's valid and doesn't already exist
    if not os.path.exists(testpath) and pathvalidate.is_valid_filepath(
                                            testpath,
                                            platform='Windows'):
        os.makedirs(testpath)
        
    # Update the parent variable
    parent.sample = entry
        
    # Update the sample label text
    parent.sampleLabel.setText(entry)
    
    # Update the config file
    parent.config['Default']['sample'] = parent.sample
    with open(parent.configfile, 'w') as cfg:
        parent.config.write(cfg)
예제 #4
0
 def test_normal_max_len(self, value, platform, max_len, expected):
     if expected is None:
         validate_filepath(value, platform=platform, max_len=max_len)
         assert is_valid_filepath(value, platform=platform, max_len=max_len)
     else:
         with pytest.raises(expected):
             validate_filepath(value, platform=platform, max_len=max_len)
예제 #5
0
    def test_normal_pathlike(self, value, replace_text, expected):
        sanitized_name = sanitize_filepath(value, replace_text)
        assert sanitized_name == expected
        assert is_pathlike_obj(sanitized_name)

        validate_filepath(sanitized_name)
        assert is_valid_filepath(sanitized_name)
예제 #6
0
    def test_normal_pathlike(self, value, replace_text, expected):
        sanitized_name = sanitize_filepath(value, replace_text)
        assert sanitized_name == expected
        assert is_pathlike_obj(sanitized_name)

        validate_filepath(sanitized_name)
        assert is_valid_filepath(sanitized_name)
예제 #7
0
 def test_normal_auto_platform_linux(self, value, expected):
     if isinstance(expected, str):
         sanitized = sanitize_filepath(value, platform="auto")
         assert is_valid_filepath(sanitized, platform="auto")
     else:
         with pytest.raises(expected):
             sanitize_filepath(value, platform="auto")
예제 #8
0
 def test_normal_rel_path(self, test_platform, value, expected):
     if expected is None:
         validate_filepath(value, platform=test_platform)
         assert is_valid_filepath(value, platform=test_platform)
     else:
         with pytest.raises(expected):
             validate_filepath(value, platform=test_platform)
예제 #9
0
 def test_normal_auto_platform_linux(self, value, expected):
     if expected is None:
         validate_filepath(value, platform="auto")
         assert is_valid_filepath(value, platform="auto")
     else:
         with pytest.raises(expected):
             validate_filepath(value, platform="auto")
예제 #10
0
 def test_minmax_len(self, value, min_len, max_len, expected):
     if expected is None:
         validate_filepath(value, min_len=min_len, max_len=max_len)
         assert is_valid_filepath(value, min_len=min_len, max_len=max_len)
     else:
         with pytest.raises(expected):
             validate_filepath(value, min_len=min_len, max_len=max_len)
예제 #11
0
 def test_minmax_len(self, value, min_len, max_len, expected):
     if expected is None:
         validate_filepath(value, min_len=min_len, max_len=max_len)
         assert is_valid_filepath(value, min_len=min_len, max_len=max_len)
     else:
         with pytest.raises(expected):
             validate_filepath(value, min_len=min_len, max_len=max_len)
예제 #12
0
    def test_exception_reserved_name(self, value, platform, expected):
        with pytest.raises(expected) as e:
            print(platform, value)
            validate_filepath(value, platform=platform)
        assert e.value.reusable_name is False
        assert e.value.reserved_name

        assert not is_valid_filepath(value, platform=platform)
예제 #13
0
    def test_normal_max_len(self, value, platform, max_len, expected):
        if expected is None:
            validate_filepath(value, platform=platform, max_len=max_len)
            assert is_valid_filepath(value, platform=platform, max_len=max_len)
            return

        with pytest.raises(expected):
            validate_filepath(value, platform=platform, max_len=max_len)
예제 #14
0
    def test_exception_reserved_name(self, value, platform, expected):
        with pytest.raises(expected) as e:
            print(platform, value)
            validate_filepath(value, platform=platform)
        assert e.value.reusable_name is False
        assert e.value.reserved_name

        assert not is_valid_filepath(value, platform=platform)
예제 #15
0
 def test_normal_multibyte(self, test_platform, value, replace_text,
                           expected):
     sanitized_name = sanitize_filepath(value,
                                        replace_text,
                                        platform=test_platform)
     assert sanitized_name == expected
     validate_filepath(sanitized_name, platform=test_platform)
     assert is_valid_filepath(sanitized_name, platform=test_platform)
예제 #16
0
 def test_normal_str(self, platform, value, replace_text, expected):
     sanitized_name = sanitize_filepath(value,
                                        platform=platform,
                                        replacement_text=replace_text)
     assert sanitized_name == expected
     assert isinstance(sanitized_name, str)
     validate_filepath(sanitized_name, platform=platform)
     assert is_valid_filepath(sanitized_name, platform=platform)
예제 #17
0
    def test_locale_jp(self, locale):
        from faker import Factory

        fake = Factory.create(locale=locale, seed=1)

        for _ in range(100):
            filepath = fake.file_path()
            validate_filepath(filepath, platform="linux")
            assert is_valid_filepath(filepath, platform="linux")
예제 #18
0
    def test_locale_jp(self, locale):
        from faker import Factory

        fake = Factory.create(locale=locale, seed=1)

        for _ in range(100):
            filepath = fake.file_path()
            validate_filepath(filepath)
            assert is_valid_filepath(filepath)
예제 #19
0
def export(export_path, default_album, library_path, edited):
    """ Export all photos, organized by album """
    export_path = os.path.expanduser(export_path)
    library_path = os.path.expanduser(library_path) if library_path else None

    if library_path is not None:
        photosdb = osxphotos.PhotosDB(library_path)
    else:
        photosdb = osxphotos.PhotosDB()

    photos = photosdb.photos()

    for p in photos:
        if not p.ismissing:
            albums = p.albums
            if not albums:
                albums = [default_album]
            for album in albums:
                click.echo(f"exporting {p.original_filename} in album {album}")

                # make sure no invalid characters in destination path (could be in album name)
                album_name = sanitize_filepath(album, platform="auto")

                # create destination folder, if necessary, based on album name
                dest_dir = os.path.join(export_path, album_name)

                # verify path is a valid path
                if not is_valid_filepath(dest_dir, platform="auto"):
                    sys.exit(f"Invalid filepath {dest_dir}")

                # create destination dir if needed
                if not os.path.isdir(dest_dir):
                    os.makedirs(dest_dir)

                filename = p.original_filename
                # export the photo but only if --edited, photo has adjustments, and 
                # path_edited is not None (can be None if edited photo is missing)
                if edited and p.hasadjustments and p.path_edited:
                    # export edited version
                    # use original filename with _edited appended but make sure suffix is 
                    # same as edited file
                    edited_filename = f"{pathlib.Path(filename).stem}_edited{pathlib.Path(p.path_edited).suffix}"
                    exported = p.export(dest_dir, edited_filename, edited=True)
                    click.echo(f"Exported {edited_filename} to {exported}")
                # export unedited version
                exported = p.export(dest_dir, filename)
                click.echo(f"Exported {filename} to {exported}")
        else:
            click.echo(f"Skipping missing photo: {p.original_filename} in album {album}")
예제 #20
0
    def WinPathValidator(path):
        """
        Path validator function that checks for OS appropiate paths

        Parameters
        ----------
        path : String
            string of directory or file path

        Returns
        -------
        Boolean
            returns if a path is valid or not
        """
        return pathvalidate.is_valid_filepath(path)
예제 #21
0
    def WinFileValidator(file):
        """
        file name validator function that checks for OS appropiate paths

        Parameters
        ----------
        path : String
            string of directory or file path

        Returns
        -------
        Boolean
            returns if a file name is valid or not
        """
        return pathvalidate.is_valid_filepath(file)
예제 #22
0
def is_filepath(val):
    """This function check if data type of value or not.
    
    Parameters
    ----------
    value : 
            value for which we want to check data type

    Returns
    ----------
    datatype: 
            a binary value based on data type of the given input argument is filepath or not
    """
    val = val.lstrip('/')
    return is_valid_filepath(val) and (val.find('/') != -1
                                       or val.find('\\') != -1)
예제 #23
0
    def process_each_msg(self, each, keyword_dict):
        new_msg = []
        sentences = each.split("\n")
        for each_sentence in sentences:
            each_sentence = each_sentence.strip('\t\n\r\f\v').strip().lower()
            tokens = each_sentence.split(" ")
            new_tokens = []
            for tok in tokens:
                tok = tok.strip("\t\n\r\f\v.:',;")
                if tok == "":
                    continue
                # print("tok = '%s'" % tok)
                if tok.isalpha():  ### check keyword
                    category = self.check_keyword(tok, keyword_dict)
                    if category:
                        tok = "<keyword category=" + category + ">" + tok + "</keyword>"
                    new_tokens.append(tok)
                else:  ### check types
                    # if "(" in tok:
                    #     if re.match('([a-z0-9]+(_?))+[a-z0-9]*\([a-z0-9, ]*\)\Z', tok):
                    #         tok = "<function>" + tok + "</function>"
                    # ----------------------
                    # TODO: argument list check, the original argument checks are problematic '[a-z0-9, ]*'
                    # it cannot match foo(int *)
                    # for now, just check if the tok is in shape of 'identifier(.*)'
                    # ----------------------
                    if "(" in tok and tok.endswith(")"):
                        parts = tok[:-1].split("(")
                        if parts[0].isidentifier():
                            tok = "<function>" + tok + "</function>"
                            # print("  |- " + tok)
                    elif "." in tok:
                        # if re.match('([a-z0-9]+(_?)(/)?)?([a-z0-9]+(_?))+[a-z0-9]*.[ch]\Z', tok):
                        #     tok = "<file_name>" + tok + "</file_name>"
                        if is_valid_filepath(tok):
                            _, f_ext = os.path.splitext(tok)
                            if f_ext == ".c" or f_ext == ".h" or f_ext == ".cpp" or f_ext == ".hpp":
                                tok = "<file_name>" + tok + "</file_name>"
                    # elif re.match('([a-z0-9]+_)+[a-z0-9]*\Z', tok):
                    #     tok = "<variable>"+tok+"</variable>"
                    elif tok.isidentifier():
                        tok = "<variable>" + tok + "</variable>"
                    new_tokens.append(tok)

            new_msg.append(new_tokens)
        return new_msg
예제 #24
0
def export(export_path, default_album, library_path):
    export_path = os.path.expanduser(export_path)
    library_path = os.path.expanduser(library_path) if library_path else None

    if library_path is not None:
        photosdb = osxphotos.PhotosDB(library_path)
    else:
        photosdb = osxphotos.PhotosDB()

    photos = photosdb.photos()

    for p in photos:
        if not p.ismissing:
            albums = p.albums
            if not albums:
                albums = [default_album]
            for album in albums:
                click.echo(f"exporting {p.filename} in album {album}")

                # make sure no invalid characters in destination path (could be in album name)
                album_name = sanitize_filepath(album, platform="auto")

                # create destination folder, if necessary, based on album name
                dest_dir = os.path.join(export_path, album_name)

                # verify path is a valid path
                if not is_valid_filepath(dest_dir, platform="auto"):
                    sys.exit(f"Invalid filepath {dest_dir}")

                # create destination dir if needed
                if not os.path.isdir(dest_dir):
                    os.makedirs(dest_dir)

                # export the photo
                if p.hasadjustments:
                    # export edited version
                    exported = p.export(dest_dir, edited=True)
                    edited_name = pathlib.Path(p.path_edited).name
                    click.echo(f"Exported {edited_name} to {exported}")
                # export unedited version
                exported = p.export(dest_dir)
                click.echo(f"Exported {p.filename} to {exported}")
        else:
            click.echo(f"Skipping missing photo: {p.filename}")
예제 #25
0
    def generate_paths(self, dir_prefix: str, vrd_prefix: str,
                       url_prefix: str):
        """ generate directory path, vrd filepath, url for named publication """

        if not os.path.isdir(dir_prefix):
            raise ValueError(f'pub dir not found: {dir_prefix}')
        if not os.path.isdir(vrd_prefix):
            raise ValueError(f'vrd dir not found: {vrd_prefix}')
        if not is_valid_filepath(url_prefix, platform='posix'):
            raise ValueError(f'url prefix invalid: {url_prefix}')
        if not url_prefix.endswith('/'):
            url_prefix = url_prefix + '/'

        slug_name = slugify(self.name)
        safe_name = sanitize_filename(slug_name, platform='auto')
        safe_name_url = sanitize_filename(slug_name, platform='posix')
        self.directory = os.path.join(dir_prefix, safe_name)
        self.vrd_filename = os.path.join(vrd_prefix, f'{safe_name}.vrd')
        self.url_path = url_prefix + safe_name_url
예제 #26
0
 def test_normal_multibyte(self, value, replace_text, expected):
     sanitized_name = sanitize_filepath(value, replace_text)
     assert sanitized_name == expected
     validate_filepath(sanitized_name)
     assert is_valid_filepath(sanitized_name)
예제 #27
0
 def test_normal_reserved_name_used_valid_place(self, value, platform):
     validate_filepath(value, platform=platform)
     assert is_valid_filepath(value, platform=platform)
예제 #28
0
 def test_normal_reserved_name(self, value, test_platform, expected):
     filename = sanitize_filepath(value, platform=test_platform)
     assert filename == expected
     assert is_valid_filepath(filename, platform=test_platform)
예제 #29
0
 def test_normal_win(self, value):
     platform = "windows"
     validate_filepath(value, platform=platform)
     assert is_valid_filepath(value, platform=platform)
예제 #30
0
 def _is_url_base_valid(self) -> bool:
     return is_valid_filepath(
         self._url_base,
         platform='posix') and self._url_base.startswith('/')
예제 #31
0
 def test_normal_reserved_name(self, value, platform, expected):
     validate_filepath(value, platform=platform)
     assert is_valid_filepath(value, platform=platform)
예제 #32
0
 def test_normal(self, value):
     validate_filepath(value, platform="windows")
     assert is_valid_filepath(value, platform="windows")
예제 #33
0
 def test_exception_invalid_win_char(self, value, platform):
     with pytest.raises(InvalidCharError):
         validate_filepath(value, platform=platform)
     assert not is_valid_filepath(value, platform=platform)
예제 #34
0
 def test_normal_space_or_period_at_tail(self, platform, value):
     validate_filepath(value, platform=platform)
     assert is_valid_filepath(value, platform=platform)
예제 #35
0
 def test_exception(self, value, expected):
     with pytest.raises(ValidationError) as e:
         validate_filepath(value)
     assert e.value.reason == ErrorReason.MALFORMED_ABS_PATH
     assert not is_valid_filepath(value)
예제 #36
0
 def test_normal_reserved_name(self, value, test_platform, expected):
     filename = sanitize_filepath(value, platform=test_platform)
     assert filename == expected
     assert is_valid_filepath(filename, platform=test_platform)
예제 #37
0
 def test_exception_type(self, value, expected):
     with pytest.raises(expected):
         sanitize_filepath(value)
     assert not is_valid_filepath(value)
예제 #38
0
 def test_exception(self, value, expected):
     with pytest.raises(expected):
         validate_filepath(value)
     assert not is_valid_filepath(value)
예제 #39
0
 def test_exception_invalid_win_char(self, value, platform):
     with pytest.raises(InvalidCharError):
         validate_filepath(value, platform=platform)
     assert not is_valid_filepath(value, platform=platform)
예제 #40
0
 def test_exception(self, value, expected):
     with pytest.raises(expected):
         validate_filepath(value)
     assert not is_valid_filepath(value)
예제 #41
0
 def test_normal_space_or_period_at_tail(self, platform, value):
     validate_filepath(value, platform=platform)
     assert is_valid_filepath(value, platform=platform)
예제 #42
0
 def test_normal(self, value):
     validate_filepath(value)
     assert is_valid_filepath(value)
예제 #43
0
def is_valid_filepath(filepath):
    """ returns True if a filepath is valid otherwise False """
    return pathvalidate.is_valid_filepath(filepath, platform="macos")
예제 #44
0
 def test_normal_str(self, platform, value, replace_text, expected):
     sanitized_name = sanitize_filepath(value, platform=platform, replacement_text=replace_text)
     assert sanitized_name == expected
     assert isinstance(sanitized_name, six.text_type)
     validate_filepath(sanitized_name, platform=platform)
     assert is_valid_filepath(sanitized_name, platform=platform)
예제 #45
0
 def test_normal_multibyte(self, value, platform):
     validate_filepath(value, platform)
     assert is_valid_filepath(value, platform=platform)
예제 #46
0
 def test_exception_type(self, value, expected):
     with pytest.raises(expected):
         sanitize_filepath(value)
     assert not is_valid_filepath(value)