mdl_convert.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. import json
  2. import re
  3. def get_field(field, block, num=1):
  4. if num == 2:
  5. res = re.search(field + r' (\d+) "([^"]*)" ', block)
  6. if res:
  7. return res[1], res[2]
  8. return "0", ""
  9. res = re.search(field + r' "([^"]*)" ', block)
  10. if res:
  11. return res[1]
  12. res = re.search(field + r" (\w*) ", block)
  13. if res:
  14. return res[1]
  15. return ""
  16. def ignore(block):
  17. s = block.split(" ")
  18. return (s[0], block)
  19. def model_name(block):
  20. return {
  21. "Type": "ModelName",
  22. "Name": get_field("Name", block),
  23. "ModelCodePage": get_field("ModelCodePage", block),
  24. "AutoAccess": get_field("AutoAccess", block),
  25. "UpdateCycle": get_field("UpdateCycle", block),
  26. "ModelStamp": get_field("ModelStamp", block),
  27. "Version": get_field("Version", block),
  28. "ModelCategoryOrderDefault": get_field("ModelCategoryOrderDefault", block),
  29. "ModelOrderedByDefault": get_field("ModelOrderedByDefault", block),
  30. "ModelNonRollupHierarchies": get_field("ModelNonRollupHierarchies", block),
  31. }
  32. def cognos_source(block):
  33. id, name = get_field("CognosSource", block, 2)
  34. return {
  35. "Type": "CognosSource",
  36. "ID": id,
  37. "Name": name,
  38. "SourceType": get_field("SourceType", block),
  39. "SourcePath": get_field("SourcePath", block),
  40. "PackageTimeStamp": get_field("PackageTimeStamp", block),
  41. }
  42. def cognos_package_datasource_connection(block):
  43. id, name = get_field("CognosPackageDatasourceConnection", block, 2)
  44. prs_id, prs_name = get_field("PackageReportSource", block, 2)
  45. return {
  46. "Type": "CognosPackageDatasourceConnection",
  47. "ID": id,
  48. "Name": name,
  49. "PackageReportSource": {
  50. "ID": prs_id,
  51. "Name": prs_name,
  52. },
  53. "CognosPackageConnection": get_field("CognosPackageConnection", block),
  54. "CognosPackageConnectionSignon": get_field(
  55. "CognosPackageConnectionSignon", block
  56. ),
  57. "CognosPackageAlwaysUseTransformerSignon": get_field(
  58. "CognosPackageAlwaysUseTransformerSignon", block
  59. ),
  60. "CognosPackagePowercubeSource": get_field(
  61. "CognosPackagePowercubeSource", block
  62. ),
  63. }
  64. def data_source(block):
  65. id, name = get_field("DataSource", block, 2)
  66. prs_id, prs_name = get_field("PackageReportSource", block, 2)
  67. return {
  68. "Type": "DataSource",
  69. "ID": id,
  70. "Name": name,
  71. "Separator": get_field("Separator", block),
  72. "SourceType": get_field("SourceType", block),
  73. "CharacterSet": get_field("CharacterSet", block),
  74. "DecimalSep": get_field("DecimalSep", block),
  75. "Thousandsep": get_field("Thousandsep", block),
  76. "HasColumns": get_field("Columns", block),
  77. "Timing": get_field("Timing", block),
  78. "PackageReportSource": {
  79. "ID": prs_id,
  80. "Name": prs_name,
  81. },
  82. "AutoSummary": get_field("AutoSummary", block),
  83. "SetCurrent": get_field("SetCurrent", block),
  84. "ServerSource": get_field("ServerSource", block),
  85. "Speed": get_field("Speed", block),
  86. "Presummarized": get_field("Presummarized", block),
  87. "StreamExtractSize": get_field("StreamExtractSize", block),
  88. "Columns": [],
  89. }
  90. def org_name(block):
  91. id, name = get_field("OrgName", block, 2)
  92. return {
  93. "Type": "OrgName",
  94. "ID": id,
  95. "Name": name,
  96. "Origin": get_field("Origin", block),
  97. "Offset": get_field("Offset", block),
  98. "Column": get_field("Column", block),
  99. "Storage": get_field("Storage", block),
  100. "Scale": get_field("Scale", block),
  101. "Size": get_field("Size", block),
  102. "Decimals": get_field("Decimals", block),
  103. "Class": get_field("Class", block),
  104. "InputScale": get_field("InputScale", block),
  105. "TimeArray": get_field("TimeArray", block),
  106. "ColSrcType": get_field("ColSrcType", block),
  107. }
  108. def dimension(block):
  109. id, name = get_field("Dimension", block, 2)
  110. return {
  111. "Type": "Dimension",
  112. "ID": id,
  113. "Name": name,
  114. "DimType": get_field("DimType", block),
  115. "EarliestDate": get_field("EarliestDate", block),
  116. "LatestDate": get_field("LatestDate", block),
  117. "ManualPeriods": get_field("ManualPeriods", block),
  118. "DaysInWeek": get_field("DaysInWeek", block),
  119. "NewCatsLock": get_field("NewCatsLock", block),
  120. "ExcludeAutoPartitioning": get_field("ExcludeAutoPartitioning", block),
  121. "DimDefaultCategory": get_field("DimDefaultCategory", block),
  122. "Root": {},
  123. "Levels": [],
  124. "Categories": [],
  125. "SpecialCategories": [],
  126. }
  127. def root(block):
  128. id, name = get_field("Root", block, 2)
  129. return {
  130. "Type": "Root",
  131. "ID": id,
  132. "Name": name,
  133. "Inclusion": get_field("Inclusion", block),
  134. "Lastuse": get_field("Lastuse", block),
  135. "Date": get_field("Date", block),
  136. "Filtered": get_field("Filtered", block),
  137. "Suppressed": get_field("Suppressed", block),
  138. "Sign": get_field("Sign", block),
  139. "HideValue": get_field("HideValue", block),
  140. "IsKeyOrphanage": get_field("IsKeyOrphanage", block),
  141. "IsTruncated": get_field("IsTruncated", block),
  142. "Blanks": get_field("Blanks", block),
  143. "Drill": {},
  144. }
  145. def levels(block):
  146. id, name = get_field("Levels", block, 2)
  147. return {
  148. "Type": "Levels",
  149. "ID": id,
  150. "Name": name,
  151. "Blanks": get_field("Blanks", block),
  152. "Inclusion": get_field("Inclusion", block),
  153. "DateFunction": get_field("DateFunction", block),
  154. "Generate": get_field("Generate", block),
  155. "RefreshLabel": get_field("RefreshLabel", block),
  156. "RefreshDescription": get_field("RefreshDescription", block),
  157. "RefreshShortName": get_field("RefreshShortName", block),
  158. "NewCatsLock": get_field("NewCatsLock", block),
  159. "CatLabFormat": get_field("CatLabFormat", block),
  160. "Timerank": get_field("Timerank", block),
  161. "UniqueCategories": get_field("UniqueCategories", block),
  162. "UniqueMove": get_field("UniqueMove", block),
  163. "Associations": [
  164. associations(b)
  165. for b in re.findall(
  166. r"Associations .*",
  167. block.replace("Associations ", "\nAssociations "),
  168. )
  169. ],
  170. }
  171. def category(block):
  172. id, name = get_field("Category", block, 2)
  173. return {
  174. "Type": "Category",
  175. "ID": id,
  176. "Name": name,
  177. "Parent": get_field("Parent", block),
  178. "Levels": get_field("Levels", block),
  179. "OrderBy": get_field("OrderBy", block),
  180. "Value": get_field("Value", block),
  181. "Label": get_field("Label", block),
  182. "Lastuse": get_field("Lastuse", block),
  183. "SourceValue": get_field("SourceValue", block),
  184. "Date": get_field("Date", block),
  185. "Filtered": get_field("Filtered", block),
  186. "Suppressed": get_field("Suppressed", block),
  187. "Sign": get_field("Sign", block),
  188. "HideValue": get_field("HideValue", block),
  189. "IsKeyOrphanage": get_field("IsKeyOrphanage", block),
  190. "IsTruncated": get_field("IsTruncated", block),
  191. "Blanks": get_field("Blanks", block),
  192. }
  193. def special_category(block):
  194. id, name = get_field("SpecialCategory", block, 2)
  195. return {
  196. "Type": "SpecialCategory",
  197. "ID": id,
  198. "Name": name,
  199. "Parent": get_field("Parent", block),
  200. "Levels": get_field("Levels", block),
  201. "Lastuse": get_field("Lastuse", block),
  202. "Rollup": get_field("Rollup", block),
  203. "TimeAggregate": get_field("TimeAggregate", block),
  204. "RunningPeriods": get_field("RunningPeriods", block),
  205. "TargetOffset": get_field("TargetOffset", block),
  206. "TargetLevel": get_field("TargetLevel", block),
  207. "ContextOffset": get_field("ContextOffset", block),
  208. "DateDrill": get_field("DateDrill", block),
  209. "Primary": get_field("Primary", block),
  210. "Sign": get_field("Sign", block),
  211. }
  212. def map_drills(block):
  213. return {
  214. "Type": "MapDrills",
  215. # "MapDrills MapDrill 1469 "
  216. }
  217. def view_name(block):
  218. id, name = get_field("ViewName", block, 2)
  219. return {
  220. "Type": "ViewName",
  221. "ID": id,
  222. "Name": name,
  223. "ViewType": get_field("Type", block),
  224. "ViewCustomView": get_field("ViewCustomView", block),
  225. }
  226. def associations(block):
  227. id, name = get_field("Associations", block, 2)
  228. return {
  229. "Type": "Associations",
  230. "ID": id,
  231. "Name": name,
  232. "AssociationType": get_field("AssociationType", block),
  233. "AssociationRole": get_field("AssociationRole", block),
  234. "AssociationReferenced": get_field("AssociationReferenced", block),
  235. "SortOrder": get_field("SortOrder", block),
  236. "SortAs": get_field("SortAs", block),
  237. }
  238. def drill(block):
  239. id, name = get_field("Drill", block, 2)
  240. return {
  241. "Type": "Drill",
  242. "ID": id,
  243. "Name": name,
  244. "Label": get_field("Label", block),
  245. "Inclusion": get_field("Inclusion", block),
  246. "Filtered": get_field("Filtered", block),
  247. "Suppressed": get_field("Suppressed", block),
  248. "PrimaryDrill": get_field("PrimaryDrill", block),
  249. "HideValue": get_field("HideValue", block),
  250. "YearBegins": get_field("YearBegins", block),
  251. "PartialWeek": get_field("PartialWeek", block),
  252. "ExtraWeek": get_field("ExtraWeek", block),
  253. "WeekBegins": get_field("WeekBegins", block),
  254. }
  255. def measure(block):
  256. id, name = get_field("Measure", block, 2)
  257. return {
  258. "Type": "Measure",
  259. "ID": id,
  260. "Name": name,
  261. "Missing": get_field("Missing", block),
  262. "IgnoreMissingValue": get_field("IgnoreMissingValue", block),
  263. "Storage": get_field("Storage", block),
  264. "OutPutScale": get_field("OutPutScale", block),
  265. "Decimals": get_field("Decimals", block),
  266. "ReverseSign": get_field("ReverseSign", block),
  267. "IsCurrency": get_field("IsCurrency", block),
  268. "IsFolder": get_field("IsFolder", block),
  269. "Format": get_field("Format", block),
  270. "DrillThrough": get_field("DrillThrough", block),
  271. "Associations": [
  272. associations(b)
  273. for b in re.findall(
  274. r"Associations .*",
  275. block.replace("Associations ", "\nAssociations "),
  276. )
  277. ],
  278. }
  279. def signon(block):
  280. id, name = get_field("Signon", block, 2)
  281. return {
  282. "Type": "Signon",
  283. "ID": id,
  284. "Name": name,
  285. "UserId": get_field("UserId", block),
  286. "PromptForPassword": get_field("PromptForPassword", block),
  287. "EncryptedPW": get_field("EncryptedPW", block),
  288. "AutoLogon": get_field("AutoLogon", block),
  289. "SignonType": get_field("SignonType", block),
  290. }
  291. def dimension_view(block):
  292. id, name = get_field("DimensionView", block, 2)
  293. return {
  294. "Type": "DimensionView",
  295. "ID": id,
  296. "Name": name
  297. # "DimensionView 1463 \"All Categories\" DimensionView 1521 \"All Categories\" DimensionView 1551 \"All Categories\"
  298. # DimensionView 1575 \"All Categories\" DimensionView 1591 \"All Categories\" DimensionView 1651 \"All Categories\"
  299. # DimensionView 1665 \"All Categories\" DimensionView 1693 \"All Categories\" DimensionView 15741 \"All Categories\"
  300. # MeasureInclude 9829 Yes MeasureInclude 10053 Yes MeasureInclude 10309 Yes MeasureInclude 10313 Yes
  301. # MeasureInclude 10317 Yes MeasureInclude 15761 Yes "
  302. }
  303. def allocation_add(block):
  304. # only in DimensionView
  305. return {
  306. "Type": "AllocationAdd",
  307. # "AllocationAdd Measure 9829 Type Default AllocationAdd Measure 10053 Type Default AllocationAdd Measure 10309 Type Default
  308. # AllocationAdd Measure 10313 Type Default AllocationAdd Measure 10317 Type Default AllocationAdd Measure 15761 Type Default "
  309. }
  310. def cube(block):
  311. id, name = get_field("Cube", block, 2)
  312. return {
  313. "Type": "Cube",
  314. "ID": id,
  315. "Name": name,
  316. "MdcFile": get_field("MdcFile", block),
  317. "EncryptedPW": get_field("EncryptedPW", block),
  318. "Status": get_field("Status", block),
  319. "CubeCreation": get_field("CubeCreation", block),
  320. "Optimize": get_field("Optimize", block),
  321. "ConsolidatedRecords": get_field("ConsolidatedRecords", block),
  322. "PartitionSize": get_field("PartitionSize", block),
  323. "PassesNumber": get_field("PassesNumber", block),
  324. "Compress": get_field("Compress", block),
  325. "IncrementalUpdate": get_field("IncrementalUpdate", block),
  326. "ServerCube": get_field("ServerCube", block),
  327. "CubeStamp": get_field("CubeStamp", block),
  328. "CubeCycle": get_field("CubeCycle", block),
  329. "BlockParentTotals": get_field("BlockParentTotals", block),
  330. "Caching": get_field("Caching", block),
  331. "UseAlternateFileName": get_field("UseAlternateFileName", block),
  332. "DeployType": get_field("DeployType", block),
  333. "DeployLocations": get_field("DeployLocations", block),
  334. "DeployToAvailableLocationsAutomatic": get_field(
  335. "DeployToAvailableLocationsAutomatic", block
  336. ),
  337. "DeployCleanupEnabled": get_field("DeployCleanupEnabled", block),
  338. "DeployCleanupNumberOfCubes": get_field("DeployCleanupNumberOfCubes", block),
  339. "DrillThrough": get_field("DrillThrough", block),
  340. "DataSourceSignon": get_field("DataSourceSignon", block),
  341. "PublishEnable": get_field("PublishEnable", block),
  342. "PublishStatus": get_field("PublishStatus", block),
  343. "PublishAllowNullSuppression": get_field("PublishAllowNullSuppression", block),
  344. "PublishAllowMultiEdgeSuppression": get_field(
  345. "PublishAllowMultiEdgeSuppression", block
  346. ),
  347. "PublishAllowAccessToSuppressionOptions": get_field(
  348. "PublishAllowAccessToSuppressionOptions", block
  349. ),
  350. }
  351. CONVERSION = {
  352. "Name": model_name,
  353. "CognosSource": cognos_source,
  354. "CognosPackageDatasourceConnection": cognos_package_datasource_connection,
  355. "DataSource": data_source,
  356. "OrgName": org_name,
  357. "Dimension": dimension,
  358. "Root": root,
  359. "Drill": drill,
  360. "Levels": levels,
  361. "Category": category,
  362. "SpecialCategory": special_category,
  363. "MapDrills": map_drills,
  364. "ViewName": view_name,
  365. "Measure": measure,
  366. "Signon": signon,
  367. "Cube": cube,
  368. }
  369. def convert_block(block):
  370. block = block.replace("\n", "")
  371. block_type = block.split(" ")[0]
  372. # block_pair = re.findall(r'("[^"]+"|\w+) ', block)
  373. # return (block_type, list(zip((block_pair[::2], block_pair[1::2]))))
  374. if block_type in CONVERSION:
  375. return CONVERSION[block_type](block)
  376. return {"Type": block_type}
  377. def main(filename):
  378. with open(filename, "r") as frh:
  379. mdl_str = frh.read()
  380. mdl_str = re.sub(r"\n+", "\n", mdl_str)
  381. mdl_str = re.sub(r"\nLevels 0 ", "Levels 0 ", mdl_str)
  382. tags = "|".join(list(CONVERSION.keys()))
  383. mdl_str = re.sub(r"\n(" + tags + r") ", r"\n\n\1 ", mdl_str)
  384. mdl_blocks = mdl_str.split("\n\n")
  385. converted = [convert_block(b) for b in mdl_blocks]
  386. result = {
  387. "Model": {},
  388. "Connections": [],
  389. "DataSources": [],
  390. "Dimensions": [],
  391. "Measures": [],
  392. "Signons": [],
  393. "Cubes": [],
  394. }
  395. types = [c["Type"] for c in converted]
  396. current = None
  397. for c, t in zip(converted, types):
  398. if t in [""]:
  399. continue
  400. if t in ["ModelName"]:
  401. result["Model"] = c
  402. if t in ["CognosSource", "CognosPackageDatasourceConnection"]:
  403. result["Connections"].append(c)
  404. if t in ["DataSource"]:
  405. current = c
  406. result["DataSources"].append(c)
  407. if t in ["OrgName"]:
  408. current["Columns"].append(c)
  409. if t in ["Dimension"]:
  410. current = c
  411. result["Dimensions"].append(c)
  412. if t in ["Root"]:
  413. current["Root"] = c
  414. if t in ["Drill"]:
  415. current["Root"]["Drill"] = c
  416. if t in ["Levels"]:
  417. current["Levels"].append(c)
  418. if t in ["Category"] and current["Name"] != "Zeit":
  419. current["Categories"].append(c)
  420. if t in ["SpecialCategory"]:
  421. current["SpecialCategories"].append(c)
  422. if t in ["Measure"]:
  423. result["Measures"].append(c)
  424. if t in ["Signon"]:
  425. result["Signons"].append(c)
  426. if t in ["Cube"]:
  427. result["Cubes"].append(c)
  428. json.dump(result, open(filename[:-4] + ".json", "w"), indent=2)
  429. if __name__ == "__main__":
  430. main("data/S_Offene_Auftraege.mdl")