def open(self, filename=None, xmlreport=None, minidump=None): if filename: try: self._ini = IniFile(filename, commentPrefix=';', keyValueSeperator='=', qt=True) self._filename = filename except IOError as e: raise SystemInfoReport.SystemInfoReportIOError(self, str(e)) elif xmlreport is not None: self._xmlreport = xmlreport if xmlreport.fast_protect_system_info is None: raise SystemInfoReport.SystemInfoReportIOError(self, 'No system info include in XMLReport %s' % (xmlreport.filename)) if sys.version_info[0] > 2: from io import StringIO stream = StringIO(xmlreport.fast_protect_system_info.rawdata.raw.decode('utf8')) else: from StringIO import StringIO stream = StringIO(xmlreport.fast_protect_system_info.rawdata.raw) self._ini = IniFile(filename=None, commentPrefix=';', keyValueSeperator='=', qt=True) self._ini.open(stream) self.platform_type = xmlreport.platform_type self.is_64_bit = xmlreport.is_64_bit #print('platform=%s' % (self.platform_type)) elif minidump: raise SystemInfoReport.SystemInfoReportIOError(self, 'Not yet implemented') self._post_open()
def from_file(cls, filename): """Reads a project from a project file.""" inifile = IniFile(filename) if inifile.is_new: return None name = (inifile.get("project.name") or os.path.basename(filename).rsplit(".")[0].title()) path = os.path.join( os.path.dirname(filename), untrusted_to_os_path(inifile.get("project.path") or "."), ) themes = inifile.get("project.themes") if themes is not None: themes = [x.strip() for x in themes.split(",")] else: themes = [] return cls( name=name, project_file=filename, tree=path, themes=themes, )
def test_new_theme(project_cli_runner): result = project_cli_runner.invoke( cli, ["dev", "new-theme"], input="Lektor Theme Name\n" "\n" "Author Name\n" "[email protected]\n" "y\n", ) assert "Create Theme?" in result.output assert result.exit_code == 0 path = os.path.join("themes", "lektor-theme-name") assert set(os.listdir(path)) == set( ["example-site", "images", "README.md", "theme.ini"] ) assert set(os.listdir(os.path.join(path, "images"))) == set(["homepage.png"]) assert set(os.listdir(os.path.join(path, "example-site"))) == set( ["lektor-theme-name.lektorproject", "README.md", "themes"] ) try: assert ( os.readlink(os.path.join(path, "example-site/themes/lektor-theme-name")) == "../../../lektor-theme-name" ) except AttributeError: pass theme_inifile = IniFile(os.path.join(path, "theme.ini")) assert theme_inifile["theme.name"] == "Lektor Theme Name" assert theme_inifile["author.email"] == "*****@*****.**" assert theme_inifile["author.name"] == "Author Name" with open(os.path.join(path, "README.md")) as f: readme_contents = f.read().strip() assert "Lektor Theme Name" in readme_contents
def __init__(self, rawdata): import StringIO stream = StringIO.StringIO(rawdata) ini = IniFile(filename=None, commentPrefix=';', keyValueSeperator='=', qt=True) ini.open(stream) self.product_name = ini.get(None, 'productName') self.product_code_name = ini.get(None, 'productCodename') self.product_version = ini.get(None, 'productVersion') self.product_target_version = ini.get(None, 'productTargetVersion') self.product_build_type = ini.get(None, 'productBuildType') self.product_build_postfix = ini.get(None, 'productBuildPostfix') self.root_revision = ini.get(None, 'sourceRootRevision') self.buildtools_revision = ini.get(None, 'sourceBuildtoolsRevision') self.external_revision = ini.get(None, 'sourceExternalRevision') self.third_party_revision = ini.get(None, 'source3rdPartyRevision') self.terra3d_revision = ini.get(None, 'sourceTerra3DRevision') self.manual_revision = ini.get(None, 'sourceManualRevision') self.jenkins_job_name = ini.get(None, 'jenkinsJobName') self.jenkins_build_number = ini.getAsInteger(None, 'jenkinsJobBuildNumber') self.jenkins_build_id = ini.get(None, 'jenkinsJobBuildId') self.jenkins_build_tag = ini.get(None, 'jenkinsJobBuildTag') self.jenkins_build_url = ini.get(None, 'jenkinsJobBuildUrl') self.jenkins_git_revision = ini.get(None, 'jenkinsJobBuildGitRevision') self.jenkins_git_branch = ini.get(None, 'jenkinsJobBuildGitBranch') self.jenkins_master = ini.get(None, 'jenkinsMaster') self.jenkins_nodename = ini.get(None, 'jenkinsNodename') self.threadname_tlsslot = ini.getAsInteger(None, 'threadNameTLSSlot')
def test_new_theme_name_only(project_cli_runner): result = project_cli_runner.invoke( cli, ["dev", "new-theme"], input='Lektor Name Theme\n' '\n' '\n' '\n' 'y\n', ) assert "Create Theme?" in result.output assert result.exit_code == 0 path = os.path.join('themes', 'lektor-theme-name') assert set(os.listdir(path)) == set( ['example-site', 'images', 'README.md', 'theme.ini']) assert set(os.listdir(os.path.join(path, 'images'))) == set(['homepage.png']) assert set(os.listdir(os.path.join(path, 'example-site'))) == set( ['lektor-theme-name.lektorproject', 'README.md', 'themes']) try: assert os.readlink(os.path.join( path, 'example-site/themes/lektor-theme-name')) == \ '../../../lektor-theme-name' except AttributeError: pass theme_inifile = IniFile(os.path.join(path, 'theme.ini')) assert theme_inifile['theme.name'] == 'Lektor Name Theme' assert theme_inifile['author.email'] == get_default_author_email() assert theme_inifile['author.name'] == get_default_author() with open(os.path.join(path, 'README.md')) as f: readme_contents = f.read().strip() assert "Lektor Name Theme" in readme_contents
def get_og_items(self, record): plugin_config = self.get_config() model_config = IniFile(record.datamodel.filename) root = get_ctx().pad.root items = { 'site_name': plugin_config.get('global.site_name', self.env.project.name), 'url': url_to(record, external=True), 'title': get_og_title(record, model_config, root), 'type': 'website', } image = get_og_image(record, root) if image is not None: items.update({ 'image': url_to(image, external=True), 'image:width': image.width, 'image:height': image.height, 'image:type': 'image/' + image.format, }) if image.parent is root: items['image:alt'] = 'logo' return items
def __init__(self, filename=None): self.filename = filename self.values = copy.deepcopy(DEFAULT_CONFIG) if filename is not None and os.path.isfile(filename): inifile = IniFile(filename) update_config_from_ini(self.values, inifile)
def test_new_theme_name_only(project_cli_runner, can_symlink, default_author, default_author_email): result = project_cli_runner.invoke( cli, ["dev", "new-theme"], input="Lektor Name Theme\n" "\n" "\n" "\n" "y\n", ) assert "Create Theme?" in result.output assert result.exit_code == 0 path = os.path.join("themes", "lektor-theme-name") assert set(os.listdir(path)) == set( ["example-site", "images", "README.md", "theme.ini"]) assert set(os.listdir(os.path.join(path, "images"))) == set(["homepage.png"]) assert set(os.listdir(os.path.join(path, "example-site"))) == set( ["lektor-theme-name.lektorproject", "README.md", "themes"]) if can_symlink: assert (os.readlink( os.path.join(path, "example-site/themes/lektor-theme-name")) == "../../../lektor-theme-name") theme_inifile = IniFile(os.path.join(path, "theme.ini")) assert theme_inifile["theme.name"] == "Lektor Name Theme" assert theme_inifile["author.email"] == default_author_email assert theme_inifile["author.name"] == default_author with open(os.path.join(path, "README.md"), encoding="utf-8") as f: readme_contents = f.read().strip() assert "Lektor Name Theme" in readme_contents
def get_config(self, fresh=False): """Returns the config specific for this plugin. By default this will be cached for the current build context but this can be disabled by passing ``fresh=True``. """ ctx = get_ctx() if ctx is not None and not fresh: cache = ctx.cache.setdefault(__name__ + ':configs', {}) cfg = cache.get(self.id) if cfg is None: cfg = IniFile(self.config_filename) cache[self.id] = cfg else: cfg = IniFile(self.config_filename) if ctx is not None: ctx.record_dependency(self.config_filename) return cfg
def get_user_download_dir(): user_dir_file = IniFile( os.path.join(os.path.expanduser("~"), ".config/user-dirs.dirs")) download_dir_val = user_dir_file.get_value('XDG_DOWNLOAD_DIR') prefix = download_dir_val.strip('"').split("/")[0] if prefix: return os.getenv("HOME") + "/" + "/".join( download_dir_val.strip('"').split("/")[1:]) return download_dir_val.strip('"')
def load_databag(filename): try: if filename.endswith('.json'): with open(filename, 'r') as f: return json.load(f, object_pairs_hook=OrderedDict) elif filename.endswith('.ini'): return decode_flat_data(IniFile(filename).items(), dict_cls=OrderedDict) except (OSError, IOError) as e: if e.errno != errno.ENOENT: raise
def iter_inis(path): try: for filename in os.listdir(path): if not filename.endswith('.ini') or filename[:1] in '_.': continue fn = os.path.join(path, filename) if os.path.isfile(fn): base = filename[:-4].decode('ascii', 'replace') inifile = IniFile(fn) yield base, inifile except OSError as e: if e.errno != errno.ENOENT: raise
def load_databag(filename): try: if filename.endswith(".json"): with open(filename, "r", encoding="utf-8") as f: return json.load(f, object_pairs_hook=OrderedDict) elif filename.endswith(".ini"): return decode_flat_data(IniFile(filename).items(), dict_cls=OrderedDict) else: return None except (OSError, IOError) as e: if e.errno != errno.ENOENT: raise return None
def iter_inis(path): try: for filename in os.listdir(path): if not filename.endswith(".ini") or filename[:1] in "_.": continue fn = os.path.join(path, filename) if os.path.isfile(fn): base = filename[:-4] base = base.encode("utf-8").decode("ascii", "replace") inifile = IniFile(fn) yield base, inifile except OSError as e: if e.errno != errno.ENOENT: raise
def from_file(cls, filename): """Reads a project from a project file.""" inifile = IniFile(filename) if inifile.is_new: return None name = inifile.get('project.name') or os.path.basename( filename).rsplit('.')[0].title() path = os.path.join(os.path.dirname(filename), to_os_path(inifile.get('project.path') or '.')) return cls( name=name, project_file=filename, tree=path, )
def inifile(site_path, junk_ini, my_plugin_id, pagination_enabled, month_index_enabled): # Make a temporary copy of our .ini file config_name = '%s.ini' % my_plugin_id orig_ini = site_path / 'configs' / config_name inifile = IniFile(str(orig_ini)) inifile.filename = str(junk_ini) def yesno(val): return 'yes' if val else 'no' inifile['pagination.enabled'] = yesno(pagination_enabled) if not month_index_enabled: del inifile['year-index.subindex'] if not isinstance(pagination_enabled, bool): inifile['pagination.per_page'] = pagination_enabled return inifile
def inifile(self, config, tmp_path): ini = IniFile(str(tmp_path / 'test.ini')) for key, value in config.items(): ini['default.' + key] = value ini.save() return ini
def inifile(self, test_ini): inifile = IniFile(str(test_ini)) inifile.filename = '/dev/null' return inifile
def num_unequal(filename, cmpFcn): """ Check whether two files are numerically unequal for the given compare function. :param filename: The base name of the files to check. :param cmpFcn: The default comparison function. Can be overriden by the filetolmatrix. :return: True, when the files do not match, false else. """ orig_name = os.path.join(args.ini_dir, args.diff_to, filename) with open(orig_name) as f: origMat = [[x for x in ln.split()] for ln in f] # Check if the first row has one more column, which is the # if len(origMat[0]) == len(origMat[1]) + 1: origBase = 1 origMat[0] = origMat[0][1:] else: origBase = 0 new_name = os.path.join(args.ini_dir, args.out_files_dir, filename) with open(new_name) as f: newMat = [[x for x in ln.split()] for ln in f] if len(newMat[0]) == len(newMat[1]) + 1: newBase = 1 newMat[0] = newMat[0][1:] else: newBase = 0 if len(origMat) - origBase != len(newMat) - newBase: if args.verbose_diff_output: printlog('num rows do not match in %s: %d != %d' % (filename, len(origMat), len(newMat))) return True if newBase == 1: cols = [ s[0] + 'x' + s[1] if len(s) == 2 and s != 'nu' else s for s in newMat[0] ] else: cols = range(len(newMat[0])) tolerances = get_tolerance_vector(filename, cols) row = 0 col = 0 try: if tolerances: inifilenameparts = filename.rsplit('_', 2) inifilename = '_'.join( inifilenameparts[0:2] ) if inifilenameparts[1] != 'transfer' else inifilenameparts[0] inifilename += "_params.ini" inifilename = os.path.join(args.ini_dir, args.out_files_dir, inifilename) if not os.path.exists(inifilename): if 'sharp_cl_params' in inifilename: inifile = IniFile() else: printlog("ini filename does not exist: %s" % inifilename) else: try: # The following split fails for *_transfer_out.* files where it not needed anyway. inifile = IniFile() inifile.readFile(inifilename) except OSError: printlog("Could not open ini filename: %s" % inifilename) for o_row, n_row in zip(origMat[origBase:], newMat[newBase:]): row += 1 if len(o_row) != len(n_row): if args.verbose_diff_output: printlog('num columns do not match in %s: %d != %d' % (filename, len(o_row), len(n_row))) return True col = 0 of_row = [float(f) for f in o_row] nf_row = [] for f in n_row: try: nf_row += [float(f)] except ValueError: sp = customsplit(f) nf_row += [float(sp[0] + 'E' + sp[1])] oldrowdict = False newrowdict = False for o, n in zip(of_row, nf_row): if isinstance(tolerances[col], Ignore): pass else: cond, tols = tolerances[col] # When the column condition is bool (True or False) or a function # returning False, then skip this column. if isinstance(cond, bool) or not cond(inifile): pass else: if isinstance(tols, float): if not cmpFcn(o, n, tols): if args.verbose_diff_output: printlog( 'value mismatch at %d, %d ("%s") of %s: %s != %s' % (row, col + 1, cols[col], filename, o, n)) return True elif not isinstance(tols, Ignore): if not oldrowdict: oldrowdict = dict(zip(cols, of_row)) newrowdict = dict(zip(cols, nf_row)) if isinstance(tols, list): cand = False for lim, rhs in tols: if lim < newrowdict["L"]: cand = rhs else: break if isinstance(cand, float): if not cmpFcn(o, n, cand): if args.verbose_diff_output: printlog( 'value mismatch at %d, %d ("%s") of %s: %s != %s' % (row, col + 1, cols[col], filename, o, n)) return True elif not isinstance(cand, Ignore): if not cand(oldrowdict, newrowdict): if args.verbose_diff_output: printlog( 'value mismatch at %d, %d ("%s") of %s: %s != %s' % (row, col + 1, cols[col], filename, o, n)) return True else: if not tols(oldrowdict, newrowdict): if args.verbose_diff_output: printlog( 'value mismatch at %d, %d ("%s") of %s: %s != %s' % (row, col + 1, cols[col], filename, o, n)) return True col += 1 return False else: # if args.verbose_diff_output: # printlog("Skipped file %s" % (filename)) return False except ValueError as e: printlog("ValueError: '%s' at %d, %d in file: %s" % (e.message, row, col + 1, filename)) return True
def open_config(self): if self.project_file is None: raise RuntimeError('This project has no project file.') return IniFile(self.project_file)
from inifile import IniFile i = IniFile('hello.ini') i['foo.bar'] = 'testing more stuff' i['foo.mup'] = 'aha!' print i.get_updated_lines() i.save()