Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

# coding: utf8 

""" Cog content generation tool. 

http://nedbatchelder.com/code/cog 

 

Copyright 2004-2019, Ned Batchelder. 

""" 

 

from __future__ import absolute_import, print_function 

 

import copy 

import getopt 

import glob 

import hashlib 

import imp 

import linecache 

import os 

import re 

import shlex 

import sys 

import traceback 

 

from .backward import PY3, StringIO, string_types, to_bytes 

 

__all__ = ['Cog', 'CogUsageError', 'main'] 

 

__version__ = '3.0.0' 

 

usage = """\ 

cog - generate content with inlined Python code. 

 

cog [OPTIONS] [INFILE | @FILELIST] ... 

 

INFILE is the name of an input file, '-' will read from stdin. 

FILELIST is the name of a text file containing file names or 

other @FILELISTs. 

 

OPTIONS: 

-c Checksum the output to protect it against accidental change. 

-d Delete the generator code from the output file. 

-D name=val Define a global string available to your generator code. 

-e Warn if a file has no cog code in it. 

-I PATH Add PATH to the list of directories for data files and modules. 

-n ENCODING Use ENCODING when reading and writing files. 

-o OUTNAME Write the output to OUTNAME. 

-p PROLOGUE Prepend the generator source with PROLOGUE. Useful to insert an 

import line. Example: -p "import math" 

-r Replace the input file with the output. 

-s STRING Suffix all generated output lines with STRING. 

-U Write the output with Unix newlines (only LF line-endings). 

-w CMD Use CMD if the output file needs to be made writable. 

A %s in the CMD will be filled with the filename. 

-x Excise all the generated output without running the generators. 

-z The end-output marker can be omitted, and is assumed at eof. 

-v Print the version of cog and exit. 

--verbosity=VERBOSITY 

Control the amount of output. 2 (the default) lists all files, 

1 lists only changed files, 0 lists no files. 

--markers='START END END-OUTPUT' 

The patterns surrounding cog inline instructions. Should 

include three values separated by spaces, the start, end, 

and end-output markers. Defaults to '[[[cog ]]] [[[end]]]'. 

-h Print this help. 

""" 

 

# Other package modules 

from .whiteutils import * 

 

class CogError(Exception): 

""" Any exception raised by Cog. 

""" 

def __init__(self, msg, file='', line=0): 

if file: 

Exception.__init__(self, "%s(%d): %s" % (file, line, msg)) 

else: 

Exception.__init__(self, msg) 

 

class CogUsageError(CogError): 

""" An error in usage of command-line arguments in cog. 

""" 

pass 

 

class CogInternalError(CogError): 

""" An error in the coding of Cog. Should never happen. 

""" 

pass 

 

class CogGeneratedError(CogError): 

""" An error raised by a user's cog generator. 

""" 

pass 

 

class CogUserException(CogError): 

""" An exception caught when running a user's cog generator. 

The argument is the traceback message to print. 

""" 

pass 

 

class Redirectable: 

""" An object with its own stdout and stderr files. 

""" 

def __init__(self): 

self.stdout = sys.stdout 

self.stderr = sys.stderr 

 

def setOutput(self, stdout=None, stderr=None): 

""" Assign new files for standard out and/or standard error. 

""" 

108 ↛ 110line 108 didn't jump to line 110, because the condition on line 108 was never false if stdout: 

self.stdout = stdout 

110 ↛ 111line 110 didn't jump to line 111, because the condition on line 110 was never true if stderr: 

self.stderr = stderr 

 

def prout(self, s, end="\n"): 

print(s, file=self.stdout, end=end) 

 

def prerr(self, s, end="\n"): 

print(s, file=self.stderr, end=end) 

 

 

class CogGenerator(Redirectable): 

""" A generator pulled from a source file. 

""" 

def __init__(self, options=None): 

Redirectable.__init__(self) 

self.markers = [] 

self.lines = [] 

self.options = options or CogOptions() 

 

def parseMarker(self, l): 

self.markers.append(l) 

 

def parseLine(self, l): 

self.lines.append(l.strip('\n')) 

 

def getCode(self): 

""" Extract the executable Python code from the generator. 

""" 

# If the markers and lines all have the same prefix 

# (end-of-line comment chars, for example), 

# then remove it from all the lines. 

prefIn = commonPrefix(self.markers + self.lines) 

if prefIn: 

self.markers = [ l.replace(prefIn, '', 1) for l in self.markers ] 

self.lines = [ l.replace(prefIn, '', 1) for l in self.lines ] 

 

return reindentBlock(self.lines, '') 

 

def evaluate(self, cog, globals, fname): 

# figure out the right whitespace prefix for the output 

prefOut = whitePrefix(self.markers) 

 

intext = self.getCode() 

if not intext: 

return '' 

 

prologue = "import " + cog.cogmodulename + " as cog\n" 

157 ↛ 158line 157 didn't jump to line 158, because the condition on line 157 was never true if self.options.sPrologue: 

prologue += self.options.sPrologue + '\n' 

code = compile(prologue + intext, str(fname), 'exec') 

 

# Make sure the "cog" module has our state. 

cog.cogmodule.msg = self.msg 

cog.cogmodule.out = self.out 

cog.cogmodule.outl = self.outl 

cog.cogmodule.error = self.error 

 

self.outstring = '' 

try: 

eval(code, globals) 

170 ↛ 171line 170 didn't jump to line 171, because the exception caught by line 170 didn't happen except CogError: 

raise 

except: 

typ, err, tb = sys.exc_info() 

frames = (tuple(fr) for fr in traceback.extract_tb(tb.tb_next)) 

frames = find_cog_source(frames, prologue) 

msg = "".join(traceback.format_list(frames)) 

msg += "{}: {}".format(typ.__name__, err) 

raise CogUserException(msg) 

 

# We need to make sure that the last line in the output 

# ends with a newline, or it will be joined to the 

# end-output line, ruining cog's idempotency. 

if self.outstring and self.outstring[-1] != '\n': 

self.outstring += '\n' 

 

return reindentBlock(self.outstring, prefOut) 

 

def msg(self, s): 

self.prout("Message: "+s) 

 

def out(self, sOut='', dedent=False, trimblanklines=False): 

""" The cog.out function. 

""" 

if trimblanklines and ('\n' in sOut): 

lines = sOut.split('\n') 

if lines[0].strip() == '': 

del lines[0] 

if lines and lines[-1].strip() == '': 

del lines[-1] 

sOut = '\n'.join(lines)+'\n' 

if dedent: 

sOut = reindentBlock(sOut) 

self.outstring += sOut 

 

def outl(self, sOut='', **kw): 

""" The cog.outl function. 

""" 

self.out(sOut, **kw) 

self.out('\n') 

 

def error(self, msg='Error raised by cog generator.'): 

""" The cog.error function. 

Instead of raising standard python errors, cog generators can use 

this function. It will display the error without a scary Python 

traceback. 

""" 

raise CogGeneratedError(msg) 

 

 

class NumberedFileReader: 

""" A decorator for files that counts the readline()'s called. 

""" 

def __init__(self, f): 

self.f = f 

self.n = 0 

 

def readline(self): 

l = self.f.readline() 

if l: 

self.n += 1 

return l 

 

def linenumber(self): 

return self.n 

 

 

class CogOptions: 

""" Options for a run of cog. 

""" 

def __init__(self): 

# Defaults for argument values. 

self.args = [] 

self.includePath = [] 

self.defines = {} 

self.bShowVersion = False 

self.sMakeWritableCmd = None 

self.bReplace = False 

self.bNoGenerate = False 

self.sOutputName = None 

self.bWarnEmpty = False 

self.bHashOutput = False 

self.bDeleteCode = False 

self.bEofCanBeEnd = False 

self.sSuffix = None 

self.bNewlines = False 

self.sBeginSpec = '[[[cog' 

self.sEndSpec = ']]]' 

self.sEndOutput = '[[[end]]]' 

self.sEncoding = "utf-8" 

self.verbosity = 2 

self.sPrologue = '' 

 

def __eq__(self, other): 

""" Comparison operator for tests to use. 

""" 

return self.__dict__ == other.__dict__ 

 

def clone(self): 

""" Make a clone of these options, for further refinement. 

""" 

return copy.deepcopy(self) 

 

def addToIncludePath(self, dirs): 

""" Add directories to the include path. 

""" 

dirs = dirs.split(os.pathsep) 

self.includePath.extend(dirs) 

 

def parseArgs(self, argv): 

# Parse the command line arguments. 

try: 

opts, self.args = getopt.getopt( 

argv, 

'cdD:eI:n:o:rs:p:Uvw:xz', 

[ 

'markers=', 

'verbosity=', 

] 

) 

except getopt.error as msg: 

raise CogUsageError(msg) 

 

# Handle the command line arguments. 

for o, a in opts: 

if o == '-c': 

self.bHashOutput = True 

elif o == '-d': 

self.bDeleteCode = True 

elif o == '-D': 

if a.count('=') < 1: 

raise CogUsageError("-D takes a name=value argument") 

name, value = a.split('=', 1) 

self.defines[name] = value 

elif o == '-e': 

self.bWarnEmpty = True 

elif o == '-I': 

self.addToIncludePath(a) 

elif o == '-n': 

self.sEncoding = a 

elif o == '-o': 

self.sOutputName = a 

elif o == '-r': 

self.bReplace = True 

elif o == '-s': 

self.sSuffix = a 

elif o == '-p': 

self.sPrologue = a 

elif o == '-U': 

self.bNewlines = True 

elif o == '-v': 

self.bShowVersion = True 

elif o == '-w': 

self.sMakeWritableCmd = a 

elif o == '-x': 

self.bNoGenerate = True 

elif o == '-z': 

self.bEofCanBeEnd = True 

elif o == '--markers': 

self._parse_markers(a) 

elif o == '--verbosity': 

self.verbosity = int(a) 

else: 

# Since getopt.getopt is given a list of possible flags, 

# this is an internal error. 

raise CogInternalError("Don't understand argument %s" % o) 

 

def _parse_markers(self, val): 

try: 

self.sBeginSpec, self.sEndSpec, self.sEndOutput = val.split(' ') 

except ValueError: 

raise CogUsageError( 

'--markers requires 3 values separated by spaces, could not parse %r' % val 

) 

 

def validate(self): 

""" Does nothing if everything is OK, raises CogError's if it's not. 

""" 

if self.bReplace and self.bDeleteCode: 

raise CogUsageError("Can't use -d with -r (or you would delete all your source!)") 

 

if self.bReplace and self.sOutputName: 

raise CogUsageError("Can't use -o with -r (they are opposites)") 

 

 

class Cog(Redirectable): 

""" The Cog engine. 

""" 

def __init__(self): 

Redirectable.__init__(self) 

self.options = CogOptions() 

self._fixEndOutputPatterns() 

self.cogmodulename = "cog" 

self.installCogModule() 

 

def _fixEndOutputPatterns(self): 

end_output = re.escape(self.options.sEndOutput) 

self.reEndOutput = re.compile(end_output + r'(?P<hashsect> *\(checksum: (?P<hash>[a-f0-9]+)\))') 

self.sEndFormat = self.options.sEndOutput + ' (checksum: %s)' 

 

def showWarning(self, msg): 

self.prout("Warning: "+msg) 

 

def isBeginSpecLine(self, s): 

return self.options.sBeginSpec in s 

 

def isEndSpecLine(self, s): 

return self.options.sEndSpec in s and not self.isEndOutputLine(s) 

 

def isEndOutputLine(self, s): 

return self.options.sEndOutput in s 

 

def installCogModule(self): 

""" Magic mumbo-jumbo so that imported Python modules 

can say "import cog" and get our state. 

""" 

self.cogmodule = imp.new_module('cog') 

self.cogmodule.path = [] 

 

def openOutputFile(self, fname): 

""" Open an output file, taking all the details into account. 

""" 

opts = {} 

mode = "w" 

if PY3: 

opts['encoding'] = self.options.sEncoding 

if self.options.bNewlines: 

if PY3: 

opts['newline'] = "\n" 

else: 

mode = "wb" 

fdir = os.path.dirname(fname) 

if os.path.dirname(fdir) and not os.path.exists(fdir): 

os.makedirs(fdir) 

return open(fname, mode, **opts) 

 

def openInputFile(self, fname): 

""" Open an input file. """ 

if fname == "-": 

return sys.stdin 

else: 

opts = {} 

if PY3: 

opts['encoding'] = self.options.sEncoding 

return open(fname, "r", **opts) 

 

def processFile(self, fIn, fOut, fname=None, globals=None): 

""" Process an input file object to an output file object. 

fIn and fOut can be file objects, or file names. 

""" 

 

sFileIn = fname or '' 

sFileOut = fname or '' 

fInToClose = fOutToClose = None 

# Convert filenames to files. 

425 ↛ 427line 425 didn't jump to line 427, because the condition on line 425 was never true if isinstance(fIn, string_types): 

# Open the input file. 

sFileIn = fIn 

fIn = fInToClose = self.openInputFile(fIn) 

429 ↛ 431line 429 didn't jump to line 431, because the condition on line 429 was never true if isinstance(fOut, string_types): 

# Open the output file. 

sFileOut = fOut 

fOut = fOutToClose = self.openOutputFile(fOut) 

 

try: 

fIn = NumberedFileReader(fIn) 

 

bSawCog = False 

 

self.cogmodule.inFile = sFileIn 

self.cogmodule.outFile = sFileOut 

self.cogmodulename = 'cog_' + hashlib.md5(sFileOut.encode()).hexdigest() 

sys.modules[self.cogmodulename] = self.cogmodule 

# if "import cog" explicitly done in code by user, note threading will cause clashes.  

sys.modules['cog'] = self.cogmodule 

 

# The globals dict we'll use for this file. 

447 ↛ 451line 447 didn't jump to line 451, because the condition on line 447 was never false if globals is None: 

globals = {} 

 

# If there are any global defines, put them in the globals. 

globals.update(self.options.defines) 

 

# loop over generator chunks 

l = fIn.readline() 

while l: 

# Find the next spec begin 

while l and not self.isBeginSpecLine(l): 

458 ↛ 459line 458 didn't jump to line 459, because the condition on line 458 was never true if self.isEndSpecLine(l): 

raise CogError("Unexpected '%s'" % self.options.sEndSpec, 

file=sFileIn, line=fIn.linenumber()) 

461 ↛ 462line 461 didn't jump to line 462, because the condition on line 461 was never true if self.isEndOutputLine(l): 

raise CogError("Unexpected '%s'" % self.options.sEndOutput, 

file=sFileIn, line=fIn.linenumber()) 

fOut.write(l) 

l = fIn.readline() 

if not l: 

break 

468 ↛ 472line 468 didn't jump to line 472, because the condition on line 468 was never false if not self.options.bDeleteCode: 

fOut.write(l) 

 

# l is the begin spec 

gen = CogGenerator(options=self.options) 

gen.setOutput(stdout=self.stdout) 

gen.parseMarker(l) 

firstLineNum = fIn.linenumber() 

self.cogmodule.firstLineNum = firstLineNum 

 

# If the spec begin is also a spec end, then process the single 

# line of code inside. 

if self.isEndSpecLine(l): 

beg = l.find(self.options.sBeginSpec) 

end = l.find(self.options.sEndSpec) 

if beg > end: 

raise CogError("Cog code markers inverted", 

file=sFileIn, line=firstLineNum) 

else: 

sCode = l[beg+len(self.options.sBeginSpec):end].strip() 

gen.parseLine(sCode) 

else: 

# Deal with an ordinary code block. 

l = fIn.readline() 

 

# Get all the lines in the spec 

while l and not self.isEndSpecLine(l): 

495 ↛ 496line 495 didn't jump to line 496, because the condition on line 495 was never true if self.isBeginSpecLine(l): 

raise CogError("Unexpected '%s'" % self.options.sBeginSpec, 

file=sFileIn, line=fIn.linenumber()) 

498 ↛ 499line 498 didn't jump to line 499, because the condition on line 498 was never true if self.isEndOutputLine(l): 

raise CogError("Unexpected '%s'" % self.options.sEndOutput, 

file=sFileIn, line=fIn.linenumber()) 

501 ↛ 503line 501 didn't jump to line 503, because the condition on line 501 was never false if not self.options.bDeleteCode: 

fOut.write(l) 

gen.parseLine(l) 

l = fIn.readline() 

505 ↛ 506line 505 didn't jump to line 506, because the condition on line 505 was never true if not l: 

raise CogError( 

"Cog block begun but never ended.", 

file=sFileIn, line=firstLineNum) 

 

510 ↛ 512line 510 didn't jump to line 512, because the condition on line 510 was never false if not self.options.bDeleteCode: 

fOut.write(l) 

gen.parseMarker(l) 

 

l = fIn.readline() 

 

# Eat all the lines in the output section. While reading past 

# them, compute the md5 hash of the old output. 

previous = "" 

hasher = hashlib.md5() 

while l and not self.isEndOutputLine(l): 

521 ↛ 522line 521 didn't jump to line 522, because the condition on line 521 was never true if self.isBeginSpecLine(l): 

raise CogError("Unexpected '%s'" % self.options.sBeginSpec, 

file=sFileIn, line=fIn.linenumber()) 

524 ↛ 525line 524 didn't jump to line 525, because the condition on line 524 was never true if self.isEndSpecLine(l): 

raise CogError("Unexpected '%s'" % self.options.sEndSpec, 

file=sFileIn, line=fIn.linenumber()) 

previous += l 

hasher.update(to_bytes(l)) 

l = fIn.readline() 

curHash = hasher.hexdigest() 

 

532 ↛ 534line 532 didn't jump to line 534, because the condition on line 532 was never true if not l and not self.options.bEofCanBeEnd: 

# We reached end of file before we found the end output line. 

raise CogError("Missing '%s' before end of file." % self.options.sEndOutput, 

file=sFileIn, line=fIn.linenumber()) 

 

# Make the previous output available to the current code 

self.cogmodule.previous = previous 

 

# Write the output of the spec to be the new output if we're 

# supposed to generate code. 

hasher = hashlib.md5() 

543 ↛ 549line 543 didn't jump to line 549, because the condition on line 543 was never false if not self.options.bNoGenerate: 

sFile = "<cog %s:%d>" % (sFileIn, firstLineNum) 

sGen = gen.evaluate(cog=self, globals=globals, fname=sFile) 

sGen = self.suffixLines(sGen) 

hasher.update(to_bytes(sGen)) 

fOut.write(sGen) 

newHash = hasher.hexdigest() 

 

bSawCog = True 

 

# Write the ending output line 

hashMatch = self.reEndOutput.search(l) 

555 ↛ 556line 555 didn't jump to line 556, because the condition on line 555 was never true if self.options.bHashOutput: 

if hashMatch: 

oldHash = hashMatch.groupdict()['hash'] 

if oldHash != curHash: 

raise CogError("Output has been edited! Delete old checksum to unprotect.", 

file=sFileIn, line=fIn.linenumber()) 

# Create a new end line with the correct hash. 

endpieces = l.split(hashMatch.group(0), 1) 

else: 

# There was no old hash, but we want a new hash. 

endpieces = l.split(self.options.sEndOutput, 1) 

l = (self.sEndFormat % newHash).join(endpieces) 

else: 

# We don't want hashes output, so if there was one, get rid of 

# it. 

570 ↛ 571line 570 didn't jump to line 571, because the condition on line 570 was never true if hashMatch: 

l = l.replace(hashMatch.groupdict()['hashsect'], '', 1) 

 

573 ↛ 575line 573 didn't jump to line 575, because the condition on line 573 was never false if not self.options.bDeleteCode: 

fOut.write(l) 

l = fIn.readline() 

 

577 ↛ 578line 577 didn't jump to line 578, because the condition on line 577 was never true if not bSawCog and self.options.bWarnEmpty: 

self.showWarning("no cog code found in %s" % sFileIn) 

finally: 

580 ↛ 581line 580 didn't jump to line 581, because the condition on line 580 was never true if fInToClose: 

fInToClose.close() 

582 ↛ 583line 582 didn't jump to line 583, because the condition on line 582 was never true if fOutToClose: 

fOutToClose.close() 

 

 

# A regex for non-empty lines, used by suffixLines. 

reNonEmptyLines = re.compile(r"^\s*\S+.*$", re.MULTILINE) 

 

def suffixLines(self, text): 

""" Add suffixes to the lines in text, if our options desire it. 

text is many lines, as a single string. 

""" 

593 ↛ 595line 593 didn't jump to line 595, because the condition on line 593 was never true if self.options.sSuffix: 

# Find all non-blank lines, and add the suffix to the end. 

repl = r"\g<0>" + self.options.sSuffix.replace('\\', '\\\\') 

text = self.reNonEmptyLines.sub(repl, text) 

return text 

 

def processString(self, sInput, fname=None): 

""" Process sInput as the text to cog. 

Return the cogged output as a string. 

""" 

fOld = StringIO(sInput) 

fNew = StringIO() 

self.processFile(fOld, fNew, fname=fname) 

return fNew.getvalue() 

 

def replaceFile(self, sOldPath, sNewText): 

""" Replace file sOldPath with the contents sNewText 

""" 

if not os.access(sOldPath, os.W_OK): 

# Need to ensure we can write. 

if self.options.sMakeWritableCmd: 

# Use an external command to make the file writable. 

cmd = self.options.sMakeWritableCmd.replace('%s', sOldPath) 

self.stdout.write(os.popen(cmd).read()) 

if not os.access(sOldPath, os.W_OK): 

raise CogError("Couldn't make %s writable" % sOldPath) 

else: 

# Can't write! 

raise CogError("Can't overwrite %s" % sOldPath) 

f = self.openOutputFile(sOldPath) 

f.write(sNewText) 

f.close() 

 

def saveIncludePath(self): 

self.savedInclude = self.options.includePath[:] 

self.savedSysPath = sys.path[:] 

 

def restoreIncludePath(self): 

self.options.includePath = self.savedInclude 

self.cogmodule.path = self.options.includePath 

sys.path = self.savedSysPath 

 

def addToIncludePath(self, includePath): 

self.cogmodule.path.extend(includePath) 

sys.path.extend(includePath) 

 

def processOneFile(self, sFile): 

""" Process one filename through cog. 

""" 

 

self.saveIncludePath() 

bNeedNewline = False 

 

try: 

self.addToIncludePath(self.options.includePath) 

# Since we know where the input file came from, 

# push its directory onto the include path. 

self.addToIncludePath([os.path.dirname(sFile)]) 

 

# How we process the file depends on where the output is going. 

if self.options.sOutputName: 

self.processFile(sFile, self.options.sOutputName, sFile) 

elif self.options.bReplace: 

# We want to replace the cog file with the output, 

# but only if they differ. 

if self.options.verbosity >= 2: 

self.prout("Cogging %s" % sFile, end="") 

bNeedNewline = True 

 

try: 

fOldFile = self.openInputFile(sFile) 

sOldText = fOldFile.read() 

fOldFile.close() 

sNewText = self.processString(sOldText, fname=sFile) 

if sOldText != sNewText: 

if self.options.verbosity >= 1: 

if self.options.verbosity < 2: 

self.prout("Cogging %s" % sFile, end="") 

self.prout(" (changed)") 

bNeedNewline = False 

self.replaceFile(sFile, sNewText) 

finally: 

# The try-finally block is so we can print a partial line 

# with the name of the file, and print (changed) on the 

# same line, but also make sure to break the line before 

# any traceback. 

if bNeedNewline: 

self.prout("") 

else: 

self.processFile(sFile, self.stdout, sFile) 

finally: 

self.restoreIncludePath() 

 

def processWildcards(self, sFile): 

files = glob.glob(sFile) 

if files: 

for sMatchingFile in files: 

self.processOneFile(sMatchingFile) 

else: 

self.processOneFile(sFile) 

 

def processFileList(self, sFileList): 

""" Process the files in a file list. 

""" 

flist = self.openInputFile(sFileList) 

lines = flist.readlines() 

flist.close() 

for l in lines: 

# Use shlex to parse the line like a shell. 

lex = shlex.shlex(l, posix=True) 

lex.whitespace_split = True 

lex.commenters = '#' 

# No escapes, so that backslash can be part of the path 

lex.escape = '' 

args = list(lex) 

if args: 

self.processArguments(args) 

 

def processArguments(self, args): 

""" Process one command-line. 

""" 

saved_options = self.options 

self.options = self.options.clone() 

 

self.options.parseArgs(args[1:]) 

self.options.validate() 

 

if args[0][0] == '@': 

if self.options.sOutputName: 

raise CogUsageError("Can't use -o with @file") 

self.processFileList(args[0][1:]) 

else: 

self.processWildcards(args[0]) 

 

self.options = saved_options 

 

def callableMain(self, argv): 

""" All of command-line cog, but in a callable form. 

This is used by main. 

argv is the equivalent of sys.argv. 

""" 

argv = argv[1:] 

 

# Provide help if asked for anywhere in the command line. 

if '-?' in argv or '-h' in argv: 

self.prerr(usage, end="") 

return 

 

self.options.parseArgs(argv) 

self.options.validate() 

self._fixEndOutputPatterns() 

 

if self.options.bShowVersion: 

self.prout("Cog version %s" % __version__) 

return 

 

if self.options.args: 

for a in self.options.args: 

self.processArguments([a]) 

else: 

raise CogUsageError("No files to process") 

 

def main(self, argv): 

""" Handle the command-line execution for cog. 

""" 

 

try: 

self.callableMain(argv) 

return 0 

except CogUsageError as err: 

self.prerr(err) 

self.prerr("(for help use -?)") 

return 2 

except CogGeneratedError as err: 

self.prerr("Error: %s" % err) 

return 3 

except CogUserException as err: 

self.prerr("Traceback (most recent call last):") 

self.prerr(err.args[0]) 

return 4 

except CogError as err: 

self.prerr(err) 

return 1 

 

 

def find_cog_source(frame_summary, prologue): 

"""Find cog source lines in a frame summary list, for printing tracebacks. 

 

Arguments: 

frame_summary: a list of 4-item tuples, as returned by traceback.extract_tb. 

prologue: the text of the code prologue. 

 

Returns 

A list of 4-item tuples, updated to correct the cog entries. 

 

""" 

prolines = prologue.splitlines() 

for filename, lineno, funcname, source in frame_summary: 

791 ↛ 803line 791 didn't jump to line 803, because the condition on line 791 was never false if not source: 

m = re.search(r"^<cog ([^:]+):(\d+)>$", filename) 

793 ↛ 794line 793 didn't jump to line 794, because the condition on line 793 was never true if m: 

if lineno <= len(prolines): 

filename = '<prologue>' 

source = prolines[lineno-1] 

lineno -= 1 # Because "import cog" is the first line in the prologue 

else: 

filename, coglineno = m.groups() 

coglineno = int(coglineno) 

lineno += coglineno - len(prolines) 

source = linecache.getline(filename, lineno).strip() 

yield filename, lineno, funcname, source 

 

 

def main(): 

"""Main function for entry_points to use.""" 

return Cog().main(sys.argv)