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

# Copyright (C) 2015 Chintalagiri Shashank 

# 

# This file is part of Tendril. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU Affero General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

# GNU Affero General Public License for more details. 

# 

# You should have received a copy of the GNU Affero General Public License 

# along with this program.  If not, see <http://www.gnu.org/licenses/>. 

""" 

gEDA Project Dox Module (:mod:`tendril.dox.gedaproject`) 

======================================================== 

 

This module generates the standard documentation set for gEDA projects. 

 

The functions here use the :mod:`tendril.dox.render` module to actually 

produce the output files after constructing the appropriate stage. 

 

Each document generator function in this module uses a predefined set of 

source files (relative to the project folder), and the output files 

generated by this module are put into predefined locations, with predefined 

names. Each function specifies the paths it operates on. 

 

``<project_doc_folder>`` is obtained from 

:func:`get_project_doc_folder` 

 

.. warning:: Unless otherwise specified in the function documentation, the 

             functions of this module will not overwrite any target output 

             files unless the pre-existing output files are older than the 

             source files. Note that this means the output will not be 

             regenerated if the template is changed. You should ``touch`` 

             one of the source files if you want to force a rebuild. 

 

.. seealso:: :mod:`tendril.dox`, :mod:`tendril.gedaif` 

 

.. rubric:: Document Set Generators 

 

.. autosummary:: 

 

    generate_docs 

 

.. rubric:: Document Generators 

 

.. autosummary:: 

 

    gen_confbom 

    gen_configdoc 

    gen_schpdf 

    gen_masterdoc 

    gen_confpdf 

    gen_cobom_csv 

    gen_pcb_pdf 

    gen_pcb_gbr 

    gen_pcb_dxf 

    gen_pcbpricing 

 

""" 

 

import os 

import csv 

import yaml 

import glob 

import shutil 

 

from tendril.gedaif import gschem 

from tendril.gedaif import conffile 

from tendril.gedaif import projfile 

from tendril.gedaif import pcb 

 

from tendril.utils import pdf 

from tendril.utils import fsutils 

 

from tendril.boms import electronics as boms_electronics 

from tendril.boms import outputbase as boms_outputbase 

 

import render 

from fs import path 

from fs.utils import copyfile 

 

from docstore import ExposedDocument 

from docstore import refdoc_fs 

 

from tendril.utils.config import PROJECTS_ROOT 

 

from tendril.utils import log 

from tendril.utils.fsutils import temp_fs 

 

workspace_fs = temp_fs.makeopendir('workspace_gpd') 

logger = log.get_logger(__name__, log.DEFAULT) 

 

 

def get_project_doc_folder(projectfolder): 

    projectfolder = os.path.relpath(projectfolder, PROJECTS_ROOT) 

    pth = path.join(projectfolder, 'doc') 

    if not refdoc_fs.exists(pth): 

        refdoc_fs.makedir(pth, recursive=True) 

    if not refdoc_fs.exists(path.join(pth, 'confdocs')): 

        refdoc_fs.makedir(path.join(pth, 'confdocs'), recursive=True) 

    return pth 

 

 

def gen_confbom(projfolder, configname): 

    """ 

    Generates a PDF of the BOM for a specified configuration of a gEDA 

    project. 

 

    :param projfolder: The gEDA project folder 

    :type projfolder: str 

    :param configname: The configuration name for which the BOM should be 

                       generated. 

    :return: The output file path. 

 

    .. rubric:: Paths 

 

    * Output File :  ``<project_doc_folder>/confdocs/<configname>-bom.pdf`` 

    * Source Files : The project's schematic folder. 

 

    .. rubric:: Template Used 

 

    ``tendril/dox/templates/geda-bom-simple.tex`` 

    (:download:`Included version 

    <../../tendril/dox/templates/geda-bom-simple.tex>`) 

 

    .. rubric:: Stage Keys Provided 

    .. list-table:: 

 

        * - ``configname`` 

          - The name of the configuration (a card or cable name). 

        * - ``desc`` 

          - The description of the configuration. 

        * - ``pcbname`` 

          - The name of the base PCB. 

        * - ``lines`` 

          - A list of :mod:`tendril.boms.outputbase.OutputBomLine` instances 

 

    """ 

 

    gpf = projfile.GedaProjectFile(projfolder) 

    sch_mtime = fsutils.get_folder_mtime(gpf.schfolder) 

 

    docfolder = get_project_doc_folder(projfolder) 

    outpath = path.join(docfolder, 'confdocs', configname + '-bom.pdf') 

    outf_mtime = fsutils.get_file_mtime(outpath, fs=refdoc_fs) 

 

    if outf_mtime is not None and outf_mtime > sch_mtime: 

        logger.debug('Skipping up-to-date ' + outpath) 

        return outpath 

 

    logger.info('Regenerating ' + outpath + os.linesep + 

                'Last modified : ' + str(sch_mtime) + 

                '; Last Created : ' + str(outf_mtime)) 

    bom = boms_electronics.import_pcb(projfolder) 

    obom = bom.create_output_bom(configname) 

 

    stage = {'configname': obom.descriptor.configname, 

             'pcbname': obom.descriptor.pcbname, 

             'lines': obom.lines} 

    for config in obom.descriptor.configurations.configurations: 

        if config['configname'] == configname: 

            stage['desc'] = config['desc'] 

 

    template = 'geda-bom-simple.tex' 

 

    workspace_outpath = workspace_fs.getsyspath(outpath) 

    workspace_fs.makedir(path.dirname(outpath), 

                         recursive=True, allow_recreate=True) 

    render.render_pdf(stage, template, workspace_outpath) 

    copyfile(workspace_fs, outpath, refdoc_fs, outpath, overwrite=True) 

 

    return outpath 

 

 

def gen_configdoc(projfolder, namebase): 

    """ 

    Generate a PDF documenting the configs of the project. The document should 

    include a reasonably thorough representation of the contents of the 

    configuration related sections of the 

    ``tendril.gedaif.conffile.ConfigsFile``. 

 

    .. todo:: Implement this. 

 

    :param projfolder: The gEDA project folder. 

    :type projfolder: str 

    :param namebase: The project name. 

    :type namebase: str 

    :return: The output file path. 

 

    .. rubric:: Paths 

 

    * Output File :  ``<project_doc_folder>/<namebase>-configs.pdf`` 

    * Source Files : The project's schematic folder. 

 

    """ 

    pass 

 

 

def gen_schpdf(projfolder, namebase): 

    """ 

    Generates a PDF file of all the project schematics listed in the 

    gEDA project file. This function does not ise jinja2 and latex. It 

    relies on :func:`tendril.gedaif.gschem.conv_gsch2pdf` instead. 

 

    :param projfolder: The gEDA project folder. 

    :type projfolder: str 

    :param namebase: The project name. 

    :type namebase: str 

    :return: The output file path. 

 

    .. rubric:: Paths 

 

    * Output File :  ``<project_doc_folder>/<namebase>-schematic.pdf`` 

    * Source Files : The project's schematic folder. 

 

    """ 

    gpf = projfile.GedaProjectFile(projfolder) 

    sch_mtime = fsutils.get_folder_mtime(gpf.schfolder) 

 

    configfile = conffile.ConfigsFile(projfolder) 

    docfolder = get_project_doc_folder(projfolder) 

 

    schpdfpath = path.join(docfolder, namebase + '-schematic.pdf') 

    outf_mtime = fsutils.get_file_mtime(schpdfpath, fs=refdoc_fs) 

 

    if outf_mtime is not None and outf_mtime > sch_mtime: 

        logger.debug('Skipping up-to-date ' + schpdfpath) 

        return schpdfpath 

 

    logger.info('Regenerating ' + schpdfpath + os.linesep + 

                'Last modified : ' + str(sch_mtime) + 

                '; Last Created : ' + str(outf_mtime)) 

 

    if configfile.configdata is not None: 

        workspace_outpath = workspace_fs.getsyspath(schpdfpath) 

        workspace_folder = workspace_fs.getsyspath(path.dirname(schpdfpath)) 

        workspace_fs.makedir(path.dirname(schpdfpath), 

                             recursive=True, allow_recreate=True) 

        pdffiles = [] 

        for schematic in gpf.schfiles: 

            schfile = os.path.normpath(projfolder + '/schematic/' + schematic) 

            pdffile = gschem.conv_gsch2pdf(schfile, workspace_folder) 

            pdffiles.append(pdffile) 

        pdf.merge_pdf(pdffiles, workspace_outpath) 

        for pdffile in pdffiles: 

            os.remove(pdffile) 

        copyfile(workspace_fs, schpdfpath, 

                 refdoc_fs, schpdfpath, 

                 overwrite=True) 

        return schpdfpath 

 

 

def gen_masterdoc(projfolder, namebase): 

    """ 

    Generates a PDF file of the project's Master documentation. It uses 

    other document generator functions to make the various parts of the 

    master document and then merges them. 

 

    .. note:: Due to the way groups and motifs are handled, an 

              unconfigured BOM is somewhat meaningless. Therefore, 

              no BOM is included in the masterdoc. 

 

    :param projfolder: The gEDA project folder. 

    :type projfolder: str 

    :param namebase: The project name. 

    :type namebase: str 

    :return: The output file path. 

 

    .. rubric:: Paths 

 

    * Output File :  ``<project_doc_folder>/<namebase>-masterdoc.pdf`` 

    * Source Files : The project's schematic folder. 

 

    .. rubric:: Included Documents 

 

    * Config Documentation, generated by :func:`gen_configdoc` 

    * Schematic PDF, generated by :func:`gen_schpdf` 

 

    """ 

    gpf = projfile.GedaProjectFile(projfolder) 

    sch_mtime = fsutils.get_folder_mtime(gpf.schfolder) 

 

    docfolder = get_project_doc_folder(projfolder) 

    masterdocfile = path.join(docfolder, namebase + '-masterdoc.pdf') 

    outf_mtime = fsutils.get_file_mtime(masterdocfile, fs=refdoc_fs) 

 

    if outf_mtime is not None and outf_mtime > sch_mtime: 

        logger.debug('Skipping up-to-date ' + masterdocfile) 

        return masterdocfile 

 

    logger.info('Regnerating ' + masterdocfile + os.linesep + 

                'Last modified : ' + str(sch_mtime) + 

                '; Last Created : ' + str(outf_mtime)) 

 

    pdffiles = [gen_configdoc(projfolder, namebase), 

                gen_schpdf(projfolder, namebase)] 

 

    for p in pdffiles: 

        if p and not workspace_fs.exists(p): 

            workspace_fs.makedir(path.dirname(p), 

                                 recursive=True, allow_recreate=True) 

            copyfile(refdoc_fs, p, workspace_fs, p) 

 

    workspace_pdffiles = [workspace_fs.getsyspath(x) 

                          for x in pdffiles if x is not None] 

 

    workspace_outpath = workspace_fs.getsyspath(masterdocfile) 

    workspace_fs.makedir(path.dirname(masterdocfile), 

                         recursive=True, allow_recreate=True) 

    pdf.merge_pdf(workspace_pdffiles, workspace_outpath) 

    copyfile(workspace_fs, masterdocfile, 

             refdoc_fs, masterdocfile, 

             overwrite=True) 

    return masterdocfile 

 

 

def gen_confpdf(projfolder, configname, namebase): 

    """ 

    Generates a PDF file of the documentation for a specific configuration 

    of a project. It uses other document generator functions to make the 

    various parts of the master document and then merges them. 

 

    :param projfolder: The gEDA project folder. 

    :type projfolder: str 

    :param configname: The name of the configuration. 

    :type configname: str 

    :param namebase: The project name. 

    :type namebase: str 

    :return: The output file path. 

 

    .. rubric:: Paths 

 

    * Output File :  ``<project_doc_folder>/confdocs/<configname>-doc.pdf`` 

    * Source Files : The project's schematic folder. 

 

    .. rubric:: Included Documents 

 

    * Configuration BOM, generated by :func:`gen_confbom` 

    * (Full) Schematic PDF, generated by :func:`gen_schpdf` 

 

    .. todo:: It may be useful to rebuild the schematics after removing 

              all the unpopulated components. This is a fairly involved 

              process, and is deferred until later. 

 

    """ 

    gpf = projfile.GedaProjectFile(projfolder) 

    sch_mtime = fsutils.get_folder_mtime(gpf.schfolder) 

 

    docfolder = get_project_doc_folder(projfolder) 

    confdocfile = path.join(docfolder, 'confdocs', configname + '-doc.pdf') 

    outf_mtime = fsutils.get_file_mtime(confdocfile, fs=refdoc_fs) 

 

    if outf_mtime is not None and outf_mtime > sch_mtime: 

        logger.debug('Skipping up-to-date ' + confdocfile) 

        return confdocfile 

 

    logger.info('Regenerating ' + confdocfile + os.linesep + 

                'Last modified : ' + str(sch_mtime) + 

                '; Last Created : ' + str(outf_mtime)) 

 

    pdffiles = [gen_confbom(projfolder, configname), 

                gen_schpdf(projfolder, namebase)] 

 

    for p in pdffiles: 

        if p and not workspace_fs.exists(p): 

            workspace_fs.makedir(path.dirname(p), 

                                 recursive=True, allow_recreate=True) 

            copyfile(refdoc_fs, p, workspace_fs, p) 

 

    workspace_pdffiles = [workspace_fs.getsyspath(x) 

                          for x in pdffiles if x is not None] 

 

    workspace_outpath = workspace_fs.getsyspath(confdocfile) 

    workspace_fs.makedir(path.dirname(confdocfile), 

                         recursive=True, allow_recreate=True) 

    pdf.merge_pdf(workspace_pdffiles, workspace_outpath) 

    copyfile(workspace_fs, confdocfile, 

             refdoc_fs, confdocfile, 

             overwrite=True) 

    return confdocfile 

 

 

def gen_cobom_csv(projfolder, namebase): 

    """ 

    Generates a CSV file in the 

    :mod:`tendril.boms.outputbase.CompositeOutputBom` format, including the 

    BOMs of the all the defined configurations of the project. This function 

    uses a :mod:`csv.writer` instead of rendering a jinja2 template. 

 

    It also generates configdocs for all the defined configurations of the 

    project, using :func:`gen_confpdf`. 

 

    :param projfolder: The gEDA project folder. 

    :type projfolder: str 

    :param namebase: The project name. 

    :type namebase: str 

    :return: The output file path. 

 

    .. rubric:: Paths 

 

    * Output Files :  ``<project_doc_folder>/confdocs/conf_boms.csv`` 

    * Also triggers : :func:`gen_confpdf` for all listed configurations. 

    * Source Files : The project's schematic folder. 

 

    """ 

    gpf = projfile.GedaProjectFile(projfolder) 

    configfile = conffile.ConfigsFile(projfolder) 

    sch_mtime = fsutils.get_folder_mtime(gpf.schfolder) 

 

    docfolder = get_project_doc_folder(projfolder) 

    cobom_csv_path = path.join(docfolder, 'confdocs', 'conf-boms.csv') 

    outf_mtime = fsutils.get_file_mtime(cobom_csv_path, fs=refdoc_fs) 

 

    if outf_mtime is not None and outf_mtime > sch_mtime: 

        logger.debug('Skipping up-to-date ' + cobom_csv_path) 

        return cobom_csv_path 

 

    logger.info('Regenerating ' + cobom_csv_path + os.linesep + 

                'Last modified : ' + str(sch_mtime) + 

                '; Last Created : ' + str(outf_mtime)) 

 

    bomlist = [] 

    for cfn in configfile.configdata['configurations']: 

        gen_confpdf(projfolder, cfn['configname'], namebase) 

        lbom = boms_electronics.import_pcb(projfolder) 

        lobom = lbom.create_output_bom(cfn['configname']) 

        bomlist.append(lobom) 

    cobom = boms_outputbase.CompositeOutputBom(bomlist) 

 

    with refdoc_fs.open(cobom_csv_path, 'wb') as f: 

        writer = csv.writer(f) 

        writer.writerow(['device'] + 

                        [x.configname for x in cobom.descriptors]) 

        for line in cobom.lines: 

            writer.writerow([line.ident] + line.columns) 

 

 

def gen_pcb_pdf(projfolder): 

    """ 

    Generates a PDF file of the PCB layers for the PCB provided by the 

    gEDA project. 

 

    The pcb file is the one listed in the gEDA project file, and the 

    pcbname is the one specified in the 

    :mod:`tendril.gedaif.conffile.ConfigsFile`. 

 

    This function does not use jinja2 and latex. It relies on 

    :func:`tendril.gedaif.pcb.conv_pcb2pdf` instead. 

 

    :param projfolder: The gEDA project folder. 

    :type projfolder: str 

    :return: The output file path. 

 

    .. rubric:: Paths 

 

    * Output File :  ``<project_doc_folder>/<pcbname>-pdf.pdf`` 

    * Source Files : The project's `.pcb` file. 

 

    """ 

    configfile = conffile.ConfigsFile(projfolder) 

    gpf = projfile.GedaProjectFile(configfile.projectfolder) 

    pcb_mtime = fsutils.get_file_mtime( 

        os.path.join(configfile.projectfolder, 'pcb', gpf.pcbfile + '.pcb') 

    ) 

    docfolder = get_project_doc_folder(projfolder) 

    pdffile = path.join(docfolder, 

                        configfile.configdata['pcbname'] + '-pcb.pdf') 

    outf_mtime = fsutils.get_file_mtime(pdffile, fs=refdoc_fs) 

 

    if outf_mtime is not None and outf_mtime > pcb_mtime: 

        logger.debug('Skipping up-to-date ' + pdffile) 

        return pdffile 

 

    logger.info('Regenerating ' + pdffile + os.linesep + 

                'Last modified : ' + str(pcb_mtime) + 

                '; Last Created : ' + str(outf_mtime)) 

 

    workspace_folder = workspace_fs.getsyspath(path.dirname(pdffile)) 

    workspace_fs.makedir(path.dirname(pdffile), 

                         recursive=True, allow_recreate=True) 

 

    pcb.conv_pcb2pdf( 

        os.path.join(configfile.projectfolder, 'pcb', gpf.pcbfile + '.pcb'), 

        workspace_folder, configfile.configdata['pcbname'] 

    ) 

 

    copyfile(workspace_fs, pdffile, refdoc_fs, pdffile, overwrite=True) 

    return pdffile 

 

 

def gen_pcb_gbr(projfolder): 

    """ 

    Generates gerber files for the PCB provided by the gEDA project, and also 

    creates a ``zip`` file of the generated gerbers. 

 

    The pcbfile is the one listed in the gEDA project file, and the 

    pcbname is the one specified in the 

    :mod:`tendril.gedaif.conffile.ConfigsFile`. 

 

    This function does not use jinja2 and latex. It relies on 

    :func:`tendril.gedaif.pcb.conv_pcb2gbr` instead. 

 

    The generated gerber files are retained in the source tree instead of 

    being moved to the refdoc filesystem due to the relative sensitivity of 

    gerber files to version mismatches. 

 

    :param projfolder: The gEDA project folder. 

    :type projfolder: str 

    :return: The output file path. 

 

    .. rubric:: Paths 

 

    * Output Files :  ``<projfolder>/gerber/*`` 

    * Output Zip File : ``<projfolder>/<pcbfile>-gerber.zip`` 

    * Source Files : The project's `.pcb` file. 

 

    """ 

    configfile = conffile.ConfigsFile(projfolder) 

    gpf = projfile.GedaProjectFile(configfile.projectfolder) 

    pcb_mtime = fsutils.get_file_mtime( 

        os.path.join(configfile.projectfolder, 'pcb', gpf.pcbfile + '.pcb') 

    ) 

    gbrfolder = os.path.join(configfile.projectfolder, 'gerber') 

    outf_mtime = None 

    if not os.path.exists(gbrfolder): 

        os.makedirs(gbrfolder) 

    else: 

        outf_mtime = fsutils.get_folder_mtime(gbrfolder) 

 

    if outf_mtime is not None and outf_mtime > pcb_mtime: 

        logger.debug('Skipping up-to-date ' + gbrfolder) 

        return gbrfolder 

 

    logger.info('Regenerating ' + gbrfolder + os.linesep + 

                'Last modified : ' + str(pcb_mtime) + 

                '; Last Created : ' + str(outf_mtime)) 

    glb = os.path.join(configfile.projectfolder, 'gerber', '*') 

    rf = glob.glob(glb) 

    for f in rf: 

        os.remove(f) 

    gbrfolder = pcb.conv_pcb2gbr( 

        os.path.join(configfile.projectfolder, 'pcb', gpf.pcbfile + '.pcb') 

    ) 

    zfile = os.path.join(projfolder, gpf.pcbfile + '-gerber.zip') 

    fsutils.zipdir(gbrfolder, zfile) 

    return gbrfolder 

 

 

def gen_pcb_dxf(projfolder): 

    """ 

    Generates a DXF file of the PCB provided by the gEDA project. 

 

    The pcb file is the one listed in the gEDA project file, and the 

    pcbname is the one specified in the 

    :mod:`tendril.gedaif.conffile.ConfigsFile`. 

 

    This function does not use jinja2 and latex. It relies on 

    :func:`tendril.gedaif.pcb.conv_pcb2dxf` instead. 

 

    The generated DXF file is retained in the source tree instead of 

    being moved to the refdoc filesystem since is it not just 

    expositional, and is a source file for other processes. 

 

    :param projfolder: The gEDA project folder. 

    :type projfolder: str 

    :return: The output file path. 

 

    .. rubric:: Paths 

 

    * Output File :  ``<projectfolder>/pcb/<pcbfile>.dxf`` 

    * Source Files : The project's `.pcb` file. 

 

    """ 

    configfile = conffile.ConfigsFile(projfolder) 

    gpf = projfile.GedaProjectFile(configfile.projectfolder) 

    pcb_mtime = fsutils.get_file_mtime( 

        os.path.join(configfile.projectfolder, 'pcb', gpf.pcbfile + '.pcb') 

    ) 

    dxffile = os.path.join(configfile.projectfolder, 'pcb', 

                           gpf.pcbfile + '.dxf') 

    outf_mtime = fsutils.get_file_mtime(dxffile) 

 

    if outf_mtime is not None and outf_mtime > pcb_mtime: 

        logger.debug('Skipping up-to-date ' + dxffile) 

        return dxffile 

 

    logger.info('Regenerating ' + dxffile + os.linesep + 

                'Last modified : ' + str(pcb_mtime) + 

                '; Last Created : ' + str(outf_mtime)) 

    dxffile = pcb.conv_pcb2dxf( 

        os.path.join(configfile.projectfolder, 'pcb', gpf.pcbfile + '.pcb'), 

        configfile.configdata['pcbname'] 

    ) 

    return dxffile 

 

 

def gen_pcbpricing(projfolder, namebase): 

    """ 

    Generates a PDF file with the pricing of the (bare) PCB provided by the 

    gEDA project. 

 

    The pcb file is the one listed in the gEDA project file, and the 

    pcbname is the one specified in the 

    :mod:`tendril.gedaif.conffile.ConfigsFile`. The pricing information is 

    read out from the PCB's ``sourcing.yaml`` file, which in turn is intended 

    to be created by sourcing modules. 

 

    .. todo:: This function presently uses 

              :func:`tendril.dox.render.render_lineplot`, which is marked for 

              deprecation. It should be rewritten to use the 

              :func:`tendril.dox.render.make_graph` route instead. 

 

    :param projfolder: The gEDA project folder. 

    :type projfolder: str 

    :param namebase: The project name. 

    :type namebase: str 

    :return: The output file path. 

 

    .. rubric:: Paths 

 

    * Output File :  ``<project_doc_folder>/<namebase>-pricing.pdf`` 

    * Source Files : ``<projectfolder>/pcb/sourcing.yaml`` 

 

    """ 

    gpf = projfile.GedaProjectFile(projfolder) 

    pcbpricingfp = os.path.join( 

        gpf.configsfile.projectfolder, 'pcb', 'sourcing.yaml' 

    ) 

    pcbpricing_mtime = fsutils.get_file_mtime(pcbpricingfp) 

 

    if not os.path.exists(pcbpricingfp): 

        return None 

 

    docfolder = get_project_doc_folder(projfolder) 

    plotfile = path.join(docfolder, namebase + '-pricing.pdf') 

    outf_mtime = fsutils.get_file_mtime(plotfile, fs=refdoc_fs) 

 

    if outf_mtime is not None and outf_mtime > pcbpricing_mtime: 

        logger.debug('Skipping up-to-date ' + pcbpricingfp) 

        return pcbpricingfp 

 

    logger.info('Regnerating ' + plotfile + os.linesep + 

                'Last modified : ' + str(pcbpricing_mtime) + 

                '; Last Created : ' + str(outf_mtime)) 

 

    with open(pcbpricingfp, 'r') as f: 

        data = yaml.load(f) 

 

    workspace_outpath = workspace_fs.getsyspath(plotfile) 

    workspace_folder = workspace_fs.getsyspath(path.dirname(plotfile)) 

    workspace_fs.makedir(path.dirname(plotfile), 

                         recursive=True, allow_recreate=True) 

 

    plot1file = os.path.join(workspace_folder, namebase + '-1pricing.pdf') 

    plot2file = os.path.join(workspace_folder, namebase + '-2pricing.pdf') 

 

    pltnote = "This pricing refers to the bare PCB only. " \ 

              "See the corresponding Config Docs for Card Pricing" 

 

    plt1data = {key: data['pricing'][key] 

                for key in data['pricing'].keys() if key <= 10} 

    plt1title = gpf.configsfile.configdata['pcbname'] 

    plt1title += " PCB Unit Price vs Order Quantity (Low Quantity)" 

    plot1file = render.render_lineplot( 

        plot1file, plt1data, plt1title, pltnote 

    ) 

 

    if max(data['pricing'].keys()) > 10: 

        plt2data = {key: data['pricing'][key] 

                    for key in data['pricing'].keys() if key > 10} 

        plt2title = gpf.configsfile.configdata['pcbname'] 

        plt2title += " PCB Unit Price vs Order Quantity (Production Quantity)" 

        plot2file = render.render_lineplot( 

            plot2file, plt2data, plt2title, pltnote 

        ) 

        pdf.merge_pdf([plot1file, plot2file], workspace_outpath) 

        os.remove(plot2file) 

    else: 

        shutil.copyfile(plot1file, workspace_outpath) 

    os.remove(plot1file) 

    copyfile(workspace_fs, plotfile, refdoc_fs, plotfile, overwrite=True) 

    return plotfile 

 

 

def generate_docs(projfolder): 

    """ 

    Generates all the docs for a specified gEDA project. 

 

    :param projfolder: The gEDA project folder. 

    :type projfolder: str 

    :return: The output file path. 

 

    .. rubric:: Paths 

 

    * Output File :  ``<project_doc_folder>/confdocs/<configname>-doc.pdf`` 

    * Source Files : The project's schematic folder. 

 

    .. rubric:: Generated Documents 

 

    * Master Doc, generated by :func:`gen_masterdoc` 

    * Cobom CSV, generated by :func:`gen_cobom_csv` 

    * PCB PDF, generated by :func:`gen_pcb_pdf` 

    * PCB Gerber, generated by :func:`gen_pcb_gbr` 

    * PCB DXF, generated by :func:`gen_pcb_dxf` 

    * PCB Pricing, generated by :func:`gen_pcbpricing` 

 

    """ 

    configfile = conffile.ConfigsFile(projfolder) 

    namebase = configfile.configdata['pcbname'] 

    if namebase is None: 

        try: 

            namebase = configfile.configdata['cblname'] 

        except KeyError: 

            logger.error("Project does not have a known identifier. " 

                         "Skipping : " + projfolder) 

            return 

    gen_masterdoc(projfolder, namebase) 

    gen_cobom_csv(projfolder, namebase) 

    if configfile.configdata['pcbname'] is not None: 

        gen_pcb_pdf(projfolder) 

        gen_pcb_gbr(projfolder) 

        gen_pcb_dxf(projfolder) 

        gen_pcbpricing(projfolder, namebase) 

 

 

def get_docs_list(projfolder, cardname=None): 

    configfile = conffile.ConfigsFile(projfolder) 

    namebase = configfile.configdata['pcbname'] 

    is_cable = False 

    if namebase is None: 

        try: 

            namebase = configfile.configdata['cblname'] 

            is_cable = True 

        except KeyError: 

            logger.error("Project does not have a known identifier. " 

                         "Skipping : " + projfolder) 

            return 

    project_doc_folder = get_project_doc_folder(projfolder) 

    if not cardname: 

        # Get all docs linked to the project 

        rval = [ExposedDocument('Project Master Doc', 

                                path.join(project_doc_folder, 

                                          namebase + '-masterdoc.pdf'), 

                                refdoc_fs), 

                ExposedDocument(namebase + ' Schematic (Full)', 

                                path.join(project_doc_folder, 

                                          namebase + '-schematic.pdf'), 

                                refdoc_fs), 

                ExposedDocument('Composite Bom (All Configs)', 

                                path.join(project_doc_folder, 

                                          'confdocs', 

                                          'conf-boms.csv'), 

                                refdoc_fs), 

                ] 

        if is_cable: 

            return rval 

        gpf = projfile.GedaProjectFile(configfile.projectfolder) 

        rval.extend([ExposedDocument(namebase + ' PCB Layers', 

                                     path.join(project_doc_folder, 

                                               namebase + '-pcb.pdf'), 

                                     refdoc_fs), 

                     ExposedDocument(namebase + ' PCB Pricing', 

                                     path.join(project_doc_folder, 

                                               namebase + '-pricing.pdf'), 

                                     refdoc_fs), 

                     # TODO This needs to be fixed. 

                     ExposedDocument(namebase + ' PCB DXF', 

                                     path.join(projfolder, 'pcb', 

                                               gpf.pcbfile + '.dxf'), 

                                     refdoc_fs), 

                     # TODO This needs to be fixed. 

                     ExposedDocument(namebase + ' PCB Gerber', 

                                     path.join(projfolder, 

                                               gpf.pcbfile + '-gerber.zip'), 

                                     refdoc_fs), 

                     ]) 

        return rval 

    else: 

        cardname = cardname.strip() 

        rval = [ExposedDocument(cardname + ' Doc', 

                                path.join(project_doc_folder, 

                                          'confdocs', cardname + '-doc.pdf'), 

                                refdoc_fs), 

                ExposedDocument(cardname + ' Reference BOM', 

                                path.join(project_doc_folder, 

                                          'confdocs', cardname + '-bom.pdf'), 

                                refdoc_fs), 

                ExposedDocument(cardname + ' Schematic (Full)', 

                                path.join(project_doc_folder, 

                                          namebase + '-schematic.pdf'), 

                                refdoc_fs), 

                ExposedDocument('Composite Bom (All Configs)', 

                                path.join(project_doc_folder, 

                                          'confdocs', 

                                          'conf-boms.csv'), 

                                refdoc_fs), 

                ExposedDocument('Project Master Doc', 

                                path.join(project_doc_folder, 

                                          namebase + '-masterdoc.pdf'), 

                                refdoc_fs), 

                ] 

        return rval