<commit_before># Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""This module contains the Module class and other classes for resources.

The Module class represents a module in the trace viewer system. A module has
a name, and may require a variety of other resources, such as stylesheets,
template objects, raw javascript, or other modules.

Other resources include HTML templates, raw javascript files, and stylesheets.
"""

import os
import re
import inspect

from tvcm import resource as resource_module
from tvcm import js_utils


class DepsException(Exception):
  """Exceptions related to module dependency resolution."""
  def __init__(self, fmt, *args):
    import style_sheet as style_sheet_module
    context = []
    frame = inspect.currentframe()
    while frame:
      locals = frame.f_locals

      module_name = None
      if 'self' in locals:
        s = locals['self']
        if isinstance(s, Module):
          module_name = s.name
        if isinstance(s, style_sheet_module.StyleSheet):
          module_name = s.name + '.css'
      if not module_name:
        if 'module' in locals:
          module = locals['module']
          if isinstance(s, Module):
            module_name = module.name
        elif 'm' in locals:
          module = locals['m']
          if isinstance(s, Module):
            module_name = module.name

      if module_name:
        if len(context):
          if context[-1] != module_name:
            context.append(module_name)
        else:
          context.append(module_name)

      frame = frame.f_back

    context.reverse()
    self.context = context
    context_str = '\n'.join(['  %s' % x for x in context])
    Exception.__init__(self, 'While loading:\n%s\nGot: %s' % (context_str, (fmt % args)))


class ModuleDependencyMetadata(object):
  def __init__(self):
    self.dependent_module_names = []
    self.dependent_raw_script_relative_paths = []
    self.style_sheet_names = []

  def AppendMetdata(self, other):
    self.dependent_module_names += other.dependent_module_names
    self.dependent_raw_script_relative_paths += \
        other.dependent_raw_script_relative_paths
    self.style_sheet_names += other.style_sheet_names


_next_module_id = 1

class Module(object):
  """Represents a javascript module.

  Interesting properties include:
    name: Module name, may include a namespace, e.g. 'tvcm.foo'.
    filename: The filename of the actual module.
    contents: The text contents of the module
    dependent_modules: Other modules that this module depends on.

  In addition to these properties, a Module also contains lists of other
  resources that it depends on.
  """
  def __init__(self, loader, name, resource, load_resource=True):
    assert isinstance(name, basestring), 'Got %s instead' % repr(name)

    global _next_module_id
    self._id = _next_module_id
    _next_module_id += 1

    self.loader = loader
    self.name = name
    self.resource = resource

    if load_resource:
      f = open(self.filename, 'r')
      self.contents = f.read()
      f.close()
    else:
      self.contents = None

    # Dependency metadata, set up during Parse().
    self.dependency_metadata = None

    # Actual dependencies, set up during load().
    self.dependent_modules = []
    self.dependent_raw_scripts = []
    self.style_sheets = []

    # Caches.
    self._all_dependent_modules_recursive = None

  def __repr__(self):
    return '%s(%s)' % (self.__class__.__name__, self.name)

  @property
  def id(self):
    return self._id

  @property
  def filename(self):
    return self.resource.absolute_path

  def isComponent(self):
    return "/third_party/components/" in self.filename

  def Parse(self):
    """Parses self.contents and fills in the module's dependency metadata."""
    raise NotImplementedError()

  def GetTVCMDepsModuleType(self):
    """Returns the tvcm.setModuleInfo type for this module"""
    raise NotImplementedError()

  def AppendJSContentsToFile(self,
                             f,
                             use_include_tags_for_scripts,
                             dir_for_include_tag_root):
    """Appends the js for this module to the provided file."""
    for dependent_raw_script in self.dependent_raw_scripts:
      if use_include_tags_for_scripts:
        rel_filename = os.path.relpath(dependent_raw_script.filename,
                                       dir_for_include_tag_root)
        f.write("""<include src="%s">\n""" % rel_filename)
      else:
        f.write(js_utils.EscapeJSIfNeeded(dependent_raw_script.contents))
        f.write('\n')

  def AppendHTMLContentsToFile(self, f, ctl, minify=False):
    """Appends the html for this module [without links] to the provided file."""
    pass
<commit_msg>Another windows fix: component detection needs to not hard-code backslashes<commit_after># Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""This module contains the Module class and other classes for resources.

The Module class represents a module in the trace viewer system. A module has
a name, and may require a variety of other resources, such as stylesheets,
template objects, raw javascript, or other modules.

Other resources include HTML templates, raw javascript files, and stylesheets.
"""

import os
import re
import inspect

from tvcm import resource as resource_module
from tvcm import js_utils


class DepsException(Exception):
  """Exceptions related to module dependency resolution."""
  def __init__(self, fmt, *args):
    import style_sheet as style_sheet_module
    context = []
    frame = inspect.currentframe()
    while frame:
      locals = frame.f_locals

      module_name = None
      if 'self' in locals:
        s = locals['self']
        if isinstance(s, Module):
          module_name = s.name
        if isinstance(s, style_sheet_module.StyleSheet):
          module_name = s.name + '.css'
      if not module_name:
        if 'module' in locals:
          module = locals['module']
          if isinstance(s, Module):
            module_name = module.name
        elif 'm' in locals:
          module = locals['m']
          if isinstance(s, Module):
            module_name = module.name

      if module_name:
        if len(context):
          if context[-1] != module_name:
            context.append(module_name)
        else:
          context.append(module_name)

      frame = frame.f_back

    context.reverse()
    self.context = context
    context_str = '\n'.join(['  %s' % x for x in context])
    Exception.__init__(self, 'While loading:\n%s\nGot: %s' % (context_str, (fmt % args)))


class ModuleDependencyMetadata(object):
  def __init__(self):
    self.dependent_module_names = []
    self.dependent_raw_script_relative_paths = []
    self.style_sheet_names = []

  def AppendMetdata(self, other):
    self.dependent_module_names += other.dependent_module_names
    self.dependent_raw_script_relative_paths += \
        other.dependent_raw_script_relative_paths
    self.style_sheet_names += other.style_sheet_names


_next_module_id = 1

class Module(object):
  """Represents a javascript module.

  Interesting properties include:
    name: Module name, may include a namespace, e.g. 'tvcm.foo'.
    filename: The filename of the actual module.
    contents: The text contents of the module
    dependent_modules: Other modules that this module depends on.

  In addition to these properties, a Module also contains lists of other
  resources that it depends on.
  """
  def __init__(self, loader, name, resource, load_resource=True):
    assert isinstance(name, basestring), 'Got %s instead' % repr(name)

    global _next_module_id
    self._id = _next_module_id
    _next_module_id += 1

    self.loader = loader
    self.name = name
    self.resource = resource

    if load_resource:
      f = open(self.filename, 'r')
      self.contents = f.read()
      f.close()
    else:
      self.contents = None

    # Dependency metadata, set up during Parse().
    self.dependency_metadata = None

    # Actual dependencies, set up during load().
    self.dependent_modules = []
    self.dependent_raw_scripts = []
    self.style_sheets = []

    # Caches.
    self._all_dependent_modules_recursive = None

  def __repr__(self):
    return '%s(%s)' % (self.__class__.__name__, self.name)

  @property
  def id(self):
    return self._id

  @property
  def filename(self):
    return self.resource.absolute_path

  def isComponent(self):
    ref = os.path.join('third_party', 'components')
    return ref in self.filename

  def Parse(self):
    """Parses self.contents and fills in the module's dependency metadata."""
    raise NotImplementedError()

  def GetTVCMDepsModuleType(self):
    """Returns the tvcm.setModuleInfo type for this module"""
    raise NotImplementedError()

  def AppendJSContentsToFile(self,
                             f,
                             use_include_tags_for_scripts,
                             dir_for_include_tag_root):
    """Appends the js for this module to the provided file."""
    for dependent_raw_script in self.dependent_raw_scripts:
      if use_include_tags_for_scripts:
        rel_filename = os.path.relpath(dependent_raw_script.filename,
                                       dir_for_include_tag_root)
        f.write("""<include src="%s">\n""" % rel_filename)
      else:
        f.write(js_utils.EscapeJSIfNeeded(dependent_raw_script.contents))
        f.write('\n')

  def AppendHTMLContentsToFile(self, f, ctl, minify=False):
    """Appends the html for this module [without links] to the provided file."""
    pass

{'fraction_non_alphanumeric': 0.08931305501684189, 'fraction_numerical': 0.0014290088802694702, 'mean_word_length': 2.925480769230769, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3080736', 'n_tokens_mistral': 2831, 'n_tokens_neox': 2739, 'n_words': 854}