From a06eda9ef0a825bfce698695acecf4d84ee35358 Mon Sep 17 00:00:00 2001 From: Cort Buffington Date: Fri, 19 May 2017 09:45:56 -0500 Subject: [PATCH] Added New ID Alias Abilities --- dmr_utils/utils.py | 102 ++++++++++++++++++++++++--------------------- setup.py | 2 +- 2 files changed, 55 insertions(+), 49 deletions(-) diff --git a/dmr_utils/utils.py b/dmr_utils/utils.py index 8b23e5a..628c5c3 100755 --- a/dmr_utils/utils.py +++ b/dmr_utils/utils.py @@ -24,16 +24,24 @@ from os.path import isfile, getmtime from time import time from urllib import URLopener from csv import reader as csv_reader +from csv import DictReader as csv_dict_reader from binascii import b2a_hex as ahex # Does anybody read this stuff? There's a PEP somewhere that says I should do this. __author__ = 'Cortney T. Buffington, N0MJS' -__copyright__ = 'Copyright (c) 2016 Cortney T. Buffington, N0MJS and the K0USY Group' +__copyright__ = 'Copyright (c) 2016-2017 Cortney T. Buffington, N0MJS and the K0USY Group' __credits__ = 'Colin Durbridge, G4EML, Steve Zingman, N4IRS; Mike Zingman' __license__ = 'GNU GPLv3' __maintainer__ = 'Cort Buffington, N0MJS' __email__ = 'n0mjs@me.com' + +# CONSTANTS +SUB_FIELDS = ('ID', 'CALLSIGN', 'NAME', 'CITY', 'STATE', 'COUNTRY', 'TYPE') +PEER_FIELDS = ('ID', 'CALLSIGN', 'CITY', 'STATE', 'COUNTRY', 'FREQ', 'CC', 'OFFSET', 'TYPE', 'LINKED', 'TRUSTEE', 'INFO', 'OTHER', 'NETWORK', ) +TGID_FIELDS = ('TGID', 'NAME') + + #************************************************ # STRING UTILITY FUNCTIONS #************************************************ @@ -88,7 +96,8 @@ def try_download(_path, _file, _url, _stale,): result = 'ID ALIAS MAPPER: \'{}\' is current, not downloaded'.format(_file) url.close() return result - + +# LEGACY VERSION - MAKES A SIMPLE {INTEGER ID: 'CALLSIGN'} DICTIONARY def mk_id_dict(_path, _file): dict = {} try: @@ -100,65 +109,62 @@ def mk_id_dict(_path, _file): return dict except IOError: return dict - -def mk_full_id_dict(_path, _file): + +# NEW VERSION - MAKES A FULL DICTIONARY OF INFORMATION BASED ON TYPE OF ALIAS FILE +# BASED ON DOWNLOADS FROM DMR-MARC, TGID IS STILL A "SIMPLE" DICTIONARY +def mk_full_id_dict(_path, _file, _type): dict = {} + if _type == 'subscriber': + fields = SUB_FIELDS + elif _type == 'peer': + fields = PEER_FIELDS + elif _type == 'tgid': + fields = TGID_FIELDS try: with open(_path+_file, 'rU') as _handle: - ids = csv_reader(_handle, dialect='excel', delimiter=',') + ids = csv_dict_reader(_handle, fieldnames=fields, restkey='OTHER', dialect='excel', delimiter=',') for row in ids: - print(repr(row[0]), repr(row[1]), row[3], row[4]) - dict[int(row[0])] = {'CALLSIGN': (row[1]), 'NAME': (row[2]), 'CITY': (row[3]), 'STATE': (row[4]), 'COUNTRY':(row[5])} + for item in row: + dict[int(row['ID'])] = row _handle.close return dict except IOError: return dict -# These are kept for legacy purposes; They only return a callsign -def get_info(_id, _dict): - if _id in _dict: - return _dict[_id] - return _id - -def get_alias(_id, _dict): - _int_id = int_id(_id) - if _int_id in _dict: - return _dict[_int_id] - return _int_id - -# These do not work yet, don't use them. -def get_callsign(_id, _dict): - if type(_id) != int: +# THESE ARE THE SAME THING FOR LEGACY PURPOSES +def get_alias(_id, _dict, *args): + if type(_id) == str: _id = int_id(_id) - if _id in _dict: - return _dict[_id]['CALLSIGN'] - return _id - -def get_name(_id, _dict): - if type(_id) != int: - _id = int_id(_id) - if _id in _dict: - return _dict[_id]['NAME'] - return _id - -def get_call_name(_id, _dict): - if type(_id) != int: - _id = int_id(_id) - if _id in _dict: - return _dict[_id]['CALLSIGN'] + ', ' + _dict[_id]['NAME'] - return _id - -def get_alias_list(_id, _dict, *args): - if type(_id) != int: - _id = int_id(_id) - retValue = [_id,] if _id in _dict: if args: + retValue = [] for _item in args: - retValue.append(_dict[_id][_item]) + try: + retValue.append(_dict[_id][_item]) + except TypeError: + return _dict[_id] + return retValue else: - retValue = retValue + [_dict[_id]['CALLSIGN'], _dict[_id]['NAME'], _dict[_id]['CITY'], _dict[_id]['STATE'], _dict[_id]['COUNTRY']] - return retValue - return retValue + return _dict[_id] + return _id + +def get_info(_id, _dict, *args): + if type(_id) == str: + _id = int_id(_id) + if _id in _dict: + if args: + retValue = [] + for _item in args: + try: + retValue.append(_dict[_id][_item]) + except TypeError: + return _dict[_id] + return retValue + else: + return _dict[_id] + return _id + + + \ No newline at end of file diff --git a/setup.py b/setup.py index 974f465..cce3b25 100755 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ def readme(): return file.read() setup(name='dmr_utils', - version='0.1.5', + version='0.1.6', description='ETSI DMR (Digital Mobile Radio) Teir II Utilities', long_description='Modules to disassemble and assemble DMR packets, including generating and decoding varoius FEC routines', classifiers=[