-
Notifications
You must be signed in to change notification settings - Fork 28
/
Python.hs
998 lines (902 loc) · 37.1 KB
/
Python.hs
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
{-# LANGUAGE DeriveDataTypeable, ExtendedDefaultRules, OverloadedLists,
QuasiQuotes, TypeFamilies, TypeSynonymInstances,
MultiParamTypeClasses #-}
module Nirum.Targets.Python ( Code
, CodeGen
, CodeGenContext ( localImports
, standardImports
, thirdPartyImports
)
, CompileError
, InstallRequires ( InstallRequires
, dependencies
, optionalDependencies
)
, Source ( Source
, sourceModule
, sourcePackage
)
, Python (Python)
, PythonVersion ( Python2
, Python3
)
, addDependency
, addOptionalDependency
, compileModule
, compilePrimitiveType
, compileTypeDeclaration
, compileTypeExpression
, empty
, insertLocalImport
, insertStandardImport
, insertThirdPartyImports
, runCodeGen
, spreadModulePaths
, stringLiteral
, toAttributeName
, toClassName
, toImportPath
, toNamePair
, unionInstallRequires
) where
import qualified Control.Monad.State as ST
import qualified Data.List as L
import Data.Maybe (fromMaybe)
import Data.Typeable (Typeable)
import GHC.Exts (IsList (toList))
import Text.Printf (printf)
import qualified Data.Map.Strict as M
import qualified Data.SemVer as SV
import qualified Data.Set as S
import qualified Data.Text as T
import Data.Text.Encoding (decodeUtf8, encodeUtf8)
import System.FilePath (joinPath)
import qualified Text.Email.Validate as E
import Text.InterpolatedString.Perl6 (q, qq)
import qualified Nirum.CodeGen as C
import Nirum.CodeGen (Failure)
import qualified Nirum.Constructs.DeclarationSet as DS
import Nirum.Constructs.Identifier ( Identifier
, toPascalCaseText
, toSnakeCaseText
, toString
)
import Nirum.Constructs.ModulePath (ModulePath, ancestors)
import Nirum.Constructs.Name (Name (Name))
import qualified Nirum.Constructs.Name as N
import Nirum.Constructs.Service ( Method ( Method
, methodName
, parameters
, returnType
)
, Parameter (Parameter)
, Service (Service)
)
import Nirum.Constructs.TypeDeclaration ( EnumMember (EnumMember)
, Field (Field, fieldName)
, PrimitiveTypeIdentifier (..)
, Tag (Tag)
, Type ( Alias
, EnumType
, PrimitiveType
, RecordType
, UnboxedType
, UnionType
)
, TypeDeclaration (..)
)
import Nirum.Constructs.TypeExpression ( TypeExpression ( ListModifier
, MapModifier
, OptionModifier
, SetModifier
, TypeIdentifier
)
)
import Nirum.Package ( BoundModule
, Package (Package, metadata, modules)
, TypeLookup (Imported, Local, Missing)
, lookupType
, resolveBoundModule
, types
)
import Nirum.Package.Metadata ( Author (Author, name, email)
, Metadata (authors, target, version)
, Target ( CompileError
, CompileResult
, compilePackage
, parseTarget
, showCompileError
, targetName
, toByteString
)
, stringField
)
import qualified Nirum.Package.ModuleSet as MS
newtype Python = Python { packageName :: T.Text
} deriving (Eq, Ord, Show, Typeable)
type Package' = Package Python
type CompileError' = T.Text
data PythonVersion = Python2
| Python3
deriving (Eq, Ord, Show)
data Source = Source { sourcePackage :: Package'
, sourceModule :: BoundModule Python
} deriving (Eq, Ord, Show)
type Code = T.Text
instance Failure CodeGenContext CompileError' where
fromString = return . T.pack
data CodeGenContext
= CodeGenContext { standardImports :: S.Set T.Text
, thirdPartyImports :: M.Map T.Text (S.Set T.Text)
, localImports :: M.Map T.Text (S.Set T.Text)
, pythonVersion :: PythonVersion
}
deriving (Eq, Ord, Show)
sourceDirectory :: PythonVersion -> T.Text
sourceDirectory Python2 = "src-py2"
sourceDirectory Python3 = "src"
empty :: PythonVersion -> CodeGenContext
empty pythonVersion' = CodeGenContext { standardImports = []
, thirdPartyImports = []
, localImports = []
, pythonVersion = pythonVersion'
}
type CodeGen = C.CodeGen CodeGenContext CompileError'
runCodeGen :: CodeGen a
-> CodeGenContext
-> (Either CompileError' a, CodeGenContext)
runCodeGen = C.runCodeGen
insertStandardImport :: T.Text -> CodeGen ()
insertStandardImport module' = ST.modify insert'
where
insert' c@CodeGenContext { standardImports = si } =
c { standardImports = S.insert module' si }
insertThirdPartyImports :: [(T.Text, S.Set T.Text)] -> CodeGen ()
insertThirdPartyImports imports = ST.modify insert'
where
insert' c@CodeGenContext { thirdPartyImports = ti } =
c { thirdPartyImports = L.foldl (M.unionWith S.union) ti importList }
importList :: [M.Map T.Text (S.Set T.Text)]
importList = map (uncurry M.singleton) imports
insertLocalImport :: T.Text -> T.Text -> CodeGen ()
insertLocalImport module' object = ST.modify insert'
where
insert' c@CodeGenContext { localImports = li } =
c { localImports = M.insertWith S.union module' [object] li }
insertTypingImport :: CodeGen ()
insertTypingImport = do
pyVer <- getPythonVersion
case pyVer of
Python2 -> return ()
Python3 -> insertStandardImport "typing"
insertEnumImport :: CodeGen ()
insertEnumImport = insertStandardImport "enum"
getPythonVersion :: CodeGen PythonVersion
getPythonVersion = fmap pythonVersion ST.get
-- | The set of Python reserved keywords.
-- See also: https://docs.python.org/3/reference/lexical_analysis.html#keywords
keywords :: S.Set T.Text
keywords = [ "False", "None", "True"
, "and", "as", "assert", "break", "class", "continue"
, "def", "del" , "elif", "else", "except", "finally"
, "for", "from", "global", "if", "import", "in", "is"
, "lambda", "nonlocal", "not", "or", "pass", "raise"
, "return", "try", "while", "with", "yield"
]
toClassName :: Identifier -> T.Text
toClassName identifier =
if className `S.member` keywords then className `T.snoc` '_' else className
where
className :: T.Text
className = toPascalCaseText identifier
toClassName' :: Name -> T.Text
toClassName' = toClassName . N.facialName
toAttributeName :: Identifier -> T.Text
toAttributeName identifier =
if attrName `S.member` keywords then attrName `T.snoc` '_' else attrName
where
attrName :: T.Text
attrName = toSnakeCaseText identifier
toAttributeName' :: Name -> T.Text
toAttributeName' = toAttributeName . N.facialName
toImportPath :: ModulePath -> T.Text
toImportPath = T.intercalate "." . map toAttributeName . toList
toNamePair :: Name -> T.Text
toNamePair (Name f b) = [qq|('{toAttributeName f}', '{toSnakeCaseText b}')|]
stringLiteral :: T.Text -> T.Text
stringLiteral string =
open $ T.concatMap esc string `T.snoc` '"'
where
open :: T.Text -> T.Text
open = if T.any (> '\xff') string then T.append [qq|u"|] else T.cons '"'
esc :: Char -> T.Text
esc '"' = "\\\""
esc '\\' = "\\\\"
esc '\t' = "\\t"
esc '\n' = "\\n"
esc '\r' = "\\r"
esc c
| c >= '\x10000' = T.pack $ printf "\\U%08x" c
| c >= '\xff' = T.pack $ printf "\\u%04x" c
| c < ' ' || c >= '\x7f' = T.pack $ printf "\\x%02x" c
| otherwise = T.singleton c
toIndentedCodes :: (a -> T.Text) -> [a] -> T.Text -> T.Text
toIndentedCodes f traversable concatenator =
T.intercalate concatenator $ map f traversable
quote :: T.Text -> T.Text
quote s = [qq|'{s}'|]
typeReprCompiler :: CodeGen (Code -> Code)
typeReprCompiler = do
insertTypingImport
ver <- getPythonVersion
return $ case ver of
Python2 -> \ t -> [qq|($t.__module__ + '.' + $t.__name__)|]
Python3 -> \ t -> [qq|typing._type_repr($t)|]
type ParameterName = Code
type ParameterType = Code
type ReturnType = Code
parameterCompiler :: CodeGen (ParameterName -> ParameterType -> Code)
parameterCompiler = do
ver <- getPythonVersion
return $ \ n t -> case ver of
Python2 -> n
Python3 -> [qq|$n: $t|]
returnCompiler :: CodeGen (ReturnType -> Code)
returnCompiler = do
ver <- getPythonVersion
return $ \ r -> case ver of
Python2 -> ""
Python3 -> [qq| -> $r|]
compileUnionTag :: Source
-> Name
-> Name
-> DS.DeclarationSet Field
-> CodeGen Code
compileUnionTag source parentname typename' fields = do
typeExprCodes <- mapM (compileTypeExpression source)
[typeExpr | (Field _ typeExpr _) <- toList fields]
let className = toClassName' typename'
tagNames = map (toAttributeName' . fieldName) (toList fields)
nameNTypes = zip tagNames typeExprCodes
slotTypes = toIndentedCodes
(\ (n, t) -> [qq|'{n}': {t}|]) nameNTypes ",\n "
slots = if length tagNames == 1
then [qq|'{head tagNames}'|] `T.snoc` ','
else toIndentedCodes (\ n -> [qq|'{n}'|]) tagNames ",\n "
hashTuple = if null tagNames
then "self.__nirum_tag__"
else [qq|({attributes},)|] :: T.Text
where
attributes :: T.Text
attributes = toIndentedCodes (\ n -> [qq|self.{n}|]) tagNames ", "
initialArgs gen = toIndentedCodes (uncurry gen) nameNTypes ", "
initialValues =
toIndentedCodes (\ n -> [qq|self.{n} = {n}|]) tagNames "\n "
nameMaps = toIndentedCodes
toNamePair
(map fieldName $ toList fields)
",\n "
parentClass = toClassName' parentname
insertTypingImport
insertThirdPartyImports [ ("nirum.validate", ["validate_union_type"])
, ("nirum.constructs", ["name_dict_type"])
]
typeRepr <- typeReprCompiler
arg <- parameterCompiler
ret <- returnCompiler
return [qq|
class $className($parentClass):
# TODO: docstring
__slots__ = (
$slots
)
__nirum_tag__ = $parentClass.Tag.{toAttributeName' typename'}
__nirum_tag_types__ = \{
$slotTypes
\}
__nirum_tag_names__ = name_dict_type([
$nameMaps
])
def __init__(self, {initialArgs arg}){ ret "None" }:
$initialValues
validate_union_type(self)
def __repr__(self){ ret "str" }:
return '\{0\}(\{1\})'.format(
{typeRepr "type(self)"},
', '.join('\{\}=\{\}'.format(attr, getattr(self, attr))
for attr in self.__slots__)
)
def __eq__(self, other){ ret "bool" }:
return isinstance(other, $className) and all(
getattr(self, attr) == getattr(other, attr)
for attr in self.__slots__
)
def __ne__(self, other){ ret "bool" }:
return not self == other
def __hash__(self){ ret "int" }:
return hash($hashTuple)
|]
compilePrimitiveType :: PrimitiveTypeIdentifier -> CodeGen Code
compilePrimitiveType primitiveTypeIdentifier = do
pyVer <- getPythonVersion
case (primitiveTypeIdentifier, pyVer) of
(Bool, _) -> return "bool"
(Bigint, _) -> return "int"
(Decimal, _) -> do
insertStandardImport "decimal"
return "decimal.Decimal"
(Int32, _) -> return "int"
(Int64, Python2) -> do
insertStandardImport "numbers"
return "numbers.Integral"
(Int64, Python3) -> return "int"
(Float32, _) -> return "float"
(Float64, _) -> return "float"
(Text, Python2) -> return "unicode"
(Text, Python3) -> return "str"
(Binary, _) -> return "bytes"
(Date, _) -> do
insertStandardImport "datetime"
return "datetime.date"
(Datetime, _) -> do
insertStandardImport "datetime"
return "datetime.datetime"
(Uuid, _) -> insertStandardImport "uuid" >> return "uuid.UUID"
(Uri, Python2) -> return "unicode"
(Uri, Python3) -> return "str"
compileTypeExpression :: Source -> TypeExpression -> CodeGen Code
compileTypeExpression Source { sourceModule = boundModule } (TypeIdentifier i) =
case lookupType i boundModule of
Missing -> fail $ "undefined identifier: " ++ toString i
Imported _ (PrimitiveType p _) -> compilePrimitiveType p
Imported m _ -> do
insertThirdPartyImports [(toImportPath m, [toClassName i])]
return $ toClassName i
Local _ -> return $ toClassName i
compileTypeExpression source (MapModifier k v) = do
kExpr <- compileTypeExpression source k
vExpr <- compileTypeExpression source v
insertTypingImport
return [qq|typing.Mapping[$kExpr, $vExpr]|]
compileTypeExpression source modifier = do
expr <- compileTypeExpression source typeExpr
insertTypingImport
return [qq|typing.$className[$expr]|]
where
typeExpr :: TypeExpression
className :: T.Text
(typeExpr, className) = case modifier of
OptionModifier t' -> (t', "Optional")
SetModifier t' -> (t', "AbstractSet")
ListModifier t' -> (t', "Sequence")
TypeIdentifier _ -> undefined -- never happen!
MapModifier _ _ -> undefined -- never happen!
compileTypeDeclaration :: Source -> TypeDeclaration -> CodeGen Code
compileTypeDeclaration _ TypeDeclaration { type' = PrimitiveType {} } =
return "" -- never used
compileTypeDeclaration src TypeDeclaration { typename = typename'
, type' = Alias ctype } = do
ctypeExpr <- compileTypeExpression src ctype
return [qq|
# TODO: docstring
{toClassName' typename'} = $ctypeExpr
|]
compileTypeDeclaration src TypeDeclaration { typename = typename'
, type' = UnboxedType itype } = do
let className = toClassName' typename'
itypeExpr <- compileTypeExpression src itype
insertTypingImport
insertThirdPartyImports [ ("nirum.validate", ["validate_boxed_type"])
, ("nirum.serialize", ["serialize_boxed_type"])
, ("nirum.deserialize", ["deserialize_boxed_type"])
]
arg <- parameterCompiler
typeRepr <- typeReprCompiler
ret <- returnCompiler
return [qq|
class $className(object):
# TODO: docstring
__nirum_inner_type__ = $itypeExpr
def __init__(self, { arg "value" itypeExpr }){ ret "None" }:
validate_boxed_type(value, $itypeExpr)
self.value = value # type: $itypeExpr
def __eq__(self, other){ ret "bool" }:
return (isinstance(other, $className) and
self.value == other.value)
def __ne__(self, other){ ret "bool" }:
return not self == other
def __hash__(self){ ret "int" }:
return hash(self.value)
def __nirum_serialize__(self){ ret "typing.Any" }:
return serialize_boxed_type(self)
@classmethod
def __nirum_deserialize__(
{arg "cls" "type"},
{arg "value" "typing.Any"}
){ ret $ quote className }:
return deserialize_boxed_type(cls, value)
def __repr__(self){ ret "str" }:
return '\{0\}(\{1!r\})'.format(
{typeRepr "type(self)"}, self.value
)
def __hash__(self){ ret "int" }:
return hash(self.value)
|]
compileTypeDeclaration _ TypeDeclaration { typename = typename'
, type' = EnumType members } = do
let className = toClassName' typename'
memberNames = T.intercalate
"\n "
[ [qq|{toAttributeName' memberName} = '{toSnakeCaseText bn}'|]
| EnumMember memberName@(Name _ bn) _ <- toList members
]
insertEnumImport
arg <- parameterCompiler
ret <- returnCompiler
return [qq|
class $className(enum.Enum):
# TODO: docstring
$memberNames
def __nirum_serialize__(self){ ret "str" }:
return self.value
@classmethod
def __nirum_deserialize__(
{arg "cls" "type"},
{arg "value" "str"}
){ ret $ quote className }:
return cls(value.replace('-', '_')) # FIXME: validate input
|]
compileTypeDeclaration src TypeDeclaration { typename = typename'
, type' = RecordType fields } = do
typeExprCodes <- mapM (compileTypeExpression src)
[typeExpr | (Field _ typeExpr _) <- toList fields]
let className = toClassName' typename'
fieldNames = map toAttributeName' [ name'
| (Field name' _ _) <- toList fields
]
nameNTypes = zip fieldNames typeExprCodes
slotTypes = toIndentedCodes
(\ (n, t) -> [qq|'{n}': {t}|]) nameNTypes ",\n "
slots = toIndentedCodes (\ n -> [qq|'{n}'|]) fieldNames ",\n "
initialArgs gen = toIndentedCodes (uncurry gen) nameNTypes ", "
initialValues = toIndentedCodes
(\ n -> [qq|self.{n} = {n}|]) fieldNames "\n "
nameMaps = toIndentedCodes
toNamePair
(map fieldName $ toList fields)
",\n "
hashText = toIndentedCodes (\ n -> [qq|self.{n}|]) fieldNames ", "
insertTypingImport
insertThirdPartyImports [ ("nirum.validate", ["validate_record_type"])
, ("nirum.serialize", ["serialize_record_type"])
, ("nirum.deserialize", ["deserialize_record_type"])
, ("nirum.constructs", ["name_dict_type"])
]
arg <- parameterCompiler
ret <- returnCompiler
typeRepr <- typeReprCompiler
let clsType = arg "cls" "type"
return [qq|
class $className(object):
# TODO: docstring
__slots__ = (
$slots,
)
__nirum_record_behind_name__ = '{toSnakeCaseText $ N.behindName typename'}'
__nirum_field_types__ = \{
$slotTypes
\}
__nirum_field_names__ = name_dict_type([
$nameMaps
])
def __init__(self, {initialArgs arg}){ret "None"}:
$initialValues
validate_record_type(self)
def __repr__(self){ret "bool"}:
return '\{0\}(\{1\})'.format(
{typeRepr "type(self)"},
', '.join('\{\}=\{\}'.format(attr, getattr(self, attr))
for attr in self.__slots__)
)
def __eq__(self, other){ret "bool"}:
return isinstance(other, $className) and all(
getattr(self, attr) == getattr(other, attr)
for attr in self.__slots__
)
def __ne__(self, other){ ret "bool" }:
return not self == other
def __nirum_serialize__(self){ret "typing.Mapping[str, typing.Any]"}:
return serialize_record_type(self)
@classmethod
def __nirum_deserialize__($clsType, value){ ret $ quote className }:
return deserialize_record_type(cls, value)
def __hash__(self){ret "int"}:
return hash(($hashText,))
|]
compileTypeDeclaration src TypeDeclaration { typename = typename'
, type' = UnionType tags } = do
fieldCodes <- mapM (uncurry (compileUnionTag src typename')) tagNameNFields
let className = toClassName' typename'
fieldCodes' = T.intercalate "\n\n" fieldCodes
enumMembers = toIndentedCodes
(\ (t, b) -> [qq|$t = '{b}'|]) enumMembers' "\n "
insertTypingImport
insertEnumImport
insertThirdPartyImports [ ("nirum.serialize", ["serialize_union_type"])
, ("nirum.deserialize", ["deserialize_union_type"])
, ("nirum.constructs", ["name_dict_type"])
]
typeRepr <- typeReprCompiler
ret <- returnCompiler
arg <- parameterCompiler
return [qq|
class $className(object):
__nirum_union_behind_name__ = '{toSnakeCaseText $ N.behindName typename'}'
__nirum_field_names__ = name_dict_type([
$nameMaps
])
class Tag(enum.Enum):
$enumMembers
def __init__(self, *args, **kwargs):
raise NotImplementedError(
"\{0\} cannot be instantiated "
"since it is an abstract class. Instantiate a concrete subtype "
"of it instead.".format({typeRepr "type(self)"})
)
def __nirum_serialize__(self){ ret "typing.Mapping[str, typing.Any]" }:
return serialize_union_type(self)
@classmethod
def __nirum_deserialize__(
{arg "cls" "type"}, value
){ ret $ quote className }:
return deserialize_union_type(cls, value)
$fieldCodes'
|]
where
tagNameNFields :: [(Name, DS.DeclarationSet Field)]
tagNameNFields = [ (tagName, fields)
| (Tag tagName fields _) <- toList tags
]
enumMembers' :: [(T.Text, T.Text)]
enumMembers' = [ ( toAttributeName' tagName
, toSnakeCaseText $ N.behindName tagName
)
| (Tag tagName _ _) <- toList tags
]
nameMaps :: T.Text
nameMaps = toIndentedCodes
toNamePair
[name' | (name', _) <- tagNameNFields]
",\n "
compileTypeDeclaration src ServiceDeclaration { serviceName = name'
, service = Service methods } = do
let methods' = toList methods
methodMetadata <- mapM compileMethodMetadata methods'
let methodMetadata' = commaNl methodMetadata
dummyMethods <- mapM compileMethod methods'
clientMethods <- mapM compileClientMethod methods'
let dummyMethods' = T.intercalate "\n\n" dummyMethods
clientMethods' = T.intercalate "\n\n" clientMethods
insertStandardImport "json"
insertThirdPartyImports [ ("nirum.constructs", ["name_dict_type"])
, ("nirum.deserialize", ["deserialize_meta"])
, ("nirum.serialize", ["serialize_meta"])
, ("nirum.rpc", [ "service_type"
, "client_type"
])
]
return [qq|
class $className(service_type):
__nirum_service_methods__ = \{
{methodMetadata'}
\}
__nirum_method_names__ = name_dict_type([
$methodNameMap
])
{dummyMethods'}
# FIXME client MUST be generated & saved on diffrent module
# where service isn't included.
class {className}_Client(client_type, $className):
{clientMethods'}
pass
|]
where
className :: T.Text
className = toClassName' name'
commaNl :: [T.Text] -> T.Text
commaNl = T.intercalate ",\n"
compileMethod :: Method -> CodeGen Code
compileMethod (Method mName params rtype _etype _anno) = do
let mName' = toAttributeName' mName
params' <- mapM compileMethodParameter $ toList params
rtypeExpr <- compileTypeExpression src rtype
ret <- returnCompiler
return [qq|
def {mName'}(self, {commaNl params'}){ ret rtypeExpr }:
raise NotImplementedError('$className has to implement {mName'}()')
|]
compileMethodParameter :: Parameter -> CodeGen Code
compileMethodParameter (Parameter pName pType _) = do
pTypeExpr <- compileTypeExpression src pType
arg <- parameterCompiler
return [qq|{arg (toAttributeName' pName) pTypeExpr}|]
compileMethodMetadata :: Method -> CodeGen Code
compileMethodMetadata Method { methodName = mName
, parameters = params
, returnType = rtype
} = do
let params' = toList params :: [Parameter]
rtypeExpr <- compileTypeExpression src rtype
paramMetadata <- mapM compileParameterMetadata params'
let paramMetadata' = commaNl paramMetadata
insertThirdPartyImports [("nirum.constructs", ["name_dict_type"])]
return [qq|'{toAttributeName' mName}': \{
'_return': $rtypeExpr,
'_names': name_dict_type([{paramNameMap params'}]),
{paramMetadata'}
\}|]
compileParameterMetadata :: Parameter -> CodeGen Code
compileParameterMetadata (Parameter pName pType _) = do
let pName' = toAttributeName' pName
pTypeExpr <- compileTypeExpression src pType
return [qq|'{pName'}': $pTypeExpr|]
methodNameMap :: T.Text
methodNameMap = toIndentedCodes
toNamePair
[mName | Method { methodName = mName } <- toList methods]
",\n "
paramNameMap :: [Parameter] -> T.Text
paramNameMap params = toIndentedCodes
toNamePair [pName | Parameter pName _ _ <- params] ",\n "
compileClientPayload :: Parameter -> CodeGen Code
compileClientPayload (Parameter pName _ _) = do
let pName' = toAttributeName' pName
return [qq|meta['_names']['{pName'}']: serialize_meta({pName'})|]
compileClientMethod :: Method -> CodeGen Code
compileClientMethod Method { methodName = mName
, parameters = params
, returnType = rtype
} = do
let clientMethodName' = toAttributeName' mName
params' <- mapM compileMethodParameter $ toList params
rtypeExpr <- compileTypeExpression src rtype
payloadArguments <- mapM compileClientPayload $ toList params
ret <- returnCompiler
return [qq|
def {clientMethodName'}(self, {commaNl params'}){ ret rtypeExpr }:
meta = self.__nirum_service_methods__['{clientMethodName'}']
return deserialize_meta(
meta['_return'],
json.loads(
self.remote_call(
self.__nirum_method_names__['{clientMethodName'}'],
payload=\{{commaNl payloadArguments}\}
)
)
)
|]
compileTypeDeclaration _ Import {} =
return "" -- Nothing to compile
compileModuleBody :: Source -> CodeGen Code
compileModuleBody src@Source { sourceModule = boundModule } = do
let types' = types boundModule
typeCodes <- mapM (compileTypeDeclaration src) $ toList types'
let moduleCode = T.intercalate "\n\n" typeCodes
return [qq|
# TODO: docs
$moduleCode
|]
data InstallRequires =
InstallRequires { dependencies :: S.Set T.Text
, optionalDependencies :: M.Map (Int, Int) (S.Set T.Text)
} deriving (Eq, Ord, Show)
addDependency :: InstallRequires -> T.Text -> InstallRequires
addDependency requires package =
requires { dependencies = S.insert package $ dependencies requires }
addOptionalDependency :: InstallRequires
-> (Int, Int) -- ^ Python version already stasified
-> T.Text -- ^ PyPI package name
-> InstallRequires
addOptionalDependency requires pyVer package =
requires { optionalDependencies = newOptDeps }
where
oldOptDeps :: M.Map (Int, Int) (S.Set T.Text)
oldOptDeps = optionalDependencies requires
newOptDeps :: M.Map (Int, Int) (S.Set T.Text)
newOptDeps = M.alter (Just . S.insert package . fromMaybe S.empty)
pyVer oldOptDeps
unionInstallRequires :: InstallRequires -> InstallRequires -> InstallRequires
unionInstallRequires a b =
a { dependencies = S.union (dependencies a) (dependencies b)
, optionalDependencies = M.unionWith S.union (optionalDependencies a)
(optionalDependencies b)
}
compileModule :: PythonVersion
-> Source
-> Either CompileError' (InstallRequires, Code)
compileModule pythonVersion' source =
case runCodeGen code' $ empty pythonVersion' of
(Left errMsg, _) -> Left errMsg
(Right code, context) -> codeWithDeps context $
[qq|# -*- coding: utf-8 -*-
{imports $ standardImports context}
{fromImports $ localImports context}
{fromImports $ thirdPartyImports context}
{code}
|]
where
code' :: CodeGen T.Text
code' = compileModuleBody source
imports :: S.Set T.Text -> T.Text
imports importSet =
if S.null importSet
then ""
else "import " `T.append` T.intercalate "," (S.elems importSet)
fromImports :: M.Map T.Text (S.Set T.Text) -> T.Text
fromImports importMap =
T.intercalate "\n"
[ [qq|from $from import {T.intercalate ", " $ S.elems vars}|]
| (from, vars) <- M.assocs importMap
]
has :: S.Set T.Text -> T.Text -> Bool
has set module' = module' `S.member` set ||
any (T.isPrefixOf $ module' `T.snoc` '.') set
require :: T.Text -> T.Text -> S.Set T.Text -> S.Set T.Text
require pkg module' set =
if set `has` module' then S.singleton pkg else S.empty
codeWithDeps :: CodeGenContext
-> Code
-> Either CompileError' (InstallRequires, Code)
codeWithDeps context c = Right (InstallRequires deps optDeps, c)
where
deps :: S.Set T.Text
deps = require "nirum" "nirum" $ M.keysSet $ thirdPartyImports context
optDeps :: M.Map (Int, Int) (S.Set T.Text)
optDeps =
[ ((3, 4), require "enum34" "enum" $ standardImports context)
, ((3, 5), require "typing" "typing" $ standardImports context)
]
spreadModulePaths :: [ModulePath] -> [Code]
spreadModulePaths modulePaths = pathsToPackageNames $ map ancestors modulePaths
where
pathsToPackageNames :: [S.Set ModulePath] -> [Code]
pathsToPackageNames modulePaths' = S.toAscList $ S.map toImportPath $
S.unions modulePaths'
compilePackageMetadata :: Package' -> InstallRequires -> Code
compilePackageMetadata package@Package { metadata = metadata' }
(InstallRequires deps optDeps) =
[qq|# -*- coding: utf-8 -*-
import sys
from setuptools import setup, __version__ as setuptools_version
install_requires = [$pInstallRequires]
polyfill_requires = \{$pPolyfillRequires}
if polyfill_requires:
# '<' operator for environment markers are supported since setuptools 17.1.
# Read PEP 496 for details of environment markers.
setup_requires = ['setuptools >= 17.1']
if tuple(map(int, setuptools_version.split('.'))) < (17, 1):
extras_require = \{}
if 'bdist_wheel' not in sys.argv:
for (major, minor), deps in polyfill_requires.items():
if sys.version_info < (major, minor):
install_requires.extend(deps)
envmarker = ":python_version=='\{0}.\{1}'"
python_versions = [(2, 6), (2, 7),
(3, 3), (3, 4), (3, 5), (3, 6)] # FIXME
for pyver in python_versions:
extras_require[envmarker.format(*pyver)] = list(\{
d
for v, vdeps in polyfill_requires.items()
if pyver < v
for d in vdeps
})
else:
extras_require = \{
":python_version<'\{0}.\{1}'".format(*pyver): deps
for pyver, deps in polyfill_requires.items()
}
else:
setup_requires = []
extras_require = \{}
SOURCE_ROOT = '{sourceDirectory Python3}'
if sys.version_info < (3, 0):
SOURCE_ROOT = '{sourceDirectory Python2}'
# TODO: description, long_description, url, license,
# keywords, classifiers
setup(
name='{pName}',
version='{pVersion}',
author=$author,
author_email=$authorEmail,
package_dir=\{'': SOURCE_ROOT},
packages=[$pPackages],
provides=[$pPackages],
requires=[$pInstallRequires],
setup_requires=setup_requires,
install_requires=install_requires,
extras_require=extras_require,
)
|]
where
csStrings :: [T.Text] -> T.Text
csStrings [] = "None"
csStrings s = stringLiteral $ T.intercalate ", " s
pName :: Code
pName = packageName $ target metadata'
pVersion :: Code
pVersion = SV.toText $ version metadata'
strings :: [Code] -> Code
strings values = T.intercalate ", " $ map stringLiteral (L.sort values)
author :: Code
author = csStrings [aName | Author { name = aName } <- authors metadata']
authorEmail :: Code
authorEmail = csStrings [ decodeUtf8 (E.toByteString e)
| Author { email = Just e } <- authors metadata'
]
pPackages :: Code
pPackages = strings $ spreadModulePaths $ MS.keys $ modules package
pInstallRequires :: Code
pInstallRequires = strings $ S.toList deps
pPolyfillRequires :: Code
pPolyfillRequires = T.intercalate ", "
[ [qq|($major, $minor): [{strings $ S.toList deps'}]|]
| ((major, minor), deps') <- M.toList optDeps
]
manifestIn :: Code
manifestIn = [q|recursive-include src *.py
recursive-include src-py2 *.py
|]
compilePackage' :: Package'
-> M.Map FilePath (Either CompileError' Code)
compilePackage' package =
M.fromList $
initFiles ++
[ ( f
, case cd of
Left e -> Left e
Right (_, cd') -> Right cd'
)
| (f, cd) <- modules'
] ++
[ ("setup.py", Right $ compilePackageMetadata package installRequires)
, ("MANIFEST.in", Right manifestIn)
]
where
toPythonFilename :: ModulePath -> [FilePath]
toPythonFilename mp = [ T.unpack (toAttributeName i)
| i <- toList mp
] ++ ["__init__.py"]
versions :: [PythonVersion]
versions = [Python2, Python3]
toFilename :: T.Text -> ModulePath -> FilePath
toFilename sourceRootDirectory mp =
joinPath $ T.unpack sourceRootDirectory : toPythonFilename mp
initFiles :: [(FilePath, Either CompileError' Code)]
initFiles = [ (toFilename (sourceDirectory ver) mp', Right "")
| mp <- MS.keys (modules package)
, mp' <- S.elems (ancestors mp)
, ver <- versions
]
modules' :: [(FilePath, Either CompileError' (InstallRequires, Code))]
modules' =
[ ( toFilename (sourceDirectory ver) modulePath'
, compileModule ver $ Source package boundModule
)
| (modulePath', _) <- MS.toAscList (modules package)
, Just boundModule <- [resolveBoundModule modulePath' package]
, ver <- versions
]
installRequires :: InstallRequires
installRequires = foldl unionInstallRequires
(InstallRequires [] [])
[deps | (_, Right (deps, _)) <- modules']
instance Target Python where
type CompileResult Python = Code
type CompileError Python = CompileError'
targetName _ = "python"
parseTarget table = do
name' <- stringField "name" table
return Python { packageName = name' }
compilePackage = compilePackage'
showCompileError _ e = e
toByteString _ = encodeUtf8