String formatting

This commit is contained in:
Ken Roberts 2016-04-16 14:32:56 -07:00
parent d92757af97
commit 37bae16307
2 changed files with 36 additions and 74 deletions

View File

@ -230,8 +230,7 @@ class ProjectorDB(Manager):
Class to access the projector database. Class to access the projector database.
""" """
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
log.debug('ProjectorDB().__init__(args="{arg}", kwargs="{kwarg}")'.format(arg=args, log.debug('ProjectorDB().__init__(args="{arg}", kwargs="{kwarg}")'.format(arg=args, kwarg=kwargs))
kwarg=kwargs))
super().__init__(plugin_name='projector', init_schema=self.init_schema) super().__init__(plugin_name='projector', init_schema=self.init_schema)
log.debug('ProjectorDB() Initialized using db url {db}'.format(db=self.db_url)) log.debug('ProjectorDB() Initialized using db url {db}'.format(db=self.db_url))
log.debug('Session: {session}'.format(session=self.session)) log.debug('Session: {session}'.format(session=self.session))
@ -445,7 +444,5 @@ class ProjectorDB(Manager):
:param source: ProjectorSource() instance to add :param source: ProjectorSource() instance to add
""" """
log.debug('Saving ProjectorSource(projector_id="{data}" ' log.debug('Saving ProjectorSource(projector_id="{data}" '
'code="{code}" text="{text}")'.format(data=source.projector_id, 'code="{code}" text="{text}")'.format(data=source.projector_id, code=source.code, text=source.text))
code=source.code,
text=source.text))
return self.save_object(source) return self.save_object(source)

View File

@ -91,8 +91,7 @@ class PJLink1(QTcpSocket):
:param poll_time: Time (in seconds) to poll connected projector :param poll_time: Time (in seconds) to poll connected projector
:param socket_timeout: Time (in seconds) to abort the connection if no response :param socket_timeout: Time (in seconds) to abort the connection if no response
""" """
log.debug('PJlink(args={args} kwargs={kwargs})'.format(args=args, log.debug('PJlink(args={args} kwargs={kwargs})'.format(args=args, kwargs=kwargs))
kwargs=kwargs))
self.name = name self.name = name
self.ip = ip self.ip = ip
self.port = port self.port = port
@ -148,8 +147,7 @@ class PJLink1(QTcpSocket):
""" """
Reset projector-specific information to default Reset projector-specific information to default
""" """
log.debug('({ip}) reset_information() connect status is {state}'.format(ip=self.ip, log.debug('({ip}) reset_information() connect status is {state}'.format(ip=self.ip, state=self.state()))
state=self.state()))
self.power = S_OFF self.power = S_OFF
self.pjlink_name = None self.pjlink_name = None
self.manufacturer = None self.manufacturer = None
@ -299,8 +297,7 @@ class PJLink1(QTcpSocket):
:param data: Optional data if called from another routine :param data: Optional data if called from another routine
""" """
log.debug('({ip}) check_login(data="{data}")'.format(ip=self.ip, log.debug('({ip}) check_login(data="{data}")'.format(ip=self.ip, data=data))
data=data))
if data is None: if data is None:
# Reconnected setup? # Reconnected setup?
if not self.waitForReadyRead(2000): if not self.waitForReadyRead(2000):
@ -320,8 +317,7 @@ class PJLink1(QTcpSocket):
# Possibility of extraneous data on input when reading. # Possibility of extraneous data on input when reading.
# Clean out extraneous characters in buffer. # Clean out extraneous characters in buffer.
dontcare = self.readLine(self.maxSize) dontcare = self.readLine(self.maxSize)
log.debug('({ip}) check_login() read "{data}"'.format(ip=self.ip, log.debug('({ip}) check_login() read "{data}"'.format(ip=self.ip, data=data.strip()))
data=data.strip()))
# At this point, we should only have the initial login prompt with # At this point, we should only have the initial login prompt with
# possible authentication # possible authentication
# PJLink initial login will be: # PJLink initial login will be:
@ -336,8 +332,7 @@ class PJLink1(QTcpSocket):
else: else:
# Process initial connection # Process initial connection
data_check = data.strip().split(' ') data_check = data.strip().split(' ')
log.debug('({ip}) data_check="{data}"'.format(ip=self.ip, log.debug('({ip}) data_check="{data}"'.format(ip=self.ip, data=data_check))
data=data_check))
# Check for projector reporting an error # Check for projector reporting an error
if data_check[1].upper() == 'ERRA': if data_check[1].upper() == 'ERRA':
# Authentication error # Authentication error
@ -354,10 +349,8 @@ class PJLink1(QTcpSocket):
return return
elif data_check[1] == '1': elif data_check[1] == '1':
# Authenticated login with salt # Authenticated login with salt
log.debug('({ip}) Setting hash with salt="{data}"'.format(ip=self.ip, log.debug('({ip}) Setting hash with salt="{data}"'.format(ip=self.ip, data=data_check[2]))
data=data_check[2])) log.debug('({ip}) pin="{data}"'.format(ip=self.ip, data=self.pin))
log.debug('({ip}) pin="{data}"'.format(ip=self.ip,
data=self.pin))
salt = qmd5_hash(salt=data_check[2].encode('ascii'), data=self.pin.encode('ascii')) salt = qmd5_hash(salt=data_check[2].encode('ascii'), data=self.pin.encode('ascii'))
else: else:
salt = None salt = None
@ -395,13 +388,11 @@ class PJLink1(QTcpSocket):
data = data_in.strip() data = data_in.strip()
if len(data) < 7: if len(data) < 7:
# Not enough data for a packet # Not enough data for a packet
log.debug('({ip}) get_data(): Packet length < 7: "{data}"'.format(ip=self.ip, log.debug('({ip}) get_data(): Packet length < 7: "{data}"'.format(ip=self.ip, data=data))
data=data))
self.send_busy = False self.send_busy = False
self.projectorReceivedData.emit() self.projectorReceivedData.emit()
return return
log.debug('({ip}) get_data(): Checking new data "{data}"'.format(ip=self.ip, log.debug('({ip}) get_data(): Checking new data "{data}"'.format(ip=self.ip, data=data))
data=data))
if data.upper().startswith('PJLINK'): if data.upper().startswith('PJLINK'):
# Reconnected from remote host disconnect ? # Reconnected from remote host disconnect ?
self.check_login(data) self.check_login(data)
@ -418,16 +409,14 @@ class PJLink1(QTcpSocket):
(prefix, class_, cmd, data) = (data_split[0][0], data_split[0][1], data_split[0][2:], data_split[1]) (prefix, class_, cmd, data) = (data_split[0][0], data_split[0][1], data_split[0][2:], data_split[1])
except ValueError as e: except ValueError as e:
log.warn('({ip}) get_data(): Invalid packet - expected header + command + data'.format(ip=self.ip)) log.warn('({ip}) get_data(): Invalid packet - expected header + command + data'.format(ip=self.ip))
log.warn('({ip}) get_data(): Received data: "{data}"'.format(ip=self.ip, log.warn('({ip}) get_data(): Received data: "{data}"'.format(ip=self.ip, data=data_in.strip()))
data=data_in.strip()))
self.change_status(E_INVALID_DATA) self.change_status(E_INVALID_DATA)
self.send_busy = False self.send_busy = False
self.projectorReceivedData.emit() self.projectorReceivedData.emit()
return return
if not (self.pjlink_class in PJLINK_VALID_CMD and cmd in PJLINK_VALID_CMD[self.pjlink_class]): if not (self.pjlink_class in PJLINK_VALID_CMD and cmd in PJLINK_VALID_CMD[self.pjlink_class]):
log.warn('({ip}) get_data(): Invalid packet - unknown command "{data}"'.format(ip=self.ip, log.warn('({ip}) get_data(): Invalid packet - unknown command "{data}"'.format(ip=self.ip, data=cmd))
data=cmd))
self.send_busy = False self.send_busy = False
self.projectorReceivedData.emit() self.projectorReceivedData.emit()
return return
@ -441,9 +430,7 @@ class PJLink1(QTcpSocket):
:param err: Error code :param err: Error code
""" """
log.debug('({ip}) get_error(err={error}): {data}'.format(ip=self.ip, log.debug('({ip}) get_error(err={error}): {data}'.format(ip=self.ip, error=err, data=self.errorString()))
error=err,
data=self.errorString()))
if err <= 18: if err <= 18:
# QSocket errors. Redefined in projector.constants so we don't mistake # QSocket errors. Redefined in projector.constants so we don't mistake
# them for system errors # them for system errors
@ -492,16 +479,13 @@ class PJLink1(QTcpSocket):
data=out.strip())) data=out.strip()))
elif not queue and len(self.send_queue) == 0: elif not queue and len(self.send_queue) == 0:
# Nothing waiting to send, so just send it # Nothing waiting to send, so just send it
log.debug('({ip}) send_command(out="{data}") Sending data'.format(ip=self.ip, log.debug('({ip}) send_command(out="{data}") Sending data'.format(ip=self.ip, data=out.strip()))
data=out.strip()))
return self._send_command(data=out) return self._send_command(data=out)
else: else:
log.debug('({ip}) send_command(out="{data}") adding to queue'.format(ip=self.ip, log.debug('({ip}) send_command(out="{data}") adding to queue'.format(ip=self.ip, data=out.strip()))
data=out.strip()))
self.send_queue.append(out) self.send_queue.append(out)
self.projectorReceivedData.emit() self.projectorReceivedData.emit()
log.debug('({ip}) send_command(): send_busy is {data}'.format(ip=self.ip, log.debug('({ip}) send_command(): send_busy is {data}'.format(ip=self.ip, data=self.send_busy))
data=self.send_busy))
if not self.send_busy: if not self.send_busy:
log.debug('({ip}) send_command() calling _send_string()'.format(ip=self.ip)) log.debug('({ip}) send_command() calling _send_string()'.format(ip=self.ip))
self._send_command() self._send_command()
@ -514,8 +498,7 @@ class PJLink1(QTcpSocket):
:param data: Immediate data to send :param data: Immediate data to send
""" """
log.debug('({ip}) _send_string()'.format(ip=self.ip)) log.debug('({ip}) _send_string()'.format(ip=self.ip))
log.debug('({ip}) _send_string(): Connection status: {data}'.format(ip=self.ip, log.debug('({ip}) _send_string(): Connection status: {data}'.format(ip=self.ip, data=self.state()))
data=self.state()))
if self.state() != self.ConnectedState: if self.state() != self.ConnectedState:
log.debug('({ip}) _send_string() Not connected - abort'.format(ip=self.ip)) log.debug('({ip}) _send_string() Not connected - abort'.format(ip=self.ip))
self.send_queue = [] self.send_queue = []
@ -526,22 +509,18 @@ class PJLink1(QTcpSocket):
return return
if data is not None: if data is not None:
out = data out = data
log.debug('({ip}) _send_string(data="{data}")'.format(ip=self.ip, log.debug('({ip}) _send_string(data="{data}")'.format(ip=self.ip, data=out.strip()))
data=out.strip()))
elif len(self.send_queue) != 0: elif len(self.send_queue) != 0:
out = self.send_queue.pop(0) out = self.send_queue.pop(0)
log.debug('({ip}) _send_string(queued data="{data}"%s)'.format(ip=self.ip, log.debug('({ip}) _send_string(queued data="{data}"%s)'.format(ip=self.ip, data=out.strip()))
data=out.strip()))
else: else:
# No data to send # No data to send
log.debug('({ip}) _send_string(): No data to send'.format(ip=self.ip)) log.debug('({ip}) _send_string(): No data to send'.format(ip=self.ip))
self.send_busy = False self.send_busy = False
return return
self.send_busy = True self.send_busy = True
log.debug('({ip}) _send_string(): Sending "{data}"'.format(ip=self.ip, log.debug('({ip}) _send_string(): Sending "{data}"'.format(ip=self.ip, data=out.strip()))
data=out.strip())) log.debug('({ip}) _send_string(): Queue = {data}'.format(ip=self.ip, data=self.send_queue))
log.debug('({ip}) _send_string(): Queue = {data}'.format(ip=self.ip,
data=self.send_queue))
self.socket_timer.start() self.socket_timer.start()
self.projectorNetwork.emit(S_NETWORK_SENDING) self.projectorNetwork.emit(S_NETWORK_SENDING)
sent = self.write(out.encode('ascii')) sent = self.write(out.encode('ascii'))
@ -558,12 +537,10 @@ class PJLink1(QTcpSocket):
:param cmd: Command to process :param cmd: Command to process
:param data: Data being processed :param data: Data being processed
""" """
log.debug('({ip}) Processing command "{data}"'.format(ip=self.ip, log.debug('({ip}) Processing command "{data}"'.format(ip=self.ip, data=cmd))
data=cmd))
if data in PJLINK_ERRORS: if data in PJLINK_ERRORS:
# Oops - projector error # Oops - projector error
log.error('({ip}) Projector returned error "{data}"'.format(ip=self.ip, log.error('({ip}) Projector returned error "{data}"'.format(ip=self.ip, data=data))
data=data))
if data.upper() == 'ERRA': if data.upper() == 'ERRA':
# Authentication error # Authentication error
self.disconnect_from_host() self.disconnect_from_host()
@ -598,8 +575,7 @@ class PJLink1(QTcpSocket):
if cmd in self.PJLINK1_FUNC: if cmd in self.PJLINK1_FUNC:
self.PJLINK1_FUNC[cmd](data) self.PJLINK1_FUNC[cmd](data)
else: else:
log.warn('({ip}) Invalid command {data}'.format(ip=self.ip, log.warn('({ip}) Invalid command {data}'.format(ip=self.ip, data=cmd))
data=cmd))
self.send_busy = False self.send_busy = False
self.projectorReceivedData.emit() self.projectorReceivedData.emit()
@ -618,8 +594,7 @@ class PJLink1(QTcpSocket):
fill = {'Hours': int(data_dict[0]), 'On': False if data_dict[1] == '0' else True} fill = {'Hours': int(data_dict[0]), 'On': False if data_dict[1] == '0' else True}
except ValueError: except ValueError:
# In case of invalid entry # In case of invalid entry
log.warn('({ip}) process_lamp(): Invalid data "{data}"'.format(ip=self.ip, log.warn('({ip}) process_lamp(): Invalid data "{data}"'.format(ip=self.ip, data=data))
data=data))
return return
lamps.append(fill) lamps.append(fill)
data_dict.pop(0) # Remove lamp hours data_dict.pop(0) # Remove lamp hours
@ -646,8 +621,7 @@ class PJLink1(QTcpSocket):
self.send_command('INST') self.send_command('INST')
else: else:
# Log unknown status response # Log unknown status response
log.warn('({ip}) Unknown power response: {data}'.format(ip=self.ip, log.warn('({ip}) Unknown power response: {data}'.format(ip=self.ip, data=data))
data=data))
return return
def process_avmt(self, data): def process_avmt(self, data):
@ -672,8 +646,7 @@ class PJLink1(QTcpSocket):
shutter = True shutter = True
mute = True mute = True
else: else:
log.warn('({ip}) Unknown shutter response: {data}'.format(ip=self.ip, log.warn('({ip}) Unknown shutter response: {data}'.format(ip=self.ip, data=data))
data=data))
update_icons = shutter != self.shutter update_icons = shutter != self.shutter
update_icons = update_icons or mute != self.mute update_icons = update_icons or mute != self.mute
self.shutter = shutter self.shutter = shutter
@ -690,8 +663,7 @@ class PJLink1(QTcpSocket):
:param data: Currently selected source :param data: Currently selected source
""" """
self.source = data self.source = data
log.info('({ip}) Setting data source to "{data}"'.format(ip=self.ip, log.info('({ip}) Setting data source to "{data}"'.format(ip=self.ip, data=self.source))
data=self.source))
return return
def process_clss(self, data): def process_clss(self, data):
@ -722,8 +694,7 @@ class PJLink1(QTcpSocket):
:param data: Projector name :param data: Projector name
""" """
self.pjlink_name = data self.pjlink_name = data
log.debug('({ip}) Setting projector PJLink name to "{data}"'.format(ip=self.ip, log.debug('({ip}) Setting projector PJLink name to "{data}"'.format(ip=self.ip, data=self.pjlink_name))
data=self.pjlink_name))
return return
def process_inf1(self, data): def process_inf1(self, data):
@ -734,8 +705,7 @@ class PJLink1(QTcpSocket):
:param data: Projector manufacturer :param data: Projector manufacturer
""" """
self.manufacturer = data self.manufacturer = data
log.debug('({ip}) Setting projector manufacturer data to "{data}"'.format(ip=self.ip, log.debug('({ip}) Setting projector manufacturer data to "{data}"'.format(ip=self.ip, data=self.manufacturer))
data=self.manufacturer))
return return
def process_inf2(self, data): def process_inf2(self, data):
@ -746,8 +716,7 @@ class PJLink1(QTcpSocket):
:param data: Model name :param data: Model name
""" """
self.model = data self.model = data
log.debug('({ip}) Setting projector model to "{data}"'.format(ip=self.ip, log.debug('({ip}) Setting projector model to "{data}"'.format(ip=self.ip, data=self.model))
data=self.model))
return return
def process_info(self, data): def process_info(self, data):
@ -758,8 +727,7 @@ class PJLink1(QTcpSocket):
:param data: Projector other info :param data: Projector other info
""" """
self.other_info = data self.other_info = data
log.debug('({ip}) Setting projector other_info to "{data}"'.format(ip=self.ip, log.debug('({ip}) Setting projector other_info to "{data}"'.format(ip=self.ip, data=self.other_info))
data=self.other_info))
return return
def process_inst(self, data): def process_inst(self, data):
@ -852,8 +820,7 @@ class PJLink1(QTcpSocket):
self.change_status(E_NOT_CONNECTED) self.change_status(E_NOT_CONNECTED)
else: else:
log.debug('({ip}) disconnect_from_host() ' log.debug('({ip}) disconnect_from_host() '
'Current status {data}'.format(ip=self.ip, 'Current status {data}'.format(ip=self.ip, data=self._get_status(self.status_connect)[0]))
data=self._get_status(self.status_connect)[0]))
if self.status_connect != E_NOT_CONNECTED: if self.status_connect != E_NOT_CONNECTED:
self.change_status(S_NOT_CONNECTED) self.change_status(S_NOT_CONNECTED)
self.reset_information() self.reset_information()
@ -936,14 +903,12 @@ class PJLink1(QTcpSocket):
:param src: Video source to select in projector :param src: Video source to select in projector
""" """
log.debug('({ip}) set_input_source(src="{data}")'.format(ip=self.ip, log.debug('({ip}) set_input_source(src="{data}")'.format(ip=self.ip, data=src))
data=src))
if self.source_available is None: if self.source_available is None:
return return
elif src not in self.source_available: elif src not in self.source_available:
return return
log.debug('({ip}) Setting input source to "{data}"'.format(ip=self.ip, log.debug('({ip}) Setting input source to "{data}"'.format(ip=self.ip, data=src))
data=src))
self.send_command(cmd='INPT', opts=src) self.send_command(cmd='INPT', opts=src)
self.poll_loop() self.poll_loop()