Edgewall Software
Modify

Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#12410 closed enhancement (fixed)

Add get_plugins_dir method to Environment class

Reported by: Ryan J Ollos Owned by: Ryan J Ollos
Priority: normal Milestone: 1.0.11
Component: general Version:
Severity: normal Keywords:
Cc: Branch:
Release Notes:
API Changes:
  • Added plugins_dir property to the Environment class, which returns the absolute path to the Environment plugins directory.
  • Added conf_dir property to the Environment class, which returns the absolute path to the Environment conf directory.
  • Deprecated trac.loader.get_plugins_dir. Environment.plugins_dir should be used instead.
  • Added htdocs_dir, log_dir and templates_dir properties to the Environment class and deprecated the get_htdocs_dir, get_log_dir and get_templates_dir methods.
Internal Changes:

Description (last modified by Ryan J Ollos)

The proposed change is a refactoring to replace the function trac.loader.get_plugins_dir with the method trac.env.Environment.get_plugins_dir.

Attachments (0)

Change History (11)

comment:1 by Ryan J Ollos, 9 years ago

Proposed changes in log:rjollos.git:t12410.

I stuck with the existing naming convention even though I think these get_* methods that return paths should instead be lazily-evaluated properties.

Should realpath and normpath be called in the similar methods get_templates_dir, get_htdocs_dir, get_log_dir and get_plugins_dir? We could add a _get_path_to_dir method:

def _get_path_to_dir(dir)
    return os.path.normcase(os.path.realpath(os.path.join(self.path, dir)))

def get_plugins_dir(self):
    return self._get_path_to_dir('plugins')

comment:2 by Ryan J Ollos, 9 years ago

Description: modified (diff)

comment:3 by Ryan J Ollos, 9 years ago

API Changes: modified (diff)

comment:4 by Ryan J Ollos, 9 years ago

Added comment:1 changes in [962e14e1/rjollos.git].

comment:5 by Christian Boos, 9 years ago

Sure.

I wonder how many times these methods are called, but now that you added a helper method, it might be a cheap optimization to memoize these calls, e.g.

    def _get_path_to_dir(self, dir):
       """Retrieve and memoize normalized path to immediate subdir of self.path"""
        _dir = '_%s_dir' % dir
        if not hasattr(self, _dir):
            path = os.path.join(self.path, dir)
            setattr(self, _dir, os.path.normcase(os.path.realpath(path)))
        return getattr(self, _dir)

comment:6 by Ryan J Ollos, 9 years ago

Committed to 1.0-stable in [14613], merged to trunk in [14614]. Deprecated function get_plugins_dir will be removed in #11901.

in reply to:  5 comment:7 by Ryan J Ollos, 9 years ago

Replying to Christian Boos:

I wonder how many times these methods are called, but now that you added a helper method, it might be a cheap optimization to memoize these calls, e.g.

In general we seem to follow the undocumented pattern of using properties for short computations or those that can be cached, and get_ for lengthier computations, database access or methods that could raise an exception.

Therefore, for API consistency it might make sense to just deprecate the 4 methods and add lazily-evaluated properties templates_dir, htdocs_dir and plugins_dir, rather than implementing lazy evaluation for the methods. It's unlikely many plugins access those methods anyway, so removing them in 1.3.1 shouldn't have a big impact.

comment:8 by Ryan J Ollos, 9 years ago

Correction: Technically we only have to deprecate 3 methods since the 4th was just added.

comment:9 by Ryan J Ollos, 9 years ago

I prepared the changes proposed in comment:7: log:rjollos.git:t12410.2.

comment:10 by Ryan J Ollos, 9 years ago

API Changes: modified (diff)
Resolution: fixed
Status: assignedclosed

Committed additional changes to 1.0-stable in [14637], merged to trunk in [14638].

comment:11 by Ryan J Ollos, 9 years ago

Additional changes in [14644], [14645].

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain Ryan J Ollos.
The resolution will be deleted. Next status will be 'reopened'.
to The owner will be changed from Ryan J Ollos to the specified user.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.