Add the location code to the automatically generated device name

This commit is contained in:
Charles Haley 2011-02-28 16:30:27 +00:00
parent 160e57ed54
commit 5d317eb534

View File

@ -55,13 +55,13 @@ class USBMS(CLI, Device):
METADATA_CACHE = 'metadata.calibre' METADATA_CACHE = 'metadata.calibre'
DRIVEINFO = 'driveinfo.calibre' DRIVEINFO = 'driveinfo.calibre'
def _update_driveinfo_record(self, dinfo, prefix, name=None): def _update_driveinfo_record(self, dinfo, prefix, location_code, name=None):
if not isinstance(dinfo, dict): if not isinstance(dinfo, dict):
dinfo = {} dinfo = {}
if dinfo.get('device_store_uuid', None) is None: if dinfo.get('device_store_uuid', None) is None:
dinfo['device_store_uuid'] = unicode(uuid.uuid4()) dinfo['device_store_uuid'] = unicode(uuid.uuid4())
if dinfo.get('device_name') is None: if dinfo.get('device_name') is None:
dinfo['device_name'] = self.get_gui_name() dinfo['device_name'] = self.get_gui_name() + '_' + location_code
if name is not None: if name is not None:
dinfo['device_name'] = name dinfo['device_name'] = name
dinfo['last_library_uuid'] = getattr(self, 'current_library_uuid', None) dinfo['last_library_uuid'] = getattr(self, 'current_library_uuid', None)
@ -70,18 +70,19 @@ class USBMS(CLI, Device):
dinfo['prefix'] = prefix.replace('\\', '/') dinfo['prefix'] = prefix.replace('\\', '/')
return dinfo return dinfo
def _update_driveinfo_file(self, prefix, name=None): def _update_driveinfo_file(self, prefix, location_code, name=None):
if os.path.exists(os.path.join(prefix, self.DRIVEINFO)): if os.path.exists(os.path.join(prefix, self.DRIVEINFO)):
with open(os.path.join(prefix, self.DRIVEINFO), 'rb') as f: with open(os.path.join(prefix, self.DRIVEINFO), 'rb') as f:
try: try:
driveinfo = json.loads(f.read(), object_hook=from_json) driveinfo = json.loads(f.read(), object_hook=from_json)
except: except:
driveinfo = None driveinfo = None
driveinfo = self._update_driveinfo_record(driveinfo, prefix, name) driveinfo = self._update_driveinfo_record(driveinfo, prefix,
location_code, name)
with open(os.path.join(prefix, self.DRIVEINFO), 'wb') as f: with open(os.path.join(prefix, self.DRIVEINFO), 'wb') as f:
f.write(json.dumps(driveinfo, default=to_json)) f.write(json.dumps(driveinfo, default=to_json))
else: else:
driveinfo = self._update_driveinfo_record({}, prefix, name) driveinfo = self._update_driveinfo_record({}, prefix, location_code, name)
with open(os.path.join(prefix, self.DRIVEINFO), 'wb') as f: with open(os.path.join(prefix, self.DRIVEINFO), 'wb') as f:
f.write(json.dumps(driveinfo, default=to_json)) f.write(json.dumps(driveinfo, default=to_json))
return driveinfo return driveinfo
@ -90,20 +91,20 @@ class USBMS(CLI, Device):
self.report_progress(1.0, _('Get device information...')) self.report_progress(1.0, _('Get device information...'))
self.driveinfo = {} self.driveinfo = {}
if self._main_prefix is not None: if self._main_prefix is not None:
self.driveinfo['main'] = self._update_driveinfo_file(self._main_prefix) self.driveinfo['main'] = self._update_driveinfo_file(self._main_prefix, 'main')
if self._card_a_prefix is not None: if self._card_a_prefix is not None:
self.driveinfo['A'] = self._update_driveinfo_file(self._card_a_prefix) self.driveinfo['A'] = self._update_driveinfo_file(self._card_a_prefix, 'A')
if self._card_b_prefix is not None: if self._card_b_prefix is not None:
self.driveinfo['B'] = self._update_driveinfo_file(self._card_b_prefix) self.driveinfo['B'] = self._update_driveinfo_file(self._card_b_prefix, 'B')
return (self.get_gui_name(), '', '', '', self.driveinfo) return (self.get_gui_name(), '', '', '', self.driveinfo)
def set_driveinfo_name(self, location_code, name): def set_driveinfo_name(self, location_code, name):
if location_code == 'main': if location_code == 'main':
self._update_driveinfo_file(self._main_prefix, name) self._update_driveinfo_file(self._main_prefix, location_code, name)
elif location_code == 'A': elif location_code == 'A':
self._update_driveinfo_file(self._card_a_prefix, name) self._update_driveinfo_file(self._card_a_prefix, location_code, name)
elif location_code == 'B': elif location_code == 'B':
self._update_driveinfo_file(self._card_b_prefix, name) self._update_driveinfo_file(self._card_b_prefix, location_code, name)
def books(self, oncard=None, end_session=True): def books(self, oncard=None, end_session=True):
from calibre.ebooks.metadata.meta import path_to_ext from calibre.ebooks.metadata.meta import path_to_ext