torproject / stem

Python controller library for Tor
https://stem.torproject.org/
GNU Lesser General Public License v3.0
280 stars 76 forks source link

Add CACHE_FILE_NAME to each "descriptor" #85

Closed jbrown299 closed 3 years ago

jbrown299 commented 3 years ago

There is code inside parse_file function which determinate type of descryptor by file name:

      if filename == 'cached-descriptors' or filename == 'cached-descriptors.new':
        return stem.descriptor.server_descriptor._parse_file(descriptor_file, validate = validate, **kwargs)
      elif filename == 'cached-extrainfo' or filename == 'cached-extrainfo.new':
        return stem.descriptor.extrainfo_descriptor._parse_file(descriptor_file, validate = validate, **kwargs)
      elif filename == 'cached-microdescs' or filename == 'cached-microdescs.new':
        return stem.descriptor.microdescriptor._parse_file(descriptor_file, validate = validate, **kwargs)
      elif filename == 'cached-consensus':
        return stem.descriptor.networkstatus._parse_file(descriptor_file, validate = validate, document_handler = document_handler, **kwargs)
      elif filename == 'cached-microdesc-consensus':
        return stem.descriptor.networkstatus._parse_file(descriptor_file, is_microdescriptor = True, validate = validate, document_handler = document_handler, **kwargs)
      else:
        raise TypeError("Unable to determine the descriptor's type. filename: '%s', first line: '%s'" % (filename, first_line))

So we can add for each type of descriptor some class variable which store default cache file name. Profit: We can use it here above. And we can use it when we want save to disk such document with this default file name.

atagar commented 3 years ago

Hi James. I don't feel strongly either way, but I don't see the second as being an advantage...

And we can use it when we want save to disk such document with this default file name.

Stem never writes into tor's cache directory. These filenames are an internal implementation detail of the tor process, and liable to change at any time. We only do the above matching as a convenience for our users (so parse_file() can read from the cache directory without a descriptor_type argument).

Feel free to reopen if moving these to a class constant can provide a more significant benefit.