PK!nginx_conf_parser/__init__.pyPK!P{{nginx_conf_parser/context.py# coding=utf-8 class Context: content = '' def __init__(self, content): self.content = content self._parse() def _parse(self): if self.content is None: raise ValueError('Content not initialized') def _extract_values(self, directive_name): more = True while more: try: directive_begin_index = self.content.index(directive_name) directive_end_index = self.content.index(';', directive_begin_index) directive_name_value = self.content[directive_begin_index:directive_end_index + 1] directive_value = directive_name_value[len(directive_name):-1].strip() if isinstance(self.__getattribute__(directive_name), list): self.__getattribute__(directive_name).append(directive_value) else: self.__setattr__(directive_name, directive_value) self.content = self.content[:directive_begin_index] + self.content[directive_end_index + 1:] except ValueError: more = False PK!O#nginx_conf_parser/events_context.py# coding=utf-8 import re class EventContext: accept_mutex_delay = None accept_mutex = None debug_connection = [] multi_accept = 'off' use = None worker_aio_requests = 32 worker_connections = 512 error_log = dict(file='logs/error.log', level='error') def __init__(self, content): # accept_mutex_delay delay = re.search(r'accept_mutex_delay\s*([^;]*)', content) self.accept_mutex_delay = delay.group(1) if delay else '500ms' # accept_mutex mutex = re.search(r'accept_mutex\s*(on|off);', content) self.accept_mutex = mutex.group(1) if mutex else 'off' # debug_connection debug = re.findall(r'debug_connection\s*([^;]*)', content) self.debug_connection = debug # multi_accept accept = re.search(r'multi_accept\s*(on|off);', content) self.multi_accept = accept.group(1) if accept else 'off' # use use = re.search(r'use\s+(select|poll|kqueue|epoll|/dev/poll|eventport);', content) self.use = use.group(1) if use else self.use # worker_aio_requests aio = re.search(r'worker_aio_requests\s*(\d+);', content) self.worker_aio_requests = int(aio.group(1)) if aio else 32 # worker_connections conn = re.search(r'worker_connections\s*(\d+);', content) self.worker_connections = int(conn.group(1)) if conn else 512 # error_log error_log = re.search( r'error_log\s*([^\s]*)\s*(debug|info|notice|warn|error|crit|alert|emerg)?;', content) if error_log: self.error_log = dict( file=error_log.group(1), level=error_log.group(2) if error_log.group(2) else 'error' ) else: self.error_log = dict( file='logs/error.log', level='error' ) PK!Xz!nginx_conf_parser/http_context.py# coding=utf-8 from .context import Context class HttpContext(Context): def _parse(self): super(HttpContext, self)._parse() PK!ρ!nginx_conf_parser/main_context.py# coding=utf-8 import re class MainContext: daemon = 'on' debug_points = None env = [] error_log = dict(file='logs/error.log', level='error') load_module = None lock_file = 'logs/nginx.lock' master_process = 'on' pcre_jit = 'off' pid = 'logs/nginx.pid' ssl_engine = None thread_pool = dict(name='default', threads=32, max_queue=65536) timer_resolution = None working_directory = None user = dict(user='nobody', group='nobody') worker_cpu_affinity = None worker_priority = 0 worker_processes = 1 worker_rlimit_core = None worker_rlimit_nofile = None worker_shutdown_timeout = None google_perftools_profiles = None def load(self, content): # daemon daemon = re.search(r'daemon\s+(on|off);', content) self.daemon = daemon.group(1) if daemon else self.daemon # debug_points dp = re.search(r'debug_points\s+(stop|abort);', content) self.debug_points = dp.group(1) if dp else self.debug_points # env env = re.findall(r'env\s+([a-zA-Z0-9=_]+);', content) self.env = env # error_log error_log = re.search( r'error_log\s+(([a-zA-Z0-9./\\\-]+)(\s+)?(debug|info|notice|warn|error|crit|alert|emerg)?);', content) if error_log: self.error_log['file'] = error_log.group(2) self.error_log['level'] = error_log.group(4) if error_log.group(4) else 'error' # load_module load_module = re.search(r'load_module\s+([a-zA-Z0-9\\/.\s]+);', content) self.load_module = load_module.group(1) if load_module else self.load_module # lock_file lock_file = re.search(r'lock_file\s+([a-zA-Z0-9\\/.\s\-]+);', content) self.lock_file = lock_file.group(1) if lock_file else self.lock_file # master_process master_process = re.search(r'master_process\s+(on|off);', content) self.master_process = master_process.group(1) if master_process else self.master_process # pcre_jit pcre = re.search(r'pcre_jit\s+(on|off);', content) self.pcre_jit = pcre.group(1) if pcre else self.pcre_jit # pid pid = re.search(r'pid\s+([a-zA-Z0-9\\/.\s\-]+);', content) self.pid = pid.group(1) if pid else self.pid # ssl_engine engine = re.search(r'ssl_engine\s+([a-zA-Z0-9\\/.\s\-]+);', content) self.ssl_engine = engine.group(1) if engine else self.ssl_engine # thread_pool pool = re.search(r'thread_pool\s+(([a-zA-Z0-9]+)\s+threads=([0-9]+)[\s+]?(max_queue=)?([0-9]+)?);', content) if pool: self.thread_pool['name'] = pool.group(2) self.thread_pool['threads'] = int(pool.group(3)) self.thread_pool['max_queue'] = int(pool.group(5)) if pool.group(5) else self.thread_pool.get('max_queue') # timer_resolution timer = re.search(r'timer_resolution\s+([0-9a-zA-Z]+);', content) self.timer_resolution = timer.group(1) if timer else self.timer_resolution # working_directory directory = re.search(r'working_directory\s+([a-zA-Z0-9/.\\_-]+);', content) self.working_directory = directory.group(1) if directory else self.working_directory # user user = re.search(r'user\s+([a-zA-Z0-9_\-]+)(\s+)?([a-zA-Z0-9_\-]+)?;', content) if user: self.user['user'] = user.group(1) self.user['group'] = user.group(3) if user.group(3) else user.group(1) # worker_cpu_affinity affinity = re.search(r'worker_cpu_affinity\s+([0-9a-zA-Z\s]+);', content) self.worker_cpu_affinity = affinity.group(1) if affinity else self.worker_cpu_affinity # worker_priority priority = re.search(r'worker_priority\s+([0-9]+);', content) self.worker_priority = int(priority.group(1)) if priority else self.worker_priority # worker_processes processes = re.search(r'worker_processes\s+([0-9]+);', content) self.worker_processes = int(processes.group(1)) if processes else self.worker_processes # worker_rlimit_core rlimit_core = re.search(r'worker_rlimit_core\s+([0-9a-zA-Z]+);', content) self.worker_rlimit_core = rlimit_core.group(1) if rlimit_core else self.worker_rlimit_core # worker_rlimit_nofile rlimit_nofile = re.search(r'worker_rlimit_nofile\s+([0-9]+);', content) self.worker_rlimit_nofile = int(rlimit_nofile.group(1)) if rlimit_nofile else self.worker_rlimit_nofile # worker_shutdown_timeout timemout = re.search(r'worker_shutdown_timeout\s+([a-zA-Z0-9]+);', content) self.worker_shutdown_timeout = timemout.group(1) if timemout else self.worker_shutdown_timeout # google_perftools_profiles pertools = re.search(r'google_perftools_profiles\s+([a-zA-Z0-9_.\-\\/]+);', content) self.google_perftools_profiles = pertools.group(1) if pertools else self.google_perftools_profiles PK!_)O#nginx_conf_parser/stream_context.py# coding=utf-8 import re from .utils import extract_upstream_zone, extract_upstream_server_parameters, extract_context from .upstream_context import UpstreamContext class StreamContext: upstreams = [] servers = [] def load(self, content): # extracting upstreams upstreams = re.findall(r'upstream\s+([a-zA-Z]+)\s+{([^}]*)', content) for upstream in upstreams: self.upstreams.append(UpstreamContext(name=upstream[0], content=upstream[1])) to_append = dict(name=upstream[0], servers=[], zone=extract_upstream_zone(upstream[1])) # server directives servers = re.findall(r'server\s+([^;]*)', upstream[1]) for server in servers: to_append.get('servers').append( dict(address=re.search(r'^([0-9a-zA-Z.:/]+)', server).group(1), parameters=extract_upstream_server_parameters(server))) # state directive state = re.search(r'state\s+([^;]*)', upstream[1]) to_append['state'] = state.group(1) if state else None # hash directive hash = re.search(r'hash\s+([^;]*)', upstream[1]) if hash: consistent = 'consistent' in hash.group(1) to_append['hash'] = dict(consistent=consistent, key=hash.group(1).split('consistent')[0].strip()) # ip_hash to_append['ip_hash'] = 'ip_hash' in upstream[1] # keep_alive connections; keep_alive = re.search(r'keep_alive\s+([^;]*)', upstreams[1]) to_append['keep_alive'] = keep_alive.group(1) if keep_alive else None self.upstreams.append(to_append) # extracting servers servers = re.findall(r'server\s+{([^}]*)', content) if __name__ == '__main__': from os.path import dirname from core.utils import extract_context with open('{0}/../tests/features/nginx_stream_sample.conf'.format(dirname(__file__))) as f: stream_string = extract_context(f, 'stream') stream_context = StreamContext() stream_context.load(stream_string) PK!YU %nginx_conf_parser/upstream_context.py# coding=utf-8 import re from .utils import extract_upstream_zone class UpstreamContext: name = None servers = [] zone = None state = None hash = None ip_hash = False keepalive = None keepalive_requests = 100 keepalive_timeout = '60s' ntlm = False least_conn = False least_time = None queue = None random = None sticky = None def __init__(self, name, content): self.name = name # zone directive self.zone = extract_upstream_zone(content) # server directive self.servers = [] servers = re.findall(r'server\s+([^\s]*)\s*([^;]*)', content) for server in servers: self.servers.append({ 'address': server[0], 'parameters': {_[0]: _[1] if _[1] != '' else True for _ in re.findall(r'(\w*)=*(\w*)', server[1]) if _[0] != ''} }) # state directive state = re.search(r'state\s+([^;]*)', content) self.state = state.group(1) if state else self.state # hash directive hash_ = re.search(r'hash\s+([^\s]*)\s*(consistent)*;', content) self.hash = dict(key=hash_.group(1), consistent=True if hash_.group(2) else False) if hash_ else self.hash # ip_hash directive self.ip_hash = 'ip_hash;' in content # keepalive directive keepalive = re.search(r'keepalive\s+(\d+);', content) self.keepalive = int(keepalive.group(1)) if keepalive else None # keekpalive_requests directive keepalive_requests = re.search(r'keepalive_requests\s+(\d+);', content) self.keepalive_timeout = int(keepalive_requests.group(1)) if keepalive_requests else 100 # keepalive_timeout directive keepalive_timeout = re.search(r'keepalive_timeout\s+(\w);', content) self.keepalive_timeout = keepalive_timeout.group(1) if keepalive_timeout else '60s' # ntlm directive self.ntlm = 'ntlm;' in content # least_conn directive self.least_conn = 'least_conn;' in content # least_time self.least_time = None least_time = re.search(r'least_time\s+([^;]*)', content) self.least_time = dict(header='header' in least_time.group(1), last_byte='last_byte' in least_time.group(1), inflight='inflight' in least_time.group(1)) \ if least_time else self.least_time # queue directive queue = re.search(r'queue\s+(\d+)\s*(timeout=(\w+))?;', content) self.queue = dict(value=int(queue.group(1)), timeout=queue.group(3) if queue.group(3) else '60s') if queue else None # random directive random = re.search(r'random\s*(two)?\s*([^;]*)', content) self.random = dict(two=True if random.group(1) else False, method=random.group(2) if random.group(2) else None) if random else None # sticky directive sticky = re.search(r'sticky\s*(cookie|route|learn)\s*([^;]*)', content) if sticky: # sticky cookie if 'cookie' == sticky.group(1): self.sticky = dict( type='cookie', name=re.search(r'^([^\s]*)', sticky.group(2)).group(1), expires=re.search(r'expires=([^\s]*)', sticky.group(2)).group(1) if 'expires' in sticky.group( 2) else None, domain=re.search(r'domain=([^\s]*)', sticky.group(2)).group(1) if 'domain' in sticky.group( 2) else None, httponly='httponly' in sticky.group(2), secure='secure' in sticky.group(2), path=re.search(r'path=([^\s]*)', sticky.group(2)).group(1) if 'path' in sticky.group(2) else None ) elif 'route' == sticky.group(1): self.sticky = dict( type='route', variables=re.findall(r'(\$\w+)', sticky.group(2)) ) elif 'learn' == sticky.group(1): zone = re.search(r'zone=([^\s]*)', sticky.group(2)).group(1) self.sticky = dict( type='learn', create=re.search(r'create=([^\s]*)', sticky.group(2)).group(1), zone=dict(name=zone.split(':')[0], size=zone.split(':')[1]), lookup=re.search(r'lookup=([^\s]*)', sticky.group(2)).group(1), timeout=re.search(r'timeout=([^\s]*)', sticky.group(2)).group(1) if 'timeout' in sticky.group( 2) else None, header='header' in sticky.group(2), sync='sync' in sticky.group(2) ) PK!|x nginx_conf_parser/utils.py# coding=utf-8 import re from _io import TextIOWrapper def extract_context(conffile, context_name): if not isinstance(conffile, TextIOWrapper) and not isinstance(conffile, str): raise TypeError('Invalid configuration file given, must be a file stream or a string') if isinstance(conffile, TextIOWrapper): content = conffile.read().replace('\n', ' ') else: content = conffile.replace('\n', ' ') try: context_begin_index = re.search(context_name + '\s+{', content).start() context_string = '' depth = 0 for c in content[context_begin_index:]: if c == '{': depth += 1 context_string += c continue if c == '}': depth -= 1 context_string += c if depth == 0: break else: continue context_string += c return context_string except AttributeError: return '' def extract_upstream_server_parameters(to_parse): parameters = {} # weight=number weight = re.search(r'weight=([0-9]+)', to_parse) parameters['weight'] = int(weight.group(1)) if weight else None # max_conns=number max_conns = re.search(r'max_conns=([0-9]+)', to_parse) parameters['max_conns'] = int(max_conns.group(1)) if max_conns else None # max_fails=number max_fails = re.search(r'max_fails=([0-9]+)', to_parse) parameters['max_fails'] = int(max_fails.group(1)) if max_conns else None # fail_timeout=time fail_timeout = re.search(r'fail_timeout=([0-9a-zA-Z]+)', to_parse) parameters['fail_timeout'] = fail_timeout.group(1) if fail_timeout else None # backup, down, resolve, drain parameters['backup'] = 'backup' in to_parse parameters['down'] = 'down' in to_parse parameters['resolve'] = 'resolve' in to_parse parameters['drain'] = 'drain' in to_parse # route=string route = re.search(r'route=([a-zA-Z0-9.-_\\/]+)', to_parse) parameters['route'] = route.group(1) if route else None # service=string service = re.search(r'service=([a-zA-Z0-9.-_\\/]+)', to_parse) parameters['service'] = service.group(1) if service else None # slow_start=time slow_start = re.search(r'slow_start=([a-zA-Z0-9]+)', to_parse) parameters['slow_start'] = slow_start.group(1) if slow_start else None return parameters def extract_upstream_zone(to_parse): zone = re.search(r'zone\s+([a-zA-Z0-9_\-.]+)\s*?([a-z0-9]+)?;', to_parse) return dict(name=zone.group(1), size=zone.group(2)) if zone and zone.group(2) else \ dict(name=zone.group(1), size=None) if zone else None PK!J7;11)nginx_conf_parser-0.1.0.dist-info/LICENSECopyright (c) 2018 The Python Packaging Authority Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.PK!H STT'nginx_conf_parser-0.1.0.dist-info/WHEEL 1 0 нR. \I$ơ7.ZON `h6oi14m,b4>4ɛpK>X;baP>PK!HÈj*nginx_conf_parser-0.1.0.dist-info/METADATARO1~_Q$ rh҈z"H[41ć;׭n۵,=SLomguQ/ ^rk{ch_Anړr9G| : _ z ?&krH|m{Hl!qH._&S؆TmciPglI9*V󓺞=xx퉾%G0! [l#$qq?⯏lՌfAU]8D;͇z>kUjVbd~ᜐvA?G8G<32[mo9lSעnS bR srHe`>@ZJ׎ѝ(0҂<>jco~{\eRn~ѕ ۫RPe$~4Oй* qH]4YG-xQtRT2["'}jL&vtl~3 ťXa_I3R .a*r<]`Z8vSGh b99 ^$NbnYz&G]U\2OAloP5s QK6MlrbKBM+$5ќ/5%);hPK!nginx_conf_parser/__init__.pyPK!P{{;nginx_conf_parser/context.pyPK!O#nginx_conf_parser/events_context.pyPK!Xz! nginx_conf_parser/http_context.pyPK!ρ! nginx_conf_parser/main_context.pyPK!_)O#!nginx_conf_parser/stream_context.pyPK!YU %*nginx_conf_parser/upstream_context.pyPK!|x =nginx_conf_parser/utils.pyPK!J7;11)Hnginx_conf_parser-0.1.0.dist-info/LICENSEPK!H STT'FMnginx_conf_parser-0.1.0.dist-info/WHEELPK!HÈj*Mnginx_conf_parser-0.1.0.dist-info/METADATAPK!Hg=(Onginx_conf_parser-0.1.0.dist-info/RECORDPK 2R