From 0f11e5118d0722b76681a6ea23242e41bc5ff21b Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 20 Jul 2014 00:04:25 +0000 Subject: [PATCH 1/7] Avoid print() with keyword arguments. This syntax isn't compatible with Python 2, so some syntax checking tools complain. Signed-off-by: brian m. carlson --- lib/newfol/database.py | 2 +- lib/newfol/filemanip.py | 1 - lib/newfol/main.py | 9 ++++----- newfol | 6 +++--- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/newfol/database.py b/lib/newfol/database.py index 8ba4bca..c24d59f 100644 --- a/lib/newfol/database.py +++ b/lib/newfol/database.py @@ -552,5 +552,5 @@ class Database: vault = self._vault(self._location, preferred, txntype, options) vault.store(self._records) with open(self.location() + "/version", "w") as fp: - print(str(int(preferred)), file=fp) + fp.write("%d\n" % int(preferred)) notify("Upgrade complete") diff --git a/lib/newfol/filemanip.py b/lib/newfol/filemanip.py index c9a47b6..abd2fc4 100644 --- a/lib/newfol/filemanip.py +++ b/lib/newfol/filemanip.py @@ -475,7 +475,6 @@ class JSONFile(FileFormat): raise CorruptFileError("json", "not a list") for rec in recs: if not isinstance(rec, Record): - print(type(rec)) raise CorruptFileError("json", "not a record") if rec.version() != 3: raise CorruptFileError("json", "old record") diff --git a/lib/newfol/main.py b/lib/newfol/main.py index 4284e8a..4345165 100644 --- a/lib/newfol/main.py +++ b/lib/newfol/main.py @@ -27,7 +27,7 @@ def log(*args): return if logfd is None: logfd = open("/tmp/newfol_debug.log", "w") - print(*args, file=logfd) + logfd.write(" ".join(*args)) logfd.flush() @@ -1165,7 +1165,7 @@ def ensure_db_dir(path): if not os.path.exists(path): os.mkdir(path, 0o700) with open(path + "/version", "w") as fp: - print(str(int(DatabaseVersion.preferred())), file=fp) + fp.write("%d\n" % int(DatabaseVersion.preferred())) def print_version(db): @@ -1186,7 +1186,7 @@ def print_version(db): def upgrade_database(db, version, txntype): def notify(text): - print(text, file=sys.stderr) + sys.stderr.write("%s\n" % text) db.upgrade(txntype, notify, version) @@ -1194,8 +1194,7 @@ def main(args): use_curses = False argobj = parse_args(args) if argobj.dbname is not None: - print("--dbname argument is obsolete; use --table instead", - file=sys.stderr) + sys.stderr.write("--dbname is obsolete; use --table instead\n") if argobj.table is None and argobj.dbname is not None: argobj.table = argobj.dbname if len(args) == 0: diff --git a/newfol b/newfol index e3c024c..3e76c73 100755 --- a/newfol +++ b/newfol @@ -13,11 +13,11 @@ if __name__ == '__main__': locale.setlocale(locale.LC_ALL, '') newfol.main.main(sys.argv[1:]) except newfol.exception.NewfolError as e: - print("E: {0}".format(str(e)), file=sys.stderr) + sys.stderr.write("E: {0}\n".format(str(e))) sys.exit(2) except newfol.exception.UpgradeNeededError as e: - print("E: {0}; try upgrading".format(str(e)), file=sys.stderr) + sys.stderr.write("E: {0}; try upgrading\n".format(str(e))) sys.exit(2) except newfol.exception.FilemanipError as e: - print("E: {0}".format(str(e)), file=sys.stderr) + sys.stderr.write("E: {0}\n".format(str(e))) sys.exit(2) From fe49c35c1479c5496520066c0d35089736a34e54 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 20 Jul 2014 00:11:39 +0000 Subject: [PATCH 2/7] Improve reporting of database data with --version. The user can now see the raw hex version as well. Signed-off-by: brian m. carlson --- lib/newfol/main.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/newfol/main.py b/lib/newfol/main.py index 4345165..7bacf45 100644 --- a/lib/newfol/main.py +++ b/lib/newfol/main.py @@ -1176,11 +1176,11 @@ def print_version(db): txntypes = db.schema().transaction_types() ver = db.version() - print("Location: " + db.location()) - print("Database Version: " + str(ver)) - print("Record Version: " + str(ver.record_version())) - print("Serialization Version: " + str(ver.serialization_version())) - print("Serialization Format: " + str(ver.serialization())) + print("Location: %s" % db.location()) + print("Database Version: %d (%#010x)" % (ver, ver)) + print("Record Version: %d" % ver.record_version()) + print("Serialization Version: %d" % ver.serialization_version()) + print("Serialization Format: %s" % ver.serialization()) print("Transaction Types: " + ", ".join(txntypes)) From 219116d414aeabec24398421d3e6312d470b2ed4 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 20 Jul 2014 00:24:08 +0000 Subject: [PATCH 3/7] Expose supported compression and serialization types. Signed-off-by: brian m. carlson --- lib/newfol/database.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/newfol/database.py b/lib/newfol/database.py index c24d59f..06ab5cb 100644 --- a/lib/newfol/database.py +++ b/lib/newfol/database.py @@ -38,7 +38,15 @@ class DatabaseVersion(int): Returns None if compression is not enabled. """ - return (None, "xz")[self.compression_version()] + return self.compression_types()[self.compression_version()] + + @staticmethod + def compression_types(): + """A tuple containing supported compression types. + + None is used to indicate the lack of compression. + """ + return (None, "xz") def serialization_version(self): """The value indicating the format in which the data is serialized.""" @@ -49,7 +57,12 @@ class DatabaseVersion(int): This value is suitable for passing to a FileStorage object. """ - return ("csv", "pickle", "json")[self.serialization_version()] + return self.serialization_types()[self.serialization_version()] + + @staticmethod + def serialization_types(): + """A tuple indicating the formats in which data can be serialized.""" + return ("csv", "pickle", "json") def record_version(self): """The record version. From 021b90bb30c7f85d3b42422e5f25cd5973f5bdc7 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 20 Jul 2014 00:28:50 +0000 Subject: [PATCH 4/7] Expand --version output. Signed-off-by: brian m. carlson --- lib/newfol/main.py | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/lib/newfol/main.py b/lib/newfol/main.py index 7bacf45..49cde31 100644 --- a/lib/newfol/main.py +++ b/lib/newfol/main.py @@ -1168,20 +1168,37 @@ def ensure_db_dir(path): fp.write("%d\n" % int(DatabaseVersion.preferred())) +def dump_database_information(txntypes, ver, label=None): + stats = [ + "Database Version: %d (%#010x)" % (ver, ver), + "Record Version: %d" % ver.record_version(), + "Serialization Version: %d" % ver.serialization_version(), + "Serialization Format: %s" % ver.serialization(), + "Compression Version: %d" % ver.compression_version(), + "Compression Format: %s" % ver.compression(), + "Transaction Types: " + ", ".join(txntypes), + ] + print() + for s in stats: + print("%s%s" % (label or '', s)) + + def print_version(db): print("newfol " + __version__) if db is None: print("No database loaded.") - return + else: + txntypes = db.schema().transaction_types() + ver = db.version() + dump_database_information(txntypes, ver) - txntypes = db.schema().transaction_types() - ver = db.version() - print("Location: %s" % db.location()) - print("Database Version: %d (%#010x)" % (ver, ver)) - print("Record Version: %d" % ver.record_version()) - print("Serialization Version: %d" % ver.serialization_version()) - print("Serialization Format: %s" % ver.serialization()) - print("Transaction Types: " + ", ".join(txntypes)) + dump_database_information(['hash'], DatabaseVersion.preferred(), + "Default ") + print() + print("Supported Serialization Types: " + ", ". + join(DatabaseVersion.serialization_types())) + print("Supported Compression Types: " + ", ". + join([str(x) for x in DatabaseVersion.compression_types()])) def upgrade_database(db, version, txntype): From 58c15a77c3579682a2a9008a3d6332c46f52fe1c Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Mon, 28 Jul 2014 23:08:31 +0000 Subject: [PATCH 5/7] Allow specifying multiple transaction types on the command line. Signed-off-by: brian m. carlson --- lib/newfol/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/newfol/main.py b/lib/newfol/main.py index 49cde31..c43aa1f 100644 --- a/lib/newfol/main.py +++ b/lib/newfol/main.py @@ -1150,7 +1150,7 @@ def parse_args(args): parser.add_argument("--minfields", dest="minfields", action="store", type=int, default=0, help="minimum number of fields per record") - parser.add_argument("--txntype", dest="txntype", action="store", + parser.add_argument("--txntype", dest="txntype", action="append", default=None, help="type of transactional helper") parser.add_argument("--dbtype", dest="dbtype", action="store", default="csv", help="type of data for import/export") From 4984feef92730434fe90fe6aa32bfaf437860a8b Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Mon, 28 Jul 2014 23:20:09 +0000 Subject: [PATCH 6/7] Allow changing transaction types during upgrade. The --txntype option, while useful, overrode the types of transactions both on load and store. While this was fine for some types of transactions, like the git type, this prevented converting from one type of hash transaction to another. Add an option, --from-txntype, which overrides the load transactions only on upgrade. Signed-off-by: brian m. carlson --- lib/newfol/database.py | 2 +- lib/newfol/main.py | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/newfol/database.py b/lib/newfol/database.py index 06ab5cb..879951f 100644 --- a/lib/newfol/database.py +++ b/lib/newfol/database.py @@ -540,7 +540,7 @@ class Database: notify = lambda x: x if txntype is None: txntype = list(self._schema.transaction_types()) - else: + elif isinstance(txntype, str): txntype = [txntype] txntype.insert(0, "hash") actual = self._version diff --git a/lib/newfol/main.py b/lib/newfol/main.py index c43aa1f..f301039 100644 --- a/lib/newfol/main.py +++ b/lib/newfol/main.py @@ -1152,6 +1152,9 @@ def parse_args(args): help="minimum number of fields per record") parser.add_argument("--txntype", dest="txntype", action="append", default=None, help="type of transactional helper") + parser.add_argument("--from-txntype", dest="from_txntype", action="append", + default=None, + help="current type of transactional helper on upgrade") parser.add_argument("--dbtype", dest="dbtype", action="store", default="csv", help="type of data for import/export") parser.add_argument("--upgrade-version", dest="upgrade_version", @@ -1221,7 +1224,10 @@ def main(args): ensure_db_dir(argobj.homedir) db = None try: - db = newfol.database.Database.load(argobj.homedir, argobj.txntype) + txntype = argobj.txntype + if argobj.from_txntype and argobj.cmd == 'upgrade': + txntype = argobj.from_txntype + db = newfol.database.Database.load(argobj.homedir, txntype) db.lock() dbd = DatabaseData() dbd._database = db From fc2b1cb17704b66c10c16df486c32aca95439761 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Tue, 29 Jul 2014 02:14:18 +0000 Subject: [PATCH 7/7] Centralize list of default transaction types. Signed-off-by: brian m. carlson --- lib/newfol/database.py | 2 +- lib/newfol/filemanip.py | 2 ++ lib/newfol/main.py | 3 ++- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/newfol/database.py b/lib/newfol/database.py index 879951f..d30f533 100644 --- a/lib/newfol/database.py +++ b/lib/newfol/database.py @@ -447,7 +447,7 @@ class Database: options["compression"] = version.compression() if txntype is None: txntype = [] - txntype.insert(0, "hash") + txntype[0:0] = newfol.filemanip.FileStorage.DEFAULT_TRANSACTION_TYPES return newfol.filemanip.FileStorage(version.serialization(), location + "/dtb", txntype, options) diff --git a/lib/newfol/filemanip.py b/lib/newfol/filemanip.py index abd2fc4..5fd9449 100644 --- a/lib/newfol/filemanip.py +++ b/lib/newfol/filemanip.py @@ -545,6 +545,8 @@ class RawFile(FileFormat): class FileStorage: """A file (or file-like object) in a certain format.""" + DEFAULT_TRANSACTION_TYPES = ('hash',) + def __init__(self, fmt, filename, txnformat=None, options=None): txnformat = self._canonicalize_transaction_types(txnformat) self._txn = self._make_transaction_store(txnformat, options) diff --git a/lib/newfol/main.py b/lib/newfol/main.py index f301039..dc0bd83 100644 --- a/lib/newfol/main.py +++ b/lib/newfol/main.py @@ -1195,7 +1195,8 @@ def print_version(db): ver = db.version() dump_database_information(txntypes, ver) - dump_database_information(['hash'], DatabaseVersion.preferred(), + default_txns = filemanip.FileStorage.DEFAULT_TRANSACTION_TYPES + dump_database_information(default_txns, DatabaseVersion.preferred(), "Default ") print() print("Supported Serialization Types: " + ", ".