pyenv-virtualenv for Windows 1.2
A 'pyenv' plugin to manage Python virtual environments, depending on different Python versions, for various Python projects.
Loading...
Searching...
No Matches
tre Namespace Reference

Functions

str getProjectPropertyFileStr (str file_path)
 Get the content of project property file.
 
 tree ((Path, str) root_path, int recursion_level=-1, bool limit_to_directories=False, int length_limit=1000, tuple exclude=())
 Given a directory path, print a visual tree structure.
 

Variables

str space = '\x1b[94m \x1b[0m'
 
str branch = '\x1b[94m│ \x1b[0m'
 
str tee = '\x1b[94m├───\x1b[0m'
 
str last = '\x1b[94m└───\x1b[0m'
 

Function Documentation

◆ getProjectPropertyFileStr()

str tre.getProjectPropertyFileStr ( str file_path)

Get the content of project property file.

Parameters
file_pathPath to project property file.
Returns
Text, which the file is containing or empty string in case of error.

Definition at line 52 of file tre.py.

52def getProjectPropertyFileStr(file_path: str) -> str:
53 result = ''
54 # noinspection PyBroadException
55 try:
56 with open(file_path, 'r') as f:
57 result = f.read().strip()
58 return result
59 except:
60 log.error(sys.exc_info())
61 return ''
62
error((str, tuple) msg)
Log error message colored to console only.
Definition log.py:179

◆ tree()

tre.tree ( (Path, str) root_path,
int recursion_level = -1,
bool limit_to_directories = False,
int length_limit = 1000,
tuple exclude = () )

Given a directory path, print a visual tree structure.

Output and highlight the content of 'pyenv-virtualenv' project property files with colors:

  • LIGHT CYAN: Python version.
  • LIGHT YELLOW: Virtual environment name.
Parameters
root_pathFolder path, from which the tree will be parsed.
recursion_levelMax recursion level to parse. Default: -1 = unlimited recursion.
limit_to_directoriesFlag to permit parsing for folders only. Default: False = parse for both, files and folders
length_limitMax. output length of the tree in lines. Default: 1000.
excludeList of folder names to exclude. Default empty list = nothing to exclude.

Definition at line 78 of file tre.py.

84):
85 # Accept type "str" coercible to "Path"
86 if isinstance(root_path, str):
87 root_path = Path(root_path)
88 # Initialize
89 files = 0
90 directories = 0
91
92 def inner(
93 dir_path: Path,
94 prefix: str= '',
95 level: int = -1,
96 excl: tuple = ()
97 ):
98 nonlocal files, directories
99 if not level:
100 return # 0, stop iterating
101 # Scan for files and folders Path objects
102 if limit_to_directories:
103 objects = dir_path.iterdir()
104 contents = [d for d in objects if d.is_dir()]
105 contents1 = list(objects)
106 else:
107 contents = list(dir_path.iterdir())
108 contents1 = contents
109 # Detect file ".tree-excludes" in files
110 excl1 = excl
111 for content in contents1:
112 if content.is_file() and (content.name == '.tree-excludes'):
113 with open(content, 'r') as f:
114 excl1 = eval(f.read())
115 if content not in contents:
116 contents.insert(0, content)
117 break
118 # End if
119 # End for
120 pointers = [tee] * (len(contents) - 1) + [last]
121 for pointer, path in zip(pointers, contents):
122 if path.is_dir():
123 # Exclude spam folders
124 excluded = False
125 for item in excl1:
126 if path.parts[-1] == item:
127 excluded = True
128 break
129 # End if
130 # End for
131 if excluded: continue
132 # OUTPUT: colorized directory
133 name = path.name
134 if path.is_junction():
135 name = f'\x1b[105m {name} \x1b[0m \x1b[95m→\x1b[0m \x1b[104m {str(path.readlink()).split(f'\\\\?\\')[-1]} \x1b[0m'
136 else: # Is directory
137 name = f'\x1b[104m {name} \x1b[0m'
138 yield prefix + pointer + name
139 directories += 1
140 extension = branch if pointer == tee else space
141 # OUTPUT: path recursively
142 yield from inner(
143 path,
144 prefix=prefix+extension,
145 level=level - 1,
146 excl=excl1
147 )
148 elif not limit_to_directories:
149 # OUTPUT: colorized file
150 name = path.name
151 if path.is_symlink():
152 name1 = f'\x1b[95m● {name}\x1b[0m'
153 if name == '.python-version':
154 name1 += f' \x1b[96m({
155 getProjectPropertyFileStr(
156 str(path)
157 )
158 })\x1b[0m'
159 elif name == '.python-env':
160 name1 += f' \x1b[93m({
161 getProjectPropertyFileStr(
162 str(path)
163 )
164 })\x1b[0m'
165 elif name == '.tree-excludes':
166 name1 += f' \x1b[95m{
167 getProjectPropertyFileStr(
168 str(path)
169 )
170 }\x1b[0m'
171 else:
172 name1 = f'\x1b[95m● {name} → \x1b[37m● {str(path.readlink())}\x1b[0m'
173 else: # Is file
174 name1 = f'\x1b[37m● {name}\x1b[0m'
175 if name == '.python-version':
176 name1 += f' \x1b[96m({
177 getProjectPropertyFileStr(
178 str(path)
179 )
180 })\x1b[0m'
181 elif name == '.python-env':
182 name1 += f' \x1b[93m({
183 getProjectPropertyFileStr(
184 str(path)
185 )
186 })\x1b[0m'
187 elif name == '.tree-excludes':
188 name1 += f' \x1b[95m{
189 getProjectPropertyFileStr(
190 str(path)
191 )
192 }\x1b[0m'
193 else:
194 name1 = f'\x1b[37m● {name}\x1b[0m'
195 yield prefix + pointer + name1
196 files += 1
197
198 # Output the root directory path in the beginning
199 print(f'\x1b[104m {str(root_path)} \x1b[0m')
200 # Exclude spam folders
201 for excl_item in exclude:
202 if root_path.parts[-1] == excl_item:
203 return
204 # Iterate through the folder tree
205 iterator = inner(
206 root_path,
207 level=recursion_level,
208 excl=exclude
209 )
210 # OUTPUT: folder tree
211 for line in islice(iterator, length_limit):
212 print(line)
213 if next(iterator, None):
214 # Obey to the length_limit
215 print(f'... length_limit, {length_limit}, reached, counted:')
216 # OUTPUT: statistics
217 print(
218 f'\n{directories} directories' + (
219 f', {files} files' if files else ''
220 )
221 )
222
223
224# --- END OF CODE ------------------------------------------------------

Variable Documentation

◆ space

str tre.space = '\x1b[94m \x1b[0m'

Definition at line 41 of file tre.py.

◆ branch

str tre.branch = '\x1b[94m│ \x1b[0m'

Definition at line 42 of file tre.py.

◆ tee

str tre.tee = '\x1b[94m├───\x1b[0m'

Definition at line 43 of file tre.py.

◆ last

str tre.last = '\x1b[94m└───\x1b[0m'

Definition at line 44 of file tre.py.