def load_docs(docs, name=''): """ Load a documentation file from the folder docs. Files will be copied to the build directory relative to docs. Any directories found within the docs directory will be loaded in their own subsection of the navigation and table of contents where the name of the section is the name of the directory. :param docs: The directory to load documentation from :param name: The name of the navigation section """ global nav, table directories = [] if not os.path.exists(docs.replace('source', 'build')): os.mkdir(docs.replace('source', 'build')) for file in os.listdir(docs): path = docs + '/' + file if os.path.isfile(path) and path.endswith('.md'): id = file.replace('.', '_') nav += nav_content.format(id, re.sub(r'(")|(<!--.*-->)', '', fileutil.read(path))) nav += nav_item.format(id, '%/' + name + '/' + file[:-3] + '.html', file[2:-3] .replace('_', ' ') .replace('index', 'Overview')) table += table_item.format('%/' + name + '/' + file[:-3] + '.html', file[2:-3] .replace('_', ' ') .replace('index', 'Overview')) fileutil.write(docs.replace('source', 'build') + '/' + file, fileutil.read(path)) elif os.path.isdir(path) and not file.startswith('.'): directories.append((file, path)) for file, path in directories: nav += nav_sec_start.format(file) table += table_sec_start.format(file) load_docs(path, name=file) nav += nav_sec_end table += table_sec_end
def mix(n, row, default=False): """ Takes an exam version and row of data and prepares a file for the formatter. If weaving is enabled a .mix with the correct name is weaved with the inline code at the start of the file. If weaving is disabled a .tex file is prepared for the formatter directly. :param n: The exam version :param row: The row of data """ logging.info(str(row)) version = str(chr(n + ord('A')) if options.state.alphabetize() else n + 1) if options.state.noweave(): fileutil.write( options.state.cwd() + '/' + config.filename.format(version=version, name=row['name'].replace(' ', '_').strip(), number=row['number']).strip('_') + '.tex', fileutil.read(options.state.template())) else: path = options.state.cwd() + '/' + config.filename.format( version=version, name=row['name'].replace(' ', '_').strip(), number=row['number']).strip('_') + '.tex' fileutil.write( path, '<%\n' + inline.replace('{number}', str(n)).replace( '{version}', str(version)).replace( '{student_first_name}', config.placeholder[options.state.format()] if default else str(row['first'])).replace( '{student_last_name}', config.placeholder[options.state.format()] if default else str(row['last'])).replace( '{student_name}', config.placeholder[options.state.format()] if default else str(row['name'])).replace( '{student_number}', config.placeholder[options.state.format()] if default else str(row['number'])) + '\n%>' + fileutil.read(options.state.template())) lib_loader.weave(path) fileutil.write( path, '<%\n' + inline.replace('{number}', str(n)).replace( '{version}', str(version)).replace( '{student_first_name}', config.placeholder[options.state.format()] if default else str(row['first'])).replace( '{student_last_name}', config.placeholder[options.state.format()] if default else str(row['last'])).replace( '{student_name}', config.placeholder[options.state.format()] if default else str(row['name'])).replace( '{student_number}', config.placeholder[options.state.format()] if default else str(row['number'])) + '\n%>' + fileutil.read(options.state.cwd() + '/' + config.filename.format( version=version, name=row['name'].replace(' ', '_'), number=row['number']).strip('_') + '.tex')) lib_loader.weave(path)
def formattingListboxListener(window, values): if values['language'][0] == "Java": window['content'].update(read('./copyrights/java-template')) if values['language'][0] == "Javascript": window['content'].update(read('./copyrights/js-template')) if values['language'][0] == "Shell": window['content'].update(read('./copyrights/shell-template')) if values['language'][0] == "Xml": window['content'].update(read('./copyrights/xml-template'))
def projectOpened(self): self.start = float(gSettings.value('TimerStartTime', 0.0)) endTime = float(gSettings.value('TimerEndTime', 8.0)) self.time = float(gSettings.value('TimerTime', 0.0)) project = ProjectFile() if project and fileutil.exists(project): with fileutil.read(project) as fh: text = fh.read() try: root = cElementTree.fromstring(text) except: root = None if root is not None: self.minTime = float(root.attrib.get('TimerMinTime', 0.0)) self.maxTime = float(root.attrib.get('TimerMaxTime', 8.0)) self.end = min(endTime, self.maxTime) self.__BPS = float(root.attrib.get('TimerBPS', 2.0)) self.__osc.setBpm(int(round(self.__BPS * 60))) self.bpmChanged.emit(self.__BPS * 60.0) return # legacy project / creating new project self.minTime = 0.0 self.maxTime = 4.0 self.end = min(endTime, self.maxTime) self.__BPS = float(gSettings.value('TimerBPS', 2.0)) self.__osc.setBpm(int(round(self.__BPS * 60))) self.bpmChanged.emit(self.__BPS * 60.0)
def composer_postprocessor(src): """ Performs a number of transformations to the source to create a more comprehensive appearance in html: - Converts LaTeX symbols to html codes - Converys markdown images [img] into `img` tags - Converts markdown links [label] into `a` tags - Converts two consecutive newlines into a `br` tag - Converts three consecutive newlines into two `br` tags Additionally the provided source is loaded into a template and solutions are made visible if the flag is set. :param src: The source to process :return: The processed source """ # String replacements src = re.sub(r'\[img\]\((.*?)\)', r'<img src="\1">', src) src = re.sub(r'\[([^\[\]\n]+)\]\(([^()\n]+)\)', r'<a href="\2">\1</a>', src) src = re.sub(r'\n{3}', '<br /><br />', src) src = re.sub(r'\n{2}', '<br />', src) src = fileutil.read( options.state.htmltemplate() ).replace('<!-- content -->', src) if options.state.solutions(): src = src.replace('display: none', '') else: src = src.replace('class="correctchoice"','class="choice"') return src
def __createIndexHtml(jsonData, dataChengjiao): colors = [ '#EF5350', '#FFA726', '#FFEA00', '#66BB6A', '#29B6F6', '#8D6E63', '#7E57C2', '#FF5252', '#EEFF41', '#283593', '#CE93D8' ] dataAvg = {} datasets = [] labels = [] count = 0 for key in jsonData: dataset = {} data = [] dataset['label'] = key dataset['data'] = data dataset['backgroundColor'] = colors[count] dataset['borderColor'] = colors[count] dataset['fill'] = False for value in jsonData[key]: dataTime = value[0] price = value[1] data.append(str(price)) if count == 0: labels.append(str(format(dataTime))) count += 1 datasets.append(dataset) dataAvg['datasets'] = datasets dataAvg['labels'] = labels htmlContent = read('./frontend/vm/index.vm') htmlContent = htmlContent.replace('${dataAvg}', json.dumps(dataAvg)) htmlContent = htmlContent.replace('${dataChengjiao}', dataChengjiao) write('./frontend/index.html', htmlContent)
def parse(): """ Converts mixed files from source to an intermediate based on the extension of the template file. Once converted the intermediate is run through the default filters and the extension formatter's processors. :return: A list of intermediates corresponding to each of the mixed files """ intermediates, parser = [], get_format(options.state.template()) if not parser['format']: raise FormatError('This format is export only!') # Loop through all weaved files for file in fileutil.with_extension('.tex'): options.post('Using ' + parser['name'] + ' format to parse ' + file) intermediate = util.Map({ 'ast': [], 'src': parser['parser_preprocessor'](fileutil.read(file)), 'fmt': parser, 'name': file.replace('.tex', '') }) intermediate.ast = parse_tokens(intermediate.src, parser) for default_filter in config.default_filters: intermediate.ast = default_filter(intermediate.ast) intermediate = parser['parser_postprocessor'](intermediate) intermediates.append(intermediate) fileutil.write(options.state.cwd() + '/parsed-ast', ''.join(str_token(intermediate.ast))) options.post('Successfully parsed', parser['name'] + '.') return intermediates
def load_source(name, directory, build): """ Parses and processes the docstrings from all python files in the given directory and copies them to the given build directory. Each file is added as an item to the navigation list and table of contents under a section with the given name. :param name: The name of the navigation and table of contents section :param directory: The directory to load documentation from :param build: The build directory """ global nav, table nav += nav_sec_start.format(name) table += table_sec_start.format(name) if not os.path.exists(build + '/' + name): os.mkdir(build + '/' + name) for file in sorted(os.listdir(directory)): path = directory + '/' + file if os.path.isfile(path) and path.endswith('.py') and '__init__' not in path: docstrings = re.findall( r'((((((def)|(class))\s[^\n]*?:\s*)?"{3}.*?)"{3})|(\n#[^\n]*\n[^\n]* =))', fileutil.read(path), re.DOTALL ) parsed = '\n***\n'.join([format_docstring(doc[0]) for doc in docstrings]) parsed += '\n***\nView the [source](%/../../pyxam/{})'.format( path.replace(os.path.dirname(os.path.dirname(__file__)), '') ) fileutil.write(build + '/' + name + '/' + file.replace('.py', '.md'), parsed) id = file.replace('.', '_') nav += nav_content.format(id, parsed.replace('"', '')) nav += nav_item.format(id, '%/' + name + '/' + file[:-3] + '.html', file[:-3]) table += table_item.format('%/' + name + '/' + file[:-3] + '.html', file[:-3]) nav += nav_sec_end table += table_sec_end
def composer_postprocessor(src): """ Performs a number of transformations to the source to create a more comprehensive appearance in html: - Converts LaTeX symbols to html codes - Converys markdown images [img] into `img` tags - Converts markdown links [label] into `a` tags - Converts two consecutive newlines into a `br` tag - Converts three consecutive newlines into two `br` tags Additionally the provided source is loaded into a template and solutions are made visible if the flag is set. :param src: The source to process :return: The processed source """ # String replacements src = re.sub(r'\[img\]\((.*?)\)', r'<img src="\1">', src) src = re.sub(r'\[([^\[\]\n]+)\]\(([^()\n]+)\)', r'<a href="\2">\1</a>', src) src = re.sub(r'\n{3}', '<br /><br />', src) src = re.sub(r'\n{2}', '<br />', src) src = fileutil.read(options.state.htmltemplate()).replace( '<!-- content -->', src) if options.state.solutions(): src = src.replace('display: none', '') else: src = src.replace('class="correctchoice"', 'class="choice"') return src
def ParseXMLWithIncludes(xmlFilePath): with fileutil.read(xmlFilePath) as fh: text = fh.read() subs = [] for result in re.finditer(r'<!--[ \t]*#[ \t]*include[ \t]+(.+)[ \t]*-->', text): inline = result.group(1).strip() with fileutil.read(os.path.join(os.path.dirname(xmlFilePath), inline)) as fh: inlineText = fh.read() subs.append((result.start(0), result.end(0), inlineText)) for start, end, repl in reversed(subs): text = '{}{}{}'.format(text[:start], repl, text[end:]) xRoot = cElementTree.fromstring(text) return xRoot
def mix(n, row, default=False): """ Takes an exam version and row of data and prepares a file for the formatter. If weaving is enabled a .mix with the correct name is weaved with the inline code at the start of the file. If weaving is disabled a .tex file is prepared for the formatter directly. :param n: The exam version :param row: The row of data """ logging.info(str(row)) version = str(chr(n + ord('A')) if options.state.alphabetize() else n + 1) if options.state.noweave(): fileutil.write( options.state.cwd() + '/' + config.filename.format(version=version, name=row['name'].replace(' ', '_').strip(), number=row['number']).strip('_') + '.tex', fileutil.read(options.state.template()) ) else: path = options.state.cwd() + '/' + config.filename.format(version=version, name=row['name'].replace(' ', '_').strip(), number=row['number']).strip('_') + '.tex' fileutil.write( path, '<%\n' + inline .replace('{number}', str(n)) .replace('{version}', str(version)) .replace('{student_first_name}', config.placeholder[options.state.format()] if default else str(row['first'])) .replace('{student_last_name}', config.placeholder[options.state.format()] if default else str(row['last'])) .replace('{student_name}', config.placeholder[options.state.format()] if default else str(row['name'])) .replace('{student_number}', config.placeholder[options.state.format()] if default else str(row['number'])) + '\n%>' + fileutil.read(options.state.template()) ) lib_loader.weave(path) fileutil.write( path, '<%\n' + inline .replace('{number}', str(n)) .replace('{version}', str(version)) .replace('{student_first_name}', config.placeholder[options.state.format()] if default else str(row['first'])) .replace('{student_last_name}', config.placeholder[options.state.format()] if default else str(row['last'])) .replace('{student_name}', config.placeholder[options.state.format()] if default else str(row['name'])) .replace('{student_number}', config.placeholder[options.state.format()] if default else str(row['number'])) + '\n%>' + fileutil.read(options.state.cwd() + '/' + config.filename.format(version=version, name=row['name'].replace(' ', '_'), number=row['number']).strip('_') + '.tex') ) lib_loader.weave(path)
def highlight(kind, highlight_color, text): etc_path = os.path.join(fileutil.getConfigPath(), 'highlights') getArr = lambda x: fileutil.read(os.path.join(etc_path, x)).split('\n') things = getArr(kind + '.txt') text = replace_with_regex(things, text, highlight_color) things = getArr(kind + '_regex.txt') text = replace_with_regex_raw(things, text, highlight_color) return text
def getScanResults(path): scan_results = [] for scanFileName in os.listdir(path): scanFilePath = os.path.join(path, scanFileName) if os.path.exists(scanFilePath) and os.path.isfile( scanFilePath) and scanFilePath.endswith(".scan.txt"): scanObj = {} scanObj["name"] = scanFilePath.split('/')[-1][:-9] scanObj["text"] = fileutil.read(scanFilePath) scan_results.append(scanObj) return scan_results
def pdf_compile(): """ Compiles any tex files in the out directory to PDF or DVI depending on what `compile_format` is set to. All additional files (aux, log, tex) are removed once compilation completes. """ options.state.cwd(options.state.out()) options.post('Compiling', len(fileutil.with_extension('.tex')), 'files.') for file in fileutil.with_extension('.tex'): if compile_format == 'dvi': fileutil.write(file, '%&latex\n' + fileutil.read(file)) try: fileutil.remove(file.replace('.tex', '.pdf')) except: pass for i in range(options.state.recomps()): lib_loader.pdflatex(file) fileutil.remove(fileutil.with_extension(['.aux', '.log', '.tex'])) options.post('Finished compiling.\n')
def getInfoObj(host, detailed=True): info = {} path = fileutil.getPortPath(host, None) #get sysinfo info["name"] = host info["system_info"] = fileutil.read(os.path.join(path, 'system_info.txt')) info["ports"] = [] if not os.path.exists(path): return None res = getScanResults(path) info["scan_results"] = res #get ports(if host), recurse if requested for filename in os.listdir(path): filepath = os.path.join(path, filename) if os.path.isdir(filepath): portObj = getPortInfoObj(filepath, detailed) if portObj: info["ports"].append(portObj) return info
def compile_doc(build, doc): """ Converts the given file from markdown to HTML and then copies it to the build directory. :param build: The directory of the file to compile :param doc: The file to compile """ pyxam.pyxam.start([ '-w', # Disable weaving '-f', 'html', # Convert to HTML '-o', build, # Output to the build directory '-t', 'doc', # Title the file doc '-htt', config.template_directory + '/docs.html', doc # Use the docs template ]) buffer = fileutil.read(build + '/doc_v1.html') fileutil.remove(doc) fileutil.remove(build + '/doc_v1.html') fileutil.write( doc.replace('.md', '.html'), buffer .replace('<!-- nav -->', nav) .replace('<!-- table -->', table + '</ul>') .replace('%/', url + '/') )
def setDefaultLanguage(window): window['language'].SetValue('Java') window['content'].update(read('./copyrights/java-template'))
def import_questions(*paths): import fileutil for path in paths: path = fileutil.find_file(path) print(fileutil.read(path))
def addFile(self, filePath): with fileutil.read(filePath) as fh: text = fh.read() text = optimizeText(text) return self.addString(text.replace('\n', '\\n\\\n') + '\\n\\0')
def __onAddScene(self): # request user for a template if there are multiple options templates = list(Templates(self.__subFolder)) if not templates: QMessageBox.critical( self, 'Could not create scene', 'Can not add scenes to this project until a template has been made to base them off.' ) return if len(templates) == 1: templateDir = TemplateSourceFolderFromName(templates[0], self.__subFolder) templatePath = TemplateFileFromName(templates[0], self.__subFolder) else: template = QInputDialog.getItem(self, 'Create scene', 'Select template', templates, 0, False) if not template[1] or not template[0] in templates: return templateDir = TemplateSourceFolderFromName(template[0], self.__subFolder) templatePath = TemplateFileFromName(template[0], self.__subFolder) name = QInputDialog.getText(self, 'Create scene', 'Scene name') if not name[1]: return scenesPath = ScenesPath(self.__subFolder) outFile = os.path.join(scenesPath, name[0] + SCENE_EXT) outDir = os.path.join(scenesPath, name[0]) if fileutil.exists(outFile): QMessageBox.critical( self, 'Could not create scene', 'A scene with name "%s" already exists. No scene was created.' % name[0]) return if fileutil.exists(outDir): if QMessageBox.warning( self, 'Scene not empty', 'A folder with name "%s" already exists. Create scene anyways?' % name[0], QMessageBox.Ok | QMessageBox.Cancel) == QMessageBox.Cancel: return else: os.makedirs(outDir.replace('\\', '/')) with fileutil.edit(outFile) as fh: fh.write('<scene camera="0,1,-10,0,0,0" template="%s"/>' % os.path.relpath(templatePath, scenesPath)) # find required template inputs (sections) xTemplate = ParseXMLWithIncludes(templatePath) for xPass in xTemplate: for xElement in xPass: if xElement.tag.lower() != 'section': continue # given a section make a stub file so the scene is complete on disk resource = os.path.join(templateDir, xElement.attrib['path']) text = '' # copy template data if there is any if fileutil.exists(resource): with fileutil.read(resource) as fh: text = fh.read() with fileutil.edit( os.path.join(outDir, xElement.attrib['path'])) as fh: fh.write(text) self.appendSceneItem(name[0])
import re import os import csv import config import options import logging import fileutil import lib_loader # A map of all currently loaded methods _methods = {} # The inline code placed at the start of every weaved file inline = fileutil.read(config.pyxam_directory + '/inline.py') def setup(): """ Mix the composed files with the specified method then copy all mixed files to the out directory with the appropriate names. If any files are in the figure directory they are copied out as well. """ for n in range(options.state.number()): mix(n, {'first': '', 'last': '', 'name': '','number': ''}, default=True) _methods[options.state.method()](options.state.number(), csv_read(options.state.population())) options.post('Template successfully weaved.') def csv_read(file): """
Mixes and weaves the template file. """ import re import os import csv import config import options import logging import fileutil import lib_loader # A map of all currently loaded methods _methods = {} # The inline code placed at the start of every weaved file inline = fileutil.read(config.pyxam_directory + '/inline.py') def setup(): """ Mix the composed files with the specified method then copy all mixed files to the out directory with the appropriate names. If any files are in the figure directory they are copied out as well. """ for n in range(options.state.number()): mix(n, { 'first': '', 'last': '', 'name': '', 'number': '' }, default=True)
def indexHtml(jsonStr): content = read('./frontend/index.html') print(content)