def get_connection(self, args, hpn_query=None, rev_query=None, port_query=None, exact_match=False, return_dictionary=True, show_connection=False): """ Return information on parts connected to args.connection -- NEED TO INCLUDE USING START/STOP_TIME!!! It should get connections immediately adjacent to one part (upstream and downstream). Returns connection_dict, a dictionary keyed on part number of adjacent connections Parameters ----------- args: arguments as per mc and parts argument parser hpn_query: the input hera part number (whole or first part thereof) port_query: a specifiable port name, default is 'all' exact_match: boolean to enforce full part number match show_connection: boolean to call show_part or not """ if hpn_query is None: hpn_query = args.connection exact_match = args.exact_match if rev_query is None: rev_query = args.revision_number if not exact_match and hpn_query[-1]!='%': hpn_query = hpn_query+'%' if port_query is None: port_query = args.specify_port connection_dict = {} db = mc.connect_to_mc_db(args) with db.sessionmaker() as session: ### Find where the part is in the upward connection for match_connection in session.query(part_connect.Connections).filter(part_connect.Connections.up.like(hpn_query)): if port_query=='all' or match_connection.b_on_up == port_query: #-#THIS SHOULD HAVE WORKED BUT DOESN'T#-# #-#if match_connection.up not in connection_dict.keys() and match_connection.up in self.connections_dictionary.keys(): #-# connection_dict[match_connection.up] = copy.deepcopy(self.connections_dictionary[match_connection.up]) #-# continue if match_connection.up not in connection_dict.keys(): revq = rev_query.upper() if revq == 'LAST': if match_connection.up not in self.last_revisions.keys(): self.last_revisions[match_connection.up] = part_connect.get_last_revision_number(args,match_connection.up,False) revq = self.last_revisions[match_connection.up] connection_dict[match_connection.up] = {'rev':revq, 'a_ports':[], 'up_parts':[], 'up_rev':[], 'b_on_up':[], 'b_ports':[], 'down_parts':[], 'down_rev':[],'a_on_down':[], 'start_on_down':[], 'stop_on_down':[], 'repr_down':[], 'start_on_up':[], 'stop_on_up':[], 'repr_up':[]} connection_dict[match_connection.up]['b_ports'].append(match_connection.b_on_up) connection_dict[match_connection.up]['down_parts'].append(match_connection.down) connection_dict[match_connection.up]['down_rev'].append(match_connection.down_rev) connection_dict[match_connection.up]['a_on_down'].append(match_connection.a_on_down) connection_dict[match_connection.up]['start_on_down'].append(match_connection.start_date) connection_dict[match_connection.up]['stop_on_down'].append(match_connection.stop_date) connection_dict[match_connection.up]['repr_down'].append(match_connection.__repr__()) ### Find where the part is in the downward connection for match_connection in session.query(part_connect.Connections).filter(part_connect.Connections.down.like(hpn_query)): if port_query=='all' or match_connection.a_on_down == port_query: #-#THIS SHOULD HAVE WORKED BUT DOESN'T#-# #-#if match_connection.down not in connection_dict.keys() and match_connection.down in self.connections_dictionary.keys(): #-# connection_dict[match_connection.down] = copy.deepcopy(self.connections_dictionary[match_connection.down]) #-# continue if match_connection.down not in connection_dict.keys(): revq = rev_query.upper() if revq == 'LAST': if match_connection.down not in self.last_revisions.keys(): self.last_revisions[match_connection.down] = part_connect.get_last_revision_number(args,match_connection.down,False) revq = self.last_revisions[match_connection.down] connection_dict[match_connection.down] = {'rev':revq, 'a_ports':[], 'up_parts':[], 'up_rev':[], 'b_on_up':[], 'b_ports':[], 'down_parts':[], 'down_rev':[],'a_on_down':[], 'start_on_down':[], 'stop_on_down':[], 'repr_down':[], 'start_on_up':[], 'stop_on_up':[], 'repr_up':[]} connection_dict[match_connection.down]['a_ports'].append(match_connection.a_on_down) connection_dict[match_connection.down]['up_parts'].append(match_connection.up) connection_dict[match_connection.down]['up_rev'].append(match_connection.up_rev) connection_dict[match_connection.down]['b_on_up'].append(match_connection.b_on_up) connection_dict[match_connection.down]['start_on_up'].append(match_connection.start_date) connection_dict[match_connection.down]['stop_on_up'].append(match_connection.stop_date) connection_dict[match_connection.down]['repr_up'].append(match_connection.__repr__()) connection_dict = self.__check_ports(args,connection_dict,rev_query) for pkey in connection_dict.keys(): if pkey not in self.connections_dictionary.keys(): self.connections_dictionary[pkey] = copy.copy(connection_dict[pkey]) if show_connection: self.show_connection(args, connection_dict) if return_dictionary: return connection_dict
def get_part(self, args, hpn_query=None, rev_query=None, exact_match=False, return_dictionary=True, show_part=False): """ Return information on a part. It will return all matching first characters unless exact_match==True. Returns part_dict, a dictionary keyed on hera part number with part data as found in hera_mc tables Parameters ----------- args: arguments as per mc and parts argument parser hpn_query: the input hera part number (whole or first part thereof) exact_match: boolean to enforce full part number match show_part: boolean to call show_part or not """ if hpn_query is None: hpn_query = args.hpn exact_match = args.exact_match if rev_query is None: rev_query = args.revision_number if not exact_match and hpn_query[-1]!='%': hpn_query = hpn_query+'%' local_parts_keys = [] part_dict = {} db = mc.connect_to_mc_db(args) with db.sessionmaker() as session: ### Get parts to check and fill in last_revisions and connections_dictionary as able for match_part in session.query(part_connect.Parts).filter(part_connect.Parts.hpn.like(hpn_query)): if match_part.hpn not in local_parts_keys: local_parts_keys.append(match_part.hpn) if match_part.hpn not in self.last_revisions.keys(): self.last_revisions[match_part.hpn] = part_connect.get_last_revision_number(args,match_part.hpn,False) if match_part.hpn not in self.connections_dictionary.keys(): self.get_connection(args, hpn_query=match_part.hpn, rev_query = rev_query, port_query='all', exact_match=True, return_dictionary=False, show_connection=False) ### Now get unique part/rev and put into dictionary for match_key in local_parts_keys: revq = rev_query.upper() if revq == 'LAST': revq = self.last_revisions[match_key] part_query = session.query(part_connect.Parts).filter( (part_connect.Parts.hpn==match_key) & (part_connect.Parts.hpn_rev==revq) ) part_and_rev = part_query.all() part_cnt = part_query.count() if not part_cnt: ### None found. continue elif part_cnt == 1: part = part_and_rev[0] ### Found only one. is_connected = self.is_in_connections_db(args,part.hpn,part.hpn_rev) psd_for_dict = part.stop_date if not part.stop_date: psd_for_dict = 'N/A' part_dict[part.hpn] = {'rev':part.hpn_rev, 'is_connected':is_connected, 'hptype': part.hptype, 'manufacturer_number': part.manufacturer_number, 'start_date': part.start_date, 'stop_date': psd_for_dict, 'a_ports': [], 'b_ports': [], 'short_description':'', 'geo':None} part_dict[part.hpn]['repr'] = part.__repr__() # Keep for now for part_info in session.query(part_connect.PartInfo).filter( (part_connect.PartInfo.hpn == part.hpn) & (part_connect.PartInfo.hpn_rev == part.hpn_rev) ): part_dict[part.hpn]['short_description'] = part_info.short_description part_dict[part.hpn]['a_ports'] = self.connections_dictionary[part.hpn]['a_ports'] part_dict[part.hpn]['b_ports'] = self.connections_dictionary[part.hpn]['b_ports'] if part.hptype == 'station': args.locate = part.hpn part_dict[part.hpn]['geo'] = geo_location.locate_station(args, show_geo=False) if part.hpn not in self.parts_dictionary.keys(): self.parts_dictionary[part.hpn] = part_dict[part.hpn] ### This only handles the most recent revs. else: ### Found more than one, which shouldn't happen. print("Warning part_handling:175: Well, being here is a surprise -- should only be one part.", part.hpn) if show_part: if len(part_dict.keys()) == 0: print(hpn_query,' not found.') else: self.show_part(args, part_dict) if return_dictionary: return part_dict