This file is indexed.

/usr/share/pyshared/martian/directive.txt is in python-martian 0.14-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
Directives
==========

When grokking a class, the grokking procedure can be informed by
directives, on a class, or a module. If a directive is absent, the
system falls back to a default. Here we introduce a general way to
define these directives, and how to use them to retrieve information
for a class for use during the grokking procedure.

A simple directive
------------------

We define a simple directive that sets a description::

  >>> from martian import Directive, CLASS, ONCE
  >>> class description(Directive):
  ...     scope = CLASS
  ...     store = ONCE
  ...     default = u''

The name of the directive is ``description``. We specify that the
directive can only be used in the scope of a class. We also specify it
can only be used a single time. Finally we define the default in case
the directive is absent (the empty string).

Now we look at the directive in action::

  >>> class Foo(object):
  ...    description(u"This is a description")

After setting the description, we bind the directive and obtain a
bound directive object.  This object has means for retrieving the data
set by the directive, in particular the ``get`` method::

  >>> description.bind().get(Foo)
  u'This is a description'

Directives in different namespaces get stored differently. We'll
define a similar directive in another namespace::

  >>> class description2(description):
  ...     pass

  >>> class Foo(object):
  ...     description(u"Description1")
  ...     description2(u"Description2")
  >>> description.bind().get(Foo)
  u'Description1'
  >>> description2.bind().get(Foo)
  u'Description2'

If we check the value of a class without the directive, we see the
default value for that directive, this case the empty unicode string::

  >>> class Foo(object):
  ...     pass
  >>> description.bind().get(Foo)
  u''

In certain cases we need to set a value on a component as if the directive was
actually used::

  >>> description.set(Foo, u'value as set')
  >>> description.bind().get(Foo)
  u'value as set'

Subclasses of the original class will inherit the properties set by the
directive:

  >>> class Foo(object):
  ...     description('This is a foo.')
  ...
  >>> class Bar(Foo):
  ...     pass
  ...
  >>> description.bind().get(Bar)
  'This is a foo.'

When we use the directive outside of class scope, we get an error
message::

  >>> description('Description')
  Traceback (most recent call last):
    ...
  GrokImportError: The 'description' directive can only be used on class level.

In particular, we cannot use it in a module::

  >>> class testmodule(FakeModule):
  ...   fake_module = True
  ...   description("Description")
  Traceback (most recent call last):
    ...
  GrokImportError: The 'description' directive can only be used on class level.

We cannot use the directive twice in the class scope. If we do so, we
get an error message as well::

  >>> class Foo(object):
  ...   description(u"Description1")
  ...   description(u"Description2")
  Traceback (most recent call last):
    ...
  GrokImportError: The 'description' directive can only be called once per class.

We cannot call the directive with no argument either::

  >>> class Foo(object):
  ...   description()
  Traceback (most recent call last):
    ...
  TypeError: description takes exactly 1 argument (0 given)

Class and module scope
----------------------

We define a ``layer`` directive that can be used in class and module
scope both::

  >>> from martian import CLASS_OR_MODULE
  >>> class layer(Directive):
  ...     scope = CLASS_OR_MODULE
  ...     store = ONCE

By default, the ``default`` property is None which is why we can omit
specifying it here.

This directive has been declared ``CLASS_OR_MODULE``, so you will
always have to pass a module to the directive. Since we don't have a
module yet we'll simply create a dummy, empty, fallback module::

  >>> dummy = object()

We can use this directive now on a class::

  >>> class Foo(object):
  ...   layer('Test')
  >>> layer.bind().get(Foo, dummy)
  'Test'

The defaulting to ``None`` works::

  >>> class Foo(object):
  ...   pass
  >>> layer.bind().get(Foo, dummy) is None
  True

We can also use it in a module::

  >>> class testmodule(FakeModule):
  ...    layer('Test2')
  ...    class Foo(object):
  ...       pass
  >>> from martiantest.fake import testmodule

When we now try to access ``layer`` on ``Foo``, we find the
module-level default which we just set. We pass the module as the
second argument to the ``get`` method to have it fall back on this::

  >>> layer.bind().get(testmodule.Foo, testmodule)
  'Test2'

Let's look at a module where the directive is not used::

  >>> class testmodule(FakeModule):
  ...   class Foo(object):
  ...      pass
  >>> from martiantest.fake import testmodule

In this case, the value cannot be found so the system falls back on
the default, ``None``::

  >>> layer.bind().get(testmodule.Foo, testmodule) is None
  True




Like with CLASS scope directive where values set are inherited by subclasses,
values set on a class or module level are inherited too, even if the subclass
is defined another module::

  >>> class testmodule_a(FakeModule):
  ...   layer('Value set on baseclass module')
  ...   class FooA(object):
  ...      pass
  >>> from martiantest.fake import testmodule_a
  >>>
  >>> class testmodule_b(FakeModule):
  ...   class FooB(testmodule_a.FooA):
  ...      pass
  >>> from martiantest.fake import testmodule_b

On the baseclass::

  >>> layer.bind().get(testmodule_a.FooA)
  'Value set on baseclass module'

Inherited by the subclass::

  >>> layer.bind().get(testmodule_b.FooB)
  'Value set on baseclass module'

Whenever there's a directive set on the baseclass' module, it will take
precedence like with "normal" inheritance::

  >>> class testmodule_c(FakeModule):
  ...   layer('Value set on subclass module')
  ...   class FooC(testmodule_a.FooA):
  ...      pass
  >>> from martiantest.fake import testmodule_c

  >>> layer.bind().get(testmodule_c.FooC)
  'Value set on subclass module'




Let's now look at this using a directive with CLASS scope only::

  >>> class layer2(Directive):
  ...     scope = CLASS
  ...     store = ONCE

Inheritance in combination with module scope
--------------------------------------------

Let's look at how our layer directive can inherit in combination with
directive use on module scope.

First we define a module which defines a class that gets the ``Test``
layer through the use of the layer directive on module scope::

  >>> class inheritmodule1(FakeModule):
  ...   layer('Test')
  ...   class Foo(object):
  ...      pass
  >>> from martiantest.fake import inheritmodule1

Now we define another module that has a class that inherits from ``Foo``::

  >>> class inheritmodule2(FakeModule):
  ...   class Bar(inheritmodule1.Foo):
  ...      pass
  >>> from martiantest.fake import inheritmodule2

We will now see that ``Bar`` has inherited the layer ``Test``::

  >>> layer.bind().get(inheritmodule2.Bar, inheritmodule2)
  'Test'

Let's try it with another level of inheritance::

  >>> class inheritmodule3(FakeModule):
  ...   class Baz(inheritmodule2.Bar):
  ...       pass
  >>> from martiantest.fake import inheritmodule3

The layer should still be inherited::

  >>> layer.bind().get(inheritmodule3.Baz, inheritmodule3)
  'Test'

Let's override now by having an explicit layer directive on the class
that subclasses ``Foo``::

  >>> class inheritmodule4(FakeModule):
  ...   class OverrideFoo(inheritmodule1.Foo):
  ...      layer('AnotherTest')
  >>> from martiantest.fake import inheritmodule4

  >>> layer.bind().get(inheritmodule4.OverrideFoo, inheritmodule4)
  'AnotherTest'

Let's override now by having an explicit layer directive on
module-level instead::

  >>> class inheritmodule5(FakeModule):
  ...   layer('AnotherTest')
  ...   class OverrideFoo(inheritmodule1.Foo):
  ...      pass
  >>> from martiantest.fake import inheritmodule5

  >>> layer.bind().get(inheritmodule5.OverrideFoo, inheritmodule5)
  'AnotherTest'

Inheritance with module scope also works for old-style classes::

  >>> class oldstyle1(FakeModule):
  ...    layer('one')
  ...    class Base:
  ...       pass
  >>> from martiantest.fake import oldstyle1
  >>> class oldstyle2(FakeModule):
  ...    class Sub(oldstyle1.Base):
  ...       pass
  >>> from martiantest.fake import oldstyle2
  >>> layer.bind().get(oldstyle2.Sub)
  'one'

Using a directive multiple times
--------------------------------

A directive can be configured to allow it to be called multiple times
in the same scope::

  >>> from martian import MultipleTimesDirective
  >>> class multi(MultipleTimesDirective):
  ...     scope = CLASS

We can now use the directive multiple times without any errors::

  >>> class Foo(object):
  ...   multi(u"Once")
  ...   multi(u"Twice")

We can now retrieve the value and we'll get a list::

  >>> multi.bind().get(Foo)
  [u'Once', u'Twice']

The default value for a MultipleTimesDirective is an empty list::

  >>> class Bar(object):
  ...   pass
  >>> multi.bind().get(Bar)
  []

Whenever the directive is used on a sub class of a component, the values set by
directives on the base classes are combined::

  >>> class Qux(Foo):
  ...     multi(u'Triple')
  ...
  >>> multi.bind().get(Qux)
  [u'Once', u'Twice', u'Triple']

You can also create a directive that ignores the values on the base classes::

  >>> from martian import MULTIPLE_NOBASE
  >>> class multi(Directive):
  ...     scope = CLASS
  ...     store = MULTIPLE_NOBASE

  >>> class Foo(object):
  ...     multi(u"Once")
  ...     multi(u"Twice")
  >>> multi.bind().get(Foo)
  [u'Once', u'Twice']

  >>> class Qux(Foo):
  ...     multi(u'Triple')
  ...     multi(u'More')
  >>> multi.bind().get(Qux)
  [u'Triple', u'More']

Using a directive multiple times, as a dictionary
-------------------------------------------------

A directive can be configured to allow it to be called multiple times in the
same scope. In this case the factory method should be overridden to return a
key-value pair::

  >>> from martian import DICT
  >>> class multi(Directive):
  ...     scope = CLASS
  ...     store = DICT
  ...     def factory(self, value):
  ...         return value.lower(), value

We can now use the directive multiple times without any errors::

  >>> class Bar(object):
  ...   multi(u"Once")
  ...   multi(u"Twice")

We can now retrieve the value and we'll get a to the items::

  >>> d = multi.bind().get(Bar)
  >>> print sorted(d.items())
  [(u'once', u'Once'), (u'twice', u'Twice')]

When the factory method does not return a key-value pair, an error is raised::

  >>> class wrongmulti(Directive):
  ...     scope = CLASS
  ...     store = DICT
  ...     def factory(self, value):
  ...         return None

  >>> class Baz(object):
  ...   wrongmulti(u"Once")
  Traceback (most recent call last):
  ...
  GrokImportError: The factory method for the 'wrongmulti' directive should
  return a key-value pair.

  >>> class wrongmulti2(Directive):
  ...     scope = CLASS
  ...     store = DICT
  ...     def factory(self, value):
  ...         return value, value, value

  >>> class Baz(object):
  ...   wrongmulti2(u"Once")
  Traceback (most recent call last):
  ...
  GrokImportError: The factory method for the 'wrongmulti2' directive should
  return a key-value pair.

Like with MULTIPLE store, values set by directives using the DICT store are
combined::

  >>> class multi(Directive):
  ...     scope = CLASS
  ...     store = DICT
  ...     def factory(self, value, andanother):
  ...         return value, andanother
  ...
  >>> class Frepple(object):
  ...   multi(1, 'AAA')
  ...   multi(2, 'BBB')
  ...
  >>> class Fropple(Frepple):
  ...   multi(1, 'CCC')
  ...   multi(3, 'DDD')
  ...   multi(4, 'EEE')

  >>> d = multi.bind().get(Fropple)
  >>> print sorted(d.items())
  [(1, 'CCC'), (2, 'BBB'), (3, 'DDD'), (4, 'EEE')]

Using MULTIPLE and DICT can also work on a module level, even though
inheritance has no meaning there::

  >>> from martian import MODULE
  >>> class multi(MultipleTimesDirective):
  ...     scope = MODULE
  ...
  >>> multi.__module__ = 'somethingelse'
  >>> class module_with_directive(FakeModule):
  ...     fake_module = True
  ...
  ...     multi('One')
  ...     multi('Two')
  ...
  >>> from martiantest.fake import module_with_directive
  >>> print multi.bind().get(module_with_directive)
  ['One', 'Two']

  >>> from martian import MODULE
  >>> class multi(Directive):
  ...     scope = MODULE
  ...     store = DICT
  ...     def factory(self, value, andanother):
  ...         return value, andanother
  ...
  >>> multi.__module__ = 'somethingelse'
  >>> class module_with_directive(FakeModule):
  ...     fake_module = True
  ...
  ...     multi(1, 'One')
  ...     multi(2, 'Two')
  ...
  >>> from martiantest.fake import module_with_directive
  >>> d = multi.bind().get(module_with_directive)
  >>> print sorted(d.items())
  [(1, 'One'), (2, 'Two')]

Directives on an interface
--------------------------

When you use ``zope.interface.Interface`` to define a new interface using
the class statement, in fact a special interface instance is created, not a
class. To let the directive store a value on an interface, we need to
use a special storage (``martian.ONCE_IFACE``)::

  >>> from martian import ONCE_IFACE
  >>> class skin(Directive):
  ...    scope = CLASS
  ...    store = ONCE_IFACE

Note that we still indicate the ``CLASS`` scope for this kind of
directive. At some point we may introduce a special scope for
directives on interfaces.

Let's try the directive. We shouldn't get an error::

  >>> class once_iface(FakeModule):
  ...   from zope.interface import Interface
  ...   class TestIface(Interface):
  ...     skin('Foo')
  >>> from martiantest.fake import once_iface

We can now retrieve the value::

  >>> skin.bind().get(once_iface.TestIface)
  'Foo'

Computed defaults
-----------------

Often instead of just supplying the system with a default, we want to
compute the default in some way. The computation of the default can be defined
in the directive class, by the ``get_default`` classmethod.

We define the ``name`` directive, which if not present, will compute its value
from the name of class, lower-cased.

  >>> class name(Directive):
  ...     scope = CLASS
  ...     store = ONCE
  ...
  ...     @classmethod
  ...     def get_default(cls, component, module=None, **data):
  ...         return component.__name__.lower()
  ...
  >>> bound_name = name.bind()
  >>>
  >>> class Foo(object):
  ...   name('bar')
  ...
  >>> bound_name.get(Foo)
  'bar'

  >>> class Foo2(object):
  ...   pass
  ...
  >>> bound_name.get(Foo2)
  'foo2'

To override the default-default behaviour you can pass a function when binding
the directive. In this case, we do not want the default behaviour of using the
lower cased class name, but have it upper cased::

  >>> def default_name_uppercase(component, module, **data):
  ...     return component.__name__.upper()
  ...
  >>> bound_name = name.bind(get_default=default_name_uppercase)

  >>> class Foo(object):
  ...   name('bar')

  >>> bound_name.get(Foo)
  'bar'

  >>> class Foo2(object):
  ...   pass
  >>> bound_name.get(Foo2)
  'FOO2'

Let's test this with a deeper inheritance hierarchy. Explicit
directives should always trump computed defaults::

  >>> class Subclass(Foo):
  ...   pass
  >>> bound_name.get(Subclass)
  'bar'

Now let's look at a hierarchy in which the explicit rule should
apply::

  >>> bound_name2 = name.bind()
  >>> class Alpha(object):
  ...    pass
  >>> class Beta(Alpha):
  ...    pass
  >>> bound_name2.get(Alpha)
  'alpha'
  >>> bound_name2.get(Beta)
  'beta'

We will now define a default rule that only triggers for particular
components in the inheritance chain, but returns ``UNKNOWN``
otherwise. This can be useful if the default rule is dependent on
outside information. In Grok for instance, the default rule for
``grok.context`` will look for a class that implements ``IContext`` in
the same module, and ``grok.templatedir`` will look for a directory
with templates with a name based on the name of the module with
``_templates`` appended.

This rule returns a value only if the module name includes the number
``1``, and will return ``UNKNOWN`` otherwise::

  >>> import martian
  >>> def default_name_lowercase3(component, module, **data):
  ...     if '1' in module.__name__:
  ...         return component.__name__.lower()
  ...     return martian.UNKNOWN

  >>> bound_name3 = name.bind(get_default=default_name_lowercase3)

This won't trigger for this module, as it doesn't have the character
``1`` in it::

  >>> class testmodule(FakeModule):
  ...    class Foo(object):
  ...       pass
  >>> from martiantest.fake import testmodule
  >>> bound_name3.get(testmodule.Foo, testmodule) is martian.UNKNOWN
  True

Now we define a module which does have ``1`` in it, so the rule should
be triggered::

  >>> class testmodule1(FakeModule):
  ...    class Foo(object):
  ...       pass
  >>> from martiantest.fake import testmodule1
  >>> bound_name3.get(testmodule1.Foo, testmodule1)
  'foo'

This also works with inheritance::

  >>> class testmodule2(FakeModule):
  ...   class Bar(testmodule1.Foo):
  ...      pass
  >>> from martiantest.fake import testmodule2
  >>> bound_name3.get(testmodule2.Bar, testmodule2)
  'foo'

Module-level explicit directives always trump computed defaults as
well.  The ``layer`` directive is ``CLASS_OR_MODULE`` scope. Let's set
up a hierarchy of modules and classes using ``layer`` to demonstrate
this::

  >>> class inheritmodule1(FakeModule):
  ...   layer('Test')
  ...   class Foo(object):
  ...      pass
  >>> from martiantest.fake import inheritmodule1
  >>> class inheritmodule2(FakeModule):
  ...   class Bar(inheritmodule1.Foo):
  ...      pass
  >>> from martiantest.fake import inheritmodule2

We define a way to compute the default in which we compute a string
based on the module name and the class name, so we can later check
whether the right module and class was passed to compute the default::

  >>> def immediate_get_default(component, module, **data):
  ...   return "computed: %s %s" % (module.__name__, component.__name__)

We don't expect the default rule to kick in as we can find an
explicitly set value::

  >>> layer.bind(get_default=immediate_get_default).get(inheritmodule2.Bar,
  ...   inheritmodule2)
  'Test'

Let's now consider a case where we have inheritance without explicit
use of the ``layer`` directive::

  >>> class inheritmodule1(FakeModule):
  ...   class Foo(object):
  ...      pass
  >>> from martiantest.fake import inheritmodule1
  >>> class inheritmodule2(FakeModule):
  ...   class Bar(inheritmodule1.Foo):
  ...      pass
  >>> from martiantest.fake import inheritmodule2

We expect to receive the computed default for ``Bar``, as
``immediate_get_default`` immediately returns a result for any
component::

  >>> layer.bind(get_default=immediate_get_default).get(inheritmodule2.Bar,
  ...   inheritmodule2)
  'computed: martiantest.fake.inheritmodule2 Bar'

Let's try the default rule that triggers upon seeing ``1`` in the
module name again, this time for the ``CLASS_OR_MODULE`` scope
directive ``layer``::

  >>> def picky_get_default(component, module, **data):
  ...     if '1' in module.__name__:
  ...         return "computed: %s %s" % (module.__name__, component.__name__)
  ...     return martian.UNKNOWN

Since only the ``Foo`` class is in a module with the character ``1``
in it (``inheritmodule1``), we will get the result for ``Foo`` (and
its module)::

  >>> layer.bind(get_default=picky_get_default).get(inheritmodule2.Bar,
  ...   inheritmodule2)
  'computed: martiantest.fake.inheritmodule1 Foo'

We will get the same result if we ask ``Foo`` directly::

  >>> layer.bind(get_default=picky_get_default).get(inheritmodule1.Foo,
  ...   inheritmodule1)
  'computed: martiantest.fake.inheritmodule1 Foo'

If we have a hierarchy that never has a module with the character
``1`` in it, we will receive ``UNKNOWN`` (and the grokkker that uses
this directive should raise an error)::

  >>> class inheritmodule3(FakeModule):
  ...   class Foo(object):
  ...      pass
  >>> from martiantest.fake import inheritmodule3
  >>> layer.bind(get_default=picky_get_default).get(inheritmodule3.Foo,
  ...   inheritmodule3) is martian.UNKNOWN
  True

Raising errors in a computed default
------------------------------------

Let's define a simple directive with a default rule that always raises
a ``GrokError`` if the class name doesn't start with an upper case
letter::

  >>> from martian.error import GrokError
  >>> class name(Directive):
  ...     scope = CLASS
  ...     store = ONCE
  ...
  ...     @classmethod
  ...     def get_default(cls, component, module, **data):
  ...         if component.__name__[0].isupper():
  ...              return component.__name__
  ...         raise GrokError(
  ...              "Component %r has a name that doesn't start with upper "
  ...              "case letter." % component, component)

Let's test it::

  >>> class A(object):
  ...   pass
  >>> class b(object):
  ...   pass
  >>> name.bind().get(A)
  'A'
  >>> name.bind().get(b)
  Traceback (most recent call last):
    ...
  GrokError: Component <class 'b'> has a name that doesn't start with upper
  case letter.

Instead of raising ``GrokError`` we can also raise ``UnknownError`` in
a computed default. This has the same meaning as returning
``UNKNOWN``, except that the error information is recorded and the
default rule is tried again on the base class in the mro chain. If the
default rule has the error raised or ``UNKNOWN`` value returned in
each step of the chain, the first ``UnknownError`` that was raised is
converted into a ``GrokError``.

This makes it possible for the default logic to raise specific errors
for the most specific class if the implicit rule failed to apply on
the class and any of its bases. This is used for instance in the
implementation of the ``get_default`` rule for ``grok.context``.

Let's test this behavior with a rule that raises an ``UnknownError``
if there is no ``foo`` attribute on the class::

  >>> from martian import UnknownError
  >>> class name(Directive):
  ...     scope = CLASS
  ...     store = ONCE
  ...
  ...     @classmethod
  ...     def get_default(cls, component, module, **data):
  ...         if not component.__dict__.has_key('foo'):
  ...             raise UnknownError(
  ...                 "Foo cannot be found for class %s" % component.__name__,
  ...                 component)
  ...         return "Found for class %s" % component.__name__

Let's try it on a simple class first::

  >>> class Test:
  ...   pass
  >>> name.bind().get(Test)
  Traceback (most recent call last):
    ...
  GrokError: Foo cannot be found for class Test

Let's try it on a new style class::

  >>> class Test(object):
  ...   pass
  >>> name.bind().get(Test)
  Traceback (most recent call last):
    ...
  GrokError: Foo cannot be found for class Test

Let's try it on a class where there is some inheritance::

  >>> class Test1(object):
  ...    pass
  >>> class Test2(Test1):
  ...    pass
  >>> name.bind().get(Test2)
  Traceback (most recent call last):
    ...
  GrokError: Foo cannot be found for class Test2

As you can see the error message will apply to the most specific
class, ``Test2``, even though of course the error will also occur for
the base class, ``Test1``.

Let's now demonstrate an inheritance scenario where the error does
not occur, because the get_default rule will succeed at some point
during the inheritance chain::

  >>> class Test1(object):
  ...    foo = 1
  >>> class Test2(Test1):
  ...    pass
  >>> name.bind().get(Test2)
  'Found for class Test1'

Computed defaults for instances
-------------------------------

In some cases directives are used to retrieve values from instances instead of
from their classes::

  >>> class name(Directive):
  ...     scope = CLASS
  ...     store = ONCE
  >>> class instancemodule(FakeModule):
  ...   class Hoi(object):
  ...     name('Test')
  ...   class NoDirectiveOnThis(object):
  ...     pass
  ...   hoi = Hoi()
  ...   no_directive_on_this = NoDirectiveOnThis()
  >>> from martiantest.fake import instancemodule

Let's try to use the directive::

  >>> name.bind().get(instancemodule.hoi, instancemodule)
  'Test'

If no directive was used on the class, we will get ``None``, the
default default value::

  >>> (name.bind().get(instancemodule.no_directive_on_this, instancemodule) is
  ...  None)
  True

Let's try it with a computed value now::

  >>> def get_default(component, module, **data):
  ...   return "The default"

  >>> name.bind(get_default=get_default).get(instancemodule.no_directive_on_this)
  'The default'

Computed default for old-style classes
--------------------------------------

We should also test old-style classes with ``CLASS`` scope directives in
combination with computed defaults::

  >>> class layer2(Directive):
  ...     scope = CLASS
  ...     store = ONCE
  ...     @classmethod
  ...     def get_default(cls, component, module=None, **data):
  ...         if '1' in module.__name__:
  ...             return 'we found it'
  ...         return martian.UNKNOWN
  ...
  >>> class oldstyle1(FakeModule):
  ...    class Base:
  ...       pass
  >>> from martiantest.fake import oldstyle1
  >>> class oldstyle2(FakeModule):
  ...    class Sub(oldstyle1.Base):
  ...       pass
  >>> from martiantest.fake import oldstyle2
  >>> layer2.bind().get(oldstyle2.Sub)
  'we found it'

And let's try it with a ``CLASS_OR_MODULE`` scope directive too::

  >>> class layer3(Directive):
  ...     scope = CLASS_OR_MODULE
  ...     store = ONCE
  ...     @classmethod
  ...     def get_default(cls, component, module=None, **data):
  ...         if '1' in module.__name__:
  ...             return 'we found it'
  ...         return martian.UNKNOWN
  >>> layer3.bind().get(oldstyle2.Sub)
  'we found it'

A marker directive
------------------

Another type of directive is a marker directive. This directive takes
no arguments at all, but when used it marks the context::

  >>> from martian import MarkerDirective
  >>> class mark(MarkerDirective):
  ...     scope = CLASS

  >>> class Foo(object):
  ...     mark()

Class ``Foo`` is now marked::

  >>> mark.bind().get(Foo)
  True

When we have a class that isn't marked, we get the default value, ``False``::

  >>> class Bar(object):
  ...    pass
  >>> mark.bind().get(Bar)
  False

If we pass in an argument, we get an error::

  >>> class Bar(object):
  ...   mark("An argument")
  Traceback (most recent call last):
    ...
  TypeError: mark takes no arguments (1 given)


Validation
----------

A directive can be supplied with a validation method. The validation method
checks whether the value passed in is allowed. It should raise
``GrokImportError`` if the value cannot be validated, together with a
description of why not.

First we define our own validation function. A validation function
takes two arguments:

* the name of the directive we're validating for

* the value we need to validate

The name can be used to format the exception properly.

We'll define a validation method that only expects integer numbers::

  >>> from martian.error import GrokImportError
  >>> class number(Directive):
  ...     scope = CLASS
  ...     store = ONCE
  ...     def validate(self, value):
  ...         if type(value) is not int:
  ...             raise GrokImportError(
  ...                 "The '%s' directive can only be called with an integer." %
  ...                 self.name)

  >>> class Foo(object):
  ...    number(3)

  >>> class Foo(object):
  ...    number("This shouldn't work")
  Traceback (most recent call last):
    ...
  GrokImportError: The 'number' directive can only be called with an integer.

Some built-in validation functions
----------------------------------

Let's look at some built-in validation functions.

The ``validateText`` function determines whether a string
is unicode or plain ascii::

  >>> from martian import validateText
  >>> class title(Directive):
  ...     scope = CLASS
  ...     store = ONCE
  ...     default = u''
  ...     validate = validateText

When we pass ascii text into the directive, there is no error::

  >>> class Foo(object):
  ...    title('Some ascii text')

We can also pass in a unicode string without error::

  >>> class Foo(object):
  ...    title(u'Some unicode text')

Let's now try it with something that's not text at all, such as a number.
This fails::

  >>> class Foo(object):
  ...    title(123)
  Traceback (most recent call last):
    ...
  GrokImportError: The 'title' directive can only be called with unicode or ASCII.

It's not allowed to call the direct with a non-ascii encoded string::

  >>> class Foo(object):
  ...   title(u'è'.encode('latin-1'))
  Traceback (most recent call last):
    ...
  GrokImportError: The 'title' directive can only be called with unicode or ASCII.

 >>> class Foo(object):
 ...   title(u'è'.encode('UTF-8'))
 Traceback (most recent call last):
   ...
 GrokImportError: The 'title' directive can only be called with unicode or ASCII.

The ``validateInterfaceOrClass`` function only accepts class or
interface objects::

  >>> from martian import validateInterfaceOrClass
  >>> class klass(Directive):
  ...     scope = CLASS
  ...     store = ONCE
  ...     validate = validateInterfaceOrClass

It works with interfaces and classes::

  >>> class Bar(object):
  ...    pass
  >>> class Foo(object):
  ...    klass(Bar)

  >>> from zope.interface import Interface
  >>> class IBar(Interface):
  ...    pass
  >>> class Foo(object):
  ...    klass(IBar)

It won't work with other things::

  >>> class Foo(object):
  ...   klass(Bar())
  Traceback (most recent call last):
    ...
  GrokImportError: The 'klass' directive can only be called with a class or an interface.

  >>> class Foo(object):
  ...   klass(1)
  Traceback (most recent call last):
    ...
  GrokImportError: The 'klass' directive can only be called with a class or an interface.

The ``validateInterface`` validator only accepts an interface::

  >>> from martian import validateInterface
  >>> class iface(Directive):
  ...     scope = CLASS
  ...     store = ONCE
  ...     validate = validateInterface

Let's try it::

  >>> class Foo(object):
  ...    iface(IBar)

It won't work with classes or other things::

  >>> class Foo(object):
  ...   iface(Bar)
  Traceback (most recent call last):
    ...
  GrokImportError: The 'iface' directive can only be called with an interface.

  >>> class Foo(object):
  ...   iface(1)
  Traceback (most recent call last):
    ...
  GrokImportError: The 'iface' directive can only be called with an interface.

The ``validateClass`` validator only accepts a class::

  >>> from martian import validateClass
  >>> class klass(Directive):
  ...    scope = CLASS
  ...    store = ONCE
  ...    validate = validateClass

  >>> class Foo(object):
  ...    klass(Bar)

But it won't work with an interface or other things::

  >>> class Foo(object):
  ...    klass(IBar)
  Traceback (most recent call last):
    ...
  GrokImportError: The 'klass' directive can only be called with a class.

  >>> class Foo(object):
  ...    klass(Bar())
  Traceback (most recent call last):
    ...
  GrokImportError: The 'klass' directive can only be called with a class.

Declaring base classes
----------------------

There's a special directive called ``martian.baseclass`` which lets
you declare that a certain class is the base class for a series of
other components.  This property should not be inherited by those
components.  Consider the following base class:

  >>> import martian
  >>> class MyBase(object):
  ...     martian.baseclass()

As you would expect, the directive will correctly identify this class as a
baseclass:

  >>> martian.baseclass.bind().get(MyBase)
  True

But, if we create a subclass of this base class, the subclass won't inherit
that property, unlike with a regular directive:

  >>> class SubClass(MyBase):
  ...     pass
  ...
  >>> martian.baseclass.bind().get(SubClass)
  False

Naturally, the directive will also report a false answer if the class doesn't
inherit from a base class at all and hasn't been marked with the directive:

  >>> class NoBase(object):
  ...     pass
  ...
  >>> martian.baseclass.bind().get(NoBase)
  False

Base classes influence computed directives: a directive computation
will not happen on a base class (or in base classes of it).

Let's define a directive with the computed rule that it will look for
an object ``Context`` in the module it finds itself in and return its
``value`` attribute::

  >>> class info(Directive):
  ...    scope = CLASS
  ...    store = ONCE
  ...    @classmethod
  ...    def get_default(cls, component, module, **data):
  ...        context = getattr(module, 'Context', None)
  ...        if context is None:
  ...            return martian.UNKNOWN
  ...        return context.value

Let use this rule with an example where no baseclass is declared
first::

  >>> class basemodule(FakeModule):
  ...    class Context(object):
  ...       value = 1
  ...    class A(object):
  ...       pass
  >>> from martiantest.fake import basemodule
  >>> class submodule(FakeModule):
  ...    class B(basemodule.A):
  ...        pass
  >>> from martiantest.fake import submodule
  >>> info.bind().get(submodule.B)
  1

Now let's apply the rule where ``A`` is declared to be a
baseclass. Since ``A`` is a base class, the computed default will not
take effect::

  >>> class basemodule2(FakeModule):
  ...    class Context(object):
  ...       value = 1
  ...    class A(object):
  ...       martian.baseclass()
  >>> from martiantest.fake import basemodule2
  >>> class submodule2(FakeModule):
  ...    class B(basemodule2.A):
  ...        pass
  >>> from martiantest.fake import submodule2
  >>> info.bind().get(submodule2.B) is martian.UNKNOWN
  True

If we change the default rule so we use ``UnknownError`` we see the same
behavior, except an error message is raised::

  >>> def get_default(component, module, **data):
  ...     context = getattr(module, 'Context', None)
  ...     if context is None:
  ...         raise UnknownError("No Context object found!", component)
  ...     return context.value

It will work for the case where no baseclass is defined, as the rule
can take effect then::

  >>> info.bind(get_default=get_default).get(submodule.B)
  1

But we will get a ``GrokError`` when a baseclass is in play::

  >>> info.bind(get_default=get_default).get(submodule2.B)
  Traceback (most recent call last):
    ...
  GrokError: No Context object found!