diff options
author | Sam Wenham <sam@wenhams.co.uk> | 2020-02-24 15:04:07 +0000 |
---|---|---|
committer | Sam Wenham <sam@wenhams.co.uk> | 2020-02-24 15:04:07 +0000 |
commit | 43dfe946b6385037141b94b775112037fe5f032f (patch) | |
tree | 72c352e67166f0702ab93d7063c902ff1bd2c621 /utils.py | |
parent | 656ddcfe93061f2f82160ff8cc45b98d27e28bd3 (diff) | |
download | troggle-43dfe946b6385037141b94b775112037fe5f032f.tar.gz troggle-43dfe946b6385037141b94b775112037fe5f032f.tar.bz2 troggle-43dfe946b6385037141b94b775112037fe5f032f.zip |
Just removing dud whitespacedjango-1.10
Diffstat (limited to 'utils.py')
-rw-r--r-- | utils.py | 20 |
1 files changed, 10 insertions, 10 deletions
@@ -23,12 +23,12 @@ def randomLogbookSentence(): #Choose again if there are no sentances (this happens if it is a placeholder entry) while len(re.findall('[A-Z].*?\.',randSent['entry'].text))==0: randSent['entry']=LogbookEntry.objects.order_by('?')[0] - + #Choose a random sentence from that entry. Store the sentence as randSent['sentence'], and the number of that sentence in the entry as randSent['number'] sentenceList=re.findall('[A-Z].*?\.',randSent['entry'].text) randSent['number']=random.randrange(0,len(sentenceList)) randSent['sentence']=sentenceList[randSent['number']] - + return randSent @@ -37,10 +37,10 @@ def save_carefully(objectType, lookupAttribs={}, nonLookupAttribs={}): -if instance does not exist in DB: add instance to DB, return (new instance, True) -if instance exists in DB and was modified using Troggle: do nothing, return (existing instance, False) -if instance exists in DB and was not modified using Troggle: overwrite instance, return (instance, False) - + The checking is accomplished using Django's get_or_create and the new_since_parsing boolean field defined in core.models.TroggleModel. - + """ instance, created=objectType.objects.get_or_create(defaults=nonLookupAttribs, **lookupAttribs) @@ -49,17 +49,17 @@ def save_carefully(objectType, lookupAttribs={}, nonLookupAttribs={}): for k, v in list(nonLookupAttribs.items()): #overwrite the existing attributes from the logbook text (except date and title) setattr(instance, k, v) instance.save() - + if created: logging.info(str(instance) + ' was just added to the database for the first time. \n') - + if not created and instance.new_since_parsing: logging.info(str(instance) + " has been modified using Troggle, so the current script left it as is. \n") if not created and not instance.new_since_parsing: logging.info(str(instance) + " existed in the database unchanged since last parse. It was overwritten by the current script. \n") return (instance, created) - + re_body = re.compile(r"\<body[^>]*\>(.*)\</body\>", re.DOTALL) re_title = re.compile(r"\<title[^>]*\>(.*)\</title\>", re.DOTALL) @@ -80,7 +80,7 @@ def get_single_match(regex, text): def href_to_wikilinks(matchobj): """ Given an html link, checks for possible valid wikilinks. - + Returns the first valid wikilink. Valid means the target object actually exists. """ @@ -91,7 +91,7 @@ def href_to_wikilinks(matchobj): return matchobj.group() #except: #print 'fail' - + re_subs = [(re.compile(r"\<b[^>]*\>(.*?)\</b\>", re.DOTALL), r"'''\1'''"), (re.compile(r"\<i\>(.*?)\</i\>", re.DOTALL), r"''\1''"), @@ -107,7 +107,7 @@ re_subs = [(re.compile(r"\<b[^>]*\>(.*?)\</b\>", re.DOTALL), r"'''\1'''"), (re.compile(r"\<a\s+href=['\"]#([^'\"]*)['\"]\s*\>(.*?)\</a\>", re.DOTALL), r"[[cavedescription:\1|\2]]"), #assumes that all links with target ids are cave descriptions. Not great. (re.compile(r"\[\<a\s+href=['\"][^'\"]*['\"]\s+id=['\"][^'\"]*['\"]\s*\>([^\s]*).*?\</a\>\]", re.DOTALL), r"[[qm:\1]]"), (re.compile(r'<a\shref="?(?P<target>.*)"?>(?P<text>.*)</a>'),href_to_wikilinks), - + ] def html_to_wiki(text, codec = "utf-8"): |