def _header(self): ''' define suite.rc header information ''' start_time = utils.datetime_to_string( utils.return_validate(self.config['options_general']['date_start']), format='%Y%m%d%H') end_time = utils.datetime_to_string( utils.return_validate(self.config['options_general']['date_end']), format='%Y%m%d%H') # define template template = """#!Jinja2 {{% set START = "{start_time}" %}} {{% set STOP = "{end_time}" %}} [cylc] # set required cylce point format cycle point format = %Y%m%d%H """ # context variables in template context = { "start_time":start_time, "end_time":end_time } return template.format(**context)
async def interval(self, ctx, *interval): """ Manage automated backups for this server Get more help on the [wiki](https://wiki.xenon.bot/en/backups#automated-backups-interval). __Arguments__ **interval**: The time between every backup or "off". (min 24h) Supported units: hours(h), days(d), weeks(w) Example: 1d 12h __Examples__ ```{b.prefix}backup interval 24h``` """ if len(interval) > 0: await ctx.invoke("backup interval on " + " ".join(interval)) return interval = await ctx.bot.db.intervals.find_one({ "guild": ctx.guild_id, "user": ctx.author.id }) if interval is None: raise ctx.f.INFO( "The **backup interval is** currently turned **off**.\n" f"Turn it on with `{ctx.bot.prefix}backup interval on 24h`.") else: raise ctx.f.INFO( embed={ "author": { "name": "Backup Interval" }, "fields": [{ "name": "Interval", "value": utils.timedelta_to_string( timedelta(hours=interval["interval"])), "inline": True }, { "name": "Last Backup", "value": utils.datetime_to_string(interval["last"]) + " UTC", "inline": False }, { "name": "Next Backup", "value": utils.datetime_to_string(interval["next"]) + " UTC", "inline": False }] })
def run_unipost_file(self, wrfout, frequency=2, use_t0=False): ''' Input variables for the function are: - wrfout: full path to a wrfout file (regular wrfout naming) - (optional) frequency: time interval in hours at which processing should take place - (optional) use_t0: boolean, process time step 0 The function provides the following functionality: description ''' time_steps = utils.timesteps_wrfout(wrfout) # convert to hours since timestep 0 td = [int((t - time_steps[0]).total_seconds()/3600) for t in time_steps] # modulo should be zero if not td[-1]%frequency==0: message = '' # TODO: add error message logger.error(message) raise IOError(message) else: # create list of booleans where module is 0 modulo = [tdi%frequency==0 for tdi in td] for idx, tstep in enumerate(time_steps): if (not use_t0 and idx==0) or (modulo[idx] is False): continue # convert time step to time string current_time = utils.datetime_to_string(tstep) # run unipost step self._run_unipost_step(wrfout, current_time, td[idx])
def detail(request, pk): try: post = Post.objects.get(id=pk) except: return render_response(request, ErrorCode.NOT_FOUND) post.increase_views() comment_list = post.comment_set.values('id', 'name', 'text', 'created_time') post.body = markdown.markdown( post.body, extensions=[ 'markdown.extensions.extra', # 'markdown.extensions.codehilite', 'markdown.extensions.toc', ]) rows = [] for comment in comment_list: row = {} row['id'] = comment.get('id') row['name'] = comment.get('name') row['text'] = comment.get('text') row['created_time'] = datetime_to_string(comment.get('created_time')) rows.append(row) article = { "id": post.id, "title": post.title, "content": post.body, "modified_time": post.modified_time, "author": post.author.username } context = {"post": article, "comment": rows} return render_response(request, ErrorCode.OK, context)
async def blacklist(self, ctx, user: wkr.UserConverter = None): if user is None: menu = BlackListMenu(ctx) return await menu.start() user = await user(ctx) entry = await ctx.client.db.blacklist.find_one({"_id": user.id}) if entry is None: raise ctx.f.ERROR(f"{user.mention} **is not on the blacklist**.") raise ctx.f.INFO( embed={ "author": { "name": str(user) }, "fields": [{ "name": "Reason", "value": entry["reason"] }, { "name": "Staff", "value": f"<@{entry['staff']}>" }, { "name": "Timestamp", "value": utils.datetime_to_string(entry["timestamp"]) }] })
def serialize(self): return { "id": self.id, "name": self.name, "datetime": datetime_to_string(self.datetime), "classification": self.classification }
async def info(self, ctx, backup_id): """ Get information about a backup __Arguments__ **backup_id**: The id of the backup or the guild id to for latest automated backup __Examples__ ```{b.prefix}backup info 3zpssue46g``` """ backup = await ctx.client.db.backups.find_one({ "_id": backup_id, "creator": ctx.author.id }) if backup is None: raise ctx.f.ERROR( f"You have **no backup** with the id `{backup_id}`.") backup["data"].pop("members", None) guild = wkr.Guild(backup["data"]) channels = utils.channel_tree(guild.channels) if len(channels) > 1024: channels = channels[:1000] + "\n...\n```" roles = "```{}```".format("\n".join([ r.name for r in sorted( guild.roles, key=lambda r: r.position, reverse=True) ])) if len(roles) > 1024: roles = roles[:1000] + "\n...\n```" raise ctx.f.DEFAULT( embed={ "title": guild.name, "fields": [{ "name": "Created At", "value": utils.datetime_to_string(backup["timestamp"]) + " UTC", "inline": False }, { "name": "Channels", "value": channels, "inline": True }, { "name": "Roles", "value": roles, "inline": True }] })
def create_email(document): email = {} docu = document.document status = document.status username = docu.owner.username title = document.title filename = document.docfile_basename string_start_datetime = utils.datetime_to_string(document.task_start) start_time = utils.format_datetime_string(string_start_datetime) string_end_datetime = utils.datetime_to_string(document.task_end) end_time = utils.format_datetime_string(string_end_datetime) diff_time = document.task_end - document.task_start total_time = utils.format_datetimediff(diff_time) doc_url = get_absolute_url(reverse( "docviewer_viewer_view", kwargs={'pk': document.pk, 'slug': document.slug} )) festos_url = get_absolute_url(document.related_url) template = loader.get_template('docviewer/email.html') context = Context({ 'status': status, 'username': username, 'document': '{} - {}'.format(title, filename), 'start_time': start_time, 'end_time': end_time, 'total_time': total_time, 'doc_url': doc_url, 'festos_url': festos_url, }) message = template.render(context) contributors = docu.get_users_with_perms() recipient_list = [docu.owner.email] + [c.email for c in contributors] email['message'] = message email['recipient_list'] = recipient_list return email
def article_list(request): post_list = Post.objects.values( 'id', 'title', 'excerpt', 'views', 'modified_time', 'tags__name', 'category__name', 'author__username').order_by('-modified_time')[:10] rows = [] for post in post_list: row = {} row['id'] = post.get('id') row['category'] = post.get('category__name') row['title'] = post.get('title') row['excerpt'] = post.get('excerpt') row['views'] = post.get('views') row['author'] = post.get('author__username') row['modified_time'] = datetime_to_string(post.get('modified_time')) row['tags'] = post.get('tags__name', []) rows.append(row) return render_response(request, ErrorCode.OK, rows)
def notify_weblaunches(api_token): beer_bot = BeerBot(os.environ['BEER_USERNAME'], os.environ['BEER_PASSWORD']) telegram_bot = telegram.Bot(api_token) start = beer_bot.get_weblaunch_start() if not start: return subscriptions = model._load_weblaunch_subscriptions() for chat_id, latest_weblaunch_date in subscriptions.iteritems(): if start == string_to_datetime(latest_weblaunch_date): continue send_weblaunch_notification(start, chat_id, telegram_bot) subscriptions[chat_id] = datetime_to_string(start) model.save_weblaunch_subscriptions(subscriptions)
async def get_items(self): args = { "limit": 10, "skip": self.page * 10, "sort": [("timestamp", pymongo.DESCENDING)], "filter": { "guilds": self.ctx.guild_id, } } logs = self.ctx.bot.db.audit_logs.find(**args) items = [] async for audit_log in logs: type = AuditLogType(audit_log["type"]) items.append((utils.datetime_to_string( audit_log["timestamp"] ), f"__{type.name.replace('_', ' ')}__: {text_formats[type].format(**audit_log, **audit_log['extra'])}" )) return items
async def get_messages(self, conv_name, batch_size=2500): """ gets all messages from the conversation and returns in a list of strings formatted name :: date :: message :: is used because it is very uncommon in messages """ # cant load unless connected await self.connected.wait() # setup conv = self.get_conv(conv_name) events = await conv.get_events() event = events[-1] messages = [] last_timestamp = event.timestamp batch_count = 1 # loops through untill the timestamp is greater # since conv.get_events loops when it runs out for some reason # this will result in an infinite loop if batch_size > total messages in conv while True: print(f"getting messages from {conv_name}, batch {batch_count}") events = await conv.get_events(event.id_, max_events=batch_size) event = events[0] if event.timestamp > last_timestamp: break # this is probably really ineffecient # but it was the best i could come up with messages = [ " ::".join( (conv.get_user(event.user_id).first_name, utils.datetime_to_string(event.timestamp), event.text)) for event in events if (isinstance(event, hangups.ChatMessageEvent)) ] + messages last_timestamp = event.timestamp batch_count += 1 return messages
def test_raise_datetime_to_string(self): self.assertRaises(TypeError, datetime_to_string(self.now), type(self.now) != datetime.datetime, self.string_now)
def test_datetime_to_string(self): self.assertEqual(self.string_now, datetime_to_string(self.now))