def _lock(self): "Lock the file" self._waitUnlock() loggero().debug("Create lock file... %s" % id(self)) fd = open(self._lock_filename, 'w') fd.close() self.permission_lockfile = True
def getContent(self): result = '' html_f = os.path.join(config.htmls_d, str(self.doc_id) + ".html") try: temp_context = self.fileContentWithUnicode(html_f) except Exception as err: loggero().error(err) return self._showPageNotFoundError() comments = self.getComments() if comments: comments_info_l = self.getComments() else: comments_info_l = None #TODO: I want to allow <font>, <b> <i> tag for escaping temp = self.jinja_env.get_template('jinja_door.html') if comments_info_l: result = temp.render(content=temp_context, doc_id=self.doc_id, comments=comments_info_l, current_user=current_user) else: # There is no comment result = temp.render(content=temp_context, doc_id=self.doc_id, current_user=current_user) # Jinja requires unicode strings result = result.encode(config.char_set) return result
def writeHtml(self, doc_id, content): "Set the content of article. The file is html." try: #Fixme: I couldn't convert content to unicode. fd = File(str(doc_id), 'a', 'wb') fd.write(content) fd.close() except Exception as err: msg = "We couldn't write %s" % doc_id loggero().error('Article.write %s' % msg) loggero().error(repr(err))
def fileContentWithUnicode(self, filename, char_set=config.char_set): "Jinja requires unicode content. So have to read the file as unicode." loggero().info('-=======> ') fd = open(filename, 'br') content = fd.read() fd.close() # Jinja require unicode loggero().info('-=======> ' + repr(type(content))) try: return content.decode(char_set) except: return content
def writeComment(doc_id, _base64_content, _base64_name='', password=''): articles = Articles() if not articles.is_article(doc_id): loggero().info('writeComment 500: %s' % doc_id) abort(500) try: comment = Comment() comment.date = time.strftime("%m/%d|%y", time.localtime()) comment.name = _base64_name # TODO: How to prevent password?? comment.password = password comment.content = _base64_content comments = Comments(doc_id) comments.updateFromObj(comment) comments.save() return u'ok' except: abort(500)
def _getInfoTableFromFileAsDict(self, doc_id): doc_id = str(doc_id) result = {} key_and_value_regexp = "^#([^ ]+) (.*)" # Our tags start point-min to \n\n. We get the content of tags. # Then we will separate the tag and value. content = self._getInfoTableFromFileAsString(doc_id) content_l = content.split('\n') regexobj = re.compile(key_and_value_regexp) for el in content_l: try: matchobj = regexobj.match(el) key = matchobj.group(1) value = matchobj.group(2) except AttributeError as err: # There is no key and value in article msg = repr(err) + "\nWe couldn't fine key and value in the document. doc_id is " + doc_id loggero().warning(msg) continue result[key] = value return result
def _init(self): loggero().debug("Init file... %s" % id(self)) try: self._waitUnlock() except IOError as err: # There is unlock file. We wait self.lock_wait_time X # self.lock_wait_interval seconds. The object assumes that # there are no process which locking the file for the seconds. # We forcely restore the file. # TODO: Log that. Think more about vulnerability. # Fixme: If serveral process continuosly lock the file, it # will over the seconds. It will be critical problem. self.restore() self._unlockForce() self._removeBackupForce() if self.mode == 'r': try: self.fd = open(self.filename, self.mode) return self.fd except FileNotFoundError as err: loggero().debug("File not found...") self._unlockForce() loggero().debug("Unlock...") self._removeBackupForce() loggero().debug("Remove backup...") raise FileNotFoundError(err) elif self._checkFile(): self._lock() self._backup() else: self._lock() self.permission_backupfile = True try: fd = open(self.filename, self.mode) except IOError: dirname = self.filename[:self.filename.rfind('/')] os.makedirs(dirname) fd = open(self.filename, self.mode) return fd