<commit_before>#!/usr/bin/python
"""Underdark Web Framework -- uWeb"""

__author__ = '<NAME> <<EMAIL>>'
__version__ = '0.14'


# Standard modules
import ConfigParser
import httplib
import os
import re
import sys

# Underdark package module
#
# If the underdark package is not directly available, add the 'ud_lib'
# directory to the module path.
try:
  import underdark
except ImportError:
  sys.path.append(
      os.path.abspath(os.path.join(os.path.dirname(__file__), 'ext_lib')))
# Underdark modules
from underdark.libs.app import logging

# Package modules
from . import pagemaker
from . import request

# Package classes
from .response import Response
from .response import Redirect
from .pagemaker import PageMaker
from .pagemaker import DebuggingPageMaker


class Error(Exception):
  """Superclass used for inheritance and external excepion handling."""


class ImmediateResponse(Exception):
  """Used to trigger an immediate repsonse, foregoing the regular returns."""


class NoRouteError(Error):
  """The server does not know how to route this request"""


class NewWeb(object):
  """Returns a configured closure for handling page requests.

  This closure is configured with a precomputed set of routes and handlers using
  the Router function. After this, incoming requests are processed and delegated
  to the correct PageMaker handler.

  The url in the received `req` object is taken and matches against the
  `router`` (refer to Router() for more documentation on this).


  Takes:
    @ page_class: PageMaker
      Class that holds request handling methods as defined in the `routes`
    @ router: request router
      The result of the Router() function.
    @ config: dict
      Configuration for the PageMaker. Typically contains entries for database
      connections, default search paths etc.

  Returns:
    RequestHandler: Configured closure that is ready to process requests.
  """
  def __init__(self, page_class, router, config):
    self.page_class = page_class
    self.router = router
    self.config = config

  def __call__(self, env, start_response):
    """WSGI request handler.

    Accpepts the WSGI `environment` dictionary and a function to start the
    response and returns a response iterator.
    """
    req = request.Request(env)
    pages = self.page_class(req, config=self.config)
    try:
      # We're specifically calling _PostInit here as promised in documentation.
      # pylint: disable=W0212
      pages._PostInit()
      # pylint: enable=W0212
      method, args = self.router(req.env['PATH_INFO'])
      response = getattr(pages, method)(*args)
    except pagemaker.ReloadModules, message:
      reload_message = reload(sys.modules[self.page_class.__module__])
      response = Response(
          content='%s\n%s' % (message, reload_message))
    except ImmediateResponse, response:
      response = response[0]
    except (NoRouteError, Exception):
      response = pages.InternalServerError(*sys.exc_info())

    if not isinstance(response, Response):
      response = Response(content=response)
    status = response.httpcode
    if isinstance(status, int):
      status = '%d %s' % (status, httplib.responses[status])
    headers = []
    for key, value in response.headers.items():
      headers.append((key, value.encode('utf8')))
    start_response(status, headers)
    return [response.content]


def ParseConfig(config_file):
  """Parses the given `config_file` and returns it as a nested dictionary."""
  parser = ConfigParser.SafeConfigParser()
  config_file = os.path.join(os.getcwd(), config_file)
  try:
    parser.read(config_file)
  except ConfigParser.ParsingError:
    raise ValueError('Not a valid config file: %r.' % config_file)
  return dict((section, dict(parser.items(section)))
              for section in parser.sections())

<commit_msg>Removed path resetting as the provided path is already an absolute path.<commit_after>#!/usr/bin/python
"""Underdark Web Framework -- uWeb"""

__author__ = '<NAME> <<EMAIL>>'
__version__ = '0.14'


# Standard modules
import ConfigParser
import httplib
import os
import re
import sys

# Underdark package module
#
# If the underdark package is not directly available, add the 'ud_lib'
# directory to the module path.
try:
  import underdark
except ImportError:
  sys.path.append(
      os.path.abspath(os.path.join(os.path.dirname(__file__), 'ext_lib')))
# Underdark modules
from underdark.libs.app import logging

# Package modules
from . import pagemaker
from . import request

# Package classes
from .response import Response
from .response import Redirect
from .pagemaker import PageMaker
from .pagemaker import DebuggingPageMaker


class Error(Exception):
  """Superclass used for inheritance and external excepion handling."""


class ImmediateResponse(Exception):
  """Used to trigger an immediate repsonse, foregoing the regular returns."""


class NoRouteError(Error):
  """The server does not know how to route this request"""


class NewWeb(object):
  """Returns a configured closure for handling page requests.

  This closure is configured with a precomputed set of routes and handlers using
  the Router function. After this, incoming requests are processed and delegated
  to the correct PageMaker handler.

  The url in the received `req` object is taken and matches against the
  `router`` (refer to Router() for more documentation on this).


  Takes:
    @ page_class: PageMaker
      Class that holds request handling methods as defined in the `routes`
    @ router: request router
      The result of the Router() function.
    @ config: dict
      Configuration for the PageMaker. Typically contains entries for database
      connections, default search paths etc.

  Returns:
    RequestHandler: Configured closure that is ready to process requests.
  """
  def __init__(self, page_class, router, config):
    self.page_class = page_class
    self.router = router
    self.config = config

  def __call__(self, env, start_response):
    """WSGI request handler.

    Accpepts the WSGI `environment` dictionary and a function to start the
    response and returns a response iterator.
    """
    req = request.Request(env)
    pages = self.page_class(req, config=self.config)
    try:
      # We're specifically calling _PostInit here as promised in documentation.
      # pylint: disable=W0212
      pages._PostInit()
      # pylint: enable=W0212
      method, args = self.router(req.env['PATH_INFO'])
      response = getattr(pages, method)(*args)
    except pagemaker.ReloadModules, message:
      reload_message = reload(sys.modules[self.page_class.__module__])
      response = Response(
          content='%s\n%s' % (message, reload_message))
    except ImmediateResponse, response:
      response = response[0]
    except (NoRouteError, Exception):
      response = pages.InternalServerError(*sys.exc_info())

    if not isinstance(response, Response):
      response = Response(content=response)
    status = response.httpcode
    if isinstance(status, int):
      status = '%d %s' % (status, httplib.responses[status])
    headers = []
    for key, value in response.headers.items():
      headers.append((key, value.encode('utf8')))
    start_response(status, headers)
    return [response.content]


def ParseConfig(config_file):
  """Parses the given `config_file` and returns it as a nested dictionary."""
  parser = ConfigParser.SafeConfigParser()
  try:
    parser.read(config_file)
  except ConfigParser.ParsingError:
    raise ValueError('Not a valid config file: %r.' % config_file)
  return dict((section, dict(parser.items(section)))
              for section in parser.sections())


{'fraction_non_alphanumeric': 0.09152719665271966, 'fraction_numerical': 0.003399581589958159, 'mean_word_length': 3.847275031685678, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7083246', 'n_tokens_mistral': 2152, 'n_tokens_neox': 2086, 'n_words': 750}