Patchwork [03,of,10,V5] rcutil: extract rc directory listing logic

login
register
mail settings
Submitter Jun Wu
Date March 27, 2017, 6:02 a.m.
Message ID <3d461afe19576560b9b4.1490594522@localhost.localdomain>
Download mbox | patch
Permalink /patch/19744/
State Accepted
Headers show

Comments

Jun Wu - March 27, 2017, 6:02 a.m.
# HG changeset patch
# User Jun Wu <quark@fb.com>
# Date 1490586365 25200
#      Sun Mar 26 20:46:05 2017 -0700
# Node ID 3d461afe19576560b9b43b4460dd850abbdbd5fe
# Parent  cd0c8320d216165bd8a62acc711e174ddef7a9f9
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r 3d461afe1957
rcutil: extract rc directory listing logic

The logic of listing a ".rc" directory is duplicated in two functions,
extract it to a single function to make the code cleaner.

Patch

diff --git a/mercurial/rcutil.py b/mercurial/rcutil.py
--- a/mercurial/rcutil.py
+++ b/mercurial/rcutil.py
@@ -25,4 +25,12 @@  systemrcpath = scmplatform.systemrcpath
 userrcpath = scmplatform.userrcpath
 
+def _expandrcpath(path):
+    '''path could be a file or a directory. return a list of file paths'''
+    p = util.expandpath(path)
+    if os.path.isdir(p):
+        join = os.path.join
+        return [join(p, f) for f, k in osutil.listdir(p) if f.endswith('.rc')]
+    return [p]
+
 def defaultrcpath():
     '''return rc paths in default.d'''
@@ -30,7 +38,5 @@  def defaultrcpath():
     defaultpath = os.path.join(util.datapath, 'default.d')
     if os.path.isdir(defaultpath):
-        for f, kind in osutil.listdir(defaultpath):
-            if f.endswith('.rc'):
-                path.append(os.path.join(defaultpath, f))
+        path = _expandrcpath(defaultpath)
     return path
 
@@ -50,11 +56,5 @@  def rcpath():
                 if not p:
                     continue
-                p = util.expandpath(p)
-                if os.path.isdir(p):
-                    for f, kind in osutil.listdir(p):
-                        if f.endswith('.rc'):
-                            _rcpath.append(os.path.join(p, f))
-                else:
-                    _rcpath.append(p)
+                _rcpath.extend(_expandrcpath(p))
         else:
             paths = defaultrcpath() + systemrcpath() + userrcpath()