def print_records(self, records, title=None): """ Perform the print operation. :arg dict records: The records to be printed. :arg str title: Optional title for the document. Default is None. :returns: The result of the print operation. :rtype: Gtk.PrintOperationResult """ # Add the title, if given. if(title): self.text_to_print = title + "\n\n" else: self.text_to_print = "" # Assemble the header and records into one string. line_format = "%-5s\t%-15s\t%-8s\t%-6s\t%-15s\t%-12s\t%-8s\t%-8s\n" self.text_to_print += line_format % ("Index", "Callsign", "Date", "Time", "Frequency", "Mode", "RST Sent", "RST Rcvd") self.text_to_print += line_format % ("-----", "--------", "----", "----", "---------", "----", "--------", "--------") for r in records: self.text_to_print += line_format % (str(r["id"]), str(r["CALL"]), str(r["QSO_DATE"]), str(r["TIME_ON"]), str(r["FREQ"]), str(r["MODE"]), str(r["RST_SENT"]), str(r["RST_RCVD"])) result = self.operation.run(self.action, parent=self.application.window) if(result == Gtk.PrintOperationResult.ERROR): error(parent=self.application.window, message="Unable to print the log.") return result
def telnet_connect(self, host, port=23, username=None, password=None): """ Connect to a user-specified Telnet server. :arg str host: The Telnet server's hostname. :arg int port: The Telnet server's port number. If no port is specified, the default Telnet server port of 23 will be used. :arg str username: The user's username. This is an optional argument. :arg str password: The user's password. This is an optional argument. """ # Handle empty host/port string (or the case where host/port are None). if (not host): error( parent=self.application.window, message= "Unable to connect to a DX cluster because no hostname was specified." ) return if (not port): logging.warning("No port specified. Assuming default port 23...") port = 23 # Use the default Telnet port. try: logging.debug("Attempting connection to Telnet server %s:%d..." % (host, port)) self.connection = telnetlib.Telnet(host, port) assert (self.connection) if (username): self.connection.read_until("login: "******"\n").encode()) if (password): self.connection.read_until("password: "******"\n").encode()) except Exception as e: message = "Could not create a connection to the Telnet server %s:%d. Check connection to the internets? Check connection details?" % ( host, port) error(parent=self.application.window, message=message) logging.exception(e) self.connection = None return logging.debug("Connection to %s:%d established." % (host, port)) self.set_items_sensitive(False) self.check_io_event = GObject.timeout_add(1000, self.on_telnet_io) return
def telnet_connect(self, host, port=23, username=None, password=None): """ Connect to a user-specified Telnet server. :arg str host: The Telnet server's hostname. :arg int port: The Telnet server's port number. If no port is specified, the default Telnet server port of 23 will be used. :arg str username: The user's username. This is an optional argument. :arg str password: The user's password. This is an optional argument. """ # Handle empty host/port string (or the case where host/port are None). if(not host): error(parent=self.application.window, message="Unable to connect to a DX cluster because no hostname was specified.") return if(not port): logging.warning("No port specified. Assuming default port 23...") port = 23 # Use the default Telnet port. try: logging.debug("Attempting connection to Telnet server %s:%d..." % (host, port)) self.connection = telnetlib.Telnet(host, port) assert(self.connection) if(username): self.connection.read_until("login: "******"\n").encode()) if(password): self.connection.read_until("password: "******"\n").encode()) except Exception as e: message = "Could not create a connection to the Telnet server %s:%d. Check connection to the internets? Check connection details?" % (host, port) error(parent=self.application.window, message=message) logging.exception(e) self.connection = None return logging.debug("Connection to %s:%d established." % (host, port)) self.set_items_sensitive(False) self.check_io_event = GObject.timeout_add(1000, self.on_telnet_io) return
def connect(self, username, password): """ Initiate a session with the hamqth.com server. Hopefully this will provide a session key. :arg str username: The username of the hamqth.com user account. :arg str password: The password of the hamqth.com user account. :returns: True if a successful connection was made to the server, and False otherwise. :rtype: bool """ logging.debug("Connecting to the hamqth.com server...") # Connect to the server. try: self.connection = http_client.HTTPSConnection("www.hamqth.com") request = "/xml.php?u=%s&p=%s" % (username, quote(password)) # Percent-escape the password in case there are reserved characters present. self.connection.request("GET", request) response = self.connection.getresponse() except Exception as e: logging.exception(e) error(parent=self.parent, message="Could not connect to the hamqth.com server. Check connection to the internets?") return False # Get the session ID. xml_data = minidom.parseString(response.read()) session_node = xml_data.getElementsByTagName("session")[0] # There should only be one Session element. session_id_node = session_node.getElementsByTagName("session_id") if(session_id_node): self.session_id = session_id_node[0].firstChild.nodeValue logging.debug("Successfully connected to the hamqth.com server. Session ID is: %s." % self.session_id) connected = True else: connected = False # If there are any errors or warnings, print them out. session_error_node = session_node.getElementsByTagName("error") if(session_error_node): session_error = session_error_node[0].firstChild.nodeValue error(parent=self.parent, message="hamqth.com session error: %s" % session_error) return connected
def lookup_callback(self, widget=None): """ Perform geocoding of the QTH location to obtain latitude-longitude coordinates. """ if(not have_geocoder): error(parent=self.parent, message="Geocoder module could not be imported. Geocoding aborted.") return logging.debug("Geocoding QTH location...") name = self.sources["QTH_NAME"].get_text() try: g = geocoder.google(name) latitude, longitude = g.latlng self.sources["QTH_LATITUDE"].set_text(str(latitude)) self.sources["QTH_LONGITUDE"].set_text(str(longitude)) logging.debug("QTH coordinates found: (%s, %s)", str(latitude), str(longitude)) except ValueError as e: error(parent=self.parent, message="Unable to lookup QTH coordinates. Is the QTH name correct?") logging.exception(e) except Exception as e: error(parent=self.parent, message="Unable to lookup QTH coordinates. Check connection to the internets? Lookup limit reached?") logging.exception(e) return
def lookup(self, full_callsign, ignore_prefix_suffix=True): """ Parse the XML tree that is returned from the qrz.com XML server to obtain the NAME, ADDRESS, STATE, COUNTRY, DXCC, CQZ, ITUZ, and IOTA field data (if present). :arg str full_callsign: The callsign to look up (without any prefix/suffix stripping). :arg bool ignore_prefix_suffix: True if callsign prefixes/suffixes should be removed prior to querying the server, False otherwise. :returns: The data in a dictionary called fields_and_data. :rtype: dict """ logging.debug("Looking up callsign. The full callsign (with a prefix and/or suffix) is %s." % full_callsign) # Remove any prefix or suffix from the callsign before performing the lookup. if(ignore_prefix_suffix): callsign = strip(full_callsign) else: callsign = full_callsign # Commence lookup. fields_and_data = {"NAME": "", "ADDRESS": "", "STATE": "", "COUNTRY": "", "DXCC": "", "CQZ": "", "ITUZ": "", "IOTA": ""} if(self.session_key): request = "/xml/current/?s=%s;callsign=%s" % (self.session_key, callsign) self.connection.request("GET", request) response = self.connection.getresponse() xml_data = minidom.parseString(response.read()) callsign_node = xml_data.getElementsByTagName("Callsign") if(callsign_node): callsign_node = callsign_node[0] # There should only be a maximum of one Callsign element. callsign_fname_node = callsign_node.getElementsByTagName("fname") callsign_name_node = callsign_node.getElementsByTagName("name") if(callsign_fname_node): fields_and_data["NAME"] = callsign_fname_node[0].firstChild.nodeValue if(callsign_name_node): # Add the surname, if present. fields_and_data["NAME"] = fields_and_data["NAME"] + " " + callsign_name_node[0].firstChild.nodeValue callsign_addr1_node = callsign_node.getElementsByTagName("addr1") callsign_addr2_node = callsign_node.getElementsByTagName("addr2") if(callsign_addr1_node): fields_and_data["ADDRESS"] = callsign_addr1_node[0].firstChild.nodeValue if(callsign_addr2_node): # Add the second line of the address, if present. fields_and_data["ADDRESS"] = (fields_and_data["ADDRESS"] + ", " if callsign_addr1_node else "") + callsign_addr2_node[0].firstChild.nodeValue callsign_state_node = callsign_node.getElementsByTagName("state") if(callsign_state_node): fields_and_data["STATE"] = callsign_state_node[0].firstChild.nodeValue callsign_country_node = callsign_node.getElementsByTagName("country") if(callsign_country_node): fields_and_data["COUNTRY"] = callsign_country_node[0].firstChild.nodeValue callsign_ccode_node = callsign_node.getElementsByTagName("ccode") if(callsign_ccode_node): fields_and_data["DXCC"] = callsign_ccode_node[0].firstChild.nodeValue callsign_cqzone_node = callsign_node.getElementsByTagName("cqzone") if(callsign_cqzone_node): fields_and_data["CQZ"] = callsign_cqzone_node[0].firstChild.nodeValue callsign_ituzone_node = callsign_node.getElementsByTagName("ituzone") if(callsign_ituzone_node): fields_and_data["ITUZ"] = callsign_ituzone_node[0].firstChild.nodeValue callsign_iota_node = callsign_node.getElementsByTagName("iota") if(callsign_iota_node): fields_and_data["IOTA"] = callsign_iota_node[0].firstChild.nodeValue else: # If there is no Callsign element, then print out the error message in the Session element. session_node = xml_data.getElementsByTagName("Session") if(session_node): session_error_node = session_node[0].getElementsByTagName("Error") if(session_error_node): session_error = session_error_node[0].firstChild.nodeValue error(parent=self.parent, message=session_error) # Return empty strings for the field data. logging.debug("Callsign lookup complete. Returning data...") return fields_and_data