Add (partial) CLI options, fixes to site editconfig.

This commit is contained in:
Jim Miller 2011-05-07 12:51:03 -05:00
parent d91f2e74fd
commit 87a57cc286
7 changed files with 111 additions and 68 deletions

View file

@ -79,7 +79,7 @@ include_subject_tags: extratags, genre, category, lastupdate, status
#include_tocpage: false
# epub->mobi conversions typically don't like tables.
titlepage_use_table: true
titlepage_use_table: false
## When using tables, make these span both columns.
wide_titlepage_entries: description, storyUrl, author URL

File diff suppressed because one or more lines are too long

View file

@ -27,6 +27,8 @@ class BaseSiteAdapter(Configurable):
def __init__(self, config, url):
Configurable.__init__(self, config)
self.addConfigSection(self.getSiteDomain())
self.addConfigSection("commandline")
self.opener = u2.build_opener(u2.HTTPCookieProcessor())
self.storyDone = False
self.metadataDone = False

View file

@ -24,6 +24,8 @@ class BaseStoryWriter(Configurable):
def __init__(self, config, story):
Configurable.__init__(self, config)
self.addConfigSection(self.getFormatName())
## Pass adapter instead, to check date before fetching all?
## Or add 'check update' method to writer?
self.story = story
self.titleLabels = {
'category':'Category',
@ -127,6 +129,8 @@ class BaseStoryWriter(Configurable):
def writeStory(self,outstream=None):
self.addConfigSection(self.story.getMetadata('site'))
self.addConfigSection(self.story.getMetadata('site')+":"+self.getFormatName())
self.addConfigSection("commandline")
for tag in self.getConfigList("extratags"):
self.story.addToList("extratags",tag)
@ -148,6 +152,8 @@ class BaseStoryWriter(Configurable):
path+=dir+"/"
if not os.path.exists(path):
os.mkdir(path) ## os.makedirs() doesn't work in 2.5.2?
## Check for output file date vs updated date here?
outstream = open(outfilename,"wb")
else:
close=False

View file

@ -57,7 +57,7 @@ h6 { text-align: center; }
<link href="stylesheet.css" type="text/css" charset="UTF-8" rel="stylesheet"/>
</head>
<body>
<h1><a href="${storyUrl}">${title}</a> by <a href="${authorUrl}">${author}</a></h1>
<h3><a href="${storyUrl}">${title}</a> by <a href="${authorUrl}">${author}</a></h3>
<div>
''')

50
main.py
View file

@ -108,7 +108,7 @@ class EditConfigServer(webapp.RequestHandler):
if uconfig is None:
uconfig = UserConfig()
uconfig.user = user
uconfig.config = self.request.get('config').encode('utf8')[:1000000] ## just in case.
uconfig.config = self.request.get('config').encode('utf8')[:10000] ## just in case.
uconfig.put()
self.redirect("/?error=configsaved")
else: # not update, assume display for edit
@ -222,8 +222,25 @@ class RecentFilesServer(webapp.RequestHandler):
template_values = dict(fics = fics, nickname = user.nickname())
path = os.path.join(os.path.dirname(__file__), 'recent.html')
self.response.out.write(template.render(path, template_values))
class UserConfigServer(webapp.RequestHandler):
def getUserConfig(self,user):
config = ConfigParser.SafeConfigParser()
## Pull user's config record.
l = UserConfig.all().filter('user =', user).fetch(1)
## TEST THIS
if l and l[0].config:
uconfig=l[0]
logging.debug('reading config from UserConfig(%s)'%uconfig.config)
config.readfp(StringIO.StringIO(uconfig.config))
else:
logging.debug('reading defaults.ini config file')
config.read('defaults.ini')
return config
class FanfictionDownloader(webapp.RequestHandler):
class FanfictionDownloader(UserConfigServer):
def get(self):
self.post()
@ -258,18 +275,7 @@ class FanfictionDownloader(webapp.RequestHandler):
adapter = None
try:
config = ConfigParser.SafeConfigParser()
## Pull user's config record.
l = UserConfig.all().filter('user =', user).fetch(1)
if l:
uconfig=l[0]
logging.debug('reading config from UserConfig')
config.readfp(StringIO.StringIO(uconfig.config))
else:
logging.debug('reading defaults.ini config file')
config.read('defaults.ini')
config = self.getUserConfig(user)
adapter = adapters.getAdapter(config,url)
logging.info('Created an adaper: %s' % adapter)
@ -321,7 +327,7 @@ class FanfictionDownloader(webapp.RequestHandler):
return
class FanfictionDownloaderTask(webapp.RequestHandler):
class FanfictionDownloaderTask(UserConfigServer):
def _printableVersion(self, text):
text = removeEntities(text)
try:
@ -362,19 +368,7 @@ class FanfictionDownloaderTask(webapp.RequestHandler):
logging.info('Creating adapter...')
try:
config = ConfigParser.ConfigParser()
config = ConfigParser.SafeConfigParser()
## Pull user's config record.
l = UserConfig.all().filter('user =', user).fetch(1)
if l:
uconfig=l[0]
logging.debug('reading config from UserConfig')
config.readfp(StringIO.StringIO(uconfig.config))
else:
logging.debug('reading defaults.ini config file')
config.read('defaults.ini')
config = self.getUserConfig(user)
adapter = adapters.getAdapter(config,url)
except Exception, e:
logging.exception(e)

View file

@ -4,45 +4,74 @@ import logging
logging.basicConfig(level=logging.DEBUG,format="%(levelname)s:%(filename)s(%(lineno)d):%(message)s")
import sys, os
from optparse import OptionParser
import getpass
from fanficdownloader import adapters,writers,exceptions
import ConfigParser
config = ConfigParser.SafeConfigParser()
logging.debug('reading defaults.ini config file, if present')
config.read('defaults.ini')
logging.debug('reading personal.ini config file, if present')
config.read('personal.ini')
def writeStory(adapter,writeformat):
def writeStory(config,adapter,writeformat):
writer = writers.getWriter(writeformat,config,adapter.getStory())
writer.writeStory()
del writer
try:
adapter = adapters.getAdapter(config,sys.argv[1])
try:
print adapter.getStory()
except exceptions.FailedToLogin, ftl:
print "Login Failed, Need Username/Password."
sys.stdout.write("Username: ")
adapter.username = sys.stdin.readline().strip()
adapter.password = getpass.getpass(prompt='Password: ')
#print("Login: `%s`, Password: `%s`" % (adapter.username, adapter.password))
print adapter.getStory()
def main():
# read in args, anything starting with -- will be treated as --<varible>=<value>
usage = "usage: %prog [options] storyurl"
parser = OptionParser(usage)
parser.add_option("-f", "--format", dest="format", default='epub',
help="write story as FORMAT, epub(default), text or html", metavar="FORMAT")
parser.add_option("-o", "--option",
action="append", dest="options",
help="set an option NAME=VALUE", metavar="NAME=VALUE")
(options, args) = parser.parse_args()
writeStory(adapter,"epub")
writeStory(adapter,"html")
writeStory(adapter,"txt")
del adapter
if len(args) != 1:
parser.error("incorrect number of arguments")
except exceptions.InvalidStoryURL, isu:
print isu
except exceptions.StoryDoesNotExist, dne:
print dne
except exceptions.UnknownSite, us:
print us
config = ConfigParser.SafeConfigParser()
logging.debug('reading defaults.ini config file, if present')
config.read('defaults.ini')
logging.debug('reading personal.ini config file, if present')
config.read('personal.ini')
config.add_section("commandline")
if options.options:
for opt in options.options:
(var,val) = opt.split('=')
config.set("commandline",var,val)
try:
adapter = adapters.getAdapter(config,args[0])
try:
print adapter.getStoryMetadataOnly()
except exceptions.FailedToLogin, ftl:
print "Login Failed, Need Username/Password."
sys.stdout.write("Username: ")
adapter.username = sys.stdin.readline().strip()
adapter.password = getpass.getpass(prompt='Password: ')
#print("Login: `%s`, Password: `%s`" % (adapter.username, adapter.password))
print adapter.getStoryMetadataOnly()
## XXX Use format.
print "format: %s" % options.format
writeStory(config,adapter,"epub")
writeStory(config,adapter,"html")
writeStory(config,adapter,"txt")
del adapter
except exceptions.InvalidStoryURL, isu:
print isu
except exceptions.StoryDoesNotExist, dne:
print dne
except exceptions.UnknownSite, us:
print us
if __name__ == "__main__":
main()