def extract_metadata(self): """Return info from the metadata.txt inside the zip file""" opened_zip_file = zipfile.ZipFile(self.filename, 'r') expected = [f.filename for f in opened_zip_file.filelist if f.filename.endswith('metadata.txt')][0] metadata = ConfigParser() try: # Py2 metadata.readfp(opened_zip_file.open(expected)) except TypeError: # Py3 md = opened_zip_file.open(expected).read() metadata.read_string(md.decode('utf-8')) result = {} def get_option(section, option, default=''): try: return metadata.get(section, option) except NoOptionError: return default result['name'] = get_option('general', 'name') result['about'] = get_option('general', 'about') result['download_url'] = get_option('general', 'about') result['version'] = get_option('general', 'version') result['description'] = get_option('general', 'description') result['qgis_minimum_version'] = get_option('general', 'qgisMinimumVersion') result['qgis_maximum_version'] = get_option('general', 'qgisMaximumVersion', '') result['author_name'] = get_option('general', 'author').replace('&', '&') result['homepage'] = get_option('general', 'homepage', '') result['tracker'] = get_option('general', 'tracker', '') result['repository'] = get_option('general', 'repository', '') result['tags'] = get_option('general', 'tags', '') result['changelog'] = get_option('general', 'changelog') result['experimental'] = get_option('general', 'experimental', False) result['deprecated'] = get_option('general', 'experimental', False) return result
def _read_ini(ini_file): #TODO: Future logger # print("Attempting INI format") # print("Parsing file: {0}".format(ini_file)) config_dict = {} config_sections = [] parser = ConfigParser(allow_no_value=True) try: parser.read(ini_file) config_sections = parser.sections() except MissingSectionHeaderError: print("ERROR: INI file {0} has no section header".format(ini_file)) with open(ini_file, 'r') as fd: parser.read_string("[top]\n" + fd.read()) config_sections = parser.sections() except ParsingError as ex: print(ex.message) msg = """ ================================================== ERROR: Provided input config file not in INI format. Unable to read/update provided config file. ================================================== """ raise Exception(msg) except Exception as ex: raise for section in config_sections: config_dict[section] = {} for key in parser.options(section): # If value has ',' convert it to Python list object if ',' in parser[section][key]: config_dict[section][key] = [ element.strip() for element in parser[section][key].split(',') ] else: config_dict[section][key] = parser[section][key] # print("INI file read as: {0}".format(config_dict)) return config_dict
def __load_encrypted(cls, p): """Loads an encrypted file into a dictionary of profile names to access and secret keys.""" decrypter = subprocess.Popen([GnuPG.path(), '-d', p], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, _ = decrypter.communicate() if not decrypter.returncode == 0: fail("ERROR: Unable to decrypt {}".format(p)) contents = stdout.strip().decode('utf-8') config = ConfigParser() try: config.read_string(contents) except: fail("ERROR: Unable to parse {} as an INI-structured file.".format( p)) return config_to_dict(config)
def _read_ini(config_filename): if "2." in sys.version: from ConfigParser import ConfigParser, ParsingError, MissingSectionHeaderError else: from configparser import ConfigParser, ParsingError, MissingSectionHeaderError print("Attempting INI format") print("Parsing file: {0}".format(config_filename)) ini_to_dict = None parser = ConfigParser(allow_no_value=True) try: parser.read(config_filename) ini_to_dict = parser._sections # with open(config_filename, 'r') as fd: # ini_to_dict = parser.read_string(fd.readlines())._sections except MissingSectionHeaderError: print("ERROR: INI file {0} has no section header".format( config_filename)) with open(config_filename, 'r') as fd: parser.read_string("[top]\n" + fd.read()) ini_to_dict = parser._sections except ParsingError as ex: print(ex.message) msg = """ ================================================== ERROR: Provided input config file not in INI format. Unable to read/update provided config file. ================================================== """ raise Exception(msg) except Exception as ex: print(ex) print("INI file read as: {0}".format(ini_to_dict)) return ini_to_dict