Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create shortcuts to files which dont exist #10

Open
martinohanlon opened this issue Jun 4, 2018 · 16 comments
Open

Create shortcuts to files which dont exist #10

martinohanlon opened this issue Jun 4, 2018 · 16 comments

Comments

@martinohanlon
Copy link
Owner

martinohanlon commented Jun 4, 2018

There are use cases (particularly when creating python packages) where it is required to create shortcuts to files which dont exist [yet].

The full path would have to be defined and a parameter passed which would determine that the shortcut should be created even if the target didnt exist.

Based on requests in #7 from @kiwi0fruit

@martinohanlon
Copy link
Owner Author

create_shortcut, create_menu_shortcut, create_desktop_shortcut should be changed to support an additional parameter target_exists which defaults to True.

If target_exists equals True and error will be raised if the target cannot be found, if target_exists equals False, a shortcut will be created even if the target cannot be found.

@martinohanlon
Copy link
Owner Author

Question @kiwi0fuit?

Should a shortcut to a directory be created if it does not exist?

If so, the target would be considered a directory if the target parameter ended in a os.pathsep (i.e. \ or /)

@kiwi0fruit
Copy link

Do we really need it for files except entry points? If not then will we have separate function that creates dir shortcuts create_shortcut_to_dir or dir creation will be a part of create_shortcut_file? If we would have the separate shortcut to dir method then target_exists can be replaced with create_target=True/False in this method.

@kiwi0fruit
Copy link

kiwi0fruit commented Jun 4, 2018

The needed and convenient part of creating shortcuts for files that do not exist is creating shortcuts to entry points and creating shortcuts to folders and creating that folders at the same time. I cannot imagine why we need it except that.

@kiwi0fruit
Copy link

Oh, I see now that you do not plan separate method for dirs. In this case I only need kwarg like create_target_that_is_dir=True/False to be added to create_shortcut, create_menu_shortcut, create_desktop_shortcut. It's explicit but unfortunately too long.

@kiwi0fruit
Copy link

I suggest closing this and not implementing this. Instead using Python 3.4+ and writing

os.makedirs(path, exist_ok=True)
sc.create_desktop_shortcut(path)

@kiwi0fruit
Copy link

Or even better: add new sc.makedirs(path) method. That is a wrapper around os.makedirs but ignores if exist and more importantly: has same raise/log_error behavior as other ShortCutter methods.

@kiwi0fruit
Copy link

I'm quite satisfied with the latest idea: looks consistent, concise and convenient.

@kiwi0fruit
Copy link

kiwi0fruit commented Jun 4, 2018

class ShortCutter:
    ...
    def makedirs(*args):
        """
        Recursively creates dirs if they don't exist.
        Utilizes self.raise_errors and self.error_log
        
        :param str *args:
            Multiple paths for folders to create.

        Returns True on success False of failure
        """
        ret = True
        for path in args:
            if not os.path.isdir(path):
                if self.raise_errors:
                    os.makedirs(path)
                else:
                    try:
                        os.makedirs(path)
                    except OSError:
                        if self.error_log is not None:
                            self.error_log.write(''.join(traceback.format_exc()))
                        ret = False
    return ret

@martinohanlon
Copy link
Owner Author

I am not convinced shortcut should be creating directories, the API is for creating shortcuts and it makes significant assumptions about how users want directories to be created.

os.makedirs already recursively creates directories recursively.

I will look at the implementation, if its possible to create a shortcut without creating the directory it links too I will otherwise I will create the directory and then the shortcut.

re create_target_that_is_dir - I will determine that a target is a directory by a final os.pathsep (\ or /)

@kiwi0fruit
Copy link

@martinohanlon My latest suggestion is to drop non-existing dirs and files support and add:

  • ShortCutter.makedirs method defined in previous post.
  • Add to three main methods entry_point=True kwarg.

So we do not need to figure out if it's a dir or file - simply check the existing object.

@martinohanlon
Copy link
Owner Author

I understand the requirement for a makedirs method? How is it any different to os.makedirs? It also doesn't seem aligned to a shortcut api.

@kiwi0fruit
Copy link

@martinohanlon See this comment to see how it's different from os.makedirs.

It's convenient to use because it utilizes self.raise_errors and self.error_log attributes set in ShortCutter constructor.

@kiwi0fruit
Copy link

@martinohanlon ShortCutter.makedirs helps create shortcuts to dirs that do not exist yet - it creates dirs in advance.

@kiwi0fruit
Copy link

kiwi0fruit commented Jun 5, 2018

@martinohanlon this all comes from using setup.py for shortcut creation. It's rather inconvenient to implement lots of things in setup.py (even find out where module would be installed - site packages - is not straight-forwad!). But still setup.py is the most logical place to create shortcuts.

PS

All my additions come from using shortcut in setup.py actually :)

@kiwi0fruit
Copy link

@martinohanlon and it's better not to fail the module installation completely if shortcuts creation fails for some reason.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants