[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Sqlite support for manual content



commit 5c4c110e5d353be3f9651eb930ded4320b45948c
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Fri Aug 25 10:26:11 2017 -0700

    Sqlite support for manual content
    
    Adding support for using sqlite to persist manual content rather than our
    config module.
---
 stem/manual.py      | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 test/unit/manual.py | 16 +++++++++++++--
 2 files changed, 70 insertions(+), 5 deletions(-)

diff --git a/stem/manual.py b/stem/manual.py
index 66e0abf3..c61a73ea 100644
--- a/stem/manual.py
+++ b/stem/manual.py
@@ -327,6 +327,9 @@ class Manual(object):
 
     # TODO: drop _from_config_cache() with stem 2.x
 
+    if path is None:
+      path = CACHE_PATH
+
     if path is not None and path.endswith('.sqlite'):
       return Manual._from_sqlite_cache(path)
     else:
@@ -334,12 +337,38 @@ class Manual(object):
 
   @staticmethod
   def _from_sqlite_cache(path):
-    pass
+    with sqlite3.connect(path) as conn:
+      cursor = conn.cursor()
+
+      cursor.execute('SELECT name, synopsis, description, man_commit, stem_commit FROM metadata')
+      name, synopsis, description, man_commit, stem_commit = cursor.fetchone()
+
+      cursor.execute('SELECT name, description FROM commandline')
+      commandline = dict(cursor.fetchall())
+
+      cursor.execute('SELECT name, description FROM signals')
+      signals = dict(cursor.fetchall())
+
+      cursor.execute('SELECT name, description FROM files')
+      files = dict(cursor.fetchall())
+
+      cursor.execute('SELECT name, category, usage, summary, description FROM torrc')
+      config_options = OrderedDict()
+
+      for entry in cursor.fetchall():
+        option, category, usage, summary, option_description = entry
+        config_options[option] = ConfigOption(option, category, usage, summary, option_description)
+
+      manual = Manual(name, synopsis, description, commandline, signals, files, config_options)
+      manual.man_commit = man_commit
+      manual.stem_commit = stem_commit
+
+      return manual
 
   @staticmethod
   def _from_config_cache(path):
     conf = stem.util.conf.Config()
-    conf.load(path if path else CACHE_PATH, commenting = False)
+    conf.load(path, commenting = False)
 
     config_options = OrderedDict()
 
@@ -469,7 +498,31 @@ class Manual(object):
       return self._save_as_config(path)
 
   def _save_as_sqlite(self, path):
-    pass
+    with sqlite3.connect(path + '.new') as conn:
+      conn.execute('CREATE TABLE metadata(name TEXT, synopsis TEXT, description TEXT, man_commit TEXT, stem_commit TEXT)')
+      conn.execute('CREATE TABLE commandline(name TEXT PRIMARY KEY, description TEXT)')
+      conn.execute('CREATE TABLE signals(name TEXT PRIMARY KEY, description TEXT)')
+      conn.execute('CREATE TABLE files(name TEXT PRIMARY KEY, description TEXT)')
+      conn.execute('CREATE TABLE torrc(name TEXT PRIMARY KEY, category TEXT, usage TEXT, summary TEXT, description TEXT)')
+
+      conn.execute('INSERT INTO metadata(name, synopsis, description, man_commit, stem_commit) VALUES (?,?,?,?,?)', (self.name, self.synopsis, self.description, self.man_commit, self.stem_commit))
+
+      for k, v in self.commandline_options.items():
+        conn.execute('INSERT INTO commandline(name, description) VALUES (?,?)', (k, v))
+
+      for k, v in self.signals.items():
+        conn.execute('INSERT INTO signals(name, description) VALUES (?,?)', (k, v))
+
+      for k, v in self.files.items():
+        conn.execute('INSERT INTO files(name, description) VALUES (?,?)', (k, v))
+
+      for v in self.config_options.values():
+        conn.execute('INSERT INTO torrc(name, category, usage, summary, description) VALUES (?,?,?,?,?)', (v.name, v.category, v.usage, v.summary, v.description))
+
+    if os.path.exists(path):
+      os.remove(path)
+
+    os.rename(path + '.new', path)
 
   def _save_as_config(self, path):
     conf = stem.util.conf.Config()
diff --git a/test/unit/manual.py b/test/unit/manual.py
index 0e6a50df..f025c1a1 100644
--- a/test/unit/manual.py
+++ b/test/unit/manual.py
@@ -189,9 +189,9 @@ class TestManual(unittest.TestCase):
     self.assertEqual('Description of this new option.', option.description)
 
   @test.require.command('man')
-  def test_saving_manual(self):
+  def test_saving_manual_as_config(self):
     """
-    Check that we can save and reload manuals.
+    Check that we can save and reload manuals as a config.
     """
 
     manual = stem.manual.Manual.from_man(EXAMPLE_MAN_PATH)
@@ -201,6 +201,18 @@ class TestManual(unittest.TestCase):
       loaded_manual = stem.manual.Manual.from_cache(tmp.name)
       self.assertEqual(manual, loaded_manual)
 
+  def test_saving_manual_as_sqlite(self):
+    """
+    Check that we can save and reload manuals as sqlite.
+    """
+
+    manual = stem.manual.Manual.from_man(EXAMPLE_MAN_PATH)
+
+    with tempfile.NamedTemporaryFile(prefix = 'saved_test_manual.', suffix = '.sqlite') as tmp:
+      manual.save(tmp.name)
+      loaded_manual = stem.manual.Manual.from_cache(tmp.name)
+      self.assertEqual(manual, loaded_manual)
+
   def test_cached_manual(self):
     manual = _cached_manual()
 



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits