From 3edf9ba34c080ad7b83cd085106f3a80424c9199 Mon Sep 17 00:00:00 2001 From: Jan Date: Wed, 25 Sep 2019 12:32:18 +0200 Subject: [PATCH 1/3] 1st test of track stuff --- clockytracklist.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++ tracktest.py | 7 ++++++ 2 files changed, 70 insertions(+) create mode 100644 clockytracklist.py create mode 100644 tracktest.py diff --git a/clockytracklist.py b/clockytracklist.py new file mode 100644 index 0000000..8141b3a --- /dev/null +++ b/clockytracklist.py @@ -0,0 +1,63 @@ +#!/usr/bin/python + +import json +import os +import time +import copy + +EXTENSIONS=("mp3","ogg","wav","mpa","m4a","flac") + +class ClockyTracklist: + """read and monitor directory with audio files""" + + def __init__(self,path="."): + """do the reading""" + self.path=path + self.tracklist=[] + self.pathlist=[] + self.count=0 + + olddir=os.getcwd() + if(self.path != '.'): + if(not os.path.isdir(self.path)): + print("ERROR: \""+self.path+"\" is not an existing directory") + exit(); + os.chdir(self.path) + + for ent in os.listdir(): + if ent.lower().endswith(EXTENSIONS) and os.access(ent,os.R_OK): + self.tracklist.append(ent) + self.pathlist.append(os.path.join(self.path,ent)) + self.count+=1 + + os.chdir(olddir) + + + def Show(self): + """Print the list""" + print("Tracklist.path = %s" %self.path) + print("Tracklist.count= %d" %self.count) + print("Tracks:") + for track in self.tracklist: + + print (" "+track) + fn=self.path+'\\'+track + if os.access(fn,os.R_OK): + print(fn+"!") + else: + print(fn+"-") + fn=self.path+'/'+track + if os.access(fn,os.R_OK): + print(fn+"!") + else: + print(fn+"-") + + print("Paths:") + for track in self.pathlist: + print (" "+track) + + f=open(track,"r") + if(f): + print("OK") + f.close() + diff --git a/tracktest.py b/tracktest.py new file mode 100644 index 0000000..5751684 --- /dev/null +++ b/tracktest.py @@ -0,0 +1,7 @@ +#!/usr/bin/python + +import clockytracklist + + +tl=clockytracklist.ClockyTracklist("mp3") +tl.Show() From 1555050909deac757dc7b695b620e3b6fa677e18 Mon Sep 17 00:00:00 2001 From: Jan Date: Wed, 25 Sep 2019 16:43:37 +0200 Subject: [PATCH 2/3] 1st setup for tracklist reading --- clockytracklist.py | 91 +++++++++++++++++++++++----------------------- tracktest.py | 9 ++++- 2 files changed, 52 insertions(+), 48 deletions(-) diff --git a/clockytracklist.py b/clockytracklist.py index 8141b3a..d0470e6 100644 --- a/clockytracklist.py +++ b/clockytracklist.py @@ -1,63 +1,62 @@ #!/usr/bin/python -import json -import os -import time -import copy +from pathlib import Path -EXTENSIONS=("mp3","ogg","wav","mpa","m4a","flac") +EXTENSIONS=(".mp3",".ogg",".wav",".mpa",".m4a",".flac") class ClockyTracklist: """read and monitor directory with audio files""" - - def __init__(self,path="."): - """do the reading""" + def __init__(self,path=".",recursive=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.tracklist=[] - self.pathlist=[] self.count=0 - olddir=os.getcwd() - if(self.path != '.'): - if(not os.path.isdir(self.path)): - print("ERROR: \""+self.path+"\" is not an existing directory") - exit(); - os.chdir(self.path) - - for ent in os.listdir(): - if ent.lower().endswith(EXTENSIONS) and os.access(ent,os.R_OK): - self.tracklist.append(ent) - self.pathlist.append(os.path.join(self.path,ent)) + p=Path(self.path) + if(not p.is_dir()): + print("ERROR: \""+path+"\" is not an directory") + exit(); + + if(self.recursive): + selection='**/*' + else: + selection='*' + for Q in p.glob(selection): + if Q.suffix in EXTENSIONS: #and os.access(Q,os.R_OK): + self.tracklist.append(Q.as_posix()) self.count+=1 - os.chdir(olddir) - - - def Show(self): + def ReadTrackList(self): """Print the list""" - print("Tracklist.path = %s" %self.path) - print("Tracklist.count= %d" %self.count) - print("Tracks:") - for track in self.tracklist: + path=Path(self.path) + if(not path.is_dir()): + print("ERROR: \""+self.path+"\" is not an directory") + exit(); - print (" "+track) - fn=self.path+'\\'+track - if os.access(fn,os.R_OK): - print(fn+"!") - else: - print(fn+"-") - fn=self.path+'/'+track - if os.access(fn,os.R_OK): - print(fn+"!") - else: - print(fn+"-") - - print("Paths:") - for track in self.pathlist: - print (" "+track) - + 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): - print("OK") f.close() - + + def CheckTrackList(self): + """Check path for filechanges""" + \ No newline at end of file diff --git a/tracktest.py b/tracktest.py index 5751684..4bee807 100644 --- a/tracktest.py +++ b/tracktest.py @@ -2,6 +2,11 @@ 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() From 4a5498866caa65bd8ff662274f03e19c73408ac5 Mon Sep 17 00:00:00 2001 From: Jan Date: Wed, 25 Sep 2019 17:11:51 +0200 Subject: [PATCH 3/3] added ChecktTrackList --- clockytracklist.py | 43 ++++++++++++++++++++++++++----------------- tracktest.py | 20 +++++++++++++++++--- 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/clockytracklist.py b/clockytracklist.py index d0470e6..37137a8 100644 --- a/clockytracklist.py +++ b/clockytracklist.py @@ -6,33 +6,27 @@ EXTENSIONS=(".mp3",".ogg",".wav",".mpa",".m4a",".flac") class ClockyTracklist: """read and monitor directory with audio files""" - def __init__(self,path=".",recursive=False): + 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.tracklist=[] - self.count=0 + self.__verbose=verbose p=Path(self.path) if(not p.is_dir()): print("ERROR: \""+path+"\" is not an directory") exit(); - if(self.recursive): - selection='**/*' - else: - selection='*' - for Q in p.glob(selection): - if Q.suffix in EXTENSIONS: #and os.access(Q,os.R_OK): - self.tracklist.append(Q.as_posix()) - self.count+=1 + self.__ReadTrackList() + if(self.__verbose): + self.ShowTrackList() - def ReadTrackList(self): - """Print the list""" + + def __ReadTrackList(self): + """do the actual reading""" + self.tracklist=[] + self.count=0 path=Path(self.path) - if(not path.is_dir()): - print("ERROR: \""+self.path+"\" is not an directory") - exit(); if(self.recursive): selection='**/*' @@ -59,4 +53,19 @@ class ClockyTracklist: def CheckTrackList(self): """Check path for filechanges""" - \ No newline at end of file + 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.") diff --git a/tracktest.py b/tracktest.py index 4bee807..6885c38 100644 --- a/tracktest.py +++ b/tracktest.py @@ -1,12 +1,26 @@ #!/usr/bin/python +import time import clockytracklist print("-------------------------------------") print("-------------------------------------") -tl=clockytracklist.ClockyTracklist("mp3",True) -tl.ShowTrackList() +t1=clockytracklist.ClockyTracklist("mp3",True,True) +t1.ShowTrackList() print("-------------------------------------") -t2=clockytracklist.ClockyTracklist("C:\data",True) +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) +