Merge branch 'clockytracklist'

This commit is contained in:
Jan
2019-09-26 14:01:03 +02:00
2 changed files with 97 additions and 0 deletions

71
clockytracklist.py Normal file
View File

@@ -0,0 +1,71 @@
#!/usr/bin/python
from pathlib import Path
EXTENSIONS=(".mp3",".ogg",".wav",".mpa",".m4a",".flac")
class ClockyTracklist:
"""read and monitor directory with audio files"""
def __init__(self,path=".",recursive=False,verbose=False):
"""Read (current or supplied) path for all audio files. Add recursive=True to check all subdirectories too"""
self.recursive=recursive
self.path=path
self.__verbose=verbose
p=Path(self.path)
if(not p.is_dir()):
print("ERROR: \""+path+"\" is not an directory")
exit();
self.__ReadTrackList()
if(self.__verbose):
self.ShowTrackList()
def __ReadTrackList(self):
"""do the actual reading"""
self.tracklist=[]
self.count=0
path=Path(self.path)
if(self.recursive):
selection='**/*'
else:
selection='*'
for entry in path.glob(selection):
if entry.suffix in EXTENSIONS:
self.tracklist.append(entry.as_posix())
self.count+=1
def ShowTrackList(self):
"""Print the list"""
print("Tracklist:")
print(" path = %s" %self.path)
print(" recursive = %s" %self.recursive)
print(" count = %d" %self.count)
print(" tracks:")
for track in self.tracklist:
print(" "+track)
f=open(track,"r")
if(f):
f.close()
def CheckTrackList(self):
"""Check path for filechanges"""
newtracklist=[]
newcount=0
newpath=Path(self.path)
if(self.recursive):
selection='**/*'
else:
selection='*'
for newentry in newpath.glob(selection):
if newentry.suffix in EXTENSIONS:
newtracklist.append(newentry.as_posix())
newcount+=1
if(newcount != self.count) or (newtracklist != self.tracklist):
self.__ReadTrackList()
if(self.__verbose):
print("Changes in filenames or number of files detected! Re-read tracklist.")

26
tracktest.py Normal file
View File

@@ -0,0 +1,26 @@
#!/usr/bin/python
import time
import clockytracklist
print("-------------------------------------")
print("-------------------------------------")
t1=clockytracklist.ClockyTracklist("mp3",True,True)
t1.ShowTrackList()
print("-------------------------------------")
t2=clockytracklist.ClockyTracklist("C:\data",True,True)
t2.ShowTrackList()
t=0
while True:
t+=1
t1.CheckTrackList()
t2.CheckTrackList()
time.sleep(1)
if(t>30):
break
print(t)