mirror of
https://github.com/erkin/ponysay
synced 2024-12-03 16:49:17 +00:00
list.py: Extracted duplicate code for listing files in a directory into a utility function.
This commit is contained in:
parent
63b852068d
commit
c71a8a0a67
1 changed files with 18 additions and 40 deletions
58
src/lists.py
58
src/lists.py
|
@ -90,6 +90,17 @@ def _columnise(ponies):
|
||||||
print()
|
print()
|
||||||
|
|
||||||
|
|
||||||
|
def _get_file_list(dir_path : str, extension : str):
|
||||||
|
"""
|
||||||
|
Return a list of files in the specified directory with have the specified file name extension.
|
||||||
|
|
||||||
|
:param dir_path: Path to the directory.
|
||||||
|
:param extension: The allowed file name extension.
|
||||||
|
"""
|
||||||
|
|
||||||
|
return [i[:-len(extension)] for i in os.listdir(dir_path) if endswith(i, extension)]
|
||||||
|
|
||||||
|
|
||||||
def simplelist(ponydirs, quoters = [], ucsiser = None):
|
def simplelist(ponydirs, quoters = [], ucsiser = None):
|
||||||
'''
|
'''
|
||||||
Lists the available ponies
|
Lists the available ponies
|
||||||
|
@ -100,13 +111,9 @@ def simplelist(ponydirs, quoters = [], ucsiser = None):
|
||||||
'''
|
'''
|
||||||
for ponydir in ponydirs: # Loop ponydirs
|
for ponydir in ponydirs: # Loop ponydirs
|
||||||
## Get all ponies in the directory
|
## Get all ponies in the directory
|
||||||
_ponies = os.listdir(ponydir)
|
ponies = _get_file_list(ponydir, '.pony')
|
||||||
|
|
||||||
## Remove .pony from all files and skip those that does not have .pony
|
print()
|
||||||
ponies = []
|
|
||||||
for pony in _ponies:
|
|
||||||
if endswith(pony, '.pony'):
|
|
||||||
ponies.append(pony[:-5])
|
|
||||||
|
|
||||||
## UCS:ise pony names, they are already sorted
|
## UCS:ise pony names, they are already sorted
|
||||||
if ucsiser is not None:
|
if ucsiser is not None:
|
||||||
|
@ -130,13 +137,7 @@ def linklist(ponydirs = None, quoters = [], ucsiser = None):
|
||||||
|
|
||||||
for ponydir in ponydirs: # Loop ponydirs
|
for ponydir in ponydirs: # Loop ponydirs
|
||||||
## Get all pony files in the directory
|
## Get all pony files in the directory
|
||||||
_ponies = os.listdir(ponydir)
|
ponies = _get_file_list(ponydirs, '.pony')
|
||||||
|
|
||||||
## Remove .pony from all files and skip those that does not have .pony
|
|
||||||
ponies = []
|
|
||||||
for pony in _ponies:
|
|
||||||
if endswith(pony, '.pony'):
|
|
||||||
ponies.append(pony[:-5])
|
|
||||||
|
|
||||||
## If there are no ponies in the directory skip to next directory, otherwise, print the directories name
|
## If there are no ponies in the directory skip to next directory, otherwise, print the directories name
|
||||||
if len(ponies) == 0:
|
if len(ponies) == 0:
|
||||||
|
@ -203,19 +204,7 @@ def onelist(standarddirs, extradirs = None, ucsiser = None):
|
||||||
@param ucsiser:(list<str>)?→void Function used to UCS:ise names
|
@param ucsiser:(list<str>)?→void Function used to UCS:ise names
|
||||||
'''
|
'''
|
||||||
## Get all pony files
|
## Get all pony files
|
||||||
_ponies = []
|
ponies = [j for i in [standarddirs, extradirs] for j in _get_file_list(i, '.pony')]
|
||||||
if standarddirs is not None:
|
|
||||||
for ponydir in standarddirs:
|
|
||||||
_ponies += os.listdir(ponydir)
|
|
||||||
if extradirs is not None:
|
|
||||||
for ponydir in extradirs:
|
|
||||||
_ponies += os.listdir(ponydir)
|
|
||||||
|
|
||||||
## Remove .pony from all files and skip those that does not have .pony
|
|
||||||
ponies = []
|
|
||||||
for pony in _ponies:
|
|
||||||
if endswith(pony, '.pony'):
|
|
||||||
ponies.append(pony[:-5])
|
|
||||||
|
|
||||||
## UCS:ise and sort
|
## UCS:ise and sort
|
||||||
if ucsiser is not None:
|
if ucsiser is not None:
|
||||||
|
@ -238,21 +227,10 @@ def balloonlist(balloondirs, isthink):
|
||||||
@param isthink:bool Whether the ponythink command is used
|
@param isthink:bool Whether the ponythink command is used
|
||||||
'''
|
'''
|
||||||
|
|
||||||
## Get all balloons
|
extension = '.think' if isthink else '.say'
|
||||||
balloonset = set()
|
|
||||||
for balloondir in balloondirs:
|
|
||||||
for balloon in os.listdir(balloondir):
|
|
||||||
## Use .think if running ponythink, otherwise .say
|
|
||||||
if isthink and endswith(balloon, '.think'):
|
|
||||||
balloon = balloon[:-6]
|
|
||||||
elif (not isthink) and endswith(balloon, '.say'):
|
|
||||||
balloon = balloon[:-4]
|
|
||||||
else:
|
|
||||||
continue
|
|
||||||
|
|
||||||
## Add the balloon if there is none with the same name
|
## Get all balloons
|
||||||
if balloon not in balloonset:
|
balloonset = set(j for i in balloondirs for j in _get_file_list(i, extension))
|
||||||
balloonset.add(balloon)
|
|
||||||
|
|
||||||
## Print all balloos, columnised
|
## Print all balloos, columnised
|
||||||
_columnise([(balloon, balloon) for balloon in balloonset])
|
_columnise([(balloon, balloon) for balloon in balloonset])
|
||||||
|
|
Loading…
Reference in a new issue