def run(args): """ add a key value pair to a file's metadata """ files = args.file G.info('found %d files' % len(files)) for filename in files: if args.madmode == 'mad': madpy.util.exer('Cannot run mad init in --mad mode') mdf = Metadata(filename) metadata = {} if args.gid: metadata['gid'] = args.gid G.info("Processing file %s" % filename) mtime = datetime.fromtimestamp(os.path.getmtime(filename)).isoformat() metadata['FileSize'] = os.path.getsize(filename) if not mdf.has_key('Sha1sum') and args.sha1sum: G.warning("Generating Shasum for %s" % filename) metadata['Sha1sum'] = _get_hash('sha1sum', filename) metadata['LastModified'] = mtime mdf.update(metadata) mdf.save()
def get_site(args): if args.password: pwd = args.password else: pwd = getpass.getpass("SMW Password: "******"Connecting to SMW") if conf.smw.https: site = mwclient.Site(('https', conf.smw.host), path=conf.smw.path) try: site = mwclient.Site(('http', conf.smw.host), path=conf.smw.path) except: try: site = mwclient.Site(('https', conf.smw.host), path=conf.smw.path) except: G.critical("Cannot login the swm site") raise try: site.login(args.username, pwd) except mwclient.errors.LoginError: exer("Can not login. (Wrong password?)") G.info("Connected to SMW") return site
def run(args): """ Upload the pages to SMW """ files = args.file hostname = socket.gethostname() G.info("this is: %s" % hostname) site = madpy.smw.get_site(args) for f in files: full_path = os.path.abspath(f) md = metadata.Metadata(f, mode=args.madmode) if not 'Gid' in md: G.critical("No Gid found - run 'mad init %s'" % f) sys.exit() G.info("Processing file %s" % f) pname = "%s" % (md.metadata['Gid']) page = site.Pages[pname] old_page_text = page.edit() separator = '<!-- MAD - Do not change anything above this line -->' if separator in old_page_text: old_page_text = old_page_text.split(separator)[1] md.metadata['Hostname'] = hostname md.metadata['FileLocation'] = full_path ppropps = [] for k in md.metadata.keys(): v = md.metadata[k] dti = conf.datatypes[k] if type(v) == type([]): for sv in v: ppropps.append("[[%s::%s| ]]" % (k, sv)) else: ppropps.append("[[%s::%s| ]]" % (k, v)) if args.attach: basename = os.path.basename(f) ufn = basename G.warning("Uploading file %s (%s)" % (ufn, full_path)) with open(full_path, 'rb') as F: site.upload(file = F, filename = ufn, description = "Mad uploaded file for %s" % pname, ignore=True) ppropps.append("[[AttachedFile::File:%s| ]]" % ufn) if args.force: new_page = "\n" + " ".join(ppropps) + "[[Category:MadObject]]\n{{" + \ args.template + "}}\n" + separator else: new_page = "\n" + " ".join(ppropps) + "[[Category:MadObject]]\n{{" + \ args.template + "}}\n" + separator + old_page_text #print new_page page.save(new_page, summary="Mad created/refreshed object data")
def run(args): """ Upload the pages to SMW """ files = args.file hostname = socket.gethostname() G.info("this is: %s" % hostname) for f in files: full_path = os.path.abspath(f) md = metadata.Metadata(f) G.info("Processing file %s" % f) md.metadata['Hostname'] = hostname md.metadata['FileLocation'] = full_path pfridge.save(md)
def run(args): """ add a key value pair to parse_argument file's metadata """ files = [] remove_keys = [] kvs = [] for parse_argument in args.setargs: #is this parse_argument file or parse_argument key=value pair? if '=' in parse_argument: #it seems this is a key=value pair key, value = parse_argument.split('=', 1) if (not args.force) and (not key in conf.datatypes.keys()): G.critical("Invalid key %s" % key) print "Available keys are:" print ", ".join(conf.datatypes.keys()) sys.exit(-1) if not value: remove_keys.append(key) G.info("Preparing %s for removal" % key) else: kvs.append((key, value)) G.debug("adding key, value pair %s, %s" % (key, value)) else: fmd = Metadata(parse_argument) files.append(fmd) G.info('found %d key value pairs' % len(kvs)) G.info('found %d files' % len(files)) if len(files) == 0: exerr("Must specify at least one file") for madfile in files: G.info("Processing file %s" % madfile) for k, v in kvs: G.debug("applying %s, %s" % (k, v)) madfile.apply_kv(k, v) madfile.remove_keys(remove_keys) madfile.save()