def dirToFile(): firstDir = True topDirLen = len(TOP_DIR) if userFileFormat == 1: dat = codecsOpen('music_list.txt', 'w', encoding='utf-8') elif userFileFormat == 2: dat = codecsOpen('music_list.md', 'w', encoding='utf-8') ''' walk produces 3 values for every iteration: path to the dir, subdirs in that dir, files in that dir ''' for root, dirs, files in walk(TOP_DIR): if firstDir: firstDir = False continue else: path = root[topDirLen:] # remove the TOP_DIR substring writeLineInFile(path, dat) dat.write('\n----------\n\n') dat.write('- No. of artists: **' + str(ARTIST_COUNT) + '**\n') dat.write('- No. of albums: **' + str(ALBUM_COUNT) + '**') dat.close() return
def resultMark(targetDir, result, okNum=None, okSize=None, errorText=None): for markName in RESULT_MARKS.itervalues(): silentRemove(join(targetDir, markName)) if okNum is not None: with open(join(targetDir, RESULT_MARKS[False]), 'wb') as f: f.write('%d,%d\r\n' % (okNum, okSize)) if result: with codecsOpen(join(targetDir, RESULT_MARKS[True]), 'wb', 'utf-8') as f: f.write(errorText)
def write_json(output_file, bindings): ''' Write the tags contains in bindings in JSON into the file output_file :param output_file: the file path to write to :param bindings: the dict containing the tags uris, labels and languages ''' results = {'bindings': bindings} output = {'HEAD': HEAD, 'results': results} try: with codecsOpen(output_file, 'w', 'utf-8') as f: f.write( dumps(output, ensure_ascii=False, indent=2, encoding='utf-8')) except URLError: log.info("Couldn't save to the file ", output_file)
def readAndParseFile(inFilename, options, encoding="ASCII"): """ Helper function to read and parse a given file and create an AST walker. """ # Read contents of input file. if encoding == 'ASCII': inFile = open(inFilename) else: inFile = codecsOpen(inFilename, encoding=encoding) lines = inFile.readlines() inFile.close() # Create the abstract syntax tree for the input file. testWalker = AstWalker(lines, options, inFilename) testWalker.parseLines() # Output the modified source. return testWalker.getLines()
def checkResultMark(targetDir): okMark = join(targetDir, RESULT_MARKS[False]) okDate = getFileModificationTime(okMark) if isfile(okMark) else 0 okText = '' if okDate: with open(okMark) as f: okText = tuple(int(i) for i in f.read().split(',')) okNum = okText[0] okSize = okText[1] if len(okText) > 1 else 0 else: okNum = okSize = None errorMark = join(targetDir, RESULT_MARKS[True]) errorDate = getFileModificationTime(errorMark) if isfile(errorMark) else 0 errorText = '' if errorDate: with codecsOpen(errorMark, 'r', 'utf-8') as f: errorText = f.read() return (bool(errorDate), max(okDate, errorDate), okNum, okSize, errorText)
def main(): """ Starts the parser on the file given by the filename as the first argument on the command line. """ from optparse import OptionParser, OptionGroup from os import sep from os.path import basename, getsize from sys import argv, exit as sysExit from chardet import detect from codecs import BOM_UTF8, open as codecsOpen def optParse(): """ Parses command line options. Generally we're supporting all the command line options that doxypy.py supports in an analogous way to make it easy to switch back and forth. We additionally support a top-level namespace argument that is used to trim away excess path information. """ parser = OptionParser(prog=basename(argv[0])) parser.set_usage("%prog [options] filename") parser.add_option( "-a", "--autobrief", action="store_true", dest="autobrief", help= "parse the docstring for @brief description and other information") parser.add_option("-c", "--autocode", action="store_true", dest="autocode", help="parse the docstring for code samples") parser.add_option( "-n", "--ns", action="store", type="string", dest="topLevelNamespace", help="specify a top-level namespace that will be used to trim paths" ) parser.add_option("-N", action="store_true", dest="trimAfterNamespace", help="start after specified top-level namespace") parser.add_option( "-t", "--tablength", action="store", type="int", dest="tablength", default=4, help="specify a tab length in spaces; only needed if tabs are used" ) parser.add_option("-s", "--stripinit", action="store_true", dest="stripinit", help="strip __init__ from namespace") group = OptionGroup(parser, "Debug Options") group.add_option("-d", "--debug", action="store_true", dest="debug", help="enable debug output on stderr") parser.add_option_group(group) ## Parse options based on our definition. (options, filename) = parser.parse_args() # Just abort immediately if we are don't have an input file. if not filename: stderr.write("No filename given." + linesep) sysExit(-1) # Turn the full path filename into a full path module location. fullPathNamespace = filename[0].replace(sep, '.')[:-3] # Use any provided top-level namespace argument to trim off excess. realNamespace = fullPathNamespace if options.topLevelNamespace: namespaceStart = fullPathNamespace.find(options.topLevelNamespace) if namespaceStart >= 0: if options.trimAfterNamespace: namespaceStart += len(options.topLevelNamespace) realNamespace = fullPathNamespace[namespaceStart:] if options.stripinit: realNamespace = realNamespace.replace('.__init__', '') options.fullPathNamespace = realNamespace return options, filename[0] # Figure out what is being requested. (options, inFilename) = optParse() # Figure out encoding of input file. numOfSampleBytes = min(getsize(inFilename), 32) sampleBytes = open(inFilename, 'rb').read(numOfSampleBytes) sampleByteAnalysis = detect(sampleBytes) encoding = sampleByteAnalysis['encoding'] # Switch to generic versions to strip the BOM automatically. if sampleBytes.startswith(BOM_UTF8): encoding = 'UTF-8-SIG' if encoding is None: sysExit(-1) if encoding.startswith("UTF-16"): encoding = "UTF-16" if encoding.startswith("UTF-32"): encoding = "UTF-32" # Read contents of input file. if encoding == 'ascii': inFile = open(inFilename) else: inFile = codecsOpen(inFilename, encoding=encoding) lines = inFile.readlines() inFile.close() # Create the abstract syntax tree for the input file. astWalker = AstWalker(lines, options, inFilename) astWalker.parseLines() # Output the modified source. print(astWalker.getLines())
def main(): """ Start it up. Starts the parser on the file given by the filename as the first argument on the command line. """ def argParse(): """ Parse command line options. Generally we're supporting all the command line options that doxypy.py supports in an analogous way to make it easy to switch back and forth. We additionally support a top-level namespace argument that is used to trim away excess path information. """ prog = basename(argv[0]) parser = ArgumentParser(prog=prog, usage="%(prog)s [options] filename") parser.add_argument( "filename", help="Input file name" ) parser.add_argument( "-a", "--autobrief", action="store_true", dest="autobrief", help="parse the docstring for @brief description and other information" ) parser.add_argument( "-c", "--autocode", action="store_true", dest="autocode", help="parse the docstring for code samples" ) parser.add_argument( "-n", "--ns", action="store", type=str, dest="topLevelNamespace", help="specify a top-level namespace that will be used to trim paths" ) parser.add_argument( "-t", "--tablength", action="store", type=int, dest="tablength", default=4, help="specify a tab length in spaces; only needed if tabs are used" ) parser.add_argument( "-s", "--stripinit", action="store_true", dest="stripinit", help="strip __init__ from namespace" ) parser.add_argument( "-O", "--object-respect", action="store_true", dest="object_respect", help="By default, doxypypy hides object class from class dependencies" "even if class inherits explictilty from objects (new-style class)," "this option disable this." ) group = parser.add_argument_group("Debug Options") group.add_argument( "-d", "--debug", action="store_true", dest="debug", help="enable debug output on stderr" ) # Parse options based on our definition. args = parser.parse_args() # Just abort immediately if we are don't have an input file. if not args.filename: stderr.write("No filename given." + linesep) sysExit(-1) # Turn the full path filename into a full path module location. fullPathNamespace = args.filename.replace(sep, '.')[:-3] # Use any provided top-level namespace argument to trim off excess. realNamespace = fullPathNamespace if args.topLevelNamespace: namespaceStart = fullPathNamespace.find(args.topLevelNamespace) if namespaceStart >= 0: realNamespace = fullPathNamespace[namespaceStart:] if args.stripinit: realNamespace = realNamespace.replace('.__init__', '') args.fullPathNamespace = realNamespace return args # Figure out what is being requested. args = argParse() # Figure out encoding of input file. numOfSampleBytes = min(getsize(args.filename), 32) sampleBytes = open(args.filename, 'rb').read(numOfSampleBytes) sampleByteAnalysis = detect(sampleBytes) encoding = sampleByteAnalysis['encoding'] or 'ascii' # Switch to generic versions to strip the BOM automatically. if sampleBytes.startswith(BOM_UTF8): encoding = 'UTF-8-SIG' if encoding.startswith("UTF-16"): encoding = "UTF-16" elif encoding.startswith("UTF-32"): encoding = "UTF-32" # Read contents of input file. if encoding == 'ascii': inFile = open(args.filename) else: inFile = codecsOpen(args.filename, encoding=encoding) lines = inFile.readlines() inFile.close() # Create the abstract syntax tree for the input file. astWalker = AstWalker(lines, args) astWalker.parseLines() # Output the modified source. # There is a "feature" in print on Windows. If linesep is # passed, it will generate 0x0D 0x0D 0x0A each line which # screws up Doxygen since it's expected 0x0D 0x0A line endings. for line in astWalker.getLines().split(linesep): print(line.rstrip())
def main(): """ Starts the parser on the file given by the filename as the first argument on the command line. """ from optparse import OptionParser, OptionGroup from os import sep from os.path import basename, getsize from sys import argv, exit as sysExit from chardet import detect from codecs import BOM_UTF8, open as codecsOpen def optParse(): """ Parses command line options. Generally we're supporting all the command line options that doxypy.py supports in an analogous way to make it easy to switch back and forth. We additionally support a top-level namespace argument that is used to trim away excess path information. """ parser = OptionParser(prog=basename(argv[0])) parser.set_usage("%prog [options] filename") parser.add_option( "-a", "--autobrief", action="store_true", dest="autobrief", help="parse the docstring for @brief description and other information" ) parser.add_option( "-c", "--autocode", action="store_true", dest="autocode", help="parse the docstring for code samples" ) parser.add_option( "-n", "--ns", action="store", type="string", dest="topLevelNamespace", help="specify a top-level namespace that will be used to trim paths" ) parser.add_option( "-t", "--tablength", action="store", type="int", dest="tablength", default=4, help="specify a tab length in spaces; only needed if tabs are used" ) parser.add_option( "-s", "--stripinit", action="store_true", dest="stripinit", help="strip __init__ from namespace" ) parser.add_option( "-O", "--object-respect", action="store_true", dest="object_respect", help="By default, doxypypy hides object class from class dependencies even if class inherits explictilty from objects (new-style class), this option disable this." ) group = OptionGroup(parser, "Debug Options") group.add_option( "-d", "--debug", action="store_true", dest="debug", help="enable debug output on stderr" ) parser.add_option_group(group) ## Parse options based on our definition. (options, filename) = parser.parse_args() # Just abort immediately if we are don't have an input file. if not filename: stderr.write("No filename given." + linesep) sysExit(-1) # Turn the full path filename into a full path module location. fullPathNamespace = filename[0].replace(sep, '.')[:-3] # Use any provided top-level namespace argument to trim off excess. realNamespace = fullPathNamespace if options.topLevelNamespace: namespaceStart = fullPathNamespace.find(options.topLevelNamespace) if namespaceStart >= 0: realNamespace = fullPathNamespace[namespaceStart:] if options.stripinit: realNamespace = realNamespace.replace('.__init__', '') options.fullPathNamespace = realNamespace return options, filename[0] # Figure out what is being requested. (options, inFilename) = optParse() # Figure out encoding of input file. numOfSampleBytes = min(getsize(inFilename), 32) sampleBytes = open(inFilename, 'rb').read(numOfSampleBytes) sampleByteAnalysis = detect(sampleBytes) encoding = sampleByteAnalysis['encoding'] or 'ascii' # Switch to generic versions to strip the BOM automatically. if sampleBytes.startswith(BOM_UTF8): encoding = 'UTF-8-SIG' if encoding.startswith("UTF-16"): encoding = "UTF-16" elif encoding.startswith("UTF-32"): encoding = "UTF-32" # Read contents of input file. if encoding == 'ascii': inFile = open(inFilename) else: inFile = codecsOpen(inFilename, encoding=encoding) lines = inFile.readlines() inFile.close() # Create the abstract syntax tree for the input file. astWalker = AstWalker(lines, options, inFilename) astWalker.parseLines() # Output the modified source. print(astWalker.getLines())