Server IP : 103.119.228.120 / Your IP : 3.149.29.98 Web Server : Apache System : Linux v8.techscape8.com 3.10.0-1160.119.1.el7.tuxcare.els2.x86_64 #1 SMP Mon Jul 15 12:09:18 UTC 2024 x86_64 User : nobody ( 99) PHP Version : 5.6.40 Disable Function : shell_exec,symlink,system,exec,proc_get_status,proc_nice,proc_terminate,define_syslog_variables,syslog,openlog,closelog,escapeshellcmd,passthru,ocinum cols,ini_alter,leak,listen,chgrp,apache_note,apache_setenv,debugger_on,debugger_off,ftp_exec,dl,dll,myshellexec,proc_open,socket_bind,proc_close,escapeshellarg,parse_ini_filepopen,fpassthru,exec,passthru,escapeshellarg,escapeshellcmd,proc_close,proc_open,ini_alter,popen,show_source,proc_nice,proc_terminate,proc_get_status,proc_close,pfsockopen,leak,apache_child_terminate,posix_kill,posix_mkfifo,posix_setpgid,posix_setsid,posix_setuid,dl,symlink,shell_exec,system,dl,passthru,escapeshellarg,escapeshellcmd,myshellexec,c99_buff_prepare,c99_sess_put,fpassthru,getdisfunc,fx29exec,fx29exec2,is_windows,disp_freespace,fx29sh_getupdate,fx29_buff_prepare,fx29_sess_put,fx29shexit,fx29fsearch,fx29ftpbrutecheck,fx29sh_tools,fx29sh_about,milw0rm,imagez,sh_name,myshellexec,checkproxyhost,dosyayicek,c99_buff_prepare,c99_sess_put,c99getsource,c99sh_getupdate,c99fsearch,c99shexit,view_perms,posix_getpwuid,posix_getgrgid,posix_kill,parse_perms,parsesort,view_perms_color,set_encoder_input,ls_setcheckboxall,ls_reverse_all,rsg_read,rsg_glob,selfURL,dispsecinfo,unix2DosTime,addFile,system,get_users,view_size,DirFiles,DirFilesWide,DirPrintHTMLHeaders,GetFilesTotal,GetTitles,GetTimeTotal,GetMatchesCount,GetFileMatchesCount,GetResultFiles,fs_copy_dir,fs_copy_obj,fs_move_dir,fs_move_obj,fs_rmdir,SearchText,getmicrotime MySQL : ON | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /usr/local/ssl/local/ssl/local/ssl/lib/python2.7/site-packages/iniparse/ |
Upload File : |
class ConfigNamespace(object): """Abstract class representing the interface of Config objects. A ConfigNamespace is a collection of names mapped to values, where the values may be nested namespaces. Values can be accessed via container notation - obj[key] - or via dotted notation - obj.key. Both these access methods are equivalent. To minimize name conflicts between namespace keys and class members, the number of class members should be minimized, and the names of all class members should start with an underscore. Subclasses must implement the methods for container-like access, and this class will automatically provide dotted access. """ # Methods that must be implemented by subclasses def _getitem(self, key): return NotImplementedError(key) def __setitem__(self, key, value): raise NotImplementedError(key, value) def __delitem__(self, key): raise NotImplementedError(key) def __iter__(self): return NotImplementedError() def _new_namespace(self, name): raise NotImplementedError(name) def __contains__(self, key): try: self._getitem(key) except KeyError: return False return True # Machinery for converting dotted access into container access, # and automatically creating new sections/namespaces. # # To distinguish between accesses of class members and namespace # keys, we first call object.__getattribute__(). If that succeeds, # the name is assumed to be a class member. Otherwise it is # treated as a namespace key. # # Therefore, member variables should be defined in the class, # not just in the __init__() function. See BasicNamespace for # an example. def __getitem__(self, key): try: return self._getitem(key) except KeyError: return Undefined(key, self) def __getattr__(self, name): try: return self._getitem(name) except KeyError: if name.startswith('__') and name.endswith('__'): raise AttributeError return Undefined(name, self) def __setattr__(self, name, value): try: object.__getattribute__(self, name) object.__setattr__(self, name, value) except AttributeError: self.__setitem__(name, value) def __delattr__(self, name): try: object.__getattribute__(self, name) object.__delattr__(self, name) except AttributeError: self.__delitem__(name) # During unpickling, Python checks if the class has a __setstate__ # method. But, the data dicts have not been initialised yet, which # leads to _getitem and hence __getattr__ raising an exception. So # we explicitly impement default __setstate__ behavior. def __setstate__(self, state): self.__dict__.update(state) class Undefined(object): """Helper class used to hold undefined names until assignment. This class helps create any undefined subsections when an assignment is made to a nested value. For example, if the statement is "cfg.a.b.c = 42", but "cfg.a.b" does not exist yet. """ def __init__(self, name, namespace): object.__setattr__(self, 'name', name) object.__setattr__(self, 'namespace', namespace) def __setattr__(self, name, value): obj = self.namespace._new_namespace(self.name) obj[name] = value def __setitem__(self, name, value): obj = self.namespace._new_namespace(self.name) obj[name] = value # ---- Basic implementation of a ConfigNamespace class BasicConfig(ConfigNamespace): """Represents a hierarchical collection of named values. Values are added using dotted notation: >>> n = BasicConfig() >>> n.x = 7 >>> n.name.first = 'paramjit' >>> n.name.last = 'oberoi' ...and accessed the same way, or with [...]: >>> n.x 7 >>> n.name.first 'paramjit' >>> n.name.last 'oberoi' >>> n['x'] 7 >>> n['name']['first'] 'paramjit' Iterating over the namespace object returns the keys: >>> l = list(n) >>> l.sort() >>> l ['name', 'x'] Values can be deleted using 'del' and printed using 'print'. >>> n.aaa = 42 >>> del n.x >>> print n aaa = 42 name.first = paramjit name.last = oberoi Nested namepsaces are also namespaces: >>> isinstance(n.name, ConfigNamespace) True >>> print n.name first = paramjit last = oberoi >>> sorted(list(n.name)) ['first', 'last'] Finally, values can be read from a file as follows: >>> from StringIO import StringIO >>> sio = StringIO(''' ... # comment ... ui.height = 100 ... ui.width = 150 ... complexity = medium ... have_python ... data.secret.password = goodness=gracious me ... ''') >>> n = BasicConfig() >>> n._readfp(sio) >>> print n complexity = medium data.secret.password = goodness=gracious me have_python ui.height = 100 ui.width = 150 """ # this makes sure that __setattr__ knows this is not a namespace key _data = None def __init__(self): self._data = {} def _getitem(self, key): return self._data[key] def __setitem__(self, key, value): self._data[key] = value def __delitem__(self, key): del self._data[key] def __iter__(self): return iter(self._data) def __str__(self, prefix=''): lines = [] keys = self._data.keys() keys.sort() for name in keys: value = self._data[name] if isinstance(value, ConfigNamespace): lines.append(value.__str__(prefix='%s%s.' % (prefix,name))) else: if value is None: lines.append('%s%s' % (prefix, name)) else: lines.append('%s%s = %s' % (prefix, name, value)) return '\n'.join(lines) def _new_namespace(self, name): obj = BasicConfig() self._data[name] = obj return obj def _readfp(self, fp): while True: line = fp.readline() if not line: break line = line.strip() if not line: continue if line[0] == '#': continue data = line.split('=', 1) if len(data) == 1: name = line value = None else: name = data[0].strip() value = data[1].strip() name_components = name.split('.') ns = self for n in name_components[:-1]: if n in ns: ns = ns[n] if not isinstance(ns, ConfigNamespace): raise TypeError('value-namespace conflict', n) else: ns = ns._new_namespace(n) ns[name_components[-1]] = value # ---- Utility functions def update_config(target, source): """Imports values from source into target. Recursively walks the <source> ConfigNamespace and inserts values into the <target> ConfigNamespace. For example: >>> n = BasicConfig() >>> n.playlist.expand_playlist = True >>> n.ui.display_clock = True >>> n.ui.display_qlength = True >>> n.ui.width = 150 >>> print n playlist.expand_playlist = True ui.display_clock = True ui.display_qlength = True ui.width = 150 >>> from iniparse import ini >>> i = ini.INIConfig() >>> update_config(i, n) >>> print i [playlist] expand_playlist = True <BLANKLINE> [ui] display_clock = True display_qlength = True width = 150 """ for name in source: value = source[name] if isinstance(value, ConfigNamespace): if name in target: myns = target[name] if not isinstance(myns, ConfigNamespace): raise TypeError('value-namespace conflict') else: myns = target._new_namespace(name) update_config(myns, value) else: target[name] = value