def generate_rss(self, rss_file=None, data=None): """ Generate RSS file from news :param rss_file: Path to file rss.xml :type rss_file: str :param data: Data to create RSS from :type data: dict data['news'] = { ... } :return: Boolean :rtype: bool :raises SystemExit: If 'news' key is not found in 'data' dict :raises SystemExit: If rss file cannot be opened """ if rss_file is not None: Utils.verbose("[rss] rss_file set to %s" % rss_file) self.rss_file = rss_file if data is None: data = self.get_news() elif 'news' not in data: Utils.error("Could not find 'news' key in data") if len(data['news']) == 0: Utils.warn("No data to display") return True items = [] try: for new in data['news']: item = Item(title=new['title'], description=new['text'], author=self.config.get('RSS', 'feed.author'), guid=Guid(self.config.get('RSS', 'feed.news.link') + '#' + str(new['item'])), pubDate=datetime.strptime(new['date'], self.config.get('RSS', 'rss.date.format') .replace('%%', '%')) ) items.append(item) feed = Feed(title=self.config.get('RSS', 'feed.title'), link=self.config.get('RSS', 'feed.link'), description=self.config.get('RSS', 'feed.description'), language=self.config.get('RSS', 'feed.language'), lastBuildDate=datetime.now(), items=items) if self.fh is None: self.fh = open(self.rss_file, 'w') Utils.uprint(feed.rss(), to=self.fh) if self.rss_file is not None: self.fh.close() except (NoOptionError, NoSectionError) as err: Utils.error("Option missing in config file: %s" % str(err)) except (OSError, IOError) as err: Utils.error("Can't open file %s: %s" % (self.rss_file, str(err))) return True
def _update_mongodb(self, data=None, collection='catalog', params=None, upsert=True): """ Function that really update the Mongodb collection ('catalog') It does an upsert to update the collection :param data: Data to be updated :type data: dict :param collection: Collection name to update (Default 'catalog') :type collection: str :param params: Extra parameters to filter documents to update :type params: dict :param upsert: Perform upsert or not :type upsert: bool :return: Boolean :rtype: bool """ if not self.manager.bank.name: Utils.error("Can't update, bank name required") if not data: Utils.warn("[%s] No data to update bioweb catalog" % self.manager.bank.name) return True if not Bioweb.CONNECTED: self._init_db() # Find arguments search_params = {'type': Bioweb.COLLECTION_TYPE, 'name': self.manager.bank.name} # Extra serach parameters? if params is not None: search_params.update(params) for item in data: if '_id' in item: search_params['_id'] = item['_id'] if (pymongo.version_tuple)[0] > 2: res = self.getCollection(collection).update_one(search_params, {'$set': item}, upsert=upsert) else: res = self.getCollection(collection).update(search_params, {'$set': item}, upsert=upsert) self._update_documents_counts(res) self._print_updated_documents() return True
def set_bank_update_news(self): """ Send a new to MongoDB to let bioweb know about bank update. Update MongoDB :return: Boolean :rtype: bool """ if not self.manager.bank: Utils.error("A bank name is required") if 'current' in self.manager.bank.bank: data = {} data['message'] = "Bank %s updated to version %s" % (self.manager.bank.name, str(self.manager.current_release())) data['date'] = Utils.time2date(self.manager.bank.bank['current']) data['operation'] = "databank update" data['type'] = Bioweb.COLLECTION_TYPE data['name'] = self.manager.bank.name return self._update_mongodb(data=[data], collection='news') Utils.warn("Can't set new %s bank version, not published yet" % self.manager.bank.name) return False
def _prepare_links(self, source=None, target=None, get_deepest=False, fallback=None, requires=None, limit=0): """ Prepare stuff to create links :param source: Source path :type source: str :param target: Destination path :type target: str :param get_deepest: Try to find deepest directory(ies) from source :type get_deepest: bool :param fallback: Alternative source if source does not exist :type fallback: str :param requires: A required file or directory :type requires: str :param limit: Limit deepest search to `limit` depth, default 0, no limit :type limit: int :return: Boolean :rtype: bool :raises SystemExit: If 'source' or 'target' are None :raises SystemExit: If 'data.dir' not set in :py:data:`global.properties` :raises SystemExit: If 'production.dir' not set in :py:data:`manager.properties` :raises SystemExit: If 'target' directory cannot be created """ self._check_source_target_parameters(source=source, target=target) data_dir = self.bank_data_dir source = os.path.join(data_dir, source) target_dir = self.manager.config.get('MANAGER', 'production.dir') bank_name = self.manager.bank.name if requires is not None: if not os.path.exists(os.path.join(data_dir, requires)): Utils.warn("[%s] Can't create %s, requires param %s not here." % (bank_name, source, requires)) return False if not os.path.isdir(source): if fallback is None: if self.manager.get_verbose(): Utils.warn("[%s] %s does not exist" % (bank_name, source)) return False else: if self.manager.get_verbose(): Utils.verbose("[%s] %s does not exist. Fallback to %s" % (bank_name, source, fallback)) source = os.path.join(data_dir, fallback) if not os.path.isdir(source): if self.manager.get_verbose(): Utils.warn("[%s] Fallback %s does not exist" % (bank_name, source)) return False if get_deepest: source = Utils.get_deepest_dir(source, full=get_deepest, limit=limit) target = os.path.join(target_dir, target) # Check destination directory where to create link(s) if not os.path.exists(target) and not os.path.isdir(target): if Manager.get_simulate() and Manager.get_verbose(): Utils.verbose("[_prepare_links] [%s] Creating directory %s" % (bank_name, target)) else: try: if not Manager.get_simulate(): os.makedirs(target) except OSError as err: Utils.error("[%s] Can't create %s dir: %s" % (bank_name, target, str(err))) self.source = source self.target = target if Manager.get_verbose(): Utils.verbose("[prepare_links] source %s" % self.source) Utils.verbose("[prepare_links] target %s" % self.target) return True