<s> import os <EOL> from uuid import uuid4 <EOL> from itertools import count <EOL> from archivekit . manifest import Manifest <EOL> from archivekit . ext import get_resource_types <EOL> from archivekit . ingest import Ingestor <EOL> from archivekit . types . source import Source <EOL> from archivekit . store . common import MANIFEST <EOL> class Package ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , store , collection , id = None ) : <EOL> self . store = store <EOL> self . collection = collection . name <EOL> self . id = id or uuid4 ( ) . hex <EOL> def has ( self , cls , name ) : <EOL> """<STR_LIT>""" <EOL> return cls ( self , name ) . exists ( ) <EOL> def all ( self , cls , * extra ) : <EOL> """<STR_LIT>""" <EOL> prefix = os . path . join ( cls . GROUP , * extra ) <EOL> for path in self . store . list_resources ( self . collection , self . id ) : <EOL> if path . startswith ( prefix ) : <EOL> yield cls . from_path ( self , path ) <EOL> def exists ( self ) : <EOL> """<STR_LIT>""" <EOL> obj = self . store . get_object ( self . collection , self . id , MANIFEST ) <EOL> return obj . exists ( ) <EOL> @ property <EOL> def manifest ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> obj = self . store . get_object ( self . collection , self . id , MANIFEST ) <EOL> self . _manifest = Manifest ( obj ) <EOL> return self . _manifest <EOL> def get_resource ( self , path ) : <EOL> """<STR_LIT>""" <EOL> for resource_type in get_resource_types ( ) . values ( ) : <EOL> prefix = os . path . join ( resource_type . GROUP , '<STR_LIT>' ) <EOL> if path . startswith ( prefix ) : <EOL> return resource_type . from_path ( self , path ) <EOL> def save ( self ) : <EOL> """<STR_LIT>""" <EOL> self . manifest . save ( ) <EOL> @ property <EOL> def source ( self ) : <EOL> """<STR_LIT>""" <EOL> sources = list ( self . all ( Source ) ) <EOL> if not len ( sources ) : <EOL> return None <EOL> return sources [ <NUM_LIT:0> ] <EOL> def ingest ( self , something , meta = None , overwrite = True ) : <EOL> """<STR_LIT>""" <EOL> ingestors = list ( Ingestor . analyze ( something ) ) <EOL> if len ( ingestors ) != <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" % something ) <EOL> ingestor = ingestors [ <NUM_LIT:0> ] <EOL> try : <EOL> meta = ingestor . generate_meta ( meta ) <EOL> name = None <EOL> for i in count ( <NUM_LIT:1> ) : <EOL> suffix = '<STR_LIT>' % i if i > <NUM_LIT:1> else '<STR_LIT>' <EOL> name = '<STR_LIT>' % ( meta [ '<STR_LIT>' ] , suffix , meta [ '<STR_LIT>' ] ) <EOL> if overwrite or not self . has ( Source , name ) : <EOL> break <EOL> source = Source ( self , name ) <EOL> source . meta . update ( meta ) <EOL> ingestor . store ( source ) <EOL> self . save ( ) <EOL> return source <EOL> finally : <EOL> ingestor . dispose ( ) <EOL> def __eq__ ( self , other ) : <EOL> return self . id == other . id <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . collection , self . id ) </s>
