From 54c24b18b164fec2db1656dd96083b73a977c825 Mon Sep 17 00:00:00 2001 From: gryf Date: Sun, 25 Sep 2022 21:13:14 +0200 Subject: [PATCH] Add ability to change name of the root item in the catalog. --- pycatalog/__init__.py | 18 ++++++++++-------- pycatalog/scan.py | 2 +- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/pycatalog/__init__.py b/pycatalog/__init__.py index 7eb7e4c..5e3fa84 100644 --- a/pycatalog/__init__.py +++ b/pycatalog/__init__.py @@ -164,7 +164,7 @@ class Iface(object): # scanob.update_files(node.id) scanob.update_files(node.id, self.engine) - def create(self, dir_to_add): + def create(self, dir_to_add, label=None): """Create new database""" self.root = dbo.File() self.root.id = 1 @@ -174,19 +174,17 @@ class Iface(object): self.root.type = 0 self.root.parent_id = 1 - if not self.dry_run: self.sess.add(self.root) - self.sess.add(config) self.sess.commit() print(misc.colorize("Creating new db against directory `%s'" % dir_to_add, 'white')) if not self.dry_run: scanob = scan.Scan(dir_to_add) - scanob.add_files(self.engine) + scanob.add_files(label=label) - def add(self, dir_to_add): + def add(self, dir_to_add, label=None): """Add new directory to the db""" self.root = self.sess.query(dbo.File) self.root = self.root.filter(dbo.File.type == 0).first() @@ -197,7 +195,7 @@ class Iface(object): print(misc.colorize("Adding directory `%s'" % dir_to_add, 'white')) if not self.dry_run: scanob = scan.Scan(dir_to_add) - scanob.add_files() + scanob.add_files(label=label) def _annotate(self, item, search_words): """ @@ -273,14 +271,14 @@ def update_db(args): def add_dir(args): """Add""" obj = Iface(args.db, args.pretend, args.debug) - obj.add(args.dir_to_add) + obj.add(args.dir_to_add, args.label) obj.close() def create_db(args): """List""" obj = Iface(args.db, args.pretend, args.debug) - obj.create(args.dir_to_add, args.imagedir) + obj.create(args.dir_to_add, args.label) obj.close() @@ -322,6 +320,8 @@ def main(): create = subparser.add_parser('create') create.add_argument('db') create.add_argument('dir_to_add') + create.add_argument('-l', '--label', help='Add label as the root item of ' + 'the added directory') create.add_argument('-p', '--pretend', help="Don't do the action, just " "give the info what would gonna to happen.", action='store_true', default=False) @@ -337,6 +337,8 @@ def main(): action='store_true', default=False) add.add_argument('-d', '--debug', help='Turn on debug', action='store_true', default=False) + add.add_argument('-l', '--label', help='Add label as the root item of the ' + 'added directory') add.set_defaults(func=add_dir) find = subparser.add_parser('find') diff --git a/pycatalog/scan.py b/pycatalog/scan.py index 3ed8919..2c77d6e 100644 --- a/pycatalog/scan.py +++ b/pycatalog/scan.py @@ -47,7 +47,7 @@ class Scan(object): self.files_count = self._get_files_count() self.current_count = 0 - def add_files(self, engine=None): + def add_files(self, label=None): """ Returns list, which contain object, modification date and file size.