from debian.deb822 import Packages, Sources
from .index        import IndexFile
from .internals    import parse_contents, joinurl, simple_repr

class Component:
    def __init__(self, suite, name):
        # not for public construction
        self.suite = suite
        self.name = name

    def __repr__(self):
        return simple_repr(self)

    def __eq__(self, other):
        return type(self) is type(other) and \
            self.suite == other.suite and \
            self.name == other.name

    @property
    def archive(self):
        return self.suite.archive

    def fetch_packages(self, arch):
        fp = self.suite.fetch_indexed_file(
            joinurl(self.name, 'binary-' + arch, 'Packages')
        )
        return Packages.iter_paragraphs(fp, use_apt_pkg=True)

    def fetch_sources(self):
        fp = self.suite.fetch_indexed_file(
            joinurl(self.name, 'source', 'Sources')
        )
        return Sources.iter_paragraphs(fp, use_apt_pkg=True)

    def fetch_i18n_index(self):
        dex = self.suite.fetch_indexed_file(joinurl(self.name, 'i18n', 'Index'))
        return IndexFile.parse(dex)

    def fetch_translation(self, lang):
        ### TODO: This won't work when the translation files aren't listed in
        ### the suite's Release
        fp = self.suite.fetch_indexed_file(
            joinurl(self.name, 'i18n', 'Translation-' + lang)
        )
        return Packages.iter_paragraphs(fp, use_apt_pkg=True)

    @property
    def has_contents(self):
        # whether the Contents files are in the component or the suite
        return any(
            joinurl(self.name, 'Contents-' + sarch + '.gz') in self.suite.files
            for sarch in ['source'] + self.suite.architectures
        )

    def fetch_contents(self, sarch):
        # Raises an error if the Contents files are at Suite level
        contents = self.suite.fetch_indexed_file(
            joinurl(self.name, 'Contents-' + sarch),
        )
        return parse_contents(contents)

    ### TODO: def fetch_release_file(self):  # Rethink name

@@ -1,13 +1,30 @@
+import logging%0A
 from 
+  
 debian.d
@@ -59,16 +59,69 @@
 es%0Afrom 
+  .errors       import NoValidCandidatesError%0Afrom   
 .index  
@@ -148,16 +148,18 @@
 le%0Afrom 
+  
 .interna
@@ -209,16 +209,51 @@
 e_repr%0A%0A
+log = logging.getLogger(__name__)%0A%0A
 class Co
@@ -1288,110 +1288,517 @@
     
-### TODO: This won't work when the translation files aren't listed in%0A        ### the suite's Release%0A
+try:%0A            fp = self.suite.fetch_indexed_file(%0A                joinurl(self.name, 'i18n', 'Translation-' + lang)%0A            )%0A        except NoValidCandidatesError:%0A            ### TODO: Fail early if the Release file contains Translation files%0A            ### other than the one the user asked for%0A            log.info('Translation file not listed in Release; trying i18n/Index instead')%0A            index = self.fetch_i18n_index()%0A            ### TODO: Allow validation against SHA1 hashes here:%0A    
     
@@ -1803,36 +1803,38 @@
       fp = self.
-suit
+archiv
 e.fetch_indexed_
@@ -1847,32 +1847,36 @@
             
+    
 joinurl(
 self.name, '
@@ -1867,50 +1867,120 @@
 url(
-self.name, 'i18n', 'Translation-' + lang)%0A
+'dists', self.suite.name, self.name, 'i18n'),%0A                'Translation-' + lang,%0A                index,%0A    
     

Support translation files not listed in Release