comparison mercurial/scmwindows.py @ 49808:7a4143428db7

typing: add type hints to the platform specific scm modules Surprisingly, pytype struggled to figure out the return types in the posix functions.
author Matt Harbison <matt_harbison@yahoo.com>
date Thu, 15 Dec 2022 15:41:59 -0500
parents d500df2e8034
children 7a80a614c9e5
comparison
equal deleted inserted replaced
49807:c5a06cc37401 49808:7a4143428db7
1 import os 1 import os
2
3 from typing import (
4 List,
5 Tuple,
6 )
2 7
3 from . import ( 8 from . import (
4 encoding, 9 encoding,
5 pycompat, 10 pycompat,
6 util, 11 util,
7 win32, 12 win32,
8 ) 13 )
14
15 if pycompat.TYPE_CHECKING:
16 from . import ui as uimod
9 17
10 try: 18 try:
11 import _winreg as winreg # pytype: disable=import-error 19 import _winreg as winreg # pytype: disable=import-error
12 20
13 winreg.CloseKey 21 winreg.CloseKey
17 25
18 # MS-DOS 'more' is the only pager available by default on Windows. 26 # MS-DOS 'more' is the only pager available by default on Windows.
19 fallbackpager = b'more' 27 fallbackpager = b'more'
20 28
21 29
22 def systemrcpath(): 30 def systemrcpath() -> List[bytes]:
23 '''return default os-specific hgrc search path''' 31 '''return default os-specific hgrc search path'''
24 rcpath = [] 32 rcpath = []
25 filename = win32.executablepath() 33 filename = win32.executablepath()
26 # Use mercurial.ini found in directory with hg.exe 34 # Use mercurial.ini found in directory with hg.exe
27 progrc = os.path.join(os.path.dirname(filename), b'mercurial.ini') 35 progrc = os.path.join(os.path.dirname(filename), b'mercurial.ini')
28 rcpath.append(progrc) 36 rcpath.append(progrc)
29 37
30 def _processdir(progrcd): 38 def _processdir(progrcd: bytes) -> None:
31 if os.path.isdir(progrcd): 39 if os.path.isdir(progrcd):
32 for f, kind in sorted(util.listdir(progrcd)): 40 for f, kind in sorted(util.listdir(progrcd)):
33 if f.endswith(b'.rc'): 41 if f.endswith(b'.rc'):
34 rcpath.append(os.path.join(progrcd, f)) 42 rcpath.append(os.path.join(progrcd, f))
35 43
66 else: 74 else:
67 _processdir(p) 75 _processdir(p)
68 return rcpath 76 return rcpath
69 77
70 78
71 def userrcpath(): 79 def userrcpath() -> List[bytes]:
72 '''return os-specific hgrc search path to the user dir''' 80 '''return os-specific hgrc search path to the user dir'''
73 home = _legacy_expanduser(b'~') 81 home = _legacy_expanduser(b'~')
74 path = [os.path.join(home, b'mercurial.ini'), os.path.join(home, b'.hgrc')] 82 path = [os.path.join(home, b'mercurial.ini'), os.path.join(home, b'.hgrc')]
75 userprofile = encoding.environ.get(b'USERPROFILE') 83 userprofile = encoding.environ.get(b'USERPROFILE')
76 if userprofile and userprofile != home: 84 if userprofile and userprofile != home:
77 path.append(os.path.join(userprofile, b'mercurial.ini')) 85 path.append(os.path.join(userprofile, b'mercurial.ini'))
78 path.append(os.path.join(userprofile, b'.hgrc')) 86 path.append(os.path.join(userprofile, b'.hgrc'))
79 return path 87 return path
80 88
81 89
82 def _legacy_expanduser(path): 90 def _legacy_expanduser(path: bytes) -> bytes:
83 """Expand ~ and ~user constructs in the pre 3.8 style""" 91 """Expand ~ and ~user constructs in the pre 3.8 style"""
84 92
85 # Python 3.8+ changed the expansion of '~' from HOME to USERPROFILE. See 93 # Python 3.8+ changed the expansion of '~' from HOME to USERPROFILE. See
86 # https://bugs.python.org/issue36264. It also seems to capitalize the drive 94 # https://bugs.python.org/issue36264. It also seems to capitalize the drive
87 # letter, as though it was processed through os.path.realpath(). 95 # letter, as though it was processed through os.path.realpath().
109 userhome = os.path.join(os.path.dirname(userhome), path[1:i]) 117 userhome = os.path.join(os.path.dirname(userhome), path[1:i])
110 118
111 return userhome + path[i:] 119 return userhome + path[i:]
112 120
113 121
114 def termsize(ui): 122 def termsize(ui: "uimod.ui") -> Tuple[int, int]:
115 return win32.termsize() 123 return win32.termsize()