| 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 |
r''' |
| 33 |
This module exists solely to give some examples (and doctest) |
| 34 |
of mercurial_extension_utils styled for Python2 syntax. |
| 35 |
Tests are copied from mercurial_extension_utils.py (before |
| 36 |
switch to py3 syntax in doctests). |
| 37 |
|
| 38 |
>>> import mercurial.ui; ui = mercurial.ui.ui() |
| 39 |
>>> ui.debug(ui_string("Simple text")) |
| 40 |
>>> ui.debug(ui_string(b"Simple binary")) |
| 41 |
>>> ui.debug(ui_string("This is %s and %s and %s", b'bin', u'txt', None)) |
| 42 |
>>> ui.debug(ui_string(b"This is %s and %s and %s", b'bin', u'txt', None)) |
| 43 |
|
| 44 |
This works because we reasonably create binary strings, as ui expects: |
| 45 |
|
| 46 |
>>> ui_string("Simple text") |
| 47 |
'Simple text' |
| 48 |
>>> ui_string(b"Simple binary") |
| 49 |
'Simple binary' |
| 50 |
>>> ui_string("This is %s and %s and %s", b'bin', u'txt', None) |
| 51 |
u'This is bin and txt and None' |
| 52 |
>>> ui_string(b"This is %s and %s and %s", b'bin', u'txt', None) |
| 53 |
u'This is bin and txt and None' |
| 54 |
|
| 55 |
>>> normalize_path("~/src") |
| 56 |
'/home/lordvader/src' |
| 57 |
>>> normalize_path("/some/where") |
| 58 |
'/some/where' |
| 59 |
>>> normalize_path("/some/where/") |
| 60 |
'/some/where' |
| 61 |
>>> normalize_path("../../../some/where") |
| 62 |
'/home/lordvader/some/where' |
| 63 |
|
| 64 |
|
| 65 |
>>> belongs_to_tree("/tmp/sub/dir", "/tmp") |
| 66 |
'/tmp' |
| 67 |
>>> belongs_to_tree("/tmp", "/tmp") |
| 68 |
'/tmp' |
| 69 |
>>> belongs_to_tree("/tmp/sub", "/tmp/sub/dir/../..") |
| 70 |
'/tmp' |
| 71 |
|
| 72 |
|
| 73 |
>>> belongs_to_tree("/usr/sub", "/tmp") |
| 74 |
|
| 75 |
|
| 76 |
>>> home_work_src = os.path.join(os.environ["HOME"], "work", "src") |
| 77 |
>>> belongs_to_tree(home_work_src, "~/work") |
| 78 |
'/home/lordvader/work' |
| 79 |
>>> belongs_to_tree("/home/lordvader/devel/webapps", "~lordvader/devel") |
| 80 |
'/home/lordvader/devel' |
| 81 |
|
| 82 |
|
| 83 |
>>> belongs_to_tree_group("/tmp/sub/dir", ["/bin", "/tmp"]) |
| 84 |
'/tmp' |
| 85 |
>>> belongs_to_tree_group("/tmp", ["/tmp"]) |
| 86 |
'/tmp' |
| 87 |
>>> belongs_to_tree_group("/tmp/sub/dir", ["/bin", "~/src"]) |
| 88 |
|
| 89 |
|
| 90 |
>>> belongs_to_tree_group("/tmp/sub/dir", ["/tmp", "/bin", "/tmp", "/tmp/sub"]) |
| 91 |
'/tmp/sub' |
| 92 |
|
| 93 |
|
| 94 |
>>> belongs_to_tree_group("/home/lordvader/src/apps", ["~/src", "/home/lordvader"]) |
| 95 |
'/home/lordvader/src' |
| 96 |
|
| 97 |
|
| 98 |
>>> pat = DirectoryPattern('~/src/{suffix}') |
| 99 |
>>> pat.is_valid() |
| 100 |
True |
| 101 |
>>> pat.search("/opt/repos/abcd") |
| 102 |
>>> pat.search("~/src/repos/in/tree") |
| 103 |
{'suffix': 'repos/in/tree'} |
| 104 |
>>> pat.search("/home/lordvader/src/repos/here" if os.system != 'nt' else "c:/users/lordvader/src/repos/here") |
| 105 |
{'suffix': 'repos/here'} |
| 106 |
>>> pat.search("/home/lordvader/src") |
| 107 |
|
| 108 |
>>> pat = DirectoryPattern('~lordvader/devel/(item)') |
| 109 |
>>> pat.search("/opt/repos/abcd") |
| 110 |
>>> pat.search("~/devel/libxuza") |
| 111 |
{'item': 'libxuza'} |
| 112 |
>>> pat.search("~/devel/libs/libxuza") |
| 113 |
>>> pat.search("/home/lordvader/devel/webapp") |
| 114 |
{'item': 'webapp'} |
| 115 |
>>> pat.search("/home/lordvader/devel") |
| 116 |
|
| 117 |
>>> from pprint import pprint # Help pass doctests below |
| 118 |
|
| 119 |
>>> pat = DirectoryPattern('/opt/repos/(group)/{suffix}') |
| 120 |
>>> pat.search("/opt/repos/abcd") |
| 121 |
>>> pprint(pat.search("/opt/repos/libs/abcd")) |
| 122 |
{'group': 'libs', 'suffix': 'abcd'} |
| 123 |
>>> pprint(pat.search("/opt/repos/apps/mini/webby")) |
| 124 |
{'group': 'apps', 'suffix': 'mini/webby'} |
| 125 |
|
| 126 |
>>> pat = DirectoryPattern('/opt/repos/(group/{suffix}') |
| 127 |
>>> pat.is_valid() |
| 128 |
False |
| 129 |
>>> pat.search('/opt/repos/some/where') |
| 130 |
|
| 131 |
Fixed strings can also be used and work reasonably: |
| 132 |
|
| 133 |
>>> pat = DirectoryPattern('~/dev/acme') |
| 134 |
>>> pat.is_valid() |
| 135 |
True |
| 136 |
>>> pat.search('/home/lordvader/dev/acme') |
| 137 |
{} |
| 138 |
>>> pat.search('/home/lordvader/dev/acme/') |
| 139 |
{} |
| 140 |
>>> pat.search('/home/lordvader/dev/acme/subdir') |
| 141 |
>>> pat.search('/home/lordvader/dev') |
| 142 |
|
| 143 |
|
| 144 |
>>> tf = TextFiller('{some}/text/to/{fill}') |
| 145 |
>>> tf.fill(some='prefix', fill='suffix') |
| 146 |
'prefix/text/to/suffix' |
| 147 |
>>> tf.fill(some='/ab/c/d', fill='x') |
| 148 |
'/ab/c/d/text/to/x' |
| 149 |
|
| 150 |
|
| 151 |
>>> tf = TextFiller('{some}/text/to/{some}') |
| 152 |
>>> tf.is_valid() |
| 153 |
True |
| 154 |
>>> tf.fill(some='val') |
| 155 |
'val/text/to/val' |
| 156 |
>>> tf.fill(some='ab/c/d', fill='x') |
| 157 |
'ab/c/d/text/to/ab/c/d' |
| 158 |
|
| 159 |
|
| 160 |
>>> tf = TextFiller('{prefix:_=___}/goto/{suffix:/=-}') |
| 161 |
>>> tf.fill(prefix='some_prefix', suffix='some/long/suffix') |
| 162 |
'some___prefix/goto/some-long-suffix' |
| 163 |
|
| 164 |
|
| 165 |
>>> tf = TextFiller('{prefix:/home/=}/docs/{suffix:.txt=.html}') |
| 166 |
>>> tf.fill(prefix='/home/joe', suffix='some/document.txt') |
| 167 |
'joe/docs/some/document.html' |
| 168 |
|
| 169 |
|
| 170 |
>>> tf = TextFiller(r'/goto/{item:/=-:\=_}/') |
| 171 |
>>> tf.fill(item='this/is/slashy') |
| 172 |
'/goto/this-is-slashy/' |
| 173 |
>>> tf.fill(item=r'this\is\back') |
| 174 |
'/goto/this_is_back/' |
| 175 |
>>> tf.fill(item=r'this/is\mixed') |
| 176 |
'/goto/this-is_mixed/' |
| 177 |
|
| 178 |
|
| 179 |
>>> tf = TextFiller(r'http://go.to/{item:/=-}, G:{item:/=\}, name: {item}') |
| 180 |
>>> print(tf.fill(item='so/me/thing')) |
| 181 |
http://go.to/so-me-thing, G:so\me\thing, name: so/me/thing |
| 182 |
|
| 183 |
|
| 184 |
>>> tf = TextFiller('{some}/text/to/{fill}') |
| 185 |
>>> tf.fill(some='prefix', badfill='suffix') |
| 186 |
|
| 187 |
|
| 188 |
>>> tf = TextFiller('{some/text/to/{fill}') |
| 189 |
>>> tf.is_valid() |
| 190 |
False |
| 191 |
>>> tf.fill(some='prefix', fill='suffix') |
| 192 |
|
| 193 |
>>> tf = TextFiller('{some}/text/to/{fill:}') |
| 194 |
>>> tf.is_valid() |
| 195 |
False |
| 196 |
>>> tf.fill(some='prefix', fill='suffix') |
| 197 |
|
| 198 |
|
| 199 |
>>> import mercurial.ui; ui = mercurial.ui.ui() |
| 200 |
>>> setconfig_dict(ui, "sect1", {'a': 7, 'bbb': 'xxx', 'c': '-'}) |
| 201 |
>>> setconfig_dict(ui, "sect2", {'v': 'vvv'}) |
| 202 |
>>> ui.config("sect1", 'a') |
| 203 |
7 |
| 204 |
>>> ui.config("sect2", 'v') |
| 205 |
'vvv' |
| 206 |
|
| 207 |
|
| 208 |
>>> import mercurial.ui; ui = mercurial.ui.ui() |
| 209 |
>>> setconfig_list(ui, "sect1", |
| 210 |
... [('a', 7), ('bbb', 'xxx'), ('c', '-'), ('a', 8)]) |
| 211 |
>>> setconfig_list(ui, "sect2", [('v', 'vvv')]) |
| 212 |
>>> ui.config("sect1", 'a') |
| 213 |
8 |
| 214 |
>>> ui.config("sect2", 'v') |
| 215 |
'vvv' |
| 216 |
|
| 217 |
|
| 218 |
>>> import mercurial.ui; ui = mercurial.ui.ui() |
| 219 |
>>> setconfig_list(ui, "foo", [ |
| 220 |
... ("pfx-some-sfx", "ala, ma kota"), |
| 221 |
... ("some.nonitem", "bela nie"), |
| 222 |
... ("x", "yes"), |
| 223 |
... ("pfx-other-sfx", 4) |
| 224 |
... ]) |
| 225 |
>>> setconfig_list(ui, "notfoo", [ |
| 226 |
... ("pfx-some-sfx", "bad"), |
| 227 |
... ("pfx-also-sfx", "too"), |
| 228 |
... ]) |
| 229 |
>>> |
| 230 |
>>> for name, value in rgxp_config_items( |
| 231 |
... ui, "foo", re.compile(r'^pfx-(\w+)-sfx$')): |
| 232 |
... print(name, value) |
| 233 |
some ala, ma kota |
| 234 |
other 4 |
| 235 |
|
| 236 |
|
| 237 |
>>> import mercurial.ui; ui = mercurial.ui.ui() |
| 238 |
>>> setconfig_list(ui, "foo", [ |
| 239 |
... ("pfx-some-sfx", "ala, ma kota"), |
| 240 |
... ("some.nonitem", "bela nie"), |
| 241 |
... ("x", "yes"), |
| 242 |
... ("pfx-other-sfx", "sth"), |
| 243 |
... ]) |
| 244 |
>>> setconfig_list(ui, "notfoo", [ |
| 245 |
... ("pfx-some-sfx", "bad"), |
| 246 |
... ("pfx-also-sfx", "too"), |
| 247 |
... ]) |
| 248 |
>>> |
| 249 |
>>> for name, value in rgxp_configlist_items( |
| 250 |
... ui, "foo", re.compile(r'^pfx-(\w+)-sfx$')): |
| 251 |
... print(name, value) |
| 252 |
some ['ala', 'ma', 'kota'] |
| 253 |
other ['sth'] |
| 254 |
|
| 255 |
|
| 256 |
>>> import mercurial.ui; ui = mercurial.ui.ui() |
| 257 |
>>> setconfig_list(ui, "foo", [ |
| 258 |
... ("pfx-some-sfx", "true"), |
| 259 |
... ("some.nonitem", "bela nie"), |
| 260 |
... ("x", "yes"), |
| 261 |
... ("pfx-other-sfx", "false"), |
| 262 |
... ]) |
| 263 |
>>> setconfig_dict(ui, "notfoo", { |
| 264 |
... "pfx-some-sfx": "1", |
| 265 |
... "pfx-also-sfx": "0", |
| 266 |
... }) |
| 267 |
>>> |
| 268 |
>>> for name, value in rgxp_configbool_items( |
| 269 |
... ui, "foo", re.compile(r'^pfx-(\w+)-sfx$')): |
| 270 |
... print(name, value) |
| 271 |
some True |
| 272 |
other False |
| 273 |
|
| 274 |
|
| 275 |
>>> import mercurial.ui; ui = mercurial.ui.ui() |
| 276 |
>>> setconfig_list(ui, "foo", [ |
| 277 |
... ("some.item", "ala, ma kota"), |
| 278 |
... ("some.nonitem", "bela nie"), |
| 279 |
... ("x", "yes"), |
| 280 |
... ("other.item", 4), |
| 281 |
... ]) |
| 282 |
>>> setconfig_dict(ui, "notfoo", { |
| 283 |
... "some.item": "bad", |
| 284 |
... "also.item": "too", |
| 285 |
... }) |
| 286 |
>>> |
| 287 |
>>> for name, value in suffix_config_items( |
| 288 |
... ui, "foo", 'item'): |
| 289 |
... print(name, value) |
| 290 |
some ala, ma kota |
| 291 |
other 4 |
| 292 |
|
| 293 |
|
| 294 |
>>> import mercurial.ui; ui = mercurial.ui.ui() |
| 295 |
>>> setconfig_list(ui, "foo", [ |
| 296 |
... ("some.item", "ala, ma kota"), |
| 297 |
... ("some.nonitem", "bela nie"), |
| 298 |
... ("x", "yes"), |
| 299 |
... ("other.item", "kazimira"), |
| 300 |
... ]) |
| 301 |
>>> setconfig_dict(ui, "notfoo", { |
| 302 |
... "some.item": "bad", |
| 303 |
... "also.item": "too", |
| 304 |
... }) |
| 305 |
>>> |
| 306 |
>>> for name, value in suffix_configlist_items( |
| 307 |
... ui, "foo", "item"): |
| 308 |
... print(name, value) |
| 309 |
some ['ala', 'ma', 'kota'] |
| 310 |
other ['kazimira'] |
| 311 |
|
| 312 |
|
| 313 |
>>> import mercurial.ui; ui = mercurial.ui.ui() |
| 314 |
>>> setconfig_list(ui, "foo", [ |
| 315 |
... ("true.item", "true"), |
| 316 |
... ("false.item", "false"), |
| 317 |
... ("one.item", "1"), |
| 318 |
... ("zero.item", "0"), |
| 319 |
... ("yes.item", "yes"), |
| 320 |
... ("no.item", "no"), |
| 321 |
... ("some.nonitem", "1"), |
| 322 |
... ("x", "yes"), |
| 323 |
... ]) |
| 324 |
>>> setconfig_dict(ui, "notfoo", { |
| 325 |
... "some.item": "0", |
| 326 |
... "also.item": "too", |
| 327 |
... }) |
| 328 |
>>> |
| 329 |
>>> for name, value in suffix_configbool_items( |
| 330 |
... ui, "foo", "item"): |
| 331 |
... print(name, str(value)) |
| 332 |
true True |
| 333 |
false False |
| 334 |
one True |
| 335 |
zero False |
| 336 |
yes True |
| 337 |
no False |
| 338 |
>>> |
| 339 |
>>> ui.setconfig("foo", "text.item", "something") |
| 340 |
>>> for name, value in suffix_configbool_items( |
| 341 |
... ui, "foo", "item"): |
| 342 |
... print(name, str(value)) |
| 343 |
Traceback (most recent call last): |
| 344 |
File "/usr/lib/python2.7/dist-packages/mercurial/ui.py", line 237, in configbool |
| 345 |
% (section, name, v)) |
| 346 |
ConfigError: foo.text.item is not a boolean ('something') |
| 347 |
|
| 348 |
|
| 349 |
>>> class SomeClass(object): |
| 350 |
... def meth(self, arg): |
| 351 |
... return "Original: " + arg |
| 352 |
>>> |
| 353 |
>>> @monkeypatch_method(SomeClass) |
| 354 |
... def meth(self, arg): |
| 355 |
... return "Patched: " + meth.orig(self, arg) |
| 356 |
>>> |
| 357 |
>>> obj = SomeClass() |
| 358 |
>>> print(obj.meth("some param")) |
| 359 |
Patched: Original: some param |
| 360 |
|
| 361 |
|
| 362 |
>>> class SomeClass(object): |
| 363 |
... def meth(self, arg): |
| 364 |
... return "Original: " + arg |
| 365 |
>>> |
| 366 |
>>> @monkeypatch_method(SomeClass, "meth") |
| 367 |
... def another_meth(self, arg): |
| 368 |
... return "Patched: " + another_meth.orig(self, arg) |
| 369 |
>>> |
| 370 |
>>> obj = SomeClass() |
| 371 |
>>> print(obj.meth("some param")) |
| 372 |
Patched: Original: some param |
| 373 |
|
| 374 |
|
| 375 |
>>> import random |
| 376 |
>>> @monkeypatch_function(random) |
| 377 |
... def seed(x=None): |
| 378 |
... print("Forcing random to seed with 0 instead of", x) |
| 379 |
... return seed.orig(0) |
| 380 |
>>> |
| 381 |
>>> random.seed() |
| 382 |
Forcing random to seed with 0 instead of None |
| 383 |
>>> random.randint(0, 10) |
| 384 |
9 |
| 385 |
|
| 386 |
>>> import random |
| 387 |
>>> @monkeypatch_function(random, 'choice') |
| 388 |
... def choice_first(sequence): |
| 389 |
... return sequence[0] |
| 390 |
>>> for x in range(0, 4): print(random.choice("abcdefgh")) |
| 391 |
a |
| 392 |
a |
| 393 |
a |
| 394 |
a |
| 395 |
|
| 396 |
|
| 397 |
>>> cmdtable = {} |
| 398 |
>>> cmd = command(cmdtable) |
| 399 |
>>> |
| 400 |
>>> @cmd("somecmd", [], "somecmd") |
| 401 |
... def mycmd(ui, repo, sth, **opts): |
| 402 |
... pass |
| 403 |
>>> |
| 404 |
>>> @cmd("othercmd", [ |
| 405 |
... ('l', 'list', None, 'List widgets'), |
| 406 |
... ('p', 'pagesize', 10, 'Page size'), |
| 407 |
... ], "othercmd [-l] [-p 20]", norepo=True) |
| 408 |
... def othercmd(ui, sth, **opts): |
| 409 |
... pass |
| 410 |
>>> |
| 411 |
>>> sorted(cmdtable.keys()) |
| 412 |
['othercmd', 'somecmd'] |
| 413 |
>>> cmdtable['othercmd'] # doctest: +ELLIPSIS |
| 414 |
(<function othercmd at ...>, [('l', 'list', None, 'List widgets'), ('p', 'pagesize', 10, 'Page size')], 'othercmd [-l] [-p 20]') |
| 415 |
|
| 416 |
|
| 417 |
>>> from mercurial import commands |
| 418 |
>>> # Syntax changed in hg3.8, trying to accomodate |
| 419 |
>>> commands.norepo if hasattr(commands, 'norepo') else ' othercmd' # doctest: +ELLIPSIS |
| 420 |
'... othercmd' |
| 421 |
>>> othercmd.__dict__['norepo'] if othercmd.__dict__ else True |
| 422 |
True |
| 423 |
>>> mycmd.__dict__['norepo'] if mycmd.__dict__ else False |
| 424 |
False |
| 425 |
|
| 426 |
|
| 427 |
>>> re = direct_import("re") |
| 428 |
>>> re.__name__ |
| 429 |
're' |
| 430 |
>>> re.search("^(.)", "Ala").group(1) |
| 431 |
'A' |
| 432 |
|
| 433 |
|
| 434 |
>>> k = direct_import("anydbm", ["dbhash", "gdbm", "dbm", "bsddb.db"]) |
| 435 |
>>> k.__name__ |
| 436 |
'anydbm' |
| 437 |
|
| 438 |
|
| 439 |
>>> m1, loaded = direct_import_ext("xml.sax.handler") |
| 440 |
>>> m1.__name__, loaded |
| 441 |
('xml.sax.handler', True) |
| 442 |
|
| 443 |
>>> m2, loaded = direct_import_ext("xml.sax.handler") |
| 444 |
>>> m2.__name__, loaded |
| 445 |
('xml.sax.handler', False) |
| 446 |
|
| 447 |
>>> m1 == m2 |
| 448 |
True |
| 449 |
|
| 450 |
|
| 451 |
>>> disable_logging("keyring") |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
''' |