Merge branch 'master' into peter
This commit is contained in:
commit
9a97a139e2
4 changed files with 62 additions and 25 deletions
|
|
@ -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.
|
||||
|
|
@ -448,7 +461,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)
|
||||
|
|
@ -541,7 +554,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
|
||||
|
|
@ -566,5 +579,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")
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
@ -546,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)
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
||||
|
||||
|
|
@ -1170,8 +1170,11 @@ 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("--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",
|
||||
|
|
@ -1185,28 +1188,46 @@ 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 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: " + 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("Transaction Types: " + ", ".join(txntypes))
|
||||
default_txns = filemanip.FileStorage.DEFAULT_TRANSACTION_TYPES
|
||||
dump_database_information(default_txns, 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):
|
||||
def notify(text):
|
||||
print(text, file=sys.stderr)
|
||||
sys.stderr.write("%s\n" % text)
|
||||
db.upgrade(txntype, notify, version)
|
||||
|
||||
|
||||
|
|
@ -1214,8 +1235,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:
|
||||
|
|
@ -1225,7 +1245,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
|
||||
|
|
|
|||
6
newfol
6
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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue