Rename var & method + PyLint adapt

Rename var, method and other to comply with PyLint rules
Change PyLint rules to be more flexible with constante names
This commit is contained in:
2017-04-17 17:33:38 +02:00
parent a3ab8d4678
commit 27dd281ed3
2 changed files with 67 additions and 57 deletions

View File

@@ -63,48 +63,48 @@ class ITunesParser:
""" """
Parse an iTunes Library and produce JSON - for ELS Parse an iTunes Library and produce JSON - for ELS
""" """
def __init__(self, libraryFile): def __init__(self, library_file):
self._albums = {} self._albums = {}
self._artists = {} self._artists = {}
self.libraryFile = libraryFile self.library_file = library_file
def toJson(self): def to_json(self):
""" """
Just do processSong() - This method suck Just do processSong() - This method suck
""" """
ret = self._processSongs() ret = self._process_songs()
# self._writeArtists() # self._write_artists()
# self._writeAlbums() # self._write_albums()
# return json.dumps(jsonObj, indent=indent, cls=SetEncoder) # return json.dumps(jsonObj, indent=indent, cls=SetEncoder)
return ret return ret
def toJsonP(self): # TODO parameter, rating=4): def to_json_p(self): # TODO parameter, rating=4):
""" """
Produce JSON-P content Produce JSON-P content
""" """
jsonContent = self.toJson() json_content = self.to_json()
jsonp = ';itgCallback(' + jsonContent + ');' jsonp = ';itgCallback(' + json_content + ');'
return jsonp return jsonp
def _readTracks(self): def _read_tracks(self):
""" """
Read library and return Tracks part Read library and return Tracks part
""" """
pl = plistlib.readPlist(self.libraryFile) plist = plistlib.readPlist(self.library_file)
return pl['Tracks'] return plist['Tracks']
def _processSongs(self): def _process_songs(self):
""" """
Return an output JSON for an ELS Bulk request - Not a correct format Return an output JSON for an ELS Bulk request - Not a correct format
TODO Just return a _correct_ JSON and treat in another place/class TODO Just return a _correct_ JSON and treat in another place/class
""" """
tracks = self._readTracks() tracks = self._read_tracks()
ret = "" ret = ""
for k in tracks: for k in tracks:
@@ -116,23 +116,23 @@ class ITunesParser:
if 'Podcast' in track or 'Has Video' in track: if 'Podcast' in track or 'Has Video' in track:
continue continue
persistentId = track['Persistent ID'] persistent_id = track['Persistent ID']
jsonTrackIndic = { json_track_index = {
"index": {"_index": "itunessongs", "_type": "song", "_id": persistentId} "index": {"_index": "itunessongs", "_type": "song", "_id": persistent_id}
} }
# Retrieve for each track artist information # Retrieve for each track artist information
self._processArtist(track) self._process_artist(track)
# Retrieve for each track album information # Retrieve for each track album information
self._processAlbum(track) self._process_album(track)
ret += json.dumps(jsonTrackIndic, indent=None, cls=SetEncoder) ret += json.dumps(json_track_index, indent=None, cls=SetEncoder)
ret += "\n" ret += "\n"
ret += json.dumps(track, indent=None, cls=SetEncoder) ret += json.dumps(track, indent=None, cls=SetEncoder)
ret += "\n" ret += "\n"
return ret return ret
def _processArtist(self, track): def _process_artist(self, track):
""" """
Process artists in the track part of library and return a JSON formated for a bulk ELS request Process artists in the track part of library and return a JSON formated for a bulk ELS request
""" """
@@ -162,11 +162,11 @@ class ITunesParser:
return return
# Split up the Genres # Split up the Genres
genreParts = track['Genre'].split('/') genre_parts = track['Genre'].split('/')
self._artists[akey]['genres'] |= set(genreParts) self._artists[akey]['genres'] |= set(genre_parts)
return return
def _processAlbum(self, track): def _process_album(self, track):
""" """
Process albums in the track part of library and return a JSON formated for a bulk ELS request Process albums in the track part of library and return a JSON formated for a bulk ELS request
""" """
@@ -196,8 +196,8 @@ class ITunesParser:
return return
# Split up the Genres # Split up the Genres
genreParts = track['Genre'].split('/') genre_parts = track['Genre'].split('/')
self._albums[akey]['genres'] |= set(genreParts) self._albums[akey]['genres'] |= set(genre_parts)
## Add different artists ## Add different artists
if 'Artist' not in track: if 'Artist' not in track:
@@ -205,44 +205,52 @@ class ITunesParser:
self._albums[akey]['artist'].add(track['Artist']) self._albums[akey]['artist'].add(track['Artist'])
return return
# def _writeArtists(self): def _write_artists(self):
# fileArtist = io.open('es-artist-data.json', 'wb') """
# for a in self._artists: Write artists data to another JSON file
# jsonTrackIndic = { """
# "index": {"_index": "itunessongs", "_type": "artist"}
# }
# fileArtist.write(json.dumps(jsonTrackIndic, indent=None, cls=SetEncoder))
# fileArtist.write("\n")
# fileArtist.write(json.dumps(self._artists[a], indent=None, cls=SetEncoder))
# fileArtist.write("\n")
# fileArtist.close()
# def _writeAlbums(self): file_artist = io.open('es-artist-data.json', 'wb')
# fileAlbums = io.open('es-albums-data.json', 'wb') for artist in self._artists:
# for a in self._albums: json_track_index = {
# jsonTrackIndic = { "index": {"_index": "itunessongs", "_type": "artist"}
# "index": {"_index": "itunessongs", "_type": "album"} }
# } file_artist.write(bytes(json.dumps(json_track_index, indent=None, cls=SetEncoder), 'UTF-8'))
# fileAlbums.write(json.dumps(jsonTrackIndic, indent=None, cls=SetEncoder)) file_artist.write(bytes("\n", 'UTF-8'))
# fileAlbums.write("\n") file_artist.write(bytes(json.dumps(self._artists[artist], indent=None, cls=SetEncoder), 'UTF-8'))
# fileAlbums.write(json.dumps(self._albums[a], indent=None, cls=SetEncoder)) file_artist.write(bytes("\n", 'UTF-8'))
# fileAlbums.write("\n") file_artist.close()
# fileAlbums.close()
def _write_albums(self):
"""
Write albums data to another JSON file
"""
file_albums = io.open('es-albums-data.json', 'wb')
for album in self._albums:
json_track_index = {
"index": {"_index": "itunessongs", "_type": "album"}
}
file_albums.write(bytes(json.dumps(json_track_index, indent=None, cls=SetEncoder), 'UTF-8'))
file_albums.write(bytes("\n", 'UTF-8'))
file_albums.write(bytes(json.dumps(self._albums[album], indent=None, cls=SetEncoder), 'UTF-8'))
file_albums.write(bytes("\n", 'UTF-8'))
file_albums.close()
#### main block #### #### main block ####
# Default input & output files # Default input & output files
defaultLibraryFile = os.path.expanduser('iTunesMiniLibrary.xml') DEFAULT_LIBRARY_FILE = os.path.expanduser('iTunesMiniLibrary.xml')
defaultOutputFile = os.path.dirname(os.path.realpath(__file__)) + '/es-music-data.json' DEFAULT_OUTPUT_FILE = os.path.dirname(os.path.realpath(__file__)) + '/es-music-data.json'
# Get options # Get options
parser = OptionParser(version="%prog 1.0") parser = OptionParser(version="%prog 1.0")
parser.add_option('-f', '--file', dest='file', type='string', parser.add_option('-f', '--file', dest='file', type='string',
help='iTunes Library XML file path', help='iTunes Library XML file path',
default=defaultLibraryFile) default=DEFAULT_LIBRARY_FILE)
parser.add_option('-o', '--output', dest='output', type='string', parser.add_option('-o', '--output', dest='output', type='string',
help='Output to file (default=./js/music-data.json)', help='Output to file (default=./js/music-data.json)',
default=defaultOutputFile) default=DEFAULT_OUTPUT_FILE)
parser.add_option('-c', '--console', dest='console', action='store_true', parser.add_option('-c', '--console', dest='console', action='store_true',
help='Output to console instead of file') help='Output to console instead of file')
parser.add_option('-p', '--jsonp', dest='jsonp', action='store_true', parser.add_option('-p', '--jsonp', dest='jsonp', action='store_true',
@@ -253,11 +261,11 @@ parser.add_option('-v', '--verbose', dest='verbose', action='store_true',
if __name__ == '__main__': if __name__ == '__main__':
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
itunesParser = ITunesParser(options.file) itunes_parser = ITunesParser(options.file)
if options.jsonp: if options.jsonp:
output = itunesParser.toJsonP() output = itunes_parser.to_json_p()
else: else:
output = itunesParser.toJson() output = itunes_parser.to_json()
if options.console: if options.console:
print(output) print(output)

View File

@@ -30,7 +30,7 @@ load-plugins=
# can either give multiple identifier separated by comma (,) or put this option # can either give multiple identifier separated by comma (,) or put this option
# multiple time (only on the command line, not in the configuration file where # multiple time (only on the command line, not in the configuration file where
# it should appear only once). # it should appear only once).
disable=C0325 #disable=C0325
[REPORTS] [REPORTS]
@@ -112,7 +112,9 @@ bad-functions=filter,apply,input
module-rgx=(([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$ module-rgx=(([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$
# Regular expression which should only match correct module level names # Regular expression which should only match correct module level names
const-rgx=(([A-Z_][A-Z0-9_]*)|(__.*__))$ #const-rgx=(([A-Z_][A-Z0-9_]*)|(__.*__))$
# For constant with lower case characters
const-rgx=(([a-zA-Z0-9_]*)|(__.*__))$
# Regular expression which should only match correct class names # Regular expression which should only match correct class names
class-rgx=[A-Z_][a-zA-Z0-9]+$ class-rgx=[A-Z_][a-zA-Z0-9]+$