|
| 1 | +import os |
| 2 | +import uuid |
| 3 | + |
| 4 | +import osw.model.entity as model |
| 5 | +from osw.auth import CredentialManager |
| 6 | +from osw.core import OSW |
| 7 | +from osw.utils.wiki import get_full_title, get_uuid |
| 8 | +from osw.wtsite import WtSite |
| 9 | + |
| 10 | +# credential manager |
| 11 | +# can use a file or hardcode the credentials, otherwise the user will be prompted to enter them |
| 12 | +cm = CredentialManager( |
| 13 | + cred_filepath=os.path.join( |
| 14 | + os.path.dirname(os.path.abspath(__file__)), "accounts.pwd.yaml" |
| 15 | + ) |
| 16 | + # CredentialManager.UserPwdCredential( |
| 17 | + # iri=wiki_domain, username=wiki_username, password=wiki_password |
| 18 | + # ) |
| 19 | +) |
| 20 | +# change the domain to your osw instance |
| 21 | +osw_obj = OSW( |
| 22 | + site=WtSite(WtSite.WtSiteConfig(iri="wiki-dev.open-semantic-lab.org", cred_mngr=cm)) |
| 23 | +) |
| 24 | + |
| 25 | +# load the required schemas / data classes |
| 26 | +force_schema_reload = False |
| 27 | +if force_schema_reload or not hasattr( |
| 28 | + model, "Corporation" |
| 29 | +): # only load if not already loaded. note: does not detect updated schemas yet |
| 30 | + osw_obj.fetch_schema( |
| 31 | + OSW.FetchSchemaParam( |
| 32 | + schema_title=[ |
| 33 | + "Category:OSWd845b96813a344458f140e48c4d063fd", # MetaDeviceCategory |
| 34 | + "Category:OSW5bf1542d9cf847db83cbc73d579ba9d6", # Device (SubclassWithMetaModel) |
| 35 | + "Category:OSW5f4a3751d23e482d80fb0b72dcd6bc31", # Corporation |
| 36 | + ], |
| 37 | + mode="replace", |
| 38 | + ) |
| 39 | + ) |
| 40 | + |
| 41 | +# to reproduce your import you should define a UUIDv5 namespace |
| 42 | +# example: id of the source dataset / file |
| 43 | +uuid_namespace = get_uuid("OSWc0dda5f5f066452dbab68cc8d5dcb022") |
| 44 | + |
| 45 | +# create a new manufacturer |
| 46 | +new_manufacturer = model.Corporation( |
| 47 | + uuid=uuid.uuid5( |
| 48 | + uuid_namespace, "MyNewManufacturer" |
| 49 | + ), # use a stable id from the source dataset / file |
| 50 | + name="MyNewManufacturer", |
| 51 | + label=[model.Label(text="My New Manufacturer", lang="en")], |
| 52 | + description=[model.Description(text="This is a test manufacturer", lang="en")], |
| 53 | +) |
| 54 | + |
| 55 | +# create a new device type with the new manufacturer as manufacturer |
| 56 | +new_category = model.MetaDeviceCategory( # here used as a device type |
| 57 | + uuid=uuid.uuid5( |
| 58 | + uuid_namespace, "MyNewDeviceCategory" |
| 59 | + ), # use a stable id from the source dataset / file |
| 60 | + subclass_of=[ |
| 61 | + "Category:OSW5bf1542d9cf847db83cbc73d579ba9d6" |
| 62 | + ], # Device (SubclassWithMetaModel) |
| 63 | + name="MyNewDeviceCategory", |
| 64 | + label=[model.Label(text="My New Category", lang="en")], |
| 65 | + description=[model.Description(text="This is a test device category", lang="en")], |
| 66 | + manufacturer=get_full_title(new_manufacturer), |
| 67 | +) |
| 68 | + |
| 69 | +# store the manufacturer as an item |
| 70 | +osw_obj.store_entity(OSW.StoreEntityParam(entities=[new_manufacturer])) |
| 71 | +# store the device type as a category. note: we have to request explicitly to create a new category/class from this meta class instance |
| 72 | +osw_obj.store_entity( |
| 73 | + OSW.StoreEntityParam( |
| 74 | + entities=[new_category], |
| 75 | + namespace="Category", |
| 76 | + # meta_category_title="Category:OSWd845b96813a344458f140e48c4d063fd" # usage of MetaDeviceCategory not yet supported |
| 77 | + ) |
| 78 | +) |
| 79 | + |
| 80 | +# reload the schema (currently there is no direct way) |
| 81 | +if force_schema_reload or not hasattr( |
| 82 | + model, "MyNewDeviceCategory" |
| 83 | +): # only load if not already loaded. note: does not detect updated schemas yet |
| 84 | + osw_obj.fetch_schema( |
| 85 | + OSW.FetchSchemaParam( |
| 86 | + schema_title=[ |
| 87 | + get_full_title(new_category), # MyNewDeviceCategory |
| 88 | + ], |
| 89 | + mode="append", |
| 90 | + ) |
| 91 | + ) |
| 92 | + |
| 93 | +# create a new device of the new device type |
| 94 | +new_instance = model.MyNewDeviceCategory( |
| 95 | + uuid=uuid.uuid5( |
| 96 | + uuid_namespace, "MyNewDevice" |
| 97 | + ), # use a stable id from the source dataset / file |
| 98 | + name="MyNewDevice", |
| 99 | + label=[model.Label(text="My New Device", lang="en")], |
| 100 | + description=[model.Description(text="This is a test device", lang="en")], |
| 101 | + serial_number="1234567890", |
| 102 | +) |
| 103 | + |
| 104 | +# store the device instance as an item |
| 105 | +osw_obj.store_entity(OSW.StoreEntityParam(entities=[new_instance])) |
0 commit comments