Flutter Engine
The Flutter Engine
Classes | Functions | Variables
main Namespace Reference

Classes

class  AllFeedsCollector
 
class  Article
 
class  DataHandler
 
class  Feed
 
class  FeedCollector
 
class  HtmlFile
 
class  MainHandler
 
class  OAuthHandler
 
class  Section
 
class  SetDefaultFeeds
 
class  SetTestFeeds
 
class  TopHandler
 
class  UpdateHtml
 
class  UploadFeed
 
class  UserData
 
class  UserLoginHandler
 

Functions

def findSectionByTitle (title)
 
def collectFeed (feed, data, continuation=None)
 
def collectArticle (feed, data)
 
def unescape (html)
 
def getFeedIcon (url)
 
def findImage (text)
 
def findImgTag (text, extensions)
 
def findVideoTag (text)
 
def makeThumbnail (text)
 
def generateThumbnail (url)
 
def main ()
 
def Main (root_directory)
 
def GetAllFilepaths (root_directory)
 
def ReportWarning (text)
 
def ReportError (text)
 
def ReadFileIntoString (filepath)
 
def WriteStringToFile (string, filepath)
 
def ConfirmAllowedCopyrightHolder (holder)
 

Variables

tuple THUMB_SIZE = (57, 57)
 
string READER_API = 'http://www.google.com/reader/api/0'
 
int MAX_SECTIONS = 5
 
int MAX_ARTICLES = 20
 
int UPDATE_COUNT = 4
 
int INITIAL_COUNT = 40
 
int SNIPPET_SIZE = 180
 
list ALLOWED_COPYRIGHT_HOLDERS
 

Detailed Description

Copyright 2011 Google Inc.

Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.

Function Documentation

◆ collectArticle()

def main.collectArticle (   feed,
  data 
)
Reads an article from the given JSON object and populates the datastore with
it.

Definition at line 614 of file main.py.

614def collectArticle(feed, data):
615 '''
616 Reads an article from the given JSON object and populates the datastore with
617 it.
618 '''
619 if not 'title' in data:
620 # Skip this articles without titles
621 return True
622
623 articleId = data['id']
624 article = Article.get_or_insert(articleId)
625 # TODO(jimhug): This aborts too early - at lease for one adafruit case.
626 if article.date == data['published']:
627 logging.info('found existing, aborting: %r, %r' %
628 (articleId, article.date))
629 return False
630
631 if data.has_key('content'):
632 content = data['content']['content']
633 elif data.has_key('summary'):
634 content = data['summary']['content']
635 else:
636 content = ''
637 #TODO(jimhug): better summary?
638 article.content = content
639 article.date = data['published']
640 article.title = unescape(data['title'])
641 article.snippet = unescape(strip_tags(content)[:SNIPPET_SIZE])
642
643 article.feed = feed
644
645 # TODO(jimhug): make this canonical so UX can change for this state
646 article.author = data.get('author', 'anonymous')
647
648 article.ensureThumbnail()
649
650 article.srcurl = ''
651 if data.has_key('alternate'):
652 for alt in data['alternate']:
653 if alt.has_key('href'):
654 article.srcurl = alt['href']
655 return True
656
657
def collectArticle(feed, data)
Definition: main.py:614
def unescape(html)
Definition: main.py:658

◆ collectFeed()

def main.collectFeed (   feed,
  data,
  continuation = None 
)
Reads a feed from the given JSON object and populates the given feed object
in the datastore with its data.

Definition at line 585 of file main.py.

585def collectFeed(feed, data, continuation=None):
586 '''
587 Reads a feed from the given JSON object and populates the given feed object
588 in the datastore with its data.
589 '''
590 if continuation is None:
591 if 'alternate' in data:
592 feed.iconUrl = getFeedIcon(data['alternate'][0]['href'])
593 feed.title = data['title']
594 feed.lastUpdated = data['updated']
595
596 articles = data['items']
597 logging.info('%d new articles for %s' % (len(articles), feed.title))
598
599 for articleData in articles:
600 if not collectArticle(feed, articleData):
601 feed.put()
602 return False
603
604 if len(articles) > 0 and data.has_key('continuation'):
605 logging.info('would have looked for more articles')
606 # TODO(jimhug): Enable this continuation check when more robust
607 #self.fetchn(feed, feedId, data['continuation'])
608
609 feed.ensureEncodedFeed(force=True)
610 feed.put()
611 return True
612
613
def getFeedIcon(url)
Definition: main.py:666
def collectFeed(feed, data, continuation=None)
Definition: main.py:585

◆ ConfirmAllowedCopyrightHolder()

def main.ConfirmAllowedCopyrightHolder (   holder)
Returns True if this is one of our allowed copyright holders.

@param holder copyright holder as a string

Definition at line 108 of file main.py.

109 """Returns True if this is one of our allowed copyright holders.
110
111 @param holder copyright holder as a string
112 """
113 return holder in ALLOWED_COPYRIGHT_HOLDERS
114
115
def ConfirmAllowedCopyrightHolder(holder)
Definition: main.py:108

◆ findImage()

def main.findImage (   text)

Definition at line 671 of file main.py.

671def findImage(text):
672 img = findImgTag(text, 'jpg|jpeg|png')
673 if img is not None:
674 return img
675
676 img = findVideoTag(text)
677 if img is not None:
678 return img
679
680 img = findImgTag(text, 'gif')
681 return img
682
683
def findImage(text)
Definition: main.py:671
def findImgTag(text, extensions)
Definition: main.py:684
def findVideoTag(text)
Definition: main.py:691

◆ findImgTag()

def main.findImgTag (   text,
  extensions 
)

Definition at line 684 of file main.py.

684def findImgTag(text, extensions):
685 m = re.search(r'src="(http://\S+\.(%s))(\?.*)?"' % extensions, text)
686 if m is None:
687 return None
688 return m.group(1)
689
690

◆ findSectionByTitle()

def main.findSectionByTitle (   title)

Definition at line 578 of file main.py.

578def findSectionByTitle(title):
579 for section in Section.all():
580 if section.fixedTitle() == title:
581 return section
582 return None
583
584
def findSectionByTitle(title)
Definition: main.py:578

◆ findVideoTag()

def main.findVideoTag (   text)

Definition at line 691 of file main.py.

691def findVideoTag(text):
692 # TODO(jimhug): Add other videos beyond youtube.
693 m = re.search(r'src="http://www.youtube.com/(\S+)/(\S+)[/|"]', text)
694 if m is None:
695 return None
696
697 return 'http://img.youtube.com/vi/%s/0.jpg' % m.group(2)
698
699

◆ generateThumbnail()

def main.generateThumbnail (   url)

Definition at line 712 of file main.py.

712def generateThumbnail(url):
713 logging.info('generating thumbnail: %s' % url)
714 thumbWidth, thumbHeight = THUMB_SIZE
715
716 result = urlfetch.fetch(url)
717 img = images.Image(result.content)
718
719 w, h = img.width, img.height
720
721 aspect = float(w) / h
722 thumbAspect = float(thumbWidth) / thumbHeight
723
724 if aspect > thumbAspect:
725 # Too wide, so crop on the sides.
726 normalizedCrop = (w - h * thumbAspect) / (2.0 * w)
727 img.crop(normalizedCrop, 0., 1. - normalizedCrop, 1.)
728 elif aspect < thumbAspect:
729 # Too tall, so crop out the bottom.
730 normalizedCrop = (h - w / thumbAspect) / h
731 img.crop(0., 0., 1., 1. - normalizedCrop)
732
733 img.resize(thumbWidth, thumbHeight)
734
735 # Chose JPEG encoding because informal experiments showed it generated
736 # the best size to quality ratio for thumbnail images.
737 nimg = img.execute_transforms(output_encoding=images.JPEG)
738 logging.info(' finished thumbnail: %s' % url)
739
740 return nimg
741
742
def generateThumbnail(url)
Definition: main.py:712

◆ GetAllFilepaths()

def main.GetAllFilepaths (   root_directory)
Return a list of all files (absolute path for each one) within a tree.

@param root_directory root directory within which to find all files

Definition at line 68 of file main.py.

68def GetAllFilepaths(root_directory):
69 """Return a list of all files (absolute path for each one) within a tree.
70
71 @param root_directory root directory within which to find all files
72 """
73 path_list = []
74 for dirpath, _, filenames in os.walk(root_directory):
75 for filename in filenames:
76 path_list.append(os.path.abspath(os.path.join(dirpath, filename)))
77 return path_list
78
79
def GetAllFilepaths(root_directory)
Definition: main.py:68

◆ getFeedIcon()

def main.getFeedIcon (   url)

Definition at line 666 of file main.py.

666def getFeedIcon(url):
667 url = urlparse.urlparse(url).netloc
668 return 'http://s2.googleusercontent.com/s2/favicons?domain=%s&alt=feed' % url
669
670

◆ main()

def main.main ( )

Definition at line 758 of file main.py.

758def main():
759 application = webapp.WSGIApplication(
760 [
761 ('/data/(.*)', DataHandler),
762
763 # This is called periodically from cron.yaml.
764 ('/update/allFeeds', AllFeedsCollector),
765 ('/update/feed', FeedCollector),
766 ('/update/user', UserLoginHandler),
767 ('/update/defaultFeeds', SetDefaultFeeds),
768 ('/update/testFeeds', SetTestFeeds),
769 ('/update/html', UpdateHtml),
770 ('/update/upload', UploadFeed),
771 ('/oauth2callback', OAuthHandler),
772 ('/', TopHandler),
773 ('/(.*)', MainHandler),
774 ],
775 debug=True)
776 webapp.util.run_wsgi_app(application)
777
778
def main()
Definition: main.py:758

◆ Main()

def main.Main (   root_directory)
Run everything.

@param root_directory root directory within which to modify all files

Definition at line 33 of file main.py.

33def Main(root_directory):
34 """Run everything.
35
36 @param root_directory root directory within which to modify all files
37 """
38 filepaths = GetAllFilepaths(root_directory)
39 for filepath in filepaths:
40 parser = fileparser.CreateParser(filepath)
41 if not parser:
42 ReportWarning('cannot find a parser for file %s, skipping...' %
43 filepath)
44 continue
45 old_file_contents = ReadFileIntoString(filepath)
46 comment_blocks = parser.FindAllCommentBlocks(old_file_contents)
47 if not comment_blocks:
48 ReportWarning('cannot find any comment blocks in file %s' %
49 filepath)
50 old_copyright_block = parser.FindCopyrightBlock(comment_blocks)
51 if not old_copyright_block:
52 ReportWarning('cannot find copyright block in file %s' % filepath)
53 (year, holder) = parser.GetCopyrightBlockAttributes(old_copyright_block)
54 if holder and not ConfirmAllowedCopyrightHolder(holder):
56 'unrecognized copyright holder "%s" in file %s, skipping...' % (
57 holder, filepath))
58 continue
59 new_copyright_block = parser.CreateCopyrightBlock(year, holder)
60 if old_copyright_block:
61 new_file_contents = old_file_contents.replace(
62 old_copyright_block, new_copyright_block, 1)
63 else:
64 new_file_contents = new_copyright_block + old_file_contents
65 WriteStringToFile(new_file_contents, filepath)
66
67
static bool WriteStringToFile(const fml::UniqueFD &fd, const std::string &contents)
def CreateParser(filepath)
Definition: fileparser.py:11
def ReportWarning(text)
Definition: main.py:80
def Main(root_directory)
Definition: main.py:33
def ReadFileIntoString(filepath)
Definition: main.py:92

◆ makeThumbnail()

def main.makeThumbnail (   text)

Definition at line 700 of file main.py.

700def makeThumbnail(text):
701 url = None
702 try:
703 url = findImage(text)
704 if url is None:
705 return None
706 return generateThumbnail(url)
707 except:
708 logging.info('error decoding: %s' % (url or text))
709 return None
710
711
def makeThumbnail(text)
Definition: main.py:700

◆ ReadFileIntoString()

def main.ReadFileIntoString (   filepath)
Returns the full contents of this file as a string.

Definition at line 92 of file main.py.

92def ReadFileIntoString(filepath):
93 """Returns the full contents of this file as a string.
94 """
95 with open(filepath, 'r') as file_handle:
96 contents = file_handle.read()
97 return contents
98
99

◆ ReportError()

def main.ReportError (   text)
Report an error and raise an exception.

Definition at line 86 of file main.py.

86def ReportError(text):
87 """Report an error and raise an exception.
88 """
89 raise IOError(text)
90
91
def ReportError(text)
Definition: main.py:86

◆ ReportWarning()

def main.ReportWarning (   text)
Report a warning, but continue.

Definition at line 80 of file main.py.

80def ReportWarning(text):
81 """Report a warning, but continue.
82 """
83 print('warning: %s' % text)
84
85
def print(*args, **kwargs)
Definition: run_tests.py:49

◆ unescape()

def main.unescape (   html)

Definition at line 658 of file main.py.

658def unescape(html):
659 "Inverse of Django's utils.html.escape function"
660 if not isinstance(html, basestring):
661 html = str(html)
662 html = html.replace('&#39;', "'").replace('&quot;', '"')
663 return html.replace('&gt;', '>').replace('&lt;', '<').replace('&amp;', '&')
664
665

◆ WriteStringToFile()

def main.WriteStringToFile (   string,
  filepath 
)
Writes this string out to filepath, replacing the file if it already
exists.

Definition at line 100 of file main.py.

100def WriteStringToFile(string, filepath):
101 """Writes this string out to filepath, replacing the file if it already
102 exists.
103 """
104 with open(filepath, 'w') as file_handle:
105 file_handle.write(string)
106
107

Variable Documentation

◆ ALLOWED_COPYRIGHT_HOLDERS

list main.ALLOWED_COPYRIGHT_HOLDERS
Initial value:
1= [
2 'Google Inc.',
3 'Skia',
4 'The Android Open Source Project',
5]

Definition at line 27 of file main.py.

◆ INITIAL_COUNT

int main.INITIAL_COUNT = 40

Definition at line 538 of file main.py.

◆ MAX_ARTICLES

int main.MAX_ARTICLES = 20

Definition at line 28 of file main.py.

◆ MAX_SECTIONS

int main.MAX_SECTIONS = 5

Definition at line 27 of file main.py.

◆ READER_API

string main.READER_API = 'http://www.google.com/reader/api/0'

Definition at line 25 of file main.py.

◆ SNIPPET_SIZE

int main.SNIPPET_SIZE = 180

Definition at line 539 of file main.py.

◆ THUMB_SIZE

tuple main.THUMB_SIZE = (57, 57)

Definition at line 24 of file main.py.

◆ UPDATE_COUNT

int main.UPDATE_COUNT = 4

Definition at line 537 of file main.py.