Example #1
0
 def get_arginfo(self):
     args = []
     args.append(
         arginfo.Arg(
             name="parser_name",
             type=arginfo.STRING,
             required=True,
             default=None,
             choices=snarkutils.list_parsers(),
             multiple=False,
             description="Parser module name.",
         )
     )
     args.append(
         arginfo.Arg(
             name="exporter_name",
             type=arginfo.STRING,
             required=True,
             default=None,
             choices=snarkutils.list_exporters(),
             multiple=False,
             description="Exporter module name.",
         )
     )
     args.append(
         arginfo.Arg(
             name="src_path",
             type=arginfo.FILE_OR_URL,
             required=False,
             default=None,
             choices=None,
             multiple=False,
             description="Source url/file to parse.",
         )
     )
     args.append(
         arginfo.Arg(
             name="dest_path",
             type=arginfo.FILE,
             required=False,
             default=None,
             choices=None,
             multiple=False,
             description="Destination file to write.",
         )
     )
     args.append(
         arginfo.Arg(
             name="first_msg",
             type=arginfo.STRING,
             required=False,
             default=None,
             choices=None,
             multiple=False,
             description="Optional substring for parsers to expect of the first comment.\nUse this to skip early comments.",
         )
     )
     args.append(
         arginfo.Arg(
             name="fudge_time",
             type=arginfo.TIMEDELTA,
             required=True,
             default=timedelta(minutes=0, seconds=0),
             choices=None,
             multiple=False,
             description="Delay all comments (+/-).",
         )
     )
     args.append(
         arginfo.Arg(
             name="ignore_users",
             type=arginfo.STRING,
             required=False,
             default=None,
             choices=None,
             multiple=True,
             description="Users to ignore (Example: @steve).",
         )
     )
     args.append(
         arginfo.Arg(
             name="ignore_regexes",
             type=arginfo.STRING,
             required=False,
             default=None,
             choices=None,
             multiple=True,
             description="Comment regexes to ignore.",
         )
     )
     args.append(
         arginfo.Arg(
             name="end_time",
             type=arginfo.TIMEDELTA,
             required=False,
             default=None,
             choices=None,
             multiple=False,
             description="Optional in-movie time to truncate comments (after fudging).\nUse this to skip late comments.",
         )
     )
     args.append(
         arginfo.Arg(
             name="color_enabled",
             type=arginfo.STRING,
             required=True,
             default="random",
             choices=["no", "random", "default"],
             multiple=False,
             description="Colored subtitles\n  no: there will be no color info.\n  random: assign colors to users randomly.\n  default: let the parser decide.\nAt low resolutions, colored text can be ugly in VLC.",
         )
     )
     args.append(
         arginfo.Arg(
             name="show_time",
             type=arginfo.TIMEDELTA,
             required=True,
             default=timedelta(minutes=0, seconds=6),
             choices=None,
             multiple=False,
             description="Duration each comment appears on-screen.",
         )
     )
     return args
Example #2
0
  def create_config_sections(self):
    """Returns a list of sections to pass to a config_ui.ConfigFrame.
    Each subsection has an apply callback that will modify the
    SnarksWrapper-enclosed config.
    """
    def config_subsection_callback(values_dict):
      self._snarks_wrapper.checkout(self.__class__.__name__)
      old_config = self._snarks_wrapper.clone_config()

      config = self._snarks_wrapper.get_config()
      snarks = self._snarks_wrapper.get_snarks()
      for (k,v) in values_dict.items():
        setattr(config, k, v)

      # Determine what changed, assuming everything, at first.
      # Each section flag may be removed, but we'll keep ANY to be safe.
      event_flags = []
      for section_list in common.SnarksEvent.SECTION_FLAGS:
        if (section_list[0] == common.SnarksEvent.FLAG_CONFIG_ALL):
          for f in section_list[2]:
            if (f not in event_flags):
              event_flags.append(f)  # Add section flag.
          if (section_list[1] not in event_flags):
            event_flags.append(section_list[1])  # Add *_ANY.
        break

      def toggle_flag(event_flags, flag, expression):
        # Sets a flag if expression is True, unsets otherwise.
        if (expression):
          if (flag not in event_flags): event_flags.append(flag)
        else:
          if (flag in event_flags): event_flags.remove(flag)

      if (config.ignore_users != old_config.ignore_users or 
          config.ignore_regexes != old_config.ignore_regexes):
        for snark in snarks:
          if (snark["user"] in config.ignore_users):
            snark["_ignored"] = True
          else:
            snark["_ignored"] = False

            for ptn in config.ignore_regexes:
              if (re.search(ptn, snark["msg"])):
                snark["_ignored"] = True
                break

        toggle_flag(event_flags, common.SnarksEvent.FLAG_SNARKS, True)

      if (config.fudge_time != old_config.fudge_time):
        diff = config.fudge_time - old_config.fudge_time
        for user in config.fudge_users:
          fudge_list = config.fudge_users[user]
          fudge_list[:] = [(ft[0]+diff, ft[1]) for ft in fudge_list]
        toggle_flag(event_flags, common.SnarksEvent.FLAG_CONFIG_FUDGES, True)

        # Strip cached globally fudged time.
        for snark in snarks:
          snark.pop("_globally fudged time", None)
        snarkutils.gui_fudge_users(config, snarks)
        toggle_flag(event_flags, common.SnarksEvent.FLAG_SNARKS, True)
      else:
        toggle_flag(event_flags, common.SnarksEvent.FLAG_CONFIG_FUDGES, False)

      toggle_flag(event_flags, common.SnarksEvent.FLAG_CONFIG_SHOW_TIME,
                  (config.show_time != old_config.show_time))

      toggle_flag(event_flags, common.SnarksEvent.FLAG_CONFIG_PARSERS,
                  (config.parser_name != old_config.parser_name))

      toggle_flag(event_flags, common.SnarksEvent.FLAG_CONFIG_EXPORTERS,
                  (config.exporter_name != old_config.exporter_name))

      self._snarks_wrapper.commit()

      event = common.SnarksEvent(event_flags)
      self._snarks_wrapper.fire_snarks_event(event)

    config = csconfig.Config(src_config=self._snarks_wrapper.clone_config())
    config_desc = config.get_description()
    config_args = config.get_arginfo()
    config.apply_current_values_to_args(config_args)

    config_section = config_ui.ConfigSection("General")
    parsers_section = config_ui.ConfigSection("Parsers")
    exporters_section = config_ui.ConfigSection("Exporters")

    config_section.append_subsection(config_ui.ConfigSubSection("General", description=config_desc, args=config_args, apply_func=config_subsection_callback))

    for parser_name in snarkutils.list_parsers():
      parsers_pkg = __import__("lib.parsers", globals(), locals(), [parser_name])
      parser_mod = getattr(parsers_pkg, parser_name)
      parser_desc = parser_mod.get_description()
      parser_args = parser_mod.get_arginfo()
      config.apply_current_values_to_args(parser_args, parser_namespace=parser_mod.ns)

      def subsection_callback(values_dict, ns=parser_mod.ns):
        self._snarks_wrapper.checkout(self.__class__.__name__)
        config = self._snarks_wrapper.get_config()
        for (k,v) in values_dict.items():
          config.parser_options[ns+k] = v
        self._snarks_wrapper.commit()

        event = common.SnarksEvent([common.SnarksEvent.FLAG_CONFIG_PARSERS])
        self._snarks_wrapper.fire_snarks_event(event)
      parsers_section.append_subsection(config_ui.ConfigSubSection(parser_name, description=parser_desc, args=parser_args, apply_func=subsection_callback))

    for exporter_name in snarkutils.list_exporters():
      exporters_pkg = __import__("lib.exporters", globals(), locals(), [exporter_name])
      exporter_mod = getattr(exporters_pkg, exporter_name)
      exporter_desc = exporter_mod.get_description()
      exporter_args = exporter_mod.get_arginfo()
      config.apply_current_values_to_args(exporter_args, exporter_namespace=exporter_mod.ns)

      def subsection_callback(values_dict, ns=exporter_mod.ns):
        self._snarks_wrapper.checkout(self.__class__.__name__)
        config = self._snarks_wrapper.get_config()
        for (k,v) in values_dict.items():
          config.exporter_options[ns+k] = v
        self._snarks_wrapper.commit()

        event = common.SnarksEvent([common.SnarksEvent.FLAG_CONFIG_EXPORTERS])
        self._snarks_wrapper.fire_snarks_event(event)
      exporters_section.append_subsection(config_ui.ConfigSubSection(exporter_name, description=exporter_desc, args=exporter_args, apply_func=subsection_callback))
    sections = [config_section, parsers_section, exporters_section]

    return sections