def replyTemplate(user, message): #Given an user and message, returns it as a reply. template = Template(sessions.current_session.config['templates']['reply']) mapping = {} mapping['message'] = message mapping['user'] = user return template.Substitute(mapping)
def format_user_info(self, index=None): user_ptr = self.get_user_info(index) if not user_ptr: return template = Template(self.session.config['templates']['user_info']) mapping = {} mapping['name'] = user_ptr['name'] mapping['screen_name'] = user_ptr['screen_name'] mapping['location'] = user_ptr['location'] mapping['url'] = user_ptr['url'] mapping['bio'] = user_ptr['description'] mapping['followers_count'] = user_ptr['followers_count'] mapping['friends_count'] = user_ptr['friends_count'] mapping['tweets_count'] = user_ptr['statuses_count'] mapping['when'] = self.relative_time( self.standardize_timestamp(user_ptr['created_at'])) mapping['time'] = self.actual_time( self.standardize_timestamp(user_ptr['created_at'])) mapping['date'] = self.actual_date( self.standardize_timestamp(user_ptr['created_at'])) if user_ptr['utc_offset'] or user_ptr['utc_offset'] == 0: mapping['local_time'] = self.user_local_time( user_ptr['utc_offset']) mapping['local_date'] = self.user_local_date( user_ptr['utc_offset']) else: mapping['local_time'] = "not available" mapping['local_date'] = "not available" return template.Substitute(mapping)
class BaseExporter(object): def __init__(self, buffer, filename, item_template=None, first=None, last=None, range_type=RANGE_ALL, **kwargs): self.buffer = buffer self.first = first self.last = last self.filename = filename if item_template is not None: self.item_template = Template(item_template) else: self.item_template = None self.range_type = range_type if self.range_type not in (RANGE_ALL, RANGE_ALL_REVERSED, RANGE_CUSTOM): raise ValueError( "'range_type' is not a valid range type identifier.") if self.range_type == RANGE_CUSTOM and (self.first is None or self.last is None): raise ValueError( "'first' and 'last' are required for a custom range.") def __enter__(self): self.buffer.session.storage_lock.acquire() try: if self.range_type == RANGE_ALL: self.first = 0 self.last = len(self.buffer) - 1 self.reversed = False elif self.range_type == RANGE_ALL_REVERSED: self.first = len(self.buffer) - 1 self.last = 0 self.reversed = True elif self.range_type == RANGE_CUSTOM: if self.first < 0: self.first = 0 elif self.first >= len(self.buffer): self.first = len(self.buffer) - 1 if self.last >= len(self.buffer): self.last = len(self.buffer) - 1 elif self.last < 0: self.last = 0 self.reversed = self.last < self.first self.export_total = abs(self.last - self.first) + 1 except: self.buffer.session.storage_lock.release() raise return self def __exit__(self, exc_type, exc_value, exc_tb): self.buffer.session.storage_lock.release() return False def GetItems(self): export_index = 1 if self.reversed: item_indices = xrange(self.first, self.last - 1, -1) else: item_indices = xrange(self.first, self.last + 1, 1) for i in item_indices: mapping = self.buffer.get_item_data(i) mapping['total'] = self.export_total mapping['index'] = export_index (yield mapping) export_index += 1 def GetFormattedItems(self): if self.item_template is None: raise NotImplementedError for item in self.GetItems(): (yield self.item_template.Substitute(item)) def Run(self): raise NotImplementedError @classmethod def GetSupportedArgs(self): return ['buffer', 'filename', 'item_template', 'range'] @classmethod def GetFileExtension(self): raise NotImplementedError @classmethod def GetName(self): raise NotImplementedError
def format_item(self, index=None, item=None, template="spoken"): template = Template(self.get_template(template)) mapping = self.get_item_data(index=index, item=item) return template.Substitute(mapping)