Ejemplo n.º 1
0
def removeDir(dirName):
  d = QDir(dirName)
  if d.exists():
    for info in d.entryInfoList(QDir.Dirs | QDir.Files | QDir.NoDotAndDotDot):
      if info.isDir():
        removeDir(info.absoluteFilePath())
      else:
        d.remove(info.fileName())
    d.rmdir(dirName)
Ejemplo n.º 2
0
def removeDir(dirName):
    d = QDir(dirName)
    if d.exists():
        for info in d.entryInfoList(QDir.Dirs | QDir.Files | QDir.NoDotAndDotDot):
            if info.isDir():
                removeDir(info.absoluteFilePath())
            else:
                d.remove(info.fileName())
        d.rmdir(dirName)
Ejemplo n.º 3
0
 def clearTempFiles(self):
     """
     Clear the directories created for each recording added to the queue
     and the files they contain.
     """
     settingsQDir = QDir(self.settingsDir)
     settingsQDir.setFilter(QDir.Dirs | QDir.NoDotAndDotDot)
     for dir in settingsQDir.entryList():
         tempQDir = QDir(self.settingsDir + '/' + dir)
         tempQDir.setFilter(QDir.Files)
         for tempFile in tempQDir.entryList():
             tempQDir.remove(tempFile)
         settingsQDir.rmdir(self.settingsDir + '/' + dir)
Ejemplo n.º 4
0
def remove_symbol(symbolTopPath, name):
    """ This function removes the symbol named name from the
    database located at symbolPath.

    NOTE: I purposefully don't use rmtree here to avoid that users
    accidentally or maliciously delete other data.
    """

    symbolPath = symbolTopPath + "/" + name
    symbolPathDir = QDir(symbolPath)
    if not symbolPathDir.exists():
        return False

    descriptionFile = symbolPath + "/description"
    svgFile         = symbolPath + "/" + name + ".svg"

    if (symbolPathDir.remove(descriptionFile) 
        and symbolPathDir.remove(svgFile) 
        and symbolPathDir.rmdir(symbolPath)):
        return True

    return False
Ejemplo n.º 5
0
    def __init__(self, notebooks):

        # Index directory of whoosh, located in notebookPath.
        self.schema = fields.Schema(
            path = fields.TEXT(stored=True),
            title = fields.TEXT(stored=True),
            content = fields.TEXT(stored=True),
            tags = fields.KEYWORD(commas=True))

        self.notebookName = notebooks[0][0]
        self.notebookPath = notebooks[0][1]
        self.notePath = os.path.join(self.notebookPath, "notes").replace(os.sep, '/')
        self.htmlPath = os.path.join(self.notebookPath, "html", "notes").replace(os.sep, '/')
        self.indexdir = os.path.join(self.notePath, ".indexdir").replace(os.sep, '/')
        self.attachmentPath = os.path.join(self.notebookPath, "attachments").replace(os.sep, '/')
        self.configfile = os.path.join(self.notebookPath, "notebook.conf").replace(os.sep, '/')
        cssPath = os.path.join(self.notebookPath, "css").replace(os.sep, '/')
        self.cssfile = os.path.join(cssPath, "notebook.css").replace(os.sep, '/')
        self.searchcssfile = os.path.join(cssPath, "search-window.css").replace(os.sep, '/')
        self.qsettings = QSettings(self.configfile, QSettings.IniFormat)

        if os.path.exists(self.configfile):
            self.extensions = readListFromSettings(self.qsettings,
                                                   "extensions")
            self.fileExt = self.qsettings.value("fileExt")
            self.attachmentImage = self.qsettings.value("attachmentImage")
            self.attachmentDocument = self.qsettings.value("attachmentDocument")
            self.version = self.qsettings.value("version")
            self.geometry = self.qsettings.value("geometry")
            self.windowstate = self.qsettings.value("windowstate")
            self.mathjax = self.qsettings.value('mathJax')
            if 'extensionsConfig' not in set(self.qsettings.childGroups()):
                self.extcfg = self.qsettings.value('extensionsConfig',  defaultValue={})
                writeDictToSettings(self.qsettings, 'extensionsConfig', self.extcfg)
            else:
                self.extcfg = readDictFromSettings(self.qsettings, 'extensionsConfig')
        else:
            self.extensions = []
            self.fileExt = ""
            self.attachmentImage = []
            self.attachmentDocument = []
            self.version = None
            self.geometry = None
            self.windowstate = None
            self.mathjax = ''
            self.extcfg = {}

        self.faulty_exts=[]

        # Default enabled python-markdown extensions.
        # http://pythonhosted.org/Markdown/extensions/index.html
        if not self.extensions:
            self.extensions = [
                   'nl2br'           # newline to break
                 , 'strkundr'        # bold-italics-underline-delete style
                 , 'codehilite'      # code syntax highlight
                 , 'fenced_code'     # code block
                 , 'headerid'        # add id to headers
                 , 'headerlink'      # add anchor to headers
                 , 'footnotes'
                 , 'asciimathml'
                 ]
            writeListToSettings(self.qsettings, "extensions", self.extensions)

        while True:
             print(self.extensions)
             try:
                 markdown.markdown("",extensions=self.extensions)
             except AttributeError as e:
                 remove_this = NOT_EXT.findall(e.args[0])[0]
                 if remove_this in self.extensions:
                     print("Found invalid markdown extension",remove_this,". Please consider removing it.")
                     print('If you want to permanently disable this, just hit OK in the Notebook Settings dialog')
                     self.extensions.remove(remove_this)
                     self.faulty_exts.append(remove_this)
             except ImportError as e:
                 if e.name.startswith('mdx_') and e.name[4:] in self.extensions:
                     print('Found missing markdown extension', e.name[4:], ', temporarily disabling.')
                     print('If you want to permanently disable this, just hit OK in the Notebook Settings dialog')
                     self.extensions.remove(e.name[4:])
                     self.faulty_exts.append(e.name[4:])
                 elif e.name in self.extensions:
                     print('Found missing markdown extension', e.name, ', temporarily disabling.')
                     print('If you want to permanently disable this, just hit OK in the Notebook Settings dialog')
                     self.extensions.remove(e.name)
                     self.faulty_exts.append(e.name)
             else:
                 self.md = markdown.Markdown(self.extensions, extension_configs=self.extcfg)
                 break

        # Default file extension name
        if not self.fileExt:
            self.fileExt = ".md"
            self.qsettings.setValue("fileExt", self.fileExt)

        # Image file types that will be copied to attachmentDir
        # Inserted as image link
        if not self.attachmentImage:
            self.attachmentImage = [".jpg", ".jpeg", ".png", ".gif", ".svg"]
            self.qsettings.setValue("attachmentImage", self.attachmentImage)

        # Document file types that will be copied to attachmentDir
        # Inserted as link
        if not self.attachmentDocument:
            self.attachmentDocument = [".pdf", ".doc", ".odt"]
            self.qsettings.setValue("attachmentDocument", self.attachmentDocument)

        # Migrate notebookPath to v0.3.0 folder structure
        if not self.version:
            notebookDir = QDir(self.notebookPath)

            # move all markdown files to notes/
            dirList = notebookDir.entryList(QDir.Dirs | QDir.NoDotAndDotDot)
            if 'css' in dirList:
                dirList.remove('css')
            fileList = notebookDir.entryList(['*.md', '*.mkd', '*.markdown'])
            notebookDir.mkdir('notes')
            for d in dirList + fileList:
                notebookDir.rename(d, os.path.join('notes', d).replace(os.sep, '/'))

            # remove .indexdir folder
            oldIndexDir = QDir(os.path.join(self.notebookPath, '.indexdir'.replace(os.sep, '/')))
            indexFileList = oldIndexDir.entryList()
            for f in indexFileList:
                oldIndexDir.remove(f)
            notebookDir.rmdir('.indexdir')

            # rename notes.css to css/notebook.css
            oldCssFile = os.path.join(self.notebookPath, 'notes.css').replace(os.sep, '/')
            QDir().mkpath(cssPath)
            if os.path.exists(oldCssFile):
                QFile.rename(oldCssFile, self.cssfile)

            self.version = '0'

        if not self.mathjax:
            self.mathjax = 'http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'
            self.qsettings.setValue('mathJax', self.mathjax)
Ejemplo n.º 6
0
 def tearDown(self):
     outputDir = QDir(self.testPath + '/output')
     outputDir.setNameFilters(['*.m4a'])
     for outputFile in outputDir.entryList():
         outputDir.remove(outputFile)            
Ejemplo n.º 7
0
def create_new_symbol(symbolPath, data): 
    """ This function creates a new knitting symbol as specified by
    the user. 
    """

    # make sure the user's symbol directory exists. If not we
    # create it
    symbolTopDir = QDir(symbolPath)
    if not symbolTopDir.exists():
        if not symbolTopDir.mkdir(symbolPath):
            QMessageBox.critical(None, msg.failedToCreateDirectoryTitle,
                                 msg.failedToCreateDirectoryText % 
                                 symbolPath,
                                 QMessageBox.Close)
            logger.error(msg.failedToCreateDirectoryText % symbolPath)
            return False

    # this should never happen since the manageKnittingSymbolDialog is
    # supposed to check. We'll check anyways.
    symbolDirPath = symbolPath + "/" + data["svgName"]
    symbolDir = QDir(symbolDirPath)
    if symbolDir.exists():
        QMessageBox.critical(None, msg.symbolExistsTitle,
                             msg.symbolExistsText % (data["category"], 
                                                     data["name"]),
                             QMessageBox.Close)
        logger.error(msg.symbolExistsText % (data["category"], data["name"]))
        return False

    symbolDir.mkdir(symbolDirPath)

    # the following try/except suite attempts to return things back
    # to the initial state if writing fails for some reason
    descriptionFilePath = None
    symbolTargetFilePath  = None

    try:
        
        # copy the svg file
        symbolTargetSvgPath = symbolDirPath + "/" + data["svgName"] + ".svg"
        if not QFile(data["svgPath"]).copy(symbolTargetSvgPath):
            QMessageBox.critical(None, msg.failedToCopySvgTitle,
                                msg.failedToCopySvgText % 
                                symbolTargetSvgPath,
                                QMessageBox.Close)
            logger.error(msg.failedToCopySvgText % symbolTargetSvgPath) 
            raise IOError

        # write the description file
        descriptionFilePath = symbolDirPath + "/" + "description"
        descriptionFileHandle = QFile(descriptionFilePath)
        if not descriptionFileHandle.open(QIODevice.WriteOnly):
            QMessageBox.critical(None, msg.failedCreateDescriptionFileTitle,
                                msg.failedCreateDescriptionFileText % 
                                (data["name"], data["category"]), 
                                QMessageBox.Close)
            logger.error(msg.failedCreateDescriptionFileText % 
                         (data["name"], data["category"]))
            raise IOError

        # finally try to write the content of the file
        try:
            write_description_content(descriptionFileHandle, data) 
        except:
            raise IOError

    # this does some cleanup in case writing fails for some reason
    except IOError:
        if descriptionFilePath:
            symbolDir.remove(descriptionFilePath)

        if symbolTargetSvgPath:
            symbolDir.remove(symbolTargetSvgPath)

        symbolDir.rmdir(symbolDirPath)
        return False

    return True
Ejemplo n.º 8
0
    def __init__(self, notebooks):

        # Index directory of whoosh, located in notebookPath.
        self.schema = fields.Schema(
            path = fields.TEXT(stored=True),
            title = fields.TEXT(stored=True),
            content = fields.TEXT(stored=True))

        self.notebookName = notebooks[0][0]
        self.notebookPath = notebooks[0][1]
        self.notePath = os.path.join(self.notebookPath, "notes")
        self.htmlPath = os.path.join(self.notebookPath, "html", "notes")
        self.indexdir = os.path.join(self.notePath, ".indexdir")
        self.attachmentPath = os.path.join(self.notebookPath, "attachments")
        self.configfile = os.path.join(self.notebookPath, "notebook.conf")
        cssPath = os.path.join(self.notebookPath, "css")
        self.cssfile = os.path.join(cssPath, "notebook.css")
        self.qsettings = QSettings(self.configfile, QSettings.NativeFormat)

        if os.path.exists(self.configfile):
            self.extensions = readListFromSettings(self.qsettings,
                                                   "extensions")
            self.fileExt = self.qsettings.value("fileExt")
            self.attachmentImage = self.qsettings.value("attachmentImage")
            self.attachmentDocument = self.qsettings.value("attachmentDocument")
            self.version = self.qsettings.value("version")
            self.geometry = self.qsettings.value("geometry")
            self.windowstate = self.qsettings.value("windowstate")
        else:
            self.extensions = []
            self.fileExt = ""
            self.attachmentImage = []
            self.attachmentDocument = []
            self.version = None
            self.geometry = None
            self.windowstate = None

        # Default enabled python-markdown extensions.
        # http://pythonhosted.org/Markdown/extensions/index.html
        if not self.extensions:
            self.extensions = [
                   'nl2br'           # newline to break
                 , 'strkundr'        # bold-italics-underline-delete style
                 , 'codehilite'      # code syntax highlight
                 , 'fenced_code'     # code block
                 , 'headerid'        # add id to headers
                 , 'headerlink'      # add anchor to headers
                 , 'footnotes'
                 ]
            writeListToSettings(self.qsettings, "extensions", self.extensions)

        # Default file extension name
        if not self.fileExt:
            self.fileExt = ".md"
            self.qsettings.setValue("fileExt", self.fileExt)

        # Image file types that will be copied to attachmentDir
        # Inserted as image link
        if not self.attachmentImage:
            self.attachmentImage = [".jpg", ".jpeg", ".png", ".gif", ".svg"]
            self.qsettings.setValue("attachmentImage", self.attachmentImage)

        # Document file types that will be copied to attachmentDir
        # Inserted as link
        if not self.attachmentDocument:
            self.attachmentDocument = [".pdf", ".doc", ".odt"]
            self.qsettings.setValue("attachmentDocument", self.attachmentDocument)

        # Migrate notebookPath to v0.3.0 folder structure
        if not self.version:
            notebookDir = QDir(self.notebookPath)

            # move all markdown files to notes/
            dirList = notebookDir.entryList(QDir.Dirs | QDir.NoDotAndDotDot)
            if 'css' in dirList:
                dirList.remove('css')
            fileList = notebookDir.entryList(['*.md', '*.mkd', '*.markdown'])
            notebookDir.mkdir('notes')
            for d in dirList + fileList:
                notebookDir.rename(d, os.path.join('notes', d))

            # remove .indexdir folder
            oldIndexDir = QDir(os.path.join(self.notebookPath, '.indexdir'))
            indexFileList = oldIndexDir.entryList()
            for f in indexFileList:
                oldIndexDir.remove(f)
            notebookDir.rmdir('.indexdir')

            # rename notes.css to css/notebook.css
            oldCssFile = os.path.join(self.notebookPath, 'notes.css')
            QDir().mkpath(cssPath)
            if os.path.exists(oldCssFile):
                QFile.rename(oldCssFile, self.cssfile)

            self.version = '0'