Mar-26-2017, 04:40 AM
Well, you can write the file in .config/program_name/file_name
importing a config file prefixed with a dot
|
Mar-26-2017, 04:40 AM
Well, you can write the file in .config/program_name/file_name
i will be writing the config files in .py format
(Mar-26-2017, 04:40 AM)wavic Wrote: Well, you can write the file in .config/program_name/file_name i will try that. i will now aim for the exec(config_str,{},config_dict) method. iow, i will open the file (this works with dots in the file name), read the file as a string up to some maximum size (probably 1048575), exec() that string with a reference to an empty directory as the local space, then pick config data out of that directory. this avoids the module vs. dot issues.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Mar-26-2017, 10:39 AM
(This post was last modified: Mar-26-2017, 10:40 AM by ichabod801.)
If you want to exec into a private namespace, you just need to provide a dictionary to the globals parameter of exec:
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures (Mar-26-2017, 10:39 AM)ichabod801 Wrote: If you want to exec into a private namespace, you just need to provide a dictionary to the globals parameter of exec: it worked for me using the locals parameter.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
not withstanding this error in python3 , i made this module:
!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import division, print_function, unicode_literals """Process a config file in Python format file pyconf.py purpose process a config file in python format email 10054452614123394844460370234029112340408691 The intent is that this command works correctly under both Python 2 and Python 3. Please report failures or code improvement to the author. """ __license__ = """ Copyright (C) 2017, by Phil D. Howard - all other rights reserved Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. The author may be contacted by decoding the number 10054452614123394844460370234029112340408691 (provu igi la numeron al duuma) """ import os, pwd from sys import argv, stderr, stdout, version_info if version_info.major<3: ints=(int,long) strs=(str,unicode,) else: ints=(int,) strs=(str,bytes,bytearray,) def pyconf(*args,**opts): if len(args)>0: m='positional arguments are not used: ' m+=', '.join([repr(a) for a in args]) raise Exception(m) uid=opts.pop('uid',os.getuid()) home=opts.pop('home',pwd.getpwuid(uid).pw_dir) maxsize=opts.pop('maxsize',1048576) if 'path' in opts: p=opts['path'] else: p=home if 'dotdir' in opts: p += '/.' + opts.pop('dotdir') if 'subdir' in opts: p += '/' + opts.pop('subdir') p += '/' + opts.pop('name') if len(opts)>0: m='unknown option' if len(opts)>1: m+='s' m+=': '+', '.join([str(n)+'='+repr(v) for n,v in opts.items()]) raise Exception(m) with open(p) as f: s=f.read(maxsize+1) if len(s)>maxsize: m='config file {} size is larger than {}' raise Exception(m.format(repr(p),repr(maxsize))) c={} print(s) exec(s,{},c) return c def main(args): """main""" # config file is ~/.config/foo/bar.conf config = pyconf(dotdir='config',subdir='foo',name='bar.conf') for n,v in sorted(config.items()): print(repr(n),'=',repr(v)) return config.pop('status',3) if __name__ == '__main__': try: result=main(argv) stdout.flush() except KeyboardInterrupt: print('') result=99 except IOError: result=98 if result is 0 or result is None or result is True: exit(0) if result is 1 or result is False: exit(1) if isinstance(result,strs): print(result,file=stderr) exit(1) try: exit(int(result)) except ValueError: print(str(result),file=stderr) except TypeError: exit(127) # EOFand made this silly config file at "~/.config/foo/bar.conf" ... that fails in python3 ... and works in python2 foo = 1 for n in range(10): locals()['foo'+str(n)+'bar'] = [n**m for m in range(9)] bar = 0i have not studied exceptions enough, yet, to know the best to use for exceptions i have the pyconf() function raise. feedback about that is sought.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Use exec with only globals.
But from my understanding you only want to import "hidden" file (starting with dot). Two ways were suggested - either use exec or hide your file to a hidden directory and use import. It seems that you insist on doing both at the same time ... Why you just dont skip your pyconf and wont use something like: import sys sys.path.append('.config/boo') import my_config_in_boo (Mar-27-2017, 08:48 AM)zivoni Wrote: Use exec with only globals. since it was suggested to me, because i had not come up with the idea on my own, i looked closely at exec(), and it gave me the idea that having all the incoming config data placed in a separate dictionary, would add beneficial safety, avoiding a chance of variable name collision. that and it allows for the case of the base file name having the dot regardless of which directory it is in. i have switched to using just 2 args on exec().
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American. |
|