]> git.parisson.com Git - deefuzzer.git/commitdiff
Whitespace and formatting fixes to better meet PEP8 Coding Style
authorachbed <github@achbed.org>
Sun, 25 Jan 2015 21:40:13 +0000 (15:40 -0600)
committerachbed <github@achbed.org>
Sun, 25 Jan 2015 21:40:13 +0000 (15:40 -0600)
Signed-off-by: achbed <github@achbed.org>
README.rst
deefuzzer/core.py
deefuzzer/station.py
scripts/deefuzzer

index e9715d4087760d2004509378259ff465f8748c0e..9f877c219220944ce6cc90068c460784d7b300c0 100644 (file)
@@ -30,7 +30,7 @@ Please see example/deefuzzer.xml for an example.
 Because our aim is to get DeeFuzzer as light as possible it is NOT capable of re-encoding or transcoding media files.
 
 News
-=====
+====
 
 0.6.6
 
@@ -144,7 +144,7 @@ or, more specificially::
 
 
 Configuration
-==============
+=============
 
 Some examples of markup configuration files:
 
@@ -178,7 +178,7 @@ from a console or any application (see deefuzzer/scripts/).
 
 
 Twitter (manual and optional)
-================================
+=============================
 
 To get track twitting, please install python-twitter, python-oauth2 and all their dependencies.
 
@@ -261,7 +261,7 @@ API
 http://files.parisson.com/doc/deefuzzer/
 
 Development
-============
+===========
 
 Everybody is welcome to participate to the DeeFuzzer project!
 We use GitHub to collaborate: https://github.com/yomguy/DeeFuzzer
index ab9547484d22990cce1ddb00359a281b5f457ac4..7fbaf07461fa354a6994387285f3796fb6ea51f4 100644 (file)
@@ -118,9 +118,7 @@ class DeeFuzzer(Thread):
 
     def _log(self, level, msg):
         try:
-            obj = {}
-            obj['msg'] = 'Core: ' + str(msg)
-            obj['level'] = level
+            obj = {'msg': 'Core: ' + str(msg), 'level': level}
             self.logqueue.put(obj)
         except:
             pass
@@ -295,8 +293,8 @@ class DeeFuzzer(Thread):
                                     self._err('Station ' + name + ' is stopped and will not be restarted.')
                                     self.station_settings[i]['station_stop_logged'] = True
                                 continue
-                                
-                            self.station_settings[i]['retries'] = self.station_settings[i]['retries'] + 1
+
+                            self.station_settings[i]['retries'] += 1
                             self._info('Restarting station ' + name + ' (try ' + str(self.station_settings[i]['retries']) + ')')
                     except Exception as e:
                         self._err('Error checking status for ' + name)
@@ -316,7 +314,7 @@ class DeeFuzzer(Thread):
                                 name = self.station_settings[i]['infos']['short_name']
                                 y = 1
                                 while name in self.station_instances.keys():
-                                    y = y + 1
+                                    y += 1
                                     name = self.station_settings[i]['infos']['short_name'] + " " + str(y)
 
                         self.station_settings[i]['station_name'] = name
index 9ab3ab65d26c014b7feac7b0691d7d82fb39c8b2..36124ddb8adf6bd779c4525cc740d35342ea2e95 100644 (file)
@@ -99,7 +99,7 @@ class Station(Thread):
             self.statusfile = station['station_statusfile']
             try:
                 if os.path.exists(self.statusfile):
-                    f = open(self.statusfile,'r')
+                    f = open(self.statusfile, 'r')
                     self.starting_id = int(f.read())
                     f.close()
             except:
@@ -107,20 +107,20 @@ class Station(Thread):
 
         if 'base_dir' in self.station:
             self.base_directory = self.station['base_dir'].strip()
-        
+
         # Media
         if 'm3u' in self.station['media'].keys():
             if not self.station['media']['m3u'].strip() == '':
                 self.media_source = self._path_add_base(self.station['media']['m3u'])
-        
+
         if 'dir' in self.station['media'].keys():
             if not self.station['media']['dir'].strip() == '':
                 self.media_source = self._path_add_base(self.station['media']['dir'])
-        
+
         if 'source' in self.station['media'].keys():
             if not self.station['media']['source'].strip() == '':
                 self.media_source = self._path_add_base(self.station['media']['source'])
-        
+
         self.media_format = self.station['media']['format']
         self.shuffle_mode = int(self.station['media']['shuffle'])
         self.bitrate = int(self.station['media']['bitrate'])
@@ -142,7 +142,7 @@ class Station(Thread):
             self.appendtype = int(self.station['server']['appendtype'])
 
         if 'type' in self.station['server']:
-            self.type = self.station['server']['type']  'icecast' | 'stream-m'
+            self.type = self.station['server']['type']  # 'icecast' | 'stream-m'
         else:
             self.type = 'icecast'
 
@@ -169,14 +169,14 @@ class Station(Thread):
         self.channel.password = self.station['server']['sourcepassword']
         self.channel.public = int(self.station['server']['public'])
         if self.channel.format == 'mp3':
-            self.channel.audio_info = { 'bitrate': str(self.bitrate),
-                                        'samplerate': str(self.samplerate),
-                                        'channels': str(self.voices),}
+            self.channel.audio_info = {'bitrate': str(self.bitrate),
+                                       'samplerate': str(self.samplerate),
+                                       'channels': str(self.voices), }
         else:
-            self.channel.audio_info = { 'bitrate': str(self.bitrate),
-                                        'samplerate': str(self.samplerate),
-                                        'quality': str(self.ogg_quality),
-                                        'channels': str(self.voices),}
+            self.channel.audio_info = {'bitrate': str(self.bitrate),
+                                       'samplerate': str(self.samplerate),
+                                       'quality': str(self.ogg_quality),
+                                       'channels': str(self.voices), }
 
         self.server_url = 'http://' + self.channel.host + ':' + str(self.channel.port)
         self.channel_url = self.server_url + self.channel.mount
@@ -242,7 +242,7 @@ class Station(Thread):
         # Jingling between each media.
         if 'jingles' in self.station:
             if 'mode' in self.station['jingles']:
-                self.jingles_mode =  int(self.station['jingles']['mode'])
+                self.jingles_mode = int(self.station['jingles']['mode'])
             if 'shuffle' in self.station['jingles']:
                 self.jingles_shuffle = int(self.station['jingles']['shuffle'])
             if 'frequency' in self.station['jingles']:
@@ -268,7 +268,7 @@ class Station(Thread):
             self.twitter_tags = self.station['twitter']['tags'].split(' ')
             try:
                 self.twitter_messages = self.station['twitter']['message']
-                if isinstance(self.twitter_messages,  dict):
+                if isinstance(self.twitter_messages, dict):
                     self.twitter_messages = list(self.twitter_messages)
             except:
                 pass
@@ -284,18 +284,16 @@ class Station(Thread):
                 self.record_callback('/record', [1])
 
         self.valid = True
-        
+
     def _path_add_base(self, a):
         return os.path.join(self.base_directory, a)
-        
+
     def _path_m3u_rel(self, a):
         return os.path.join(os.path.dirname(self.source), a)
-        
+
     def _log(self, level, msg):
         try:
-            obj = {}
-            obj['msg'] = 'Station ' + str(self.channel_url) + ': ' + str(msg)
-            obj['level'] = str(level)
+            obj = {'msg': 'Station ' + str(self.channel_url) + ': ' + str(msg), 'level': str(level)}
             self.logqueue.put(obj)
         except:
             pass
@@ -341,7 +339,7 @@ class Station(Thread):
         self.twitter = Twitter(self.twitter_key, self.twitter_secret)
         self.twitter_mode = value
         message = "received OSC message '%s' with arguments '%d'" % (path, value)
-        
+
         # IMPROVEMENT: The URL paths should be configurable because they're 
         # server-implementation specific
         self.m3u_url = self.channel.url + '/m3u/' + self.m3u.split(os.sep)[-1]
@@ -363,9 +361,9 @@ class Station(Thread):
         if value:
             if not os.path.exists(self.record_dir):
                 os.makedirs(self.record_dir)
-            self.rec_file = self.short_name.replace('/', '_') + '-' + \
-              datetime.datetime.now().strftime("%x-%X").replace('/', '_') + \
-                                                '.' + self.channel.format
+            self.rec_file = self.short_name.replace('/', '_') + '-'
+            self.rec_file += datetime.datetime.now().strftime("%x-%X").replace('/', '_')
+            self.rec_file += '.' + self.channel.format
             self.recorder = Recorder(self.record_dir)
             self.recorder.open(self.rec_file)
         else:
@@ -382,10 +380,10 @@ class Station(Thread):
                 if self.channel.format == 'ogg':
                     media = Ogg(self.record_dir + os.sep + self.rec_file)
                 media.metadata = {'artist': self.artist.encode('utf-8'),
-                                    'title': self.title.encode('utf-8'),
-                                    'album': self.short_name.encode('utf-8'),
-                                    'genre': self.channel.genre.encode('utf-8'),
-                                    'date' : date.encode('utf-8'),}
+                                  'title': self.title.encode('utf-8'),
+                                  'album': self.short_name.encode('utf-8'),
+                                  'genre': self.channel.genre.encode('utf-8'),
+                                  'date': date.encode('utf-8'), }
                 media.write_tags()
 
         self.record_mode = value
@@ -400,7 +398,7 @@ class Station(Thread):
 
     def get_playlist(self):
         file_list = []
-        
+
         try:
             if os.path.isdir(self.media_source):
                 self.q.get(1)
@@ -408,14 +406,14 @@ class Station(Thread):
                     for root, dirs, files in os.walk(self.media_source):
                         for file in files:
                             s = file.split('.')
-                            ext = s[len(s)-1]
-                            if ext.lower() == self.channel.format and not os.sep+'.' in file:
+                            ext = s[len(s) - 1]
+                            if ext.lower() == self.channel.format and not os.sep + '.' in file:
                                 file_list.append(root + os.sep + file)
                     file_list.sort()
                 except:
                     pass
                 self.q.task_done()
-                
+
             if os.path.isfile(self.media_source):
                 self.q.get(1)
                 try:
@@ -434,7 +432,7 @@ class Station(Thread):
                 self.q.task_done()
         except:
             pass
-        
+
         return file_list
 
     def get_jingles(self):
@@ -442,8 +440,8 @@ class Station(Thread):
         for root, dirs, files in os.walk(self.jingles_dir):
             for file in files:
                 s = file.split('.')
-                ext = s[len(s)-1]
-                if ext.lower() == self.channel.format and not os.sep+'.' in file:
+                ext = s[len(s) - 1]
+                if ext.lower() == self.channel.format and not os.sep + '.' in file:
                     file_list.append(root + os.sep + file)
         file_list.sort()
         return file_list
@@ -466,9 +464,9 @@ class Station(Thread):
                     artist = artist.encode('utf-8')
 
                     artist_names = artist.split(' ')
-                    artist_tags = ' #'.join(list(set(artist_names)-set(['&', '-'])))
+                    artist_tags = ' #'.join(list(set(artist_names) - {'&', '-'}))
                     message = '#NEWTRACK ! %s #%s on #%s RSS: ' % \
-                             (song.replace('_', ' '), artist_tags, self.short_name)
+                              (song.replace('_', ' '), artist_tags, self.short_name)
                     message = message[:113] + self.feeds_url
                     self.update_twitter(message)
 
@@ -481,7 +479,7 @@ class Station(Thread):
 
             if not self.counter:
                 self.id = 0
-                if self.starting_id > -1 and self.starting_id < lp_new:
+                if -1 < self.starting_id < lp_new:
                     self.id = self.starting_id
                 self.playlist = new_playlist
                 self.lp = lp_new
@@ -505,7 +503,7 @@ class Station(Thread):
                 new_tracks = new_playlist_set - playlist_set
                 self.new_tracks = list(new_tracks.copy())
 
-                if self.twitter_mode == 1  and self.counter:
+                if self.twitter_mode == 1 and self.counter:
                     self.tweet()
 
                 # Shake it, Fuzz it !
@@ -520,7 +518,6 @@ class Station(Thread):
 
                 self._info('Generating new playlist (' + str(self.lp) + ' tracks)')
 
-
             if self.jingles_mode and not (self.counter % self.jingles_frequency) and self.jingles_length:
                 media = self.jingles_list[self.jingle_id]
                 self.jingle_id = (self.jingle_id + 1) % self.jingles_length
@@ -566,9 +563,9 @@ class Station(Thread):
                     except:
                         continue
                 if self.feeds_showfilename:
-                    file_meta.metadata['filename'] = file_name.decode( "utf-8" )   #decode needed for some weird filenames
+                    file_meta.metadata['filename'] = file_name.decode("utf-8")  # decode needed for some weird filenames
                 if self.feeds_showfilepath:
-                    file_meta.metadata['filepath'] = media.decode( "utf-8" )   #decode needed for some weird filenames
+                    file_meta.metadata['filepath'] = media.decode("utf-8")  # decode needed for some weird filenames
             except:
                 pass
             self.q.task_done()
@@ -620,13 +617,13 @@ class Station(Thread):
                 media_link = self.feeds_media_url + media.file_name
                 media_link = media_link.decode('utf-8')
                 rss_item_list.append(RSSItem(
-                    title = song,
-                    link = media_link,
-                    description = media_description,
-                    enclosure = Enclosure(media_link, str(media.size), 'audio/mpeg'),
-                    guid = Guid(media_link),
-                    pubDate = media_date,)
-                    )
+                    title=song,
+                    link=media_link,
+                    description=media_description,
+                    enclosure=Enclosure(media_link, str(media.size), 'audio/mpeg'),
+                    guid=Guid(media_link),
+                    pubDate=media_date, )
+                )
             else:
                 media_link = self.metadata_url + '/' + media.file_name + '.xml'
                 try:
@@ -634,19 +631,19 @@ class Station(Thread):
                 except:
                     continue
                 rss_item_list.append(RSSItem(
-                    title = song,
-                    link = media_link,
-                    description = media_description,
-                    guid = Guid(media_link),
-                    pubDate = media_date,)
-                    )
+                    title=song,
+                    link=media_link,
+                    description=media_description,
+                    guid=Guid(media_link),
+                    pubDate=media_date, )
+                )
             json_data.append(json_item)
 
-        rss = RSS2(title = channel_subtitle, \
-                            link = self.channel.url, \
-                            description = self.channel.description.decode('utf-8'), \
-                            lastBuildDate = date_now, \
-                            items = rss_item_list,)
+        rss = RSS2(title=channel_subtitle,
+                   link=self.channel.url,
+                   description=self.channel.description.decode('utf-8'),
+                   lastBuildDate=date_now,
+                   items=rss_item_list, )
         self.q.get(1)
         try:
             if self.feeds_rss:
@@ -659,7 +656,7 @@ class Station(Thread):
         try:
             if self.feeds_json:
                 f = open(rss_file + '.json', 'w')
-                f.write(json.dumps(json_data, separators=(',',':')))
+                f.write(json.dumps(json_data, separators=(',', ':')))
                 f.close()
         except:
             pass
@@ -710,8 +707,8 @@ class Station(Thread):
         self.artist = self.artist.encode('utf-8')
         self.metadata_file = self.metadata_dir + os.sep + self.current_media_obj[0].file_name + '.xml'
         self.update_feeds(self.current_media_obj, self.feeds_current_file, '(currently playing)')
-        self._info('DeeFuzzing:  id = %s, name = %s' \
-            % (self.id, self.current_media_obj[0].file_name))
+        self._info('DeeFuzzing:  id = %s, name = %s'
+                   % (self.id, self.current_media_obj[0].file_name))
         self.player.set_media(self.media)
 
         self.q.get(1)
@@ -728,8 +725,10 @@ class Station(Thread):
         self.channel.set_callback(FileReader(self.media).read_callback)
 
     def update_twitter_current(self):
+        if not self.__twitter_should_update():
+            return
         artist_names = self.artist.split(' ')
-        artist_tags = ' #'.join(list(set(artist_names)-set(['&', '-'])))
+        artist_tags = ' #'.join(list(set(artist_names) - {'&', '-'}))
         message = '%s %s on #%s' % (self.prefix, self.song, self.short_name)
         tags = '#' + ' #'.join(self.twitter_tags)
         message = message + ' ' + tags
@@ -748,7 +747,7 @@ class Station(Thread):
             return True
         except:
             self._err('channel could not be opened')
-            
+
         return False
 
     def channel_close(self):
@@ -781,7 +780,7 @@ class Station(Thread):
             self.counter = (self.counter % self.jingles_frequency) + self.jingles_frequency
             if self.relay_mode:
                 self.set_relay_mode()
-            elif os.path.exists(self.media) and not os.sep+'.' in self.media:
+            elif os.path.exists(self.media) and not os.sep + '.' in self.media:
                 if self.lp == 0:
                     self._err('has no media to stream !')
                     return False
@@ -792,11 +791,25 @@ class Station(Thread):
             self._err('icecastloop_nextmedia: Error: ' + str(e))
         return False
 
+    def __twitter_should_update(self):
+        """Returns whether or not an update should be sent to Twitter"""
+        if not self.twitter_mode:
+            # Twitter posting disabled.  Return false.
+            return False
+
+        if self.relay_mode:
+            # We are in relay mode. Return true.
+            return True
+
+        if self.jingles_mode and (self.counter % self.jingles_frequency):
+            # We should be playing a jingle, and we don't send jingles to Twiitter.
+            return False
+        return True
+
+
     def icecastloop_metadata(self):
         try:
-            if (not (self.jingles_mode and (self.counter % self.jingles_frequency)) or \
-                                self.relay_mode) and self.twitter_mode:
-                self.update_twitter_current()
+            self.update_twitter_current()
             self.channel.set_metadata({'song': self.song, 'charset': 'utf-8'})
             return True
         except Exception, e:
@@ -833,9 +846,9 @@ class Station(Thread):
                     # first = True
                     for self.chunk in self.stream:
                         # if first:
-                        #     first = False
+                            # first = False
                         # else:
-                        #     break
+                            # break
 
                         if self.next_media or not self.run_mode:
                             break
@@ -860,7 +873,7 @@ class Station(Thread):
                                     self.recorder.close()
                                 return
                             try:
-                                self.channel.set_metadata({'song': self.song, 'charset': 'utf8',})
+                                self.channel.set_metadata({'song': self.song, 'charset': 'utf8', })
                                 self._info('channel restarted')
                                 self.channel.send(self.chunk)
                                 self.channel.sync()
@@ -871,7 +884,7 @@ class Station(Thread):
                                     self.recorder.close()
                                 return
 
-                    # send chunk loop end
+                                # send chunk loop end
                 # while run_mode loop end
 
                 self._info("Play mode ended. Stopping stream.")
index 174dd4da157970a20072eb6533abffc7178f7baa..3c9c6ef5643d6e97427bc59b9e8cdd046d379e09 100755 (executable)
@@ -42,6 +42,7 @@ def prog_info():
 
     return desc % (deefuzzer.__version__, platform_system, year)
 
+
 def main():
     if len(sys.argv) >= 2:
         d = deefuzzer.core.DeeFuzzer(sys.argv[-1])
@@ -50,6 +51,7 @@ def main():
         text = prog_info()
         sys.exit(text)
 
+
 if __name__ == '__main__':
     main()