Exemple #1
0
  def _from_config_cache(path):
    conf = stem.util.conf.Config()
    conf.load(path, commenting = False)

    config_options = OrderedDict()

    for key in conf.keys():
      if key.startswith('config_options.'):
        key = key.split('.')[1]

        if key not in config_options:
          config_options[key] = ConfigOption(
            conf.get('config_options.%s.name' % key, ''),
            conf.get('config_options.%s.category' % key, ''),
            conf.get('config_options.%s.usage' % key, ''),
            conf.get('config_options.%s.summary' % key, ''),
            conf.get('config_options.%s.description' % key, '')
          )

    manual = Manual(
      conf.get('name', ''),
      conf.get('synopsis', ''),
      conf.get('description', ''),
      conf.get('commandline_options', OrderedDict()),
      conf.get('signals', OrderedDict()),
      conf.get('files', OrderedDict()),
      config_options,
    )

    manual.man_commit = conf.get('man_commit', None)
    manual.stem_commit = conf.get('stem_commit', None)

    return manual
Exemple #2
0
    def test_persistence(self):
        expected = {
            '0756B7CD4DFC8182BE23143FAC0642F515182CEB':
            stem.directory.Fallback(
                address='5.9.110.236',
                or_port=9001,
                dir_port=9030,
                fingerprint='0756B7CD4DFC8182BE23143FAC0642F515182CEB',
                nickname='rueckgrat',
                has_extrainfo=True,
                orport_v6=('2a01:4f8:162:51e2::2', 9001),
                header=HEADER,
            ),
            '01A9258A46E97FF8B2CAC7910577862C14F2C524':
            stem.directory.Fallback(
                address='193.171.202.146',
                or_port=9001,
                dir_port=9030,
                fingerprint='01A9258A46E97FF8B2CAC7910577862C14F2C524',
                nickname=None,
                has_extrainfo=False,
                orport_v6=None,
                header=HEADER,
            ),
        }

        excepted_config = {
            'tor_commit': ['abc'],
            'stem_commit': ['def'],
            'header.type': ['fallback'],
            'header.version': ['2.0.0'],
            'header.timestamp': ['20170526090242'],
            '01A9258A46E97FF8B2CAC7910577862C14F2C524.address':
            ['193.171.202.146'],
            '01A9258A46E97FF8B2CAC7910577862C14F2C524.or_port': ['9001'],
            '01A9258A46E97FF8B2CAC7910577862C14F2C524.dir_port': ['9030'],
            '01A9258A46E97FF8B2CAC7910577862C14F2C524.has_extrainfo':
            ['false'],
            '0756B7CD4DFC8182BE23143FAC0642F515182CEB.address':
            ['5.9.110.236'],
            '0756B7CD4DFC8182BE23143FAC0642F515182CEB.or_port': ['9001'],
            '0756B7CD4DFC8182BE23143FAC0642F515182CEB.dir_port': ['9030'],
            '0756B7CD4DFC8182BE23143FAC0642F515182CEB.nickname': ['rueckgrat'],
            '0756B7CD4DFC8182BE23143FAC0642F515182CEB.has_extrainfo': ['true'],
            '0756B7CD4DFC8182BE23143FAC0642F515182CEB.orport6_address':
            ['2a01:4f8:162:51e2::2'],
            '0756B7CD4DFC8182BE23143FAC0642F515182CEB.orport6_port': ['9001'],
        }

        with tempfile.NamedTemporaryFile(prefix='fallbacks.') as tmp:
            stem.directory.Fallback._write(expected, 'abc', 'def', HEADER,
                                           tmp.name)

            conf = stem.util.conf.Config()
            conf.load(tmp.name)
            self.assertEqual(excepted_config, dict(conf))

            self.assertEqual(expected,
                             stem.directory.Fallback.from_cache(tmp.name))
Exemple #3
0
  def from_cache(path = None):
    """
    Provides manual information cached with Stem. Unlike
    :func:`~stem.manual.Manual.from_man` and
    :func:`~stem.manual.Manual.from_remote` this doesn't have any system
    requirements, and is faster too. Only drawback is that this manual
    content is only as up to date as the Stem release we're using.

    :param str path: cached manual content to read, if not provided this uses
      the bundled manual information

    :returns: :class:`~stem.manual.Manual` with our bundled manual information

    :raises: **IOError** if a **path** was provided and we were unable to read it
    """

    conf = stem.util.conf.Config()
    conf.load(path if path else CACHE_PATH, commenting = False)

    config_options = OrderedDict()

    for key in conf.keys():
      if key.startswith('config_options.'):
        key = key.split('.')[1]

        if key not in config_options:
          config_options[key] = ConfigOption(
            conf.get('config_options.%s.name' % key, ''),
            conf.get('config_options.%s.category' % key, ''),
            conf.get('config_options.%s.usage' % key, ''),
            conf.get('config_options.%s.summary' % key, ''),
            conf.get('config_options.%s.description' % key, '')
          )

    manual = Manual(
      conf.get('name', ''),
      conf.get('synopsis', ''),
      conf.get('description', ''),
      conf.get('commandline_options', {}),
      conf.get('signals', {}),
      conf.get('files', {}),
      config_options,
    )

    manual.man_commit = conf.get('man_commit', None)
    manual.stem_commit = conf.get('stem_commit', None)

    return manual
Exemple #4
0
    def from_cache(
        path: str = FALLBACK_CACHE_PATH
    ) -> Dict[str, 'stem.directory.Fallback']:
        conf = stem.util.conf.Config()
        conf.load(path)
        headers = collections.OrderedDict([(k.split('.', 1)[1], conf.get(k))
                                           for k in conf.keys()
                                           if k.startswith('header.')])

        results = {}

        for fingerprint in set([key.split('.')[0] for key in conf.keys()]):
            if fingerprint in ('tor_commit', 'stem_commit', 'header'):
                continue

            attr = {}

            for attr_name in ('address', 'or_port', 'dir_port', 'nickname',
                              'has_extrainfo', 'orport6_address',
                              'orport6_port'):
                key = '%s.%s' % (fingerprint, attr_name)
                attr[attr_name] = conf.get(key)

                if not attr[attr_name] and attr_name not in ('nickname',
                                                             'has_extrainfo',
                                                             'orport6_address',
                                                             'orport6_port'):
                    raise IOError("'%s' is missing from %s" %
                                  (key, FALLBACK_CACHE_PATH))

            if attr['orport6_address'] and attr['orport6_port']:
                orport_v6 = (attr['orport6_address'],
                             int(attr['orport6_port']))
            else:
                orport_v6 = None

            results[fingerprint] = Fallback(
                address=attr['address'],
                or_port=int(attr['or_port']),
                dir_port=int(attr['dir_port']),
                fingerprint=fingerprint,
                nickname=attr['nickname'],
                has_extrainfo=attr['has_extrainfo'] == 'true',
                orport_v6=orport_v6,
                header=headers,
            )

        return results
Exemple #5
0
  def from_cache(path = None):
    """
    Provides manual information cached with Stem. Unlike
    :func:`~stem.manual.Manual.from_man` and
    :func:`~stem.manual.Manual.from_remote` this doesn't have any system
    requirements, and is faster too. Only drawback is that this manual
    content is only as up to date as the Stem release we're using.

    :param str path: cached manual content to read, if not provided this uses
      the bundled manual information

    :returns: :class:`~stem.manual.Manual` with our bundled manual information

    :raises: **IOError** if a **path** was provided and we were unable to read it
    """

    conf = stem.util.conf.Config()
    conf.load(path if path else CACHE_PATH, commenting = False)

    config_options = OrderedDict()

    for key in conf.keys():
      if key.startswith('config_options.'):
        key = key.split('.')[1]

        if key not in config_options:
          config_options[key] = ConfigOption(
            conf.get('config_options.%s.name' % key, ''),
            conf.get('config_options.%s.category' % key, ''),
            conf.get('config_options.%s.usage' % key, ''),
            conf.get('config_options.%s.summary' % key, ''),
            conf.get('config_options.%s.description' % key, '')
          )

    manual = Manual(
      conf.get('name', ''),
      conf.get('synopsis', ''),
      conf.get('description', ''),
      conf.get('commandline_options', {}),
      conf.get('signals', {}),
      conf.get('files', {}),
      config_options,
    )

    manual.man_commit = conf.get('man_commit', None)
    manual.stem_commit = conf.get('stem_commit', None)

    return manual