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

from __future__ import absolute_import 

 

import re 

import logging 

import itertools 

import os 

import webob as wo 

import pkg_resources as pr 

import mimetypes 

import inspect 

import warnings 

import wsgiref.util 

 

from .widgets import Widget 

from .util import MultipleReplacer 

import tw2.core.core 

from .params import Param, Variable, ParameterError, Required 

from .middleware import register_resource 

from .js import encoder, js_symbol 

 

from markupsafe import Markup 

import six 

 

log = logging.getLogger(__name__) 

 

 

# TBD is there a better place to put this? 

mimetypes.init() 

mimetypes.types_map['.ico'] = 'image/x-icon' 

 

 

class JSSymbol(js_symbol): 

    """ Deprecated compatibility shim with old TW2 stuff.  Use js_symbol. """ 

 

    def __init__(self, *args, **kw): 

        warnings.warn("JSSymbol is deprecated.  Please use js_symbol") 

 

        if len(args) > 1: 

            raise ValueError("JSSymbol must receive up to only one arg.") 

 

        if len(args) == 1 and 'src' in kw: 

            raise ValueError("JSSymbol must receive only one src arg.") 

 

        if len(args) == 1: 

            kw['src'] = args[0] 

 

        super(JSSymbol, self).__init__(**kw) 

 

        # Backwards compatibility for accessing the source. 

        self.src = self._name 

 

 

class ResourceBundle(Widget): 

    """ Just a list of resources. 

 

    Use it as follows: 

 

        >>> jquery_ui = ResourceBundle(resources=[jquery_js, jquery_css]) 

        >>> jquery_ui.inject() 

 

    """ 

 

    @classmethod 

    def inject(cls): 

        cls.req().prepare() 

 

    def prepare(self): 

        super(ResourceBundle, self).prepare() 

 

        rl = tw2.core.core.request_local() 

        rl_resources = rl.setdefault('resources', []) 

        rl_location = rl['middleware'].config.inject_resources_location 

 

        if self not in rl_resources: 

            for r in self.resources: 

                r.req().prepare() 

 

 

class Resource(ResourceBundle): 

    location = Param( 

        'Location on the page where the resource should be placed.' \ 

        'This can be one of: head, headbottom, bodytop or bodybottom. '\ 

        'None means the resource will not be injected, which is still '\ 

        'useful, e.g. static images.', default=None) 

    id = None 

    template = None 

 

    def prepare(self): 

        super(Resource, self).prepare() 

 

        rl = tw2.core.core.request_local() 

        rl_resources = rl.setdefault('resources', []) 

        rl_location = rl['middleware'].config.inject_resources_location 

 

        if self not in rl_resources: 

            if self.location is '__use_middleware': 

                self.location = rl_location 

 

            rl_resources.append(self) 

 

 

class Link(Resource): 

    ''' 

    A link to a file. 

    ''' 

    id = None 

    link = Param( 

        'Direct web link to file. If this is not specified, it is ' + 

        'automatically generated, based on :attr:`modname` and ' + 

        ':attr:`filename`.', 

    ) 

    modname = Param( 

        'Name of Python module that contains the file.', 

        default=None, 

    ) 

    filename = Param( 

        'Path to file, relative to module base.', 

        default=None, 

    ) 

    no_inject = Param( 

        "Don't inject this link. (Default: False)", 

        default=False, 

    ) 

    whole_dir = Param( 

        "Make the whole directory available.  (Default: False)", 

        default=False, 

    ) 

 

    @classmethod 

    def guess_modname(cls): 

        """ Try to guess my modname. 

 

        If I wasn't supplied any modname, take a guess by stepping back up the 

        frame stack until I find something not in tw2.core 

        """ 

 

        try: 

            frame, i = inspect.stack()[0][0], 0 

            while frame.f_globals['__name__'].startswith('tw2.core'): 

                frame, i = inspect.stack()[i][0], i + 1 

 

            return frame.f_globals['__name__'] 

        except Exception: 

            return None 

 

    @classmethod 

    def post_define(cls): 

 

        if not cls.no_inject: 

            if getattr(cls, 'filename', None) and \ 

               type(cls.filename) != property: 

 

                if not cls.modname: 

                    cls.modname = cls.guess_modname() 

 

                register_resource( 

                    cls.modname or '__anon__', cls.filename, cls.whole_dir 

                ) 

 

    def prepare(self): 

        rl = tw2.core.core.request_local() 

        if not self.no_inject: 

            if not hasattr(self, 'link'): 

                # TBD shouldn't we test for this in __new__ ? 

                if not self.filename: 

                    raise ParameterError( 

                        "Either 'link' or 'filename' must be specified" 

                    ) 

                resources = rl['middleware'].resources 

                self.link = resources.resource_path( 

                    self.modname or '__anon__', self.filename 

                ) 

            super(Link, self).prepare() 

 

    def __hash__(self): 

        return hash( 

            hasattr(self, 'link') and \ 

            self.link or \ 

            ((self.modname or '') + self.filename) 

        ) 

 

    def __eq__(self, other): 

        return (isinstance(other, Link) and self.link == other.link 

            and self.modname == other.modname 

            and self.filename == other.filename) 

 

    def __repr__(self): 

        return "%s('%s')" % ( 

            self.__class__.__name__, 

            getattr(self, 'link', '%s/%s' % (self.modname, self.filename)) 

        ) 

 

 

class DirLink(Link): 

    ''' A whole directory as a resource. 

 

    Unlike :class:`JSLink` and :class:`CSSLink`, this resource doesn't inject 

    anything on the page.. but it does register all resources under the 

    marked directory to be served by the middleware. 

 

    This is useful if you have a css file that pulls in a number of other 

    static resources like icons and images. 

    ''' 

    link = Variable() 

    filename = Required 

    whole_dir = True 

 

    def prepare(self): 

        resources = tw2.core.core.request_local()['middleware'].resources 

        self.link = resources.resource_path( 

            self.modname, 

            self.filename, 

        ) 

 

 

class JSLink(Link): 

    ''' 

    A JavaScript source file. 

    ''' 

    location = '__use_middleware' 

    template = 'tw2.core.templates.jslink' 

 

 

class CSSLink(Link): 

    ''' 

    A CSS style sheet. 

    ''' 

    media = Param('Media tag', default='all') 

    location = 'head' 

    template = 'tw2.core.templates.csslink' 

 

 

class JSSource(Resource): 

    """ 

    Inline JavaScript source code. 

    """ 

    src = Param('Source code', default=None) 

    location = 'bodybottom' 

    template = 'tw2.core.templates.jssource' 

 

    def __eq__(self, other): 

        return isinstance(other, JSSource) and self.src == other.src 

 

    def __repr__(self): 

        return "%s('%s')" % (self.__class__.__name__, self.src) 

 

    def prepare(self): 

        super(JSSource, self).prepare() 

        if not self.src: 

            raise ValueError("%r must be provided a 'src' attr" % self) 

        self.src = Markup(self.src) 

 

 

class CSSSource(Resource): 

    """ 

    Inline Cascading Style-Sheet code. 

    """ 

    src = Param('CSS code', default=None) 

    location = 'head' 

    template = 'tw2.core.templates.csssource' 

 

    def __eq__(self, other): 

        return isinstance(other, CSSSource) and self.src == other.src 

 

    def __repr__(self): 

        return "%s('%s')" % (self.__class__.__name__, self.src) 

 

    def prepare(self): 

        super(CSSSource, self).prepare() 

        if not self.src: 

            raise ValueError("%r must be provided a 'src' attr" % self) 

        self.src = Markup(self.src) 

 

 

class _JSFuncCall(JSSource): 

    """ 

    Internal use inline JavaScript function call. 

 

    Please use tw2.core.js_function(...) externally. 

    """ 

    src = None 

    function = Param('Function name', default=None) 

    args = Param('Function arguments', default=None) 

    location = 'bodybottom'  # TBD: afterwidget? 

 

    def __str__(self): 

        if not self.src: 

            self.prepare() 

        return self.src 

 

    def prepare(self): 

        if not self.src: 

            args = '' 

            if isinstance(self.args, dict): 

                args = encoder.encode(self.args) 

            elif self.args: 

                args = ', '.join(encoder.encode(a) for a in self.args) 

 

            self.src = '%s(%s)' % (self.function, args) 

        super(_JSFuncCall, self).prepare() 

 

    def __hash__(self): 

        if self.args: 

            if isinstance(self.args, dict): 

                sargs = encoder.encode(self.args) 

            else: 

                sargs = ', '.join(encoder.encode(a) for a in self.args) 

        else: 

            sargs = None 

 

        return hash((hasattr(self, 'src') and self.src or '') + (sargs or '')) 

 

    def __eq__(self, other): 

        return (getattr(self, 'src', None) == getattr(other, 'src', None) 

                and getattr(self, 'args', None) == getattr(other, 'args', None) 

                ) 

 

 

class ResourcesApp(object): 

    """WSGI Middleware to serve static resources 

 

    This handles URLs like this: 

        /resources/tw2.forms/static/forms.css 

 

    Where: 

        resources       is the prefix 

        tw2.forms       is a python package name 

        static          is a directory inside the package 

        forms.css       is the file to retrieve 

 

    For this to work, the file must have been registered in advance, 

    using :meth:`register`. There is a ResourcesApp instance for each 

    TwMiddleware instance. 

    """ 

 

    def __init__(self, config): 

        self._paths = {} 

        self._dirs = [] 

        self.config = config 

 

    def register(self, modname, filename, whole_dir=False): 

        """ Register a file for static serving. 

 

        After this method has been called, for say ('tw2.forms', 

        'static/forms.css'), the URL /resources/tw2.forms/static/forms.css will 

        then serve that file from within the tw2.forms package. This works 

        correctly for zipped eggs. 

 

        *Security Consideration* - This file will be readable by users of the 

        application, so make sure it contains no confidential data. For 

        DirLink resources, the whole directory, and subdirectories will be 

        readable. 

 

        `modname` 

            The python module that contains the file to publish. You can also 

            pass a pkg_resources.Requirement instance to point to the root of 

            an egg distribution. 

 

        `filename` 

            The path, relative to the base of the module, of the file to be 

            published. If *modname* is None, it's an absolute path. 

        """ 

        if isinstance(modname, pr.Requirement): 

            modname = os.path.basename(pr.working_set.find(modname).location) 

 

        path = modname + '/' + filename.lstrip('/') 

 

        if whole_dir: 

            if path not in self._dirs: 

                self._dirs.append(path) 

        else: 

            if path not in self._paths: 

                self._paths[path] = (modname, filename) 

 

    def resource_path(self, modname, filename): 

        """ Return a resource's web path. """ 

 

        if isinstance(modname, pr.Requirement): 

            modname = os.path.basename(pr.working_set.find(modname).location) 

 

        path = modname + '/' + filename.lstrip('/') 

        return self.config.script_name + self.config.res_prefix + path 

 

    def __call__(self, environ, start_response): 

        req = wo.Request(environ) 

        try: 

            path = environ['PATH_INFO'] 

            path = path[len(self.config.res_prefix):] 

 

            if path not in self._paths: 

                if '..' in path:  # protect against directory traversal 

                    raise IOError() 

                for d in self._dirs: 

                    if path.startswith(d.replace('\\', '/')): 

                        break 

                else: 

                    raise IOError() 

            modname, filename = path.lstrip('/').split('/', 1) 

            ct, enc = mimetypes.guess_type(os.path.basename(filename)) 

            if modname and modname != '__anon__': 

                stream = pr.resource_stream(modname, filename) 

            else: 

                stream = open(filename) 

        except IOError: 

            resp = wo.Response(status="404 Not Found") 

        else: 

            stream = wsgiref.util.FileWrapper(stream, self.config.bufsize) 

            resp = wo.Response(app_iter=stream, content_type=ct) 

            if enc: 

                resp.content_type_params['charset'] = enc 

        resp.cache_control = {'max-age': int(self.config.res_max_age)} 

        return resp(environ, start_response) 

 

 

class _ResourceInjector(MultipleReplacer): 

    """ 

    ToscaWidgets can inject resources that have been registered for injection 

    in the current request. 

 

    Usually widgets register them when they're displayed and they have 

    instances of :class:`tw2.core.resources.Resource` declared at their 

    :attr:`tw2.core.Widget.javascript` or :attr:`tw2.core.Widget.css` 

    attributes. 

 

    Resources can also be registered manually from a controller or template by 

    calling their :meth:`tw2.core.resources.Resource.inject` method. 

 

    When a page including widgets is rendered, Resources that are registered 

    for injection are collected in a request-local storage area (this means 

    any thing stored here is only visible to one single thread of execution 

    and that its contents are freed when the request is finished) where they 

    can be rendered and injected in the resulting html. 

 

    ToscaWidgets' middleware can take care of injecting them automatically 

    (default) but they can also be injected explicitly, example:: 

 

       >>> from tw2.core.resources import JSLink, inject_resources 

       >>> JSLink(link="http://example.com").inject() 

       >>> html = "<html><head></head><body></body></html>" 

       >>> inject_resources(html) 

       '<html><head><script type="text/javascript" 

        src="http://example.com"></script></head><body></body></html>' 

 

    Once resources have been injected they are popped from request local and 

    cannot be injected again (in the same request). This is useful in case 

    :class:`injector_middleware` is stacked so it doesn't inject them again. 

 

    Injecting them explicitly is necessary if the response's body is being 

    cached before the middleware has a chance to inject them because when the 

    cached version is served no widgets are being rendered so they will not 

    have a chance to register their resources. 

    """ 

 

    def __init__(self): 

        return MultipleReplacer.__init__(self, { 

            r'<head(?!er).*?>': self._injector_for_location('head'), 

            r'</head(?!er).*?>': self._injector_for_location( 

                'headbottom', False 

            ), 

            r'<body.*?>': self._injector_for_location('bodytop'), 

            r'</body.*?>': self._injector_for_location('bodybottom', False) 

        }, re.I | re.M) 

 

    def _injector_for_location(self, key, after=True): 

        def inject(group, resources, encoding): 

            inj = six.u('\n').join([ 

                r.display(displays_on='string') 

                for r in resources 

                if r.location == key 

            ]) 

            if after: 

                return group + inj 

            return  inj + group 

        return inject 

 

    def __call__(self, html, resources=None, encoding=None): 

        """Injects resources, if any, into html string when called. 

 

        .. note:: 

           Ignore the ``self`` parameter if seeing this as 

           :func:`tw.core.resource_injector.inject_resources` docstring 

           since it is an alias for an instance method of a private class. 

 

        ``html`` must be a ``encoding`` encoded string. If ``encoding`` is not 

        given it will be tried to be derived from a <meta>. 

 

        """ 

        if resources is None: 

            resources = tw2.core.core.request_local().get('resources', None) 

        if resources: 

            encoding = encoding or find_charset(html) or 'utf-8' 

            html = MultipleReplacer.__call__( 

                self, html, resources, encoding 

            ) 

            tw2.core.core.request_local().pop('resources', None) 

        return html 

 

 

# Bind __call__ directly so docstring is included in docs 

inject_resources = _ResourceInjector().__call__ 

 

 

_charset_re = re.compile( 

    r"charset\s*=\s*(?P<charset>[\w-]+)([^\>])*", re.I | re.M) 

 

 

def find_charset(string): 

    m = _charset_re.search(string) 

    if m: 

        return m.group('charset').lower()