1st setup for tracklist reading

This commit is contained in:
Jan
2019-09-25 16:43:37 +02:00
parent 3edf9ba34c
commit 1555050909
2 changed files with 52 additions and 48 deletions

View File

@@ -1,63 +1,62 @@
#!/usr/bin/python #!/usr/bin/python
import json from pathlib import Path
import os
import time
import copy
EXTENSIONS=("mp3","ogg","wav","mpa","m4a","flac") EXTENSIONS=(".mp3",".ogg",".wav",".mpa",".m4a",".flac")
class ClockyTracklist: class ClockyTracklist:
"""read and monitor directory with audio files""" """read and monitor directory with audio files"""
def __init__(self,path=".",recursive=False):
def __init__(self,path="."): """Read (current or supplied) path for all audio files. Add recursive=True to check all subdirectories too"""
"""do the reading""" self.recursive=recursive
self.path=path self.path=path
self.tracklist=[] self.tracklist=[]
self.pathlist=[]
self.count=0 self.count=0
olddir=os.getcwd() p=Path(self.path)
if(self.path != '.'): if(not p.is_dir()):
if(not os.path.isdir(self.path)): print("ERROR: \""+path+"\" is not an directory")
print("ERROR: \""+self.path+"\" is not an existing directory") exit();
exit();
os.chdir(self.path) if(self.recursive):
selection='**/*'
for ent in os.listdir(): else:
if ent.lower().endswith(EXTENSIONS) and os.access(ent,os.R_OK): selection='*'
self.tracklist.append(ent) for Q in p.glob(selection):
self.pathlist.append(os.path.join(self.path,ent)) if Q.suffix in EXTENSIONS: #and os.access(Q,os.R_OK):
self.tracklist.append(Q.as_posix())
self.count+=1 self.count+=1
os.chdir(olddir) def ReadTrackList(self):
def Show(self):
"""Print the list""" """Print the list"""
print("Tracklist.path = %s" %self.path) path=Path(self.path)
print("Tracklist.count= %d" %self.count) if(not path.is_dir()):
print("Tracks:") print("ERROR: \""+self.path+"\" is not an directory")
for track in self.tracklist: exit();
print (" "+track) if(self.recursive):
fn=self.path+'\\'+track selection='**/*'
if os.access(fn,os.R_OK): else:
print(fn+"!") selection='*'
else: for entry in path.glob(selection):
print(fn+"-") if entry.suffix in EXTENSIONS:
fn=self.path+'/'+track self.tracklist.append(entry.as_posix())
if os.access(fn,os.R_OK): self.count+=1
print(fn+"!")
else:
print(fn+"-") def ShowTrackList(self):
"""Print the list"""
print("Paths:") print("Tracklist:")
for track in self.pathlist: print(" path = %s" %self.path)
print (" "+track) print(" recursive = %s" %self.recursive)
print(" count = %d" %self.count)
print(" tracks:")
for track in self.tracklist:
print(" "+track)
f=open(track,"r") f=open(track,"r")
if(f): if(f):
print("OK")
f.close() f.close()
def CheckTrackList(self):
"""Check path for filechanges"""

View File

@@ -2,6 +2,11 @@
import clockytracklist import clockytracklist
print("-------------------------------------")
print("-------------------------------------")
tl=clockytracklist.ClockyTracklist("mp3",True)
tl.ShowTrackList()
print("-------------------------------------")
t2=clockytracklist.ClockyTracklist("C:\data",True)
t2.ShowTrackList()
tl=clockytracklist.ClockyTracklist("mp3")
tl.Show()