Coverage for src/metador_core/cli/general.py: 0%

54 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-02 09:33 +0000

1import platform 

2import tempfile 

3from pathlib import Path 

4 

5import typer 

6from rich import print 

7 

8from metador_core import __version__ 

9 

10app = typer.Typer() 

11 

12 

13@app.command("info") 

14def info(): 

15 """Show information about the system and Python environment.""" 

16 un = platform.uname() 

17 print(f"[b]System:[/b] {un.system} {un.release} {un.version}") 

18 print( 

19 f"[b]Python:[/b] {platform.python_version()} ({platform .python_implementation()})" 

20 ) 

21 print("[b]Env:[/b]") 

22 # TODO: print versions of all relevant packages / starting with metador-* 

23 print("metador-core", __version__) 

24 

25 

26@app.command("check") 

27def check(): 

28 """Run a self-test to ensure that central metador subsystems work correctly.""" 

29 from datetime import datetime 

30 

31 from metador_core.container import MetadorContainer 

32 from metador_core.packer.utils import pack_file 

33 from metador_core.plugins import schemas 

34 from metador_core.widget.dashboard import Dashboard 

35 from metador_core.widget.jupyter import Previewable 

36 

37 today = datetime.today().isoformat() 

38 

39 print("Loading schema plugins...") 

40 

41 BibMeta = schemas["core.bib"] 

42 Person = schemas["core.person"] 

43 DBMeta = schemas["core.dashboard"] 

44 

45 Material = schemas["example.matsci.material"] 

46 Method = schemas["example.matsci.method"] 

47 Instrument = schemas["example.matsci.instrument"] 

48 Specimen = schemas["example.matsci.specimen"] 

49 MSInfo = schemas["example.matsci.info"] 

50 

51 print("Constructing metadata objects...") 

52 

53 author = Person( 

54 id_="https://orcid.org/0000-0002-1825-0097", 

55 givenName="Josiah", 

56 familyName="Carberry", 

57 ) 

58 my_bibmeta = BibMeta( 

59 name="Title for my container", 

60 abstract="This is a Metador-compliant container", 

61 author=[author], 

62 dateCreated=today, 

63 ) 

64 

65 msi = MSInfo( 

66 abstract="hello", 

67 author=[Person(name="Anton Pirogov")], 

68 dateCreated=today, 

69 material=[ 

70 Material( 

71 materialName="bla", 

72 chemicalComposition="bla", 

73 density=1, 

74 crystalGrainType="single_crystal", 

75 ) 

76 ], 

77 method=[ 

78 Method( 

79 instrument=Instrument( 

80 instrumentName="microscope", instrumentModel="micro2000" 

81 ), 

82 specimen=Specimen(diameter=2.5, gaugeLength=123), 

83 ) 

84 ], 

85 ) 

86 

87 with tempfile.TemporaryDirectory() as tmpdir: 

88 csvfile = Path(tmpdir) / "testfile.csv" 

89 with open(csvfile, "w") as f: 

90 f.write( 

91 """AtomID,Time,PosX,PosY,PosZ 

92AtomA,0,0,0,0 

93AtomB,0,0,1,0 

94AtomC,0,0,0,1 

95AtomA,1,1,0,0 

96AtomB,1,0,0.5,0 

97AtomC,1,0,0,0.25 

98""" 

99 ) 

100 

101 print("Creating Metador container with test (meta)data...") 

102 container_path = Path(tmpdir) / "test_container.h5" 

103 with MetadorContainer(container_path, "w") as mc: 

104 # Attach the bibliographic metadata to the very top 

105 mc["/"].meta["core.bib"] = my_bibmeta 

106 

107 # add a file 

108 node = pack_file(mc, csvfile) 

109 # add more specific metadata 

110 node.meta[MSInfo] = msi 

111 # make it visible in the dashboard with high prio 

112 node.meta[DBMeta] = DBMeta.show(group=1, priority=10) 

113 

114 print("Opening Metador container...") 

115 with Previewable(MetadorContainer(container_path)) as mc: 

116 print("Try to access metadata...") 

117 mc[csvfile.name].meta[MSInfo] 

118 print("Try instantiating dashboard...") 

119 Dashboard(mc).show() 

120 

121 print("[b][green]Self-check successfully completed![/green][/b]")