-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpycms-documentation.txt
More file actions
418 lines (316 loc) · 11.7 KB
/
pycms-documentation.txt
File metadata and controls
418 lines (316 loc) · 11.7 KB
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
Documentation for pycms
=======================
pycms is a lightweight web content management system.
Import
------
To use the pycms API, first import the pycms module.
>>> import pycms
>>>
Creating a new instance
-----------------------
An instance represents a hierarchy of pages, along with templates.
It is initialised with the path to a root directory. This is an
ordinary directory, with a subdirectory for templates.
>>> instance = pycms.Instance("pycmsroot")
>>>
Initialising files and directories
----------------------------------
pycms needs a working environment to store the CMS contents in.
For a new pycms instance, this working environment must be created
before pycms can be used. Otherwise an exception will be raised when
trying to run pycms.
>>> instance.serve(test = True)
Traceback (most recent call last):
...
RuntimeError: Working environment directory 'pycmsroot' not found. Did you run pycms.envinit("pycmsroot")?
>>>
The 'test' flag will terminate pycms after a short while, for
automated testing.
The envinit() method will take care of creating the environment.
>>> instance.envinit()
>>>
There now is a basic environment to work with. Let's take a look:
>>> import glob
>>> files = glob.glob("pycmsroot/*")
>>> files.sort()
>>> files
['pycmsroot/_templates', 'pycmsroot/_uri_template_map.json', 'pycmsroot/index.html', 'pycmsroot/static']
>>> glob.glob("pycmsroot/_templates/*")
['pycmsroot/_templates/index_template.html']
>>>
Adding templates
----------------
pycms templates are simple HTML files with placeholder lines.
To add a template, drop it into the '_templates' folder.
>>> with open("pycmsroot/_templates/new_template.html", "wt") as templatefile:
... templatefile.write('<!DOCTYPE html>\n<html>\n<meta charset="utf-8"/>\n<head>\n <title>\n TITLE\n </title>\n</head>\n<body>\n CONTENT\n</body>\n</html>')
...
135
>>> files = glob.glob("pycmsroot/_templates/*")
>>> files.sort()
>>> files
['pycmsroot/_templates/index_template.html', 'pycmsroot/_templates/new_template.html']
>>>
Creating pages based on templates
---------------------------------
pycms pages are tied to an URI and based on a template. To create a
new page, all of these must be given.
>>> instance.create_page("/test", "new_template.html")
>>>
Any trailing URI slash will be stripped before creating the page.
"/test/" and "/test" refer to the same object, though.
>>> instance.create_page("/test/", "new_template.html")
Traceback (most recent call last):
...
RuntimeError: URI "/test/" can not be created because "pycmsroot/test" already exists.
>>>
pycms uses the traditional index page convention to represent contents
on disk. The command will create a new directory and HTML file in the
working environment.
>>> glob.glob("pycmsroot/test/*")
['pycmsroot/test/index.html']
>>> with open("pycmsroot/test/index.html") as f:
... print(f.read())
<!DOCTYPE html>
<html>
<meta charset="utf-8"/>
<head>
<title>
TITLE
</title>
</head>
<body>
CONTENT
</body>
</html>
>>>
It also registers the URI in the template-URI map.
>>> with open("pycmsroot/_uri_template_map.json") as f:
... print(f.read())
{"/": "index_template.html", "/test": "new_template.html"}
>>>
Editing page content
--------------------
You can edit page content in the pycms instance's working environment
straight away with your favourite HTML editor.
pycms expects that you only change the placeholder elements of the
respective page template. Anything concerning the templates will
likely affect other pages as well, so you are expected to change
the template in that case -- see the next section for this.
>>> with open("pycmsroot/test/index.html", "rt") as f:
... html = f.read()
...
>>> html = html.replace("TITLE", "My first pycms page")
>>> html = html.replace("CONTENT", '<h1>My fist pycms page</h1>\n<p>This is my first pycms page.</p>')
>>> with open("pycmsroot/test/index.html", "wt") as f:
... f.write(html)
...
205
>>> with open("pycmsroot/test/index.html") as f:
... print(f.read())
<!DOCTYPE html>
<html>
<meta charset="utf-8"/>
<head>
<title>
My first pycms page
</title>
</head>
<body>
<h1>My fist pycms page</h1>
<p>This is my first pycms page.</p>
</body>
</html>
>>>
Editing templates and applying changes
--------------------------------------
pycms templates make it very convenient to change the style, layout
and repeating content on several pages.
pycms has to be able to track the changes you are about to make to
a template. So you first have to signal that you are going to edit
it.
>>> instance.edit_template("new_template.html")
>>>
This will create a backup of the current template, with the suffix
'.old' added.
>>> files = glob.glob("pycmsroot/_templates/*")
>>> files.sort()
>>> files
['pycmsroot/_templates/index_template.html', 'pycmsroot/_templates/new_template.html', 'pycmsroot/_templates/new_template.html.old']
>>>
You can now go ahead and edit the original template.
>>> with open("pycmsroot/_templates/new_template.html", "wt") as templatefile:
... templatefile.write('<!DOCTYPE html>\n<html>\n<meta charset="utf-8"/>\n<head>\n <title>\n TITLE\n </title>\n</head>\n<body>\n<hr>\n CONTENT\n<hr>\n</body>\n</html>')
...
145
After that, notify pycms that you are done updating the template. This
will apply pending changes to all pages using the template, and delete
any preceding versions of the template.
>>> instance.update()
>>> with open("pycmsroot/test/index.html") as f:
... print(f.read())
<!DOCTYPE html>
<html>
<meta charset="utf-8"/>
<head>
<title>
My first pycms page
</title>
</head>
<body>
<hr>
<h1>My fist pycms page</h1>
<p>This is my first pycms page.</p>
<hr>
</body>
</html>
>>> files = glob.glob("pycmsroot/_templates/*")
>>> files.sort()
>>> files
['pycmsroot/_templates/index_template.html', 'pycmsroot/_templates/new_template.html']
>>>
Removing a page
---------------
Removing a page is simple:
>>> instance.remove_page("/test")
>>>
Again, a trailing slash will be stripped:
>>> instance.remove_page("/test/")
Traceback (most recent call last):
...
RuntimeError: URI "/test/" can not be removed because "pycmsroot/test" does not exist.
>>>
On disk, the directory and the index page will be removed:
>>> files = glob.glob("pycmsroot/*")
>>> files.sort()
>>> files
['pycmsroot/_templates', 'pycmsroot/_uri_template_map.json', 'pycmsroot/index.html', 'pycmsroot/static']
The URI will also be removed from the template-URI map.
>>> with open("pycmsroot/_uri_template_map.json") as f:
... print(f.read())
{"/": "index_template.html"}
>>>
However the template used persists:
>>> files = glob.glob("pycmsroot/_templates/*")
>>> files.sort()
>>> files
['pycmsroot/_templates/index_template.html', 'pycmsroot/_templates/new_template.html']
>>>
Removing the root URI "/" will remove the index page only, not the
root directory.
>>> instance.remove_page("/")
>>> with open("pycmsroot/_uri_template_map.json") as f:
... print(f.read())
{}
>>> files = glob.glob("pycmsroot/*")
>>> files.sort()
>>> files
['pycmsroot/_templates', 'pycmsroot/_uri_template_map.json', 'pycmsroot/static']
>>>
This of course is a state one would not want to keep.
>>> instance.create_page("/", "index_template.html")
>>>
Running the pycms web server
----------------------------
Once there is a working environment, pycms can serve content from it.
The serve() method runs an HTTP server.
>>> instance.serve(test = True)
>>>
pycms data representation
-------------------------
When preparing to serve, pymcs parses the working environment and
creates an internal data structure. This is done by the
pycms.envparse() method. This method expects the path to the
working environment and a pycms.CMS instance as input. For
demonstration purposes, we will use a dummy object here, bypassing
pycms.CMS.__init__().
>>> class DummyCMS:
... pass
...
>>> dummy_cms = DummyCMS()
>>> dummy_cms.__dict__["__class__"] = pycms.CMS
>>> pycms.envparse("pycmsroot", dummy_cms)
>>>
The CMS instance will be populated by the parsing results. In this
case, only the single root page is created, which can be obtained by
calling the CMS instance as a method. This yields the default page
created by pycms.envinit() above.
>>> dummy_cms()
'<!DOCTYPE html>\n <html>\n <meta charset="utf-8"/>\n <head>\n <title>\n TITLE\n </title>\n </head>\n <body>\n CONTENT\n </body>\n </html>\n '
>>>
Static content and special directories
--------------------------------------
There is a folder 'static' below the pycms root directory where you
can store static content. It can neither be removed nor re-created.
>>> instance.remove_page("/static")
Traceback (most recent call last):
...
RuntimeError: "/static/" is a special URI and can not be removed.
>>>
>>> instance.create_page("/static", "new_template.html")
Traceback (most recent call last):
...
RuntimeError: "/static/" is a special URI and can not be re-created.
>>>
The same goes for the '_templates' folder.
>>> instance.remove_page("/_templates")
Traceback (most recent call last):
...
RuntimeError: "/_templates/" is a special URI and can not be removed.
>>>
>>> instance.create_page("/_templates", "new_template.html")
Traceback (most recent call last):
...
RuntimeError: "/_templates/" is a special URI and can not be re-created.
>>>
Deleting a pycms environment
----------------------------
To do this, simply delete the project folder.
>>> import shutil
>>> shutil.rmtree("pycmsroot")
>>>
Command line interface
----------------------
To make pycms serve content over an HTTP socket, issue the command
python pycms.py htmlroot
on the command line. `htmlroot` is a directory containing the data to serve
from and to save to. If written as above, pycms assumes it to be a local
subdirectory. You can also give full qualified paths as `/home/user/htmlroot`
or `C:\htmlroot\`, depending on your operatings system.
Helper Classes and Methods
--------------------------
pycms contains various helpers under the hood to perform its work.
### LineReplacement
The LineReplacement class is being used to compute differences between
templates and actual pages. It utilises knowledge about how pycms
templates work.
>>> source = """<html>
... <head><title>Test Template</title></head>
... <body>
... REPLACE_THIS
... </body>
... </html>"""
>>> result = """<html>
... <head><title>Test Template</title></head>
... <body>
... <div>
... <p>Replaced!</p>
... </div>
... </body>
... </html>"""
>>> lp = pycms.LineReplacement(source, result)
>>> lp.replacements
{'REPLACE_THIS': '<div>\n <p>Replaced!</p>\n </div>'}
>>>
It also has a method to apply the found replacements to new strings.
>>> original = """<html>
... <head><title>Another Template</title></head>
... <body>
... <div>
... REPLACE_THIS
... </div>
... </body>
... </html>"""
>>> lp.replace(original)
'<html>\n<head><title>Another Template</title></head>\n<body>\n <div>\n<div>\n <p>Replaced!</p>\n </div>\n </div>\n</body>\n</html>'
>>>